Search
 
SCRIPT & CODE EXAMPLE
 

SQL

cursor in sql server

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;

/*
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
*/
Comment

what is cursor in sql server with example

/*
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
*/

ECLARE @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;
Comment

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;
Comment

SQL Cursor

DECLARE @CONTACTID INT 

--will need to declare at least one variable

--in this case, to store values when iterating through cursor

DECLARE SIMPLE_CURSOR CURSOR FOR 
SELECT ID
FROM CONTACT

OPEN SIMPLE_CURSOR

FETCH NEXT FROM SIMPLE_CURSOR  --Start the cursor
INTO @CONTACTID

WHILE @@FETCH_STATUS = 0  --while there is a loaded record, keep processing
BEGIN

--do whatever you need to do
print ('This is where the magic happens! 
Do whatever you need to do (update/insert/delete/stored proc/etc.')

FETCH NEXT FROM SIMPLE_CURSOR INTO @CONTACTID  --fetch next record
END

CLOSE SIMPLE_CURSOR   --close and deallocate
DEALLOCATE SIMPLE_CURSOR
Comment

PREVIOUS NEXT
Code Example
Sql :: redirection 301 htaccess nom de domaine 
Sql :: how to put 0 or 000 depending IDCustomer length in sql server 
Sql :: how to convert null to float in mysql 
Sql :: less than and between in sql query 
Sql :: description query in sql 
Sql :: postgres insert new row advance count 
Sql :: oracle no data found error code 
Sql :: postgresql sum 
Sql :: oracle swap partition 
Sql :: normalization in sql 
Sql :: sql round datetime 
Sql :: default username and password for oracle 11g 
Sql :: tsql cte in a transaction 
Sql :: sqlite trim 
Sql :: add role to group postgres 
Sql :: stored function in sql 
Sql :: Write an SQL query to print details of the Workers whose FIRST_NAME ends with ‘h’ and contains six alphabets. 
Sql :: postgres ERROR: relation "user" does not exist 
Sql :: oracle procedure teamplate 
Sql :: store date time in mysql 
Sql :: update or insert sql 
Sql :: oracle insert multiple rows into same table 
Sql :: mysql create database 
Sql :: sql limit 
Sql :: SQL AS With More Than One Column 
Sql :: how to install mysql without admin rights 
Sql :: mysql and or 
Sql :: sql update from two different database 
Sql :: A good way of running a SQL query in JDBC using a parameterized statement 
Sql :: create relationship with betwen two tables in postgersql 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =