问题
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