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 2000 Forums
 SQL Server Development (2000)
 Opening a Cursor

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-04-15 : 07:43:57
Matt writes "Can you use the exec command to run a prepared sql string and use it to open a cursor?

delcare @sql
set @sql = 'Select * From Employee'

declare #mycursor cursor scroll static read_only for
exec(@sql)


open #mycursor"

dsdeming

479 Posts

Posted - 2003-04-15 : 08:43:52
You can try something like this ( running in Northwind ):

DECLARE @sql varchar( 1000 ), @iEmployee int
SET @sql = 'DECLARE #mycursor CURSOR SCROLL STATIC READ_ONLY FOR SELECT EmployeeID FROM Employees'
EXEC(@sql)
OPEN #mycursor

FETCH NEXT FROM #mycursor INTO @iEmployee

WHILE @@FETCH_STATUS = 0
BEGIN
SELECT LastName, FirstName FROM Employees WHERE EmployeeID = @iEmployee
FETCH NEXT FROM #mycursor INTO @iEmployee
END
CLOSE #mycursor
DEALLOCATE #mycursor


Go to Top of Page
   

- Advertisement -