oracle sql if condition then select statement1 else select statement2

人走茶凉 提交于 2019-12-20 04:53:52

问题


I have parameter :prmtr and what I wanted is to use a select statement based on the parameter input.

I tried this:

if :prmtr= 'A' then
    select * from tblA;
else
    select * from tblB;
end if;

But it wont work.

Is there some other way to do this?


回答1:


You may try something like this with a CURSOR variable and PRINT command. This works in SQL* plus and in SQL developer or TOAD when run as script.

VARIABLE prmtr VARCHAR2
EXEC :PRMTR := 'A'  -- SET values of parameter

VARIABLE x refcursor -- a cursor variable

DECLARE
BEGIN
    IF :PRMTR = 'A' THEN
      OPEN :x FOR
        SELECT *
        FROM   employees;
    ELSE
      OPEN :x FOR
        SELECT *
        FROM   departments;
    END IF;
END;
/

PRINT x  -- gives you the result of the query.


来源:https://stackoverflow.com/questions/49787518/oracle-sql-if-condition-then-select-statement1-else-select-statement2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!