1

我正在开发一个将返回两个游标的包。一个光标是带有数字主键的项目列表。另一个光标是与项目关联的文件列表

到目前为止的代码:

procedure get_items_with_files(
           o_results out sys_refcursor,
           o_files out sys_refcursor
) is 
begin

   begin
      open o_results for
          select item_id,
                 item_name
          from items;
   end;

   begin
      open o_files for
           select item_id
                  item_file_name
           from item_files if
           where if.item_id in (select item_id from TABLE(CAST(o_results)));
   end;
end get_items_with_files;

我遇到问题的领域:

  1. 在 table(cast(cursor)) 部分获取缺少关键字错误
  2. 我可以按原样访问代码中的光标还是需要将其复制到内部变量中?我试图创建一个 sys_refcursor 类型的变量和一个“set v_cursor := o_results”,但得到一个缺失或无效的选项错误。
4

1 回答 1

0

您不能使用O_RESULTS光标打开O_FILES光标。

您可以查询该ITEMS表以打开两个游标,但这会引入一些数据在您打开O_RESULTS游标和打开游标之间发生变化O_FILES并且两个结果集不同步的可能性。

procedure get_items_with_files(
           o_results out sys_refcursor,
           o_files out sys_refcursor
) is 
begin

   begin
      open o_results for
          select item_id,
                 item_name
          from items;
   end;

   begin
      open o_files for
           select item_id
                  item_file_name
           from item_files if
           where if.item_id in (select item_id from items);
   end;
end get_items_with_files;

返回表示连接两个表的结果的单个游标会更常见

procedure get_items_with_files(
           o_results out sys_refcursor
) is 
begin
  open o_results for
      select item_id,
             item_name,
             item_file_name
        from items
             join item_files using (item_id);
end get_items_with_files;

但是,如果您的所有过程都在打开游标,则创建视图而不是创建过程然后查询视图而不是调用过程会更常见。

于 2012-03-01T20:37:24.200 回答