Showing posts with label Cursors in SQL Server. Show all posts
Showing posts with label Cursors in SQL Server. Show all posts

Feb 11, 2010

Cursors in SQL Server

USE Northwind
GO

DECLARE @Counter INT
DECLARE @EmployeeID INT

DECLARE Employees_Cursor CURSOR FOR
SELECT EmployeeID FROM Employees

OPEN Employees_Cursor

FETCH NEXT FROM Employees_Cursor into @EmployeeID

WHILE @@FETCH_STATUS = 0
BEGIN
SELECT * FROM Orders WHERE EmployeeID = @EmployeeID
FETCH NEXT FROM Employees_Cursor into @EmployeeID
END

CLOSE Employees_Cursor
DEALLOCATE Employees_Cursor