条件に基づいて、あるテーブルから別のテーブルにデータを挿入する必要があります。
1.If Key is found update records
2.If key is not found insert the record.
SQL Server 2005を使用しています。したがって、merge
ステートメントを使用できません。これを達成するための代替案を提案してください
条件に基づいて、あるテーブルから別のテーブルにデータを挿入する必要があります。
1.If Key is found update records
2.If key is not found insert the record.
SQL Server 2005を使用しています。したがって、merge
ステートメントを使用できません。これを達成するための代替案を提案してください
からコピーする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
)
IF EXISTS(--Query to check for the existence of your condition here)
BEGIN
--UPDATE HERE
END ELSE
BEGIN
--INSERT HERE
END
これは、同様の質問に対する私の回答からのストアドプロシージャの例です。
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