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

MSSQL CURSOR

/* DECLARA EL CURSOR PARA EL id de product */
/* Y CARGA EN MEMORIA */
DECLARE ProdId CURSOR FOR SELECT id FROM product
/* DECLARA VARIABLE DONDE SE ALMACENARA */
DECLARE @identificador INT
/* UTILITZA EN MEMORIA*/
OPEN ProdId 
/* AGAFA EL PRIMER VALOR */
FETCH next FROM ProdInfo INTO @identificador
WHILE @@fetch_status = 0
BEGIN
    PRINT @identificador
    FETCH NEXT FROM ProdId INTO @identificador
END
/* TANCA UTILITZACIO */
CLOSE  ProdId 
/* ELIMINA DE MEMORIA */
DEALLOCATE ProdId
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 :: nested select sql 
Sql :: mysql delete older duplicates 
Sql :: alter table drop partition hive 
Sql :: How to check if a column exists in a SQL Server table? 
Sql :: when matched in sql server 
Sql :: how to move a column to different spot mysql 
Sql :: DELETE DUPLICATE VALUES FROM A TABLE IN SQL SERVER 
Sql :: mysql update 
Sql :: mysql date_format 
Sql :: sql add calculated column 
Sql :: drop database mysql 
Sql :: having in sql 
Sql :: Select All From A Table In A MySQL Database 
Sql :: SQL/update 
Sql :: ORACLE CALL BACK TRACE 
Sql :: How To Rename Table Using MySQL RENAME TABLE Statement 
Sql :: check ksql db health 
Sql :: if role exists sql 
Sql :: tsql find procedure with name 
Sql :: show sql property syntax for jpa. 
Sql :: Question 7: Write an SQL query to print details of the Workers who have joined in Feb’2014. 
Sql :: creating tables in sql with python 
Sql :: greater than or equal to symbol in postgres 
Sql :: keys in sql with example 
Sql :: add column first position mysql 
Sql :: import Data in MySQL without using any other software 
Sql :: drop a field in psql django 
Sql :: pl sql command line run 
Sql :: how to switch database in psql 
Sql :: order by postgres 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =