sql - Insert into 2 tables from a single select query using TSQL -
i trying insert 3 tables 1 single select statement. here trying do:
insert dbo.temp1 (name, location, city) select name, location, city mytable.
i want able insert 3 tables once run select statement inserting temp1, temp2 , temp3.
how can this? thanks.
you can't in 1 step*
what can insert initial query #temp
table (or @table
variable) staging area, , insert tables there. wrap steps in transaction retain acid:
begin tran select name, location, city #temp mytable; insert temp1(name, location, city) select name, location, city #temp; -- same temp2 , temp3. commit tran
*
excluding hacks such view instead-of trigger.
the staging table important concurrency point of view, repeating original query 3 times may result in different results if there interim concurrent changes source table.
Comments
Post a Comment