I have a table, accounts
, which contains the vast majority of the data I am concerned with. accountTypes
contains different types of accounts (Business, Personal, etc.). I want to perform calculations on each row of accounts
based on the accountType
. The best method I can think of for doing this would be to create UDFs, then store the names of the UDFs in a table typeFunctions
, consisting of rows of accountTypeID
and functionName
, as in: P, "f_ageOfAccount"
and B, "f_daysSinceLastcontact"
.
Unfortunately, joining this table to the accounts table and looping through it to exec
the function name seems very kludgy.
Is there a better way to approach this problem?
Context: Currently, the legacy version of this application stores ad hoc SQL in typeFunctions
and execs the SQL, looping through the accounts with a cursor. Obviously, this has some performance implications and is not optimal when dealing with complex calculations.
Best Answer
Any time you use a scaler function you have to run the function once for each row. Complex CASE statements will usually be faster for SQL to process, even though there are painful to read.