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.
| Author |
Topic |
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2010-03-24 : 12:08:21
|
| i have a statement insert into table1(x,y,z) select x,y,z from table2 where moved=0now this works but I want to at the same time while it inserts the data into table1 it should update table2 and set field called moved=1 what's the best way to do this? |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-24 : 12:15:27
|
| [code]declare @inserted_items table(x <datatype>,y datatype,z...)insert into table1(x,y,z)output inserted.x,inserted.y,inserted.z into @inserted_itemsselect x,y,z from table2 where moved=0update t2set moved =1from table2 t2join @inserted_items ion i.x=t2.xand i.y=t2.yand i.z = t2.z[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-24 : 12:17:39
|
| if any of x,y,z is pk of table2 then you need to include only that in @inserted_items as well as in last join------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2010-03-24 : 14:20:19
|
| thanks - this is perfect -- now I have an id but if the id might not match in both tables (as auto increment) then I have to go by matching up all the other fields? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-24 : 14:24:28
|
| yup...you need to match all other fields------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|