使用开始和结束块时获取游标作为结果
Getting an cursor as result when using begin and end block
我正在尝试将多个 slq 语句的结果用于 firedac 查询编辑器中的一个结果(游标):
Sql 编辑器中的命令
DECLARE
MyVar1 varchar2(100);
MyVar2 varchar2(100);
BEGIN
SELECT USERINCDE INTO MyVar1 FROM P_USR WHERE USEREXCDE='PH1';
SELECT USERINCDE INTO MyVar2 FROM P_USR WHERE USEREXCDE='PH2';
END;
SELECT MyVar1,MyVar2 AS TEST FROM DUAL
以上代码不能作为 select 并且开始和结束块不能组合。
有人对工作代码有建议吗?
根据您的代码,我假设每个查询 returns 一个且只有一个值。
如果这是正确的,要获得所需的结果(1 行,2 列),您可以使用 CROSS JOIN
:
SELECT V1.USERINCDE MyVar1, V2.USERINCDE MyVar2 FROM
(SELECT USERINCDE FROM P_USR WHERE USEREXCDE='PH1') V1
CROSS JOIN
(SELECT USERINCDE FROM P_USR WHERE USEREXCDE='PH2') V2;
或者简单的两个子查询:
SELECT (SELECT USERINCDE FROM P_USR WHERE USEREXCDE='PH1') MyVar1,
(SELECT USERINCDE FROM P_USR WHERE USEREXCDE='PH2') MyVar2 FROM DUAL
总的来说这是一个奇怪的愿望。如果你想 return 来自匿名块的一些值,你应该这样做:
BEGIN
SELECT USERINCDE INTO :MyVar1 FROM P_USR WHERE USEREXCDE='PH1';
SELECT USERINCDE INTO :MyVar2 FROM P_USR WHERE USEREXCDE='PH2';
END;
但是如果你非常非常需要使用游标,你可以这样做:
DECLARE
MyVar1 varchar2(100);
MyVar2 varchar2(100);
BEGIN
SELECT USERINCDE INTO MyVar1 FROM P_USR WHERE USEREXCDE='PH1';
SELECT USERINCDE INTO MyVar2 FROM P_USR WHERE USEREXCDE='PH2';
OPEN :result FOR SELECT MyVar1, MyVar2 AS TEST FROM DUAL;
END;
在最后一种情况下,结果将是 ref cursor。
我正在尝试将多个 slq 语句的结果用于 firedac 查询编辑器中的一个结果(游标):
Sql 编辑器中的命令
DECLARE
MyVar1 varchar2(100);
MyVar2 varchar2(100);
BEGIN
SELECT USERINCDE INTO MyVar1 FROM P_USR WHERE USEREXCDE='PH1';
SELECT USERINCDE INTO MyVar2 FROM P_USR WHERE USEREXCDE='PH2';
END;
SELECT MyVar1,MyVar2 AS TEST FROM DUAL
以上代码不能作为 select 并且开始和结束块不能组合。 有人对工作代码有建议吗?
根据您的代码,我假设每个查询 returns 一个且只有一个值。
如果这是正确的,要获得所需的结果(1 行,2 列),您可以使用 CROSS JOIN
:
SELECT V1.USERINCDE MyVar1, V2.USERINCDE MyVar2 FROM
(SELECT USERINCDE FROM P_USR WHERE USEREXCDE='PH1') V1
CROSS JOIN
(SELECT USERINCDE FROM P_USR WHERE USEREXCDE='PH2') V2;
或者简单的两个子查询:
SELECT (SELECT USERINCDE FROM P_USR WHERE USEREXCDE='PH1') MyVar1,
(SELECT USERINCDE FROM P_USR WHERE USEREXCDE='PH2') MyVar2 FROM DUAL
总的来说这是一个奇怪的愿望。如果你想 return 来自匿名块的一些值,你应该这样做:
BEGIN
SELECT USERINCDE INTO :MyVar1 FROM P_USR WHERE USEREXCDE='PH1';
SELECT USERINCDE INTO :MyVar2 FROM P_USR WHERE USEREXCDE='PH2';
END;
但是如果你非常非常需要使用游标,你可以这样做:
DECLARE
MyVar1 varchar2(100);
MyVar2 varchar2(100);
BEGIN
SELECT USERINCDE INTO MyVar1 FROM P_USR WHERE USEREXCDE='PH1';
SELECT USERINCDE INTO MyVar2 FROM P_USR WHERE USEREXCDE='PH2';
OPEN :result FOR SELECT MyVar1, MyVar2 AS TEST FROM DUAL;
END;
在最后一种情况下,结果将是 ref cursor。