MERGE


◎ MERGE

MERGE [Target_Table_Name] AS A
USING [Source_Table_Name] AS B
ON A.ID = B.ID

WHEN MATCHED THEN
    UPDATE SET A.Name = B.Name

--如果Source Table內的資料沒有存在於Target Table
--則將Source Table的資料INSERT到Target Table
WHEN NOT MATCHED BY TARGET THEN

    INSERT ( ID, Name )
    VALUES ( B.ID, B.Name )

--如果Target Table內的資料沒有存在於Source Table
--則從Target Table刪除這些不存在於Source Table的資料
WHEN NOT MATCHED BY SOURCE THEN
    DELETE;

GO

results matching ""

    No results matching ""