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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 loop through resultset, inserting into existng tbl

Author  Topic 

djfiii
Starting Member

13 Posts

Posted - 2008-05-27 : 16:10:56
Hello! quick question.

Say I have a table, and I select the primary key as follows:

select fID from findings;

how can I loop through that resultset and execute the following insert at each iteration:

insert into owners (fID, uID) values (@curFID, 273);


where @curFID is the current record from the resultset? I've done some playing with while loops, however the tutorials I found were fairly basic and only provided examples using static loop controls. any help is appreciated!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-27 : 16:12:34
Do not loop through data when you can do this with a set-based approach:

insert into owners (fID, uID)
select fID, 273 from findings

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

djfiii
Starting Member

13 Posts

Posted - 2008-05-27 : 16:25:02
perfect, thanks!!
Go to Top of Page
   

- Advertisement -