ChrisM@Work (2/5/2013)
Steven Willis (2/5/2013)
Just a generic observation...If this function is taking two hours to run--even with all of the cursors and subcursors--then you are probably accessing a LOT of data. I don't think a function is even appropriate. Is this being called by another procedure or query? If so the issues raised are multiplied by how many times the function itself is being called.
You should write a procedure instead. ..
Not necessarily. A properly written iTVF inlines like a view. This function is just a 4-table query with a few sums in it. I can't see any reason why it shouldn't be written as an iTVF regardless of how many rows the base tables contain.
+100!
--Jeff Moden
Change is inevitable... Change for the better is not.