1,105,633 Community Members

What is cursor in SQL and when to use

Member Avatar
pratham3090
Newbie Poster
8 posts since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

What is cursor in SQL and when to use

Member Avatar
pritaeas
mod_pritaeas
11,332 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,835 [?]
Skill Endorsements: 156 [?]
Moderator
Featured
Sponsor
 
0
 

A cursor is basically a table, generated from a query, so you can iterate through the rows. It is usually used in a stored procedure or trigger.

Member Avatar
adam_k
Veteran Poster
1,056 posts since Jun 2011
Reputation Points: 239 [?]
Q&As Helped to Solve: 212 [?]
Skill Endorsements: 17 [?]
 
0
 

A cursor is the tool we've got to iterate through records.
You can loop through the results of your query and do whatever you have to do with each record.
I've got to disagree with pritaeas on the sp or triggers part. You use a cursor when you need to handle each record differently, in a specific order where a case won't cover things. Usually they are too expensive (in terms of resources) and don't get the job done as fast as processing bulk data would.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: