|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
T-SQL - Insert data into 2 tables in a single shot
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. |
|
#2
|
||||
|
||||
|
SELECT * INTO T2 FROM T1
This will create a copy of T1 and stick it into T2. Often used by DBA's to create a backup of a single table prior to modifying the original table... just in case. The INSERT statement must target a single table.
__________________
IF ADVICE = 'GOOD' INSERT INTO scales (REPUTATION_POINTS, REASON) VALUES (+1, 'BECAUSE ITS GOOD TO DO THAT') END |
|
#3
|
|||
|
|||
|
I suppose that depends on what you mean by "one shot" It is not possible to do it in a single statement (unless you use a trigger on the first table that does an insert into the second table.)
However, if "one shot" means that the insert into both tables happens or doesn't happen together in a single transaction, then sure it is possible: Code:
BEGIN TRANSACTION INSERT <table1>..... INSERT <table2>..... COMMIT TRANSACTION While this is not a single statement, the inserts live or die together--either both tables get the data or neither of them get it, so it is "one shot" in that sense. |
![]() |
| Viewing: ASP Free Forums > Database > Microsoft SQL Server > T-SQL - Insert data into 2 tables in a single shot |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|