0

我想创建一个简单的 IF THEN ELSE PL SQL,而不创建像下面这样用于 SQL 工具的匿名块:

variable a varchar2(20) 
variable b varchar2(20) 

EXEC :a := '&insert_value_a'
EXEC :b := '&insert_value_b'

--下一部分我不知道该怎么做:

IF 值 :a 是给定的并且它不是 NULL THEN

EXECUTE
SELECT column1,column2,column3 
FROM table
WHERE condition
AND column2 = :a or column3 = :a;

ELSE

-- 如果给定值 b 使用它来获取值 :a

EXECUTE
SELECT column2 INTO :a 
FROM table
WHERE column_x = :b;

SELECT column1,column2,column3 
    FROM table
    WHERE condition
    AND column2 = :a or column3 = :a;
END;
4

1 回答 1

0

尝试使用 NVL,如下所示:

SELECT column1,column2,column3 
  FROM table
 WHERE condition
   AND column2 = NVL(:a,SELECT column2
                          FROM table
                         WHERE column_x = :b)
    or column3 = NVL(:a,SELECT column2
                          FROM table
                        WHERE column_x = :b);
于 2015-09-16T16:16:26.847 回答