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
 What is Cursor? how to use it?

Author  Topic 

stan9186
Starting Member

7 Posts

Posted - 2007-07-06 : 00:14:33
Hi everyone...

May I know what is Cursor is? Why and when we should use Cursor? How it improve the performance.. I have try to read some tutorial but still does not get it...

I try to test this query but it returns the first rows.. How to use cursor actually?

DECLARE authors_cursor CURSOR
FOR SELECT * FROM authors
OPEN authors_cursor
FETCH NEXT FROM authors_cursor
CLOSE authors_cursor
deallocate authors_cursor

sachinsamuel
Constraint Violating Yak Guru

383 Posts

Posted - 2007-07-06 : 00:34:11

Check the below links

http://www.sqlteam.com/article/cursors-an-overview

http://www.databasejournal.com/features/mssql/article.php/1439731

It recommended to avoid cursors. Check the below link to learn how to avoid cursor.

http://www.sql-server-performance.com/dp_no_cursors.asp

Regards
Sachin

Don't sit back because of failure. It will come back to check if you still available. -- Binu
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2007-07-06 : 01:18:00
"Why and when we should use Cursor? How it improve the performance?"

cursors hardly ever improve performance. the only case I know of where they might is when calculating running totals.

If you are new to sql, probably best to not even bother with them now - stick to the set-based ways of doing things.


elsasoft.org
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-07-06 : 02:16:15
quote:
Originally posted by stan9186

Hi everyone...

May I know what is Cursor is? Why and when we should use Cursor? How it improve the performance.. I have try to read some tutorial but still does not get it...

I try to test this query but it returns the first rows.. How to use cursor actually?

DECLARE authors_cursor CURSOR
FOR SELECT * FROM authors
OPEN authors_cursor
FETCH NEXT FROM authors_cursor
CLOSE authors_cursor
deallocate authors_cursor



First explain what you want
Then it can be decided if you need cursor

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2007-07-06 : 06:20:33
If you do insist on using cursors....you also need to include a "loop" construct, which allows you to get more than just the 1st record....(search here for example of cursors)

But best advice...is stay away from them until absolutely necessary.
Go to Top of Page

pootle_flump

1064 Posts

Posted - 2007-07-06 : 08:30:37
Another reason you might want to use them is for performing admin functions.

Personally I can't be bothered learning the syntax so just use loops if I do need to process a set of data iteratively.
Go to Top of Page
   

- Advertisement -