PLSQL - 如何将给定值数组的值检索到集合中?

发布于 2024-08-23 05:14:48 字数 1168 浏览 8 评论 0原文

我有一个接受文件夹 ID 数组并需要返回文档 ID 列表的过程。文件夹与文档以一对多的关系关联——每个文件夹有许多文档。具体来说,有一个文档表,其中包含文件夹表的parent_folderid fk。

这就是我所拥有的:

PROCEDURE get_folder_documents_ (
    paa_folderids_i IN gtyp_folderids_table
) IS                                                                                 
    lnt_temp_docids       &&MATTER_SCHEMA..docid_tab := &&MATTER_SCHEMA..docid_tab();
    lv_current_table_size NUMBER := gnt_documentids.COUNT;
BEGIN                                                     
    FOR i IN paa_folderids_i.FIRST .. paa_folderids_i.LAST
    LOOP                 
        SELECT documentid
          BULK COLLECT INTO lnt_temp_docids
          FROM t$documents
         WHERE parent_folderid = paa_folderids_i(i);

        FOR j IN 1 .. lnt_temp_docids.COUNT
        LOOP                                                   
            lv_current_table_size := lv_current_table_size + 1;

            gnt_documentids.EXTEND(1);                                   
            gnt_documentids(lv_current_table_size) := lnt_temp_docids(j);
        END LOOP;
    END LOOP;
END get_folder_documents_;

有更好的方法吗?

I have a procedure that accepts an array of folder IDs and needs to return a list of document IDs. Folders are associated to documents in a one-to-many relationship--there are many documents for each folder. Specifically, there is a documents table which has a parent_folderid fk to the folders table.

This is what I have:

PROCEDURE get_folder_documents_ (
    paa_folderids_i IN gtyp_folderids_table
) IS                                                                                 
    lnt_temp_docids       &&MATTER_SCHEMA..docid_tab := &&MATTER_SCHEMA..docid_tab();
    lv_current_table_size NUMBER := gnt_documentids.COUNT;
BEGIN                                                     
    FOR i IN paa_folderids_i.FIRST .. paa_folderids_i.LAST
    LOOP                 
        SELECT documentid
          BULK COLLECT INTO lnt_temp_docids
          FROM t$documents
         WHERE parent_folderid = paa_folderids_i(i);

        FOR j IN 1 .. lnt_temp_docids.COUNT
        LOOP                                                   
            lv_current_table_size := lv_current_table_size + 1;

            gnt_documentids.EXTEND(1);                                   
            gnt_documentids(lv_current_table_size) := lnt_temp_docids(j);
        END LOOP;
    END LOOP;
END get_folder_documents_;

Is there a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄人书 2024-08-30 05:14:48

如果 gtyp_folderids_table 被声明为 SQL 类型(而不是 PL/SQL 类型),您可以通过 table() 函数在 SQL 语句中使用它,如下所示:

    SELECT documentid
      BULK COLLECT INTO gnt_documentids
      FROM t$documents
     WHERE parent_folderid in ( select * from table( paa_folderids_i)); 

< strong>编辑

如果您想要 PL/SQL 答案,在 10g 中有一种更有效的方法 - 或者至少是一种需要更少打字的方法;)。

Oracle 引入了一些简洁的集合运算符,我们可以将它们与集合一起使用。以下示例使用 MULTISET UNION 将多个集合合并为一个...

SQL> set serveroutput on size unlimited
SQL>
SQL> declare
  2      v1 sys.dbms_debug_vc2coll
  3          := sys.dbms_debug_vc2coll('SAM I AM', 'FOX IN SOCKS');
  4      v2 sys.dbms_debug_vc2coll
  5          := sys.dbms_debug_vc2coll('MR KNOX', 'GRINCH');
  6      v3 sys.dbms_debug_vc2coll
  7          := sys.dbms_debug_vc2coll('LORAX', 'MAISIE');
  8      v_all sys.dbms_debug_vc2coll := sys.dbms_debug_vc2coll();
  9  begin
 10      dbms_output.put_line('V_ALL has '|| v_all.count() ||' elements');
 11      v_all := v1 multiset union v2;
 12      dbms_output.put_line('V_ALL has '|| v_all.count() ||' elements');
 13      v_all := v_all multiset union v3;
 14      dbms_output.put_line('V_ALL has '|| v_all.count() ||' elements');
 15  end;
 16  /
V_ALL has 0 elements
V_ALL has 4 elements
V_ALL has 6 elements

PL/SQL procedure successfully completed.

SQL>

了解有关 10g 中集合的更多信息

If gtyp_folderids_table is declared as a SQL type (as opposed to a PL/SQL type) you could use it in a SQL statement via a table() function like this:

    SELECT documentid
      BULK COLLECT INTO gnt_documentids
      FROM t$documents
     WHERE parent_folderid in ( select * from table( paa_folderids_i)); 

edit

If you want a PL/SQL answer, in 10g there is a more effective way - or at least an approach which requires less typing ;).

Oracle introduced some neat-o set operators which we can use with collections. The following example uses MULTISET UNION to munge several collections into one...

SQL> set serveroutput on size unlimited
SQL>
SQL> declare
  2      v1 sys.dbms_debug_vc2coll
  3          := sys.dbms_debug_vc2coll('SAM I AM', 'FOX IN SOCKS');
  4      v2 sys.dbms_debug_vc2coll
  5          := sys.dbms_debug_vc2coll('MR KNOX', 'GRINCH');
  6      v3 sys.dbms_debug_vc2coll
  7          := sys.dbms_debug_vc2coll('LORAX', 'MAISIE');
  8      v_all sys.dbms_debug_vc2coll := sys.dbms_debug_vc2coll();
  9  begin
 10      dbms_output.put_line('V_ALL has '|| v_all.count() ||' elements');
 11      v_all := v1 multiset union v2;
 12      dbms_output.put_line('V_ALL has '|| v_all.count() ||' elements');
 13      v_all := v_all multiset union v3;
 14      dbms_output.put_line('V_ALL has '|| v_all.count() ||' elements');
 15  end;
 16  /
V_ALL has 0 elements
V_ALL has 4 elements
V_ALL has 6 elements

PL/SQL procedure successfully completed.

SQL>

Find out more about collections in 10g.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文