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
 insert into 2 tables single shot

Author  Topic 

frank.svs
Constraint Violating Yak Guru

368 Posts

Posted - 2009-08-04 : 21:30:05

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.


TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-08-04 : 21:55:23
you can only insert into 1 table per statement. Your sample code, however, is fine. but it is CREATING one table based on the structure (and content) of the other.

Be One with the Optimizer
TG
Go to Top of Page

frank.svs
Constraint Violating Yak Guru

368 Posts

Posted - 2009-08-05 : 08:29:13
Just want to know is there any way or not to do so?
Go to Top of Page

rohitkumar
Constraint Violating Yak Guru

472 Posts

Posted - 2009-08-05 : 08:44:37
put two such statements in one transaction
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-08-05 : 10:21:09
As I said, "you can only insert into 1 table per statement".

Burt what do you mean by "in a single shot"?

Because as rohitkumar says you can combine your two inserts into a single transaction block. That transaction can then then be either committed or rolled back with a single command. But that actual insert statements need to be one for each target table.

Be One with the Optimizer
TG
Go to Top of Page

slimt_slimt
Aged Yak Warrior

746 Posts

Posted - 2009-08-05 : 14:43:04
you can achieve this with SSIS, but it would be much faster with transaction statment and within two inserts.
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2009-08-05 : 15:55:18
What version of SQL are you using?

You might be able to use Composable DML to do it..?
[url]http://weblogs.sqlteam.com/peterl/archive/2009/04/08/Composable-DML.aspx[/url]
Go to Top of Page
   

- Advertisement -