what are cursors in SqlServer

Posted by Rajeshk under Sql Server on 1/10/2013 | Points: 10 | Views : 1617 | Status : [Member] | Replies : 3
hi all,
can anybody tell me the about cursors in the context of sql server with an example?

Thanks&Regards
rajeshkommireddy@gmail.com



Responses

Posted by: muralikrishnasurap-12704 on: 1/10/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Rajeshk,
Cursors is a sqlserver oject which holds set of rows,we can fetch one by one row from cursor object and you can perform operations
on row by row.

Best Regards
Murali Krishna.S

Rajeshk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Saratvaddilli on: 1/11/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi rajeshk,

Cursor is an object which is used for looping a table.

Avoid Cursors

I suggest you that avoid cursors :
Because they are the SLOWEST way to access data inside SQL Server.

Then when to use them:
When you want to loop through the result set of a table
Advantages of cursor : http://www.dotnetfunda.com/forums/thread1283-how-and-when-to-use-cursors-in-sqlserver.aspx

Thanks and Regards
V.SaratChand
Show difficulties that how difficult you are

Rajeshk, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sourabh07 on: 1/17/2013 [Member] Starter | Points: 25

Up
0
Down
Hi

Cursor - The cursors will be used when we want to perform the looping of the table result set. In the sql server we don't have any inbuilt functions to do the looping of the Table results like Array in the any programming language.

Example -

DECLARE C1 CURSOR
FOR SELECT EID FROM employee

OPEN C1
FETCH NEXT FROM C1 INTO @ID

WHILE @@FETCH_STATUS = 0
BEGIN

--- CALCULATING THE DIFFERENCE

SELECT @DIFF = DATEDIFF(day,TAndCDate,GETDATE())
FROM employee
WHERE eID = @ID



IF @DIFF > 0 AND @DIFF < 360
BEGIN

ACTION 1

END

IF @DIFF > 360
BEGIN

ACTIOn 2

END

FETCH NEXT FROM C1 INTO @ID
END
CLOSE C1
DEALLOCATE C1


Sourabh07

Rajeshk, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response