Cursor

24/05/2011 13:36

Steps:-

1. Declare the Cursor
2. Open the Cursor
3. Fetch the Cursor
4. Close the cursor
5. Deallocate the cursor

DECLARE S_Cursor CURSOR FOR
SELECT * FROM TblEmpMaster

OPEN S_Cursor

FETCH NEXT FROM S_Cursor
WHILE @@FETCH_STATUS = 0
BEGIN
   FETCH NEXT FROM S_Cursor   
END

CLOSE S_Cursor

DEALLOCATE s_Cursor
GO
----------------------------------------------------------------------------------
-- Execute the SELECT statement alone to show the
-- full result set that is used by the cursor.
SELECT prodcid, Category FROM ProductCategory
ORDER BY Category

-- Declare the cursor.
DECLARE SS_cursor SCROLL CURSOR FOR
SELECT EmpID,EmpName FROM TblEmpMaster

OPEN SS_cursor

-- Fetch the last row in the cursor.
FETCH LAST FROM SS_cursor

-- Fetch the row immediately prior to the current row in the cursor.
FETCH PRIOR FROM SS_cursor

-- Fetch the second row in the cursor.
FETCH ABSOLUTE 2 FROM SS_cursor

-- Fetch the row that is three rows after the current row.
FETCH RELATIVE 3 FROM SS_cursor

-- Fetch the row that is two rows prior to the current row.
FETCH RELATIVE -2 FROM SS_cursor

CLOSE SS_cursor

DEALLOCATE SS_cursor
GO