• There are a LOT of possible culprits in here. The excessive amount of scalar functions is certainly one of them. By my count there are 9 columns that are direct inline scalar function calls with a few more thrown into the joins and where predicates. I would be my hat that the scalar functions are the worst part of your performance woes. This level of scalar UDF means that your query is looking at every single row of these tables and given the names I am guessing these tables are quite large.

    There are also a lot of variables in here (why are the names so generic?). I doubt you changed the variable names when you posted since all the rest of the information seems to be the real stuff. In general you will find working with code a lot easier if you give reasonable names to your variables. We have no idea what datatypes are here in your columns and all your variables. There may be tons of implicit conversions here too.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/