0

我有以下SP,

    MERGE INTO DeviceCatalog DC
    USING (SELECT @UniqueID AS UniqueID) T ON (DC.UniqueID = T.UniqueID)
    WHEN MATCHED THEN
        UPDATE   

    WHEN NOT MATCHED THEN
        INSERT  

    WHEN NOT MATCHED THEN
    IF(Condition)
    BEGIN
        EXEC DeleteDeviceID;
    END;

我不知道如何使这项工作?请帮忙

更新:我能够做到这一点,

    MERGE INTO DeviceCatalog DC
    USING (SELECT @UniqueID AS UniqueID) T ON (DC.UniqueID = T.UniqueID)
    WHEN MATCHED THEN
        UPDATE   

    WHEN NOT MATCHED THEN
        INSERT  

    WHEN NOT MATCHED THEN
        DELETE WHERE    [UniqueID] = @OldUniqueID; 
    END

但只有删除语句在抱怨。

4

2 回答 2

1

MERGE 语句的“WHEN NOT MATCHED”子句不支持删除。你必须使用'WHEN NOT MATCHED BY SOURCE'。

MERGE INTO DeviceCatalog DC
USING (SELECT @UniqueID AS UniqueID) T ON (DC.UniqueID = T.UniqueID)
WHEN MATCHED THEN
    UPDATE  SET col1='somevalue'

WHEN NOT MATCHED THEN
    INSERT INTO  article (UniqueID) VALUES 'somevalue'

WHEN NOT MATCHED BY SOURCE AND UniqueID = @OldUniqueID  THEN
    DELETE; 
于 2013-10-03T07:33:23.253 回答
0
MERGE INTO DeviceCatalog DC
    USING (SELECT @UniqueID AS UniqueID) T ON (DC.UniqueID = T.UniqueID)
    WHEN MATCHED THEN
        UPDATE  [write what you hav to update] 

    WHEN NOT MATCHED THEN
        INSERT  [write what to insert] 

    WHEN NOT MATCHED THEN
        DELETE WHERE    [UniqueID] = @OldUniqueID; 
    END

看看这个:http: //blog.sqlauthority.com/2008/08/28/sql-server-2008-introduction-to-merge-statement-one-statement-for-insert-update-delete/

于 2013-10-03T06:54:03.160 回答