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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to write dynamic select statements for cursors

Author  Topic 

Swati Jain
Posting Yak Master

139 Posts

Posted - 2009-02-05 : 06:47:24
hello
in 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?
Go to Top of Page

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
Go to Top of Page

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 OUTPUT

FETCH NEXT FROM @my_cur
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-02-05 : 08:19:24
quote:
Originally posted by Swati Jain

just 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?
Go to Top of Page

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.
Go to Top of Page
   

- Advertisement -