Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Loop through a result set
Message
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Miscellaneous
Thread ID:
01116848
Message ID:
01116862
Views:
13
This message has been marked as the solution to the initial question of the thread.
Hi, Einar,

A cursor will work - but cursor performance is generally lackluster. If you have a few dozen rows, it's not too bad, but for anything much larger, it's been documented to affect server performance.

If it's going to be a small # of rows, you can do....
DECLARE MyCursor CURSOR FORWARD_ONLY READ_ONLY FOR  SELECT  MyColumnName1, MyColumnName2 From blah blah blah

OPEN MyCursor
FETCH Next FROM MyCursor INTO @MyColumnName1, @MyColumnName2
WHILE (@@FETCH_STATUS=0) BEGIN
   -- run your code using @MyColumnName1 and 2

    FETCH Next FROM MyCursor INTO @MyColumnName1, @MyColumnName2
END

CLOSE MyCursor
DEALLOCATE MyCursor
Here's a URL with some tips on cursors (including performance tips).

http://www.mssqlcity.com/Tips/tipCursor.htm

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

Again, if you're talking about a large amount of data, performance can be very slow.


SQL Server 2005 has new capabilities that "might" help, depending on your situation. If you can post some specifics about what you're looking to do, maybe we can go from there. Unless it's something really unusual, often you can use subqueries or derived tables or some other means if you need to do row-by-row operations.

Kevin
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform