检查where子句中的null条件

时间:2017-04-17 05:52:23

标签: sql oracle stored-procedures

PROCEDURE if IN_CLIENT_ID is null中,它返回GLOBAL_ID为空的行,如果IN_CLIENT_ID有某个值,则返回GLOBAL_ID=IN_CLIENT_ID行。
当我使用GLOBAL_ID=IN_CLIENT_ID结果时,如果IN_CLIENT_ID具有某个值,则结果正常。但不是IN_CLIENT_ID为空。

然后我检查IN_CLIENT_ID is null查询及其工作方面。但是我不想两次写同样的查询。

CREATE OR REPLACE PROCEDURE PROCEDURE123(
  IN_CLIENT_ID IN VARCHAR2,
  EXP_RECORDSET OUT SYS_REFCURSOR
) AS
BEGIN 
    IF IN_CLIENT_ID IS NULL THEN
        OPEN EXP_RECORDSET FOR  
        SELECT client.id  FROM GLOBAL_CLIENT client 
              WHERE  AND client.GLOBAL_ID IS NULL
          ORDER BY client.id;
    ELSE
        OPEN EXP_RECORDSET FOR  
        SELECT client.id  FROM GLOBAL_CLIENT client 
              WHERE  AND client.GLOBAL_ID=IN_CLIENT_ID
          ORDER BY client.id;
    END IF;
END;

如果在WHERE子句中有条件,是否有任何方法适用。

2 个答案:

答案 0 :(得分:2)

请尝试以下方法......

CREATE OR REPLACE PROCEDURE PROCEDURE123(
    IN_CLIENT_ID IN VARCHAR2,
    EXP_RECORDSET OUT SYS_REFCURSOR
) AS
BEGIN 
    OPEN EXP_RECORDSET FOR  
    SELECT client.id
    FROM GLOBAL_CLIENT client
    WHERE DECODE( client.GLOBAL_ID,
                  client.IN_CLIENT_ID,
                  1,
                  0 ) = 1
    ORDER BY client.id;
END;

以上是https://asktom.oracle.com/pls/asktom/f%3Fp%3D100:11:0::::P11_QUESTION_ID:4410543900346652511

如果您有任何问题或意见,请随时发表评论。

答案 1 :(得分:1)

您可以通过在where子句

中使用AND OR或连词来完成此操作
OPEN EXP_RECORDSET FOR  
    SELECT client.id  
    FROM GLOBAL_CLIENT client 
    WHERE  ( IN_CLIENT_ID IS NULL AND client.GLOBAL_ID IS NULL) OR 
           ( IN_CLIENT_ID IS NOT NULL AND client.GLOBAL_ID=IN_CLIENT_ID)
    ORDER BY client.id;