all groups > sql server (alternate) > november 2004 >
You're in the

sql server (alternate)

group:

What is a cursor?


What is a cursor? seearoomhawaii NO[at]SPAM resonant.com
11/30/2004 3:17:41 PM
sql server (alternate):
I had a friend write a stored procedure to perform a function for one
of my clients. What he wrote doesn't fully do what I need, and I hope
to finish it myself. I have programming sense, but not so much with
SQL.

I'm trying to figure out the code, and he has used something called a
"cursor." I'm not sure whether this is an SQL construct or a structure
that he has just labeled "cursor." My guess is that it is an SQL
construct. Can anyone give me a quick run down of how this works?

sincerely,
Tyler H.
-----------------------------------------------
<a href="http://www.seearoomhawaii.com/bed-breakfasts/">bed &
Re: What is a cursor? David Portas
12/1/2004 12:01:52 AM
Cursors are a method of processing data row-by-row, usually in a loop. See
DECLARE CURSOR in Books Online for details and examples.

In SQL Server cursors are usually slow and inefficient compared to other
methods of manipulating data. Most of the time cursors are not necessary and
good SQL developers will avoid them where they can.

You don't say what this cursor code is doing. Maybe your code is performing
some operation that is inherently procedural in nature - such as sending an
email for each row in a table. If that's the case then a cursor is a
reasonable solution. On the other hand if the cursor is just manipulating
data then chances are there are much simpler / better / faster
alternatives - that's not always the case but 99% it is so.

--
David Portas
SQL Server MVP
--

Re: What is a cursor? Danny
12/1/2004 12:23:53 AM
See SQL Books Online for a full explanation, but think of it as a pointer to
a set of rows that depending on the options called can move forwards or
backwards through the set.

In general, cursors are an inefficient last ditch effort that should be
avoided for both performance and locking reasons. TSQL is a set oriented
language and again another generality when coded in a set oriented way will
out perform row oriented approaches.

The SQL group in this news group is pretty good about helping out if you
post an explanation of the issue, the object creation, and data insert
scripts for testing.

Danny

[quoted text, click to view]

AddThis Social Bookmark Button