[英]Oracle PL/SQL switch between raw and hierarchical select
I need in one Oracle PL/SQL select to switch between raw select and hierarchical select statements depending on some variable. 我需要在一个Oracle PL / SQL select中根据某些变量在原始选择语句和分层选择语句之间切换。 But, cannot use If statement in select.
但是,不能在选择中使用If语句。 For example, I have hierarchical statement
例如,我有层次结构声明
select a.*
from myTable a
start with a.id = varValue
connect by prior a.id = a.parentId
if some varCondition is 0. If varCondition = 1 then select should give result the same as result of statement select a.* from myTable a where a.id = varValue
Something like select a.* from myTable a start with a.id = varValue connect by prior a.id = decode(varCondition, ...)
如果某些varCondition为0。如果varCondition = 1,则select应该给出与语句结果相同的结果
select a.* from myTable a where a.id = varValue
类似于select a.* from myTable a start with a.id = varValue connect by prior a.id = decode(varCondition, ...)
Is it possible? 可能吗?
You can use NULL
in the CONNECT BY
clause to ensure it is always false when your varCondition
variable is 1
and to use the hierarchy in other cases: 您可以在
CONNECT BY
子句中使用NULL
,以确保当varCondition
变量为1
时始终为False,并在其他情况下使用层次结构:
SELECT *
FROM myTable
START WITH id = varValue
CONNECT BY PRIOR id = DECODE( varCondition, 1, NULL, parentId )
You can do it this way: 您可以这样操作:
PROCEDURE GET_RECORDS(v_action IN VARCHAR2)
IS
CURSOR get_records
IS
IF(v_action = 'DO THIS') THEN
SELECT * from <THIS>;
ELSE
SELECT * from <THAT>;
END IF;
BEGIN
OPEN get_records;
FETCH get_records
INTO v_thing;
v_loop := 0;
WHILE get_records%FOUND
LOOP
FETCH get_records
INTO v_thing;
END LOOP;
CLOSE get_records;
END;
(Source: [ Conditionally define a Cursor in Oracle ) (来源:[ 有条件地在Oracle中定义游标 )
Or you can use this: 或者您可以使用以下命令:
declare
SQL_Text varchar2(32760) := 'select * from dual'; --your query goes here
SQL_Text2 varchar2(32760) := 'select * from dual'; --your query goes here
cur sys_refcursor;
begin
IF some_condition THEN
open cur for SQL_Text;
ELSE
open cur for SQL_Text2;
END IF;
-- ...
end;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.