0

11000行(2つの列はURNと日付)のテーブルがあり、それに行を追加したいと思います。ただし、新しい行を追加するときにURNがすでに存在する場合は、日付が更新されるように前のレコードを上書きする必要があります。例えば、

select urn, GETDATE() AS Date
into table1
from table2 

urn 10253の日付が2005年5月23日で表1にあるが、urnが表2にある場合は、urn10253の日付を2012年10月10日に置き換える必要があります。

4

2 に答える 2

2

マージを使用した構文は次のとおりです。sqlserver2008から機能します。

merge into table1 t1 
using table2 t2 on t1.urn = t2.urn
when not matched then
insert (urn,date)values(t2.urn,t2.date)
when matched then
update
set t1.date = t2.date;
于 2012-10-10T13:20:05.267 に答える
1

-最初に一致するすべてのレコードを更新します

Update t1 SET date=t2.date 
from table1 t1 inner join table2 t2 
on t1.urn=t2.urn

-すべての新しいレコードを挿入します

INSERT INTO table1
select * from table2 t1 where NOT EXISTS(select * from table1 t1 where t1.urn=t2.urn)
于 2012-10-10T12:46:00.373 に答える