使用替换变量的PL SQL + SQL简单语句

时间:2015-09-16 10:12:20

标签: plsql sqltools

我想创建一个简单的IF THEN ELSE PL SQL而不创建如下所示的匿名块,以便在SQL TOOLS中使用:

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

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

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

IF值:给出a并且它不是NULL那么

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;

1 个答案:

答案 0 :(得分: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);