0% found this document useful (0 votes)
11 views9 pages

CURSOR

Advanced SQL - CURSOR notes

Uploaded by

Uapi Muukua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views9 pages

CURSOR

Advanced SQL - CURSOR notes

Uploaded by

Uapi Muukua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

CURSOR

Database Programming (DPG621S)


2022
WHAT IS A CURSOR?

 Temporary Memory
 It is Allocated by Database Server at the Time of Performing DML
operations on Table by User.
 Cursors are used to store Database Tables.
 There are 2 types of Cursors: Implicit Cursors, and Explicit Cursors.
LIFECYCLE
CURSOR SYNTAX
 Step 1: Declare
 Step 2: Open Cursor
 Step 3: Fetch Cursor
 Step 4: Close Cursor
 Step 5: Deallocate Cursor
Example 1

DECLARE @name Varchar(100), @Salary INT


DECLARE DB_Cursor CURSOR
FOR SELECT Name, Salary
FROM EMPLOYEES

OPEN DB_Cursor

FETCH NEXT FROM DB_Cursor INTO @Name,


@Salary
PRINT @Name+' '+CAST(@Salary AS VARCHAR)
CLOSE DB_Cursor
DEALLOCATE DB_Cursor
Example 2
DECLARE @name Varchar(100), @Salary
INT
DECLARE DB_Cursor CURSOR
FOR SELECT Name, Salary
FROM EMPLOYEES

OPEN DB_Cursor

FETCH NEXT FROM DB_Cursor INTO @Name,


@Salary
PRINT @Name+' '+CAST(@Salary AS
VARCHAR)
FETCH NEXT FROM DB_Cursor INTO @Name,
@Salary
PRINT @Name+' '+CAST(@Salary AS
VARCHAR)
FETCH NEXT FROM DB_Cursor INTO @Name,
@Salary
PRINT @Name+' '+CAST(@Salary AS
VARCHAR)
Example 3: USING A LOOP

DECLARE @name Varchar(100), @Salary


INT
DECLARE DB_Cursor CURSOR
FOR SELECT Name, Salary
FROM EMPLOYEES

OPEN DB_Cursor
FETCH NEXT FROM DB_Cursor INTO @Name,
@Salary
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT @Name+' '+CAST(@Salary AS
VARCHAR)
FETCH NEXT FROM DB_Cursor INTO @Name,
@Salary

END
CLOSE DB_Cursor
DEALLOCATE DB_Cursor
PRACTICAL DECLARE @name VARCHAR(50)
DECLARE @path VARCHAR(256)
DECLARE @fileName VARCHAR(256)

EXAMPLE DECLARE @fileDate VARCHAR(20)


SET @path = 'C:\Backup\'
SELECT @fileDate =
CONVERT(VARCHAR(20),GETDATE(),112)
 Lets do a backup of all
DECLARE db_cursor CURSOR FOR
our databases on the SELECT name
server using a cursor. FROM MASTER.dbo.sysdatabases
WHERE name NOT IN
('master','model','msdb','tempdb')
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
SET @fileName = @path + @name + '_' +
@fileDate + '.BAK'
BACKUP DATABASE @name TO DISK = @fileName
FETCH NEXT FROM db_cursor INTO @name
END
CLOSE db_cursor
DEALLOCATE db_cursor
 Example 2

DECLARE @DB_Name VARCHAR(100)

DECLARE db_cursor CURSOR


FOR
SELECT NAME
FROM MASTER.dbo.sysdatabases
WHERE name NOT IN ('master','model','msdb','tempdb')

OPEN DB_Cursor
FETCH NEXT FROM DB_Cursor INTO @DB_Name
WHILE @@FETCH_STATUS = 0

BEGIN
PRINT 'BACKUP DATABASE ' +@DB_Name +'TO DISK =''c:\BackupDrive'' WITH INIT,
COMPRESSION'
FETCH NEXT FROM DB_Cursor INTO @DB_Name
END
CLOSE db_cursor
DEALLOCATE db_cursor

You might also like