Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loop in ms sql 2000

Status
Not open for further replies.

b00gieman

Programmer
Jul 9, 2007
60
DE
Hi!
Is there a possible way for looping through the records of a table(like using foreach - oracle)?
 
Yes it's called a FETCH cursor. 99% of the time it's better to not use a cursor in SQL Server. SQL Server processes data is a ROWSET faster than a fetch cursor.

Code:
DECLARE @name as nvarchar(128)
DECLARE MyCursor CURSOR FOR SELECT name from sysdatabases
open MyCursor
FETCH NEXT FROM MyCursor into @name
WHILE @@FETCH_STATUS = 0
BEGIN
     --Do something with the variable here.
     FETCH NEXT FROM MyCursor into @name
END
CLOSE MyCursor
DEALLOCATE MyCursor

What's the end result that you are trying to get to?

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
I'm trying to select some rows that fulfill a certain condition.For each row,I want to gather some information and use it...
 
In that case carry on with your cursor. That would pretty much be one of the few cases where cursors are probably going to be needed.

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

--Anything is possible. All it takes is a little research. (Me)
[noevil]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top