Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Swati Jain
Posting Yak Master
139 Posts |
Posted - 2009-02-05 : 06:47:24
|
| helloin following case,DECLARE curSBAllotments CURSOR FOR dynamic sql query.How to write dynamic select statements for cursors? |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2009-02-05 : 07:04:14
|
| Why would you want to? |
 |
|
|
Swati Jain
Posting Yak Master
139 Posts |
Posted - 2009-02-05 : 07:20:24
|
quote: Originally posted by robvolk Why would you want to?
just to change the select query depending on input to sp |
 |
|
|
tonymorell10
Yak Posting Veteran
90 Posts |
Posted - 2009-02-05 : 07:44:18
|
| Use a cursor variable. Here's a simple example.DECLARE @my_cur CURSOR, @sql nvarchar(max), @output nvarchar(100)SELECT @sql = N'SET @my_cur = CURSOR FOR SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES; OPEN @my_cur', @output = N'@my_cur cursor OUTPUT'EXEC sp_executesql @sql, @output, @my_cur OUTPUTFETCH NEXT FROM @my_cur |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2009-02-05 : 08:19:24
|
quote: Originally posted by Swati Jainjust to change the select query depending on input to sp
Ummm, what is the point of using a stored procedure if the code can vary each time it's executed? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-05 : 11:55:04
|
| i dont think you need to use cursor. Can you be more specific on requirement so that we can suggest better approach to use. |
 |
|
|
|
|
|