Blog author:
Sabarimahesh | Posted on: 3/23/2012 | Category:
SQL Server Blogs | Views: 1158 | Status:
[Member] |
Points: 75
|
Alert Moderator
WHAT IS CURSOR ??:
- Cursor is a Database Objects
- Cursor Performs Row By Row Basis
- Cursor Define a Result in Set of Data Row
- Cursor Points a Row in Set of Row
- Cursor Refers Only One Row at a Time
WHAT CURSOR PERFORMS ??:
- Declare cursor
- Open cursor
- Fetch row from the cursor
- Process fetched row
- Close cursor
- De-allocate cursor
ADVANTAGE:
- Cursor is like Data type used to define multi-value variable,
When we declare Static variable (ex: int a) it holds only one value,
But declaring cursor we can Hold Multi-value (Ex: Select Id From emptbl)
DISADVANTAGE:
- This affects the Performance to become less
SYNTEX:
DECLARE CURSOR_NAME CURSOR
QUERY DESCRIPTION:
DECLARE CURSOR:
Two Ways to Declare
- DECLARE @id INT
- DECLARE @ getid CURSOR SET @getid = CURSOR FOR select id from cidtbl
(OR)
DECLARE getid CURSOR FOR select id from cidtbl
OPEN CURSOR:
OPEN @getid
FETCH ROW FROM THE CURSOR
FETCH NEXT
FROM @getid INTO @id
WHILE @@FETCH_STATUS
BEGIN
PRINT @id
FETCH NEXT
FROM @getid INTO @id
END
CLOSE CURSOR
CLOSE @getid
DE-ALLOCATED CURSOR
DE-ALLOCATED @getid
Using the code
DECLARE @id INT
DECLARE @ getid CURSOR SET @getid = CURSOR FOR select id from cidtbl
OPEN @getid
FETCH NEXT
FROM @getid INTO @id
WHILE @@FETCH_STATUS
BEGIN
PRINT @id
FETCH NEXT
FROM @getid INTO @id
END
CLOSE @getid
DE-ALLOCATED @getid Life is a Race
Thanks & Regards
By
Sabari Mahesh P M
Found interesting? Add this to: