oracle sql if condition then select statement1 else select statement2

前端 未结 1 410
情话喂你
情话喂你 2021-01-24 07:18

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         


        
1条回答
  •  粉色の甜心
    2021-01-24 08:16

    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.
    

    0 讨论(0)
提交回复
热议问题