
SQL Server Stored Procedures Links – Executing Dynamic SQL, Working with Temp Tables
September 17, 2011I wanted to run some dynamically generated SQL in a User Defined Function in SQL Server. I ended up having to move to a Stored Procedure to take advantage of EXEC, but below are links to articles that really helped me out along the way.
1) Using EXEC To Run on Dynamic SQL
http://www.4guysfromrolla.com/webtech/020600-1.shtml
Note: EXEC can’t be used in User Defined Functions
2) How t0 Insert Data From Dynamic SQL into a Cursor
Define the cursor in your SQL code (ie ‘Declare users_cursor CURSOR FOR SELECT userId FROM users’ )
http://stackoverflow.com/questions/1045880/using-a-cursor-with-dynamic-sql-in-a-stored-procedure
3) Returning a Table from a Stored Procedure
http://stackoverflow.com/questions/1443663/how-to-return-temporary-table-from-stored-procedure
4) Selecting Data from a Stored Procedure Table in a User Defined Function
I used the INSERT author_sales EXECUTE get_author_sales from the Comments
http://stackoverflow.com/questions/653714/how-to-select-into-temp-table-from-stored-procedure