使用开始和结束块时获取光标作为结果

时间:2015-01-14 16:08:24

标签: oracle delphi delphi-xe7 firedac

我试图在firedac查询编辑器中使用多个slq语句的结果作为一个结果(游标):

编辑器中的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

以上代码不能作为选择使用,并且无法组合开始和结束块。 有没有人为工作代码提供消息?

2 个答案:

答案 0 :(得分:3)

鉴于您的代码,我假设每个查询返回一个且只返回一个值

如果这是正确的,要获得所需的结果(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

答案 1 :(得分:0)

一般来说,这是一个奇怪的愿望。如果您希望从匿名块返回一些值,您应该执行以下操作:

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