2012-03-01 15 views
1

我正在处理将返回两个游标的包。一个游标是具有数字主键的项目的列表。另一个光标与项目在Proc中使用Oracle Cursor并返回它?

到目前为止的代码相关联的文件列表:

  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; 
    

    我遇到的问题与该地区(cast(cursor))部分

  2. 我可以在代码中访问游标吗?还是需要将其复制到内部变量?我试图创建一个sys_refcursor类型的变量和一个“set v_cursor:= o_results”,但得到一个丢失或无效的选项错误。

回答

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; 

如果你所有的程序做的是打开游标,结果一个光标,这将是更常见的是创建视图而不是创建过程,然后查询视图而不是调用过程。

+0

谢谢。威尔正在等待一个结果,我重新分析了代码。虽然我的实际游标比例子要复杂得多,但我意识到我可以在我所关心的表格所需的字段上进行嵌套选择。 – SpaceCowboy74 2012-03-01 20:50:58

+0

哦,对于多个游标,这是因为我无法为第一个游标中的每个项目返回多行。这是一个/一个要求。我可能会做一些SQLfu并使file_name列表以逗号分隔,但我认为最终用户可以长期使用两个数据表数据集。 (每个项目有多个文件) – SpaceCowboy74 2012-03-01 20:53:35