如何拥有AND& DB2的SQL Merge语句中的OR条件

时间:2015-02-03 02:57:49

标签: sql db2

我有一个合并语句,当我不必考虑空值时,它会起作用:

这有效:

MERGE INTO LTABLE L
USING (SELECT 1392 UCL_USER_ID,11 REGISTER_ID ,5 REGION_ID  FROM DUAL ) B
    ON (    L.UCL_USER_ID = B.UCL_USER_ID
            AND L.REGISTER_ID = B.REGISTER_ID
            AND (L.REGION_ID = B.REGION_ID)
        )
WHEN NOT MATCHED
    THEN
        INSERT (
            L.LTABLE_ID
            ,L.UCL_USER_ID
            ,L.REGISTER_ID
            ,L.REGION_ID
            )
        VALUES (
            SEQ_LTABLE_ID.NEXTVAL
            ,1392
            ,11
            ,5);

当我必须考虑REGION_ID的空值时,下面有效:

MERGE INTO LTABLE L
USING (SELECT 1392 UCL_USER_ID,11 REGISTER_ID ,NULL REGION_ID   FROM DUAL ) B
    ON (    L.UCL_USER_ID = B.UCL_USER_ID
            AND L.REGISTER_ID = B.REGISTER_ID
            AND (L.REGION_ID IS NULL AND B.REGION_ID IS NULL)
        )
WHEN NOT MATCHED
    THEN
        INSERT (
            L.LTABLE_ID
            ,L.UCL_USER_ID
            ,L.REGISTER_ID
            ,L.REGION_ID
            )
        VALUES (
            SEQ_LTABLE_ID.NEXTVAL
            ,1392
            ,11
            ,NULL);

问题是当它可以为null或某个数值时,如何组合这两个条件。我尝试了以下但sql开发人员给出了查询不正确的错误。

AND ((L.REGION_ID = B.REGION_ID) OR (L.REGION_ID IS NULL AND B.REGION_ID IS NULL))

错误:

SQL Error: DB2 SQL Error: SQLCODE=-418, SQLSTATE=42610, SQLERRMC=FCS already resolved to different type, DRIVER=4.17.29

1 个答案:

答案 0 :(得分:0)

这只是猜测,但问题可能是NULL的默认类型。也许演员会解决这个问题:

MERGE INTO LTABLE L
USING (SELECT 1392 UCL_USER_ID, 11 as REGISTER_ID,
              CAST(NULL as VARCHAR(255)) as REGION_ID
       FROM DUAL ) B
    ON (    L.UCL_USER_ID = B.UCL_USER_ID
            AND L.REGISTER_ID = B.REGISTER_ID
            AND (L.REGION_ID IS NULL AND B.REGION_ID IS NULL)
        )

REGION_ID的适当类型。