可以在执行合并语句之前使用case语句吗?

时间:2016-06-17 15:57:43

标签: sql-server merge

专家,你好。 case语句可以用于执行或不执行合并语句吗?我是合并功能的新手。在我的情况下,我将值传递给我的存储过程中的值如下:

 @1ID int = NULL
,@1Value float = NULL
,@2ID int = NULL
,@2Value float = NULL
,@3ID int = NULL
,@3Value float = NULL
,@4ID int = NULL
,@4Value float = NULL

AS
--1
MERGE FACT_CookReading AS T
    USING
    (
        SELECT 
             @1ID
            ,@1Value

    ) AS S (
             CookSampleId
            ,ReadingValue

    ) ON (
        T.CookSampleID = S.CookSampleID

    )
    WHEN MATCHED THEN
        UPDATE SET

            ReadingValue = S.ReadingValue

    WHEN NOT MATCHED THEN
        INSERT (
            CookSampleID
            ,ReadingValue

        ) VALUES (
             @1ID
            ,@1Value

        )
;
--2
MERGE FACT_CookReading AS T
    USING
    (
        SELECT 
             @2ID
            ,@2Value

    ) AS S (
             CookSampleId
            ,ReadingValue

    ) ON (
        T.CookSampleID = S.CookSampleID

    )
    WHEN MATCHED THEN
        UPDATE SET

            ReadingValue = S.ReadingValue

    WHEN NOT MATCHED THEN
        INSERT (
            CookSampleID
            ,ReadingValue

        ) VALUES (
             @2ID
            ,@2Value

        )
;

--3
MERGE FACT_CookReading AS T
    USING
    (
        SELECT 
             @3ID
            ,@3Value

    ) AS S (
             CookSampleId
            ,ReadingValue

    ) ON (
        T.CookSampleID = S.CookSampleID


    )
    WHEN MATCHED THEN
        UPDATE SET

            ReadingValue = S.ReadingValue

    WHEN NOT MATCHED THEN
        INSERT (
            CookSampleID
            ,ReadingValue

        ) VALUES (
             @3ID
            ,@3Value

        )
;

--4
MERGE FACT_CookReading AS T
    USING
    (
        SELECT 
             @4ID
            ,@4Value

    ) AS S (
             CookSampleId
            ,ReadingValue

    ) ON (
        T.CookSampleID = S.CookSampleID

    )
    WHEN MATCHED THEN
        UPDATE SET

            ReadingValue = S.ReadingValue

    WHEN NOT MATCHED THEN
        INSERT (
            CookSampleID
            ,ReadingValue

        ) VALUES (
             @4ID
            ,@4Value

        )
;

我遇到的问题是输入的某些参数可能有空值。这会导致存储过程失败("无法将值NULL插入列...&#34 )。使用这个例子,我可能有@ 1ID / @ 1Value,@ 2ID / @ 2Value和@ 3ID / @ 3Value的值,但不是@ 4ID / @ 4Value。因此,如果@ 4ID = NULL,我希望处理@ 4ID的merge语句不执行。我希望我能做到这样的事情:

--4
CASE IF @4ID <> NULL THEN
MERGE FACT_CookReading AS T
    USING
    (
        SELECT 
             @4ID
            ,@4Value

    ) AS S (
             CookSampleId
            ,ReadingValue

    ) ON (
        T.CookSampleID = S.CookSampleID


    )
    WHEN MATCHED THEN
        UPDATE SET

            ReadingValue = S.ReadingValue

    WHEN NOT MATCHED THEN
        INSERT (
            CookSampleID
            ,ReadingValue

        ) VALUES (
             @4ID
            ,@4Value

        )
;

但似乎无法做到。可能有一些更好的方法来处理这个问题。我热切期待你的建议。谢谢你的时间!

1 个答案:

答案 0 :(得分:0)

我会使用条件更新/插入将整个内容压缩为一个MERGE。

正如Jeroen指出的那样 - ID必须是唯一的,否则可能会产生不良影响。

MERGE FACT_CookReading AS T
    USING (SELECT @1ID AS CookSampleId, @1Value AS ReadingValue UNION
           SELECT @2ID, @2Value UNION
           SELECT @3ID, @3Value UNION
           SELECT @4ID, @4Value) AS S 
    ON T.CookSampleID = S.CookSampleID

    WHEN MATCHED AND S.CookSampleID IS NOT NULL THEN
        UPDATE SET
            ReadingValue = S.ReadingValue

    WHEN NOT MATCHED AND S.CookSampleID IS NOT NULL THEN
        INSERT (CookSampleID,ReadingValue) 
        VALUES (S.CookSampleID,S.ReadingValue);