insert - update from one table to another if doesnt exist in the original table in sql server 2005 -


I have 2 tables with the same column - T1, T2 in the T2 based on the key column I want to update the table T1 column from the column: If the key column is present then update the remaining columns of T1 to T1, if not present, then enter the entire row from T2 to T1.

This query does not work:

  if existing (key in T2 ou key from selection key) (select key from T1) Update T1 SET T1 C1 = T2. C1, T1c2 = T2C2, T2 to WHERE T2.keyC (Select key from T1) ELSE (Choose from T2 * Choose from T2)   

How to fix any idea

Greg

< P> Thank you in advance,

Since you use SQL Server 2005, you can not use the merge and Micro statement to. Insert an update and one. Announced @T1 table (Key Sea Int, C1 en) @ T2 Table (Key Seam Int, C1 Int) Announce @ T1 values ​​(1, 1) Insert @t2 values ​​(1, 10) @t2 values ​​(2, 20) - update all rows @t1 set c1 = t2c1 to t1 to t1 in t1 t Type 2 as T2 on T2 .keyc = T2. KeyC - Insert new rows @ T1 (Key C, C1), select key C, C1 to T2 as T2 where not present (* T1 as * T1 Choose where T1, T1. Any = T 2.key C)

Comments