Pages

Showing posts with label cursor. Show all posts
Showing posts with label cursor. Show all posts

Monday 20 May 2013

Using CURSOR in SQL


DECLARE @variable int  -- Declaring variable to use in cursor 
-- You can declare as many variables as per your need
--------------------------------------------------------
DECLARE @cursorName CURSOR   -- Declaring cursor
SET @cursorName = CURSOR FAST_FORWARD
FOR
SELECT column_name FROM   Table_Name   -- query for cursor
OPEN @cursorName
FETCH NEXT FROM @cursorName
INTO @variable
WHILE @@FETCH_STATUS = 0
BEGIN
update Table_Name set column_name= something where column_name=something -- write your query here

FETCH NEXT FROM @cursorName
INTO @variable
END
CLOSE @cursorName
DEALLOCATE @cursorName