Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Cursor in microsoft SQL server

Author  Topic 

JohnCash1
Starting Member

39 Posts

Posted - 2012-09-18 : 21:16:16
Hi,

Could someone explain to me what is a SQL cursor, what it does, and how to use it. Could you provide a simple example.

Thank you, appreciate it.

robvolk
Most Valuable Yak

15732 Posts

Posted - 2012-09-18 : 21:29:37
If you really must know: http://msdn.microsoft.com/en-us/library/ms191179%28SQL.90%29.aspx

Cursors are best avoided in SQL Server. Row-by-row access slows performance and often creates lengthy/bloated code compared to regular SQL statements.
Go to Top of Page

Vishal_sql
Posting Yak Master

102 Posts

Posted - 2012-09-18 : 21:44:05
I agree robvolk.
When you have large dataset , the performance is harmed by cursor.

but you can still know the cursor

http://blog.sqlauthority.com/2007/01/01/sql-server-simple-example-of-cursor/
Go to Top of Page

JohnCash1
Starting Member

39 Posts

Posted - 2012-09-18 : 21:46:09
cool, thanks for the info and suggestions.

So do you mean that I don't need cursor at all and anything that cursor does or is use by cursor can be done with SQL statements?
Go to Top of Page

Vishal_sql
Posting Yak Master

102 Posts

Posted - 2012-09-18 : 21:55:55
Its depends on the scenarios and expertise .

Cursors make some scenario's easy to implement as most of us think in procedural way like for loop.

Most of experts prefer to use Set Based Approach for solving problems.

Cursor works for each record at a time.

http://weblogs.sqlteam.com/jeffs/archive/2007/04/30/thinking-set-based-or-not.aspx

Go to Top of Page

JohnCash1
Starting Member

39 Posts

Posted - 2012-09-19 : 00:15:49
pretty interesting article I found on set base, procedure and user define functions.

http://www.codeproject.com/Articles/34142/Understanding-Set-based-and-Procedural-approaches

I guess we don't need cursor and could do everything in set base which is much better right?
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-09-19 : 00:47:57
further reading material for you
The Road to Professional Database Development: Set-Based Thinking


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -