Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SQL

attributes of cursor in sql

/*
Declare – Declares the cursor with a name and the select statement which populates the result set
Open – Opens a cursor and populates the cursor by executing the select statement which is specified while declaring a cursor
Fetch – To retrieve a specific row from the cursor based on the fetch arguments like NEXT, FIRST, LAST, etc
Close – Closes the current result set of SQL Server cursor and can be reopened
Deallocate – Removes cursor reference and releases all the resources associated with a cursor
*/

DECLARE @TempTable AS TABLE (Id INT, [Name] VARCHAR(200));

INSERT INTO @TempTable
VALUES (1,'Test Name1'),
(2,'Test Name2'),
(3,'Test Name2')

DECLARE 
    @Id INT, 
    @Name VARCHAR(200);

DECLARE cursor_personInfo CURSOR
FOR SELECT 
        Id, 
        [Name]
    FROM 
       @TempTable;

OPEN cursor_personInfo;

FETCH NEXT FROM cursor_personInfo INTO 
    @Id, 
    @Name;

WHILE @@FETCH_STATUS = 0
    BEGIN
        PRINT CONVERT(VARCHAR(10),@Id) +' '+ @Name;

        FETCH NEXT FROM cursor_personInfo INTO 
            @Id, 
            @Name;
    END;

CLOSE cursor_personInfo;

DEALLOCATE cursor_personInfo;
Source by www.sqlservertutorial.net #
 
PREVIOUS NEXT
Tagged: #attributes #cursor #sql
ADD COMMENT
Topic
Name
5+1 =