2015-09-16 72 views
0

我想沒有在SQL工具,如下面創建一個annonymous塊以創建一個簡單的IF THEN ELSE PL SQL:PL SQL + SQL簡單語句中使用替代變量

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

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

- 這下一部分我不「知道怎麼做:

IF值:一個被賦予它不是null,則

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

ELSE 

- 如果值b給出使用它來獲取價值:一個

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; 

回答

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); 
+0

謝謝,成功了! – user5240377