Jul 2, 2010

SQL Server cursor example

DECLARE db_cursor CURSOR FOR
SELECT name
FROM table
WHERE --some condition--

OPEN db_cursor 
FETCH NEXT FROM db_cursor INTO @name 

WHILE @@FETCH_STATUS = 0 
BEGIN 

--sql query using @name goes here--
FETCH NEXT FROM db_cursor INTO @name 
END 

CLOSE db_cursor 
DEALLOCATE db_cursor

No comments: