Cursor in SQL Server

Example of a simple cursor, SQL Server.


DECLARE @id integer
DECLARE @name varchar(500)

DECLARE myCursor CURSOR FOR
SELECT cursomerId, [name]
FROM CUSTOMER

OPEN myCursor

FETCH NEXT FROM myCursor
INTO @id, @name

WHILE @@FETCH_STATUS = 0
BEGIN
-- do something (updates, insert into other tables, etc)
print @name

FETCH NEXT FROM myCursor
INTO @id, @name

END

CLOSE myCursor
DEALLOCATE myCursor

No comments: