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
 General SQL Server Forums
 New to SQL Server Programming
 Reading one table insert into another

Author  Topic 

Steve2106
Posting Yak Master

183 Posts

Posted - 2009-09-24 : 16:58:21
Hi There,

Can someone point me in the right direction.
I need to know how I can loop through one table and insert into another.
First Table:
AutoId, Name

Second Table:
AutoId, FirstTableAutoId,Code

Code field above will be the same for all records.

Hope that makes sense.

Thanks for your help.

Best Rgards


Steve

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-09-24 : 17:04:40
You shouldn't need to loop at all. If you just want to move data, then just use INSERT/SELECT. If you describe your issue in better detail and/or provide sample data, then we can better help you.

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

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

AjarnMark
SQL Slashing Gunting Master

3246 Posts

Posted - 2009-09-24 : 20:00:27
Expanding on Tara's response, it would be something along the lines of...

INSERT INTO Table2 (FirstTableAutoID, Code)
SELECT AutoID, 'CodeConstantValue' FROM Table1

--------------------------------------------
Brand yourself at EmeraldCityDomains.com
Go to Top of Page

Steve2106
Posting Yak Master

183 Posts

Posted - 2009-09-25 : 03:16:17
That's perfect.

Thanks for taking the time to give the code, I appreciate it.

Best Regards,

quote:
Originally posted by AjarnMark

Expanding on Tara's response, it would be something along the lines of...

INSERT INTO Table2 (FirstTableAutoID, Code)
SELECT AutoID, 'CodeConstantValue' FROM Table1

--------------------------------------------
Brand yourself at EmeraldCityDomains.com



Steve
Go to Top of Page
   

- Advertisement -