LearnItFirst User Forum

SQL Server 2008 DBA Training Videos
Welcome Guest Search | New Posts | Members | Log In | Register

insert data into 2 tables Options
frank.svs
Posted: Tuesday, August 04, 2009 8:34:05 PM
Rank: Newbie

Joined: 11/20/2007
Posts: 8
Points: -76
Where do you live?: india
Hi All,

In SQL Server, Can we load data into two table from a single table in a single shot?

something like we do it as below:

select * into <tname2> from <tname1>

IS there any way to achieve this?

Thanks in advance.

Scott Whigham
Posted: Monday, August 10, 2009 12:48:08 PM


Rank: Super Mod

Joined: 3/20/2006
Posts: 476
Points: 1,053
Where do you live?: Dallas, TX
Whoa - sorry about that, Frank - I had it in my head that I had replied to this but I don't see it now Frown

Anyway - I'm not 100% clear on your question. The sample code you wrote would only load one table - it's a standard SELECT INTO statement - yet your question seems to be asking if you can insert into two separate tables at the same time. I'm answering this as though this is a SELECT INTO statement just because of your sample code but please correct me if I've answered the wrong question!

Your syntax - the SELECT INTO - does not allow you to just "insert" into a separate table; it requires that the table does NOT exist prior to execution and it will create the table, then insert the rows into it. Example:
Code:
SELECT * INTO MyNewTable FROM dbo.MyExistingTable
In this example, dbo.MyNewTable does not exist prior to running this statement but it will be (a) created, and then (b) the results of the query SELECT * FROM dbo.MyExistingTable will be inserted into it.
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.