2

重複の可能性:
アップサートのバージョンを選択/挿入:高い同時実行性のデザインパターンはありますか?

条件に基づいて、あるテーブルから別のテーブルにデータを挿入する必要があります。

1.If Key is found update records
2.If key is not found insert the record.

SQL Server 2005を使用しています。したがって、mergeステートメントを使用できません。これを達成するための代替案を提案してください

4

3 に答える 3

5

からコピーするSourceTableにはDesitinationTable

update  dst
set     col1 = src.col1
from    DestinationTable dst
join    SourceTable src
on      src.Key = dst.Key

insert  DestinationTable 
        (Key, col1)
select  Key
,       col1
from    SourceTable src
where   not exists
        (
        select  *
        from    DestinationTable dst
        where   src.Key = dst.Key
        )
于 2012-12-10T10:50:51.853 に答える
5
IF EXISTS(--Query to check for the existence of your condition here)
BEGIN
  --UPDATE HERE
END ELSE
BEGIN
  --INSERT HERE
END
于 2012-12-10T10:54:19.467 に答える
0

これは、同様の質問に対する私の回答からのストアドプロシージャの例です。

   CREATE PROCEDURE dbo.update_table1 
    @Field1 int, --key1
    @Field2 int, --data fields
    @Field3 int, 
    @Field4 int

    AS

    SET NOCOUNT ON
    update table1 set Field2=@Field2,Field3=@Field3,Field4=@Field4 
            where Field1=@Field1;
    IF(@@Rowcount=0)
    BEGIN
         insert into table1(Field1,Field2,Field3,Field4) 
                    values (@Field1,@Field2,@Field3,@Field4);
    END
    GO
于 2012-12-10T11:07:08.940 に答える