Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / October 2007

Tip: Looking for answers? Try searching our database.

How do I loop through the records in a temporary table

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tim Kelley - 31 Oct 2007 18:39 GMT
How do I loop through the records in a temporary table?

Thanks
John Bell - 31 Oct 2007 19:27 GMT
Hi Tim

Processing rows is usually less efficent than using set based commands on
your data set, therefore if you can use a set based solution is should be
better. You can use a CURSOR on a temporary table for example:

SELECT LastName, FirstName
INTO #Employees
FROM Northwind.dbo.Employees
WHERE LastName like 'B%'

DECLARE @LastName [nvarchar] (20) ,
    @FirstName [nvarchar] (10)

DECLARE Employee_Cursor CURSOR FOR
SELECT LastName, FirstName
FROM #Employees
OPEN Employee_Cursor

FETCH NEXT FROM Employee_Cursor INTO @LastName, @FirstName
WHILE @@FETCH_STATUS = 0
BEGIN
   PRINT @FirstName + N' ' + @LastName
   FETCH NEXT FROM Employee_Cursor INTO @LastName, @FirstName
END

CLOSE Employee_Cursor
DEALLOCATE Employee_Cursor
DROP TABLE #Employees

Check out information on DECLARING/OPENING/CLOSING/DEALLOCATING and FETCHING
from cursors in Books Online

John

> How do I loop through the records in a temporary table?
>
> Thanks
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.