I want to pass a SELECT query as parameter to STORED PROCEDURE in SQL Server 2008.
In that sp execute that query and store to temp table and then I want to loop a cursor on the 2 field of that temp table.
So how can I do that??
Mainly this part : execute that query and store to temp table
Edit :
Query :
SELECT * FROM SomeView WHERE COL1 = '1' AND COL = GETDATE() ORDER BY COL0
There are many fields in SomeView but I want only 2 : COLS1, CLOS2
I want to loop CURSOR on that 2 fields only
Here is how you can store query result from a dynamic SQL query to a temp table:
But you should really consider alternative to cursor, cursors degrade performance, cursors operate row by row whereas queries do set based operations. SQL Server is optimized for set based data access. If you can write a query don’t use cursor.