Oracle 将游标取入游标

Oracle fetch cursor into cursor

我只是想知道如何将游标提取到另一个游标中。 我有以下包裹:

create or replace
PACKAGE Matching AS 
  type Cursor_Return is ref Cursor;
    Procedure Get_Data
      (Cus_ID in Varchar2,
       Cursor_back OUT Cursor_Return,
       Cursor_back2 OUT Cursor_Return);
END BARCODEMATCHING;

create or replace
PACKAGE BODY Matching AS

  Procedure Matching_Proc
      (Cus_ID in Varchar2,
       Cursor_back OUT Cursor_Return,
       Cursor_back2 OUT Cursor_Return
       ) AS
  BEGIN
    Open Cursor_back for 'Select * from cus.customerHead where CustomerID = ' || cus_Id;
    Open Cursor_back2 for 'Select Cus_Location, Cus_zipcode from cus.customerBody where = CustomerID = ' || cus_ID;
    Fetch Cursor_back2 into Cursor_back;
END Matching_Proc;


END Matching;

到目前为止,这是我的代码。我只需要 return 光标:'Cursor_back'。当我尝试 运行 此代码时,出现错误: ORA-06512: 缺少表达式。 有没有办法解决这个问题?我可以声明我的两个列,我想以另一种方式移交给 Cursor_back2 吗? 我只是想 return Cursor_back 有两列(最多四列)所以我有一个像这样的输出:

cus.customerbody.cus_location | cus.customerbody.cus_zipcode | cus.customerhead.cus_id | cus.customerhead.cus_Name | and so on

提前致谢。

您收到 "ORA-06512: Missing Expression" 错误是因为您在此行中有一个额外的 = 符号:

Open Cursor_back2 for 'Select Cus_Location, Cus_zipcode from cus.customerBody where = CustomerID = ' || cus_ID;

应该是where = CustomerID =,不是where = CustomerID =。游标语句不需要是动态的,你可以使用:

Open Cursor_back for
  Select * from cus.customerHead where CustomerID = cus_Id;
Open Cursor_back2 for
  Select Cus_Location, Cus_zipcode from cus.customerBody where CustomerID = cus_ID;

如果您坚持使用动态版本,其中直到 运行 时间才能对查询进行语法检查,那么由于您将 cus_ID 作为字符串传递,您可能需要将其括起来在转义单引号中作为动态 SQL 语句的一部分。但是不要使用动态 SQL 除非你真的必须。

不过您并不真的需要两个游标,因为您正试图合并来自两个相关查询的值。您需要表之间的连接,以及单个输出参数游标,例如:

Open Cursor_back for
  Select cb.cus_location, cb.cus_zipcode, ch.cus_id, ch.cus_Name
  from cus.customerHead ch
  join cus.customerBody cb
  on cb.customerID = ch.customerID
  where ch.CustomerID =  cus_Id;