Oracle将游标提取到游标中

时间:2015-01-12 16:45:43

标签: oracle stored-procedures 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;

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

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

提前致谢。

1 个答案:

答案 0 :(得分:0)

你正在获得" ORA-06512:缺少表达"错误,因为此行中有一个额外的=符号:

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;