Oracle 如何在pl/sql块中填充嵌套对象表?

Oracle 如何在pl/sql块中填充嵌套对象表?,oracle,plsql,Oracle,Plsql,我在努力解决一个问题,我认为这个问题相当简单 我在数据库中有一个类型T_OPERATION_标记,创建为: CREATE OR REPLACE TYPE t_operation_tag AS OBJECT( tag_name VARCHAR2(30), tag_value VARCHAR2(30), CONSTRUCTOR FUNCTION t_operation_tag RETURN SELF AS RESULT ) 我还有另一种类型的T_OPERATION_标记,定义如下 C

我在努力解决一个问题,我认为这个问题相当简单

我在数据库中有一个类型T_OPERATION_标记,创建为:

CREATE OR REPLACE TYPE t_operation_tag AS OBJECT(
  tag_name  VARCHAR2(30),
  tag_value VARCHAR2(30),
  CONSTRUCTOR FUNCTION t_operation_tag RETURN SELF AS RESULT
)
我还有另一种类型的T_OPERATION_标记,定义如下

CREATE OR REPLACE TYPE t_operation_tags AS TABLE OF t_operation_tag;
然后在我的pl/sql块中,我有以下代码

DECLARE
   p_op_tags     t_operation_tags;
BEGIN

   p_op_tags := t_operation_tags();
   FOR i IN (SELECT tag_name, tag_value 
              FROM op_tags_table
             WHERE some_condition)
   LOOP

   --How to append new lines to p_op_tags ?

   END LOOP;

END;
因此,如果FOR循环中的SELECT查询返回五行,那么我如何用这五行填充p_OP_TAGS对象表?

如下所示:

DECLARE
   p_op_tags t_operation_tags;
   p_cursor sys_refcursor;
   p_limit number := 5;
BEGIN
 open p_cursor for
     SELECT t_operation_tag(tag_name, tag_value)
       FROM op_tags_table
  ;
  fetch p_cursor bulk collect into p_op_tags limit p_limit;
  DBMS_OUTPUT.put_line(p_op_tags(4).tag_name);
  close p_cursor;
END;
或者,如果您更喜欢循环子句:

DECLARE
  p_op_tag t_operation_tag;
  p_op_tags t_operation_tags;
  p_limit number := 5;
BEGIN
  p_op_tags := t_operation_tags();
  for i in (SELECT tag_name, tag_value 
          FROM op_tags_table
         WHERE some_condition
           and rownum < p_limit + 1)
  loop
    p_op_tag := t_operation_tag(i.tag_name, i.tag_value);
    p_op_tags.extend();
    p_op_tags(p_op_tags.COUNT) := p_op_tag;
  end loop;
  DBMS_OUTPUT.put_line(p_op_tags(4).tag_name);
END;
/ 
像这样:

DECLARE
   p_op_tags t_operation_tags;
   p_cursor sys_refcursor;
   p_limit number := 5;
BEGIN
 open p_cursor for
     SELECT t_operation_tag(tag_name, tag_value)
       FROM op_tags_table
  ;
  fetch p_cursor bulk collect into p_op_tags limit p_limit;
  DBMS_OUTPUT.put_line(p_op_tags(4).tag_name);
  close p_cursor;
END;
或者,如果您更喜欢循环子句:

DECLARE
  p_op_tag t_operation_tag;
  p_op_tags t_operation_tags;
  p_limit number := 5;
BEGIN
  p_op_tags := t_operation_tags();
  for i in (SELECT tag_name, tag_value 
          FROM op_tags_table
         WHERE some_condition
           and rownum < p_limit + 1)
  loop
    p_op_tag := t_operation_tag(i.tag_name, i.tag_value);
    p_op_tags.extend();
    p_op_tags(p_op_tags.COUNT) := p_op_tag;
  end loop;
  DBMS_OUTPUT.put_line(p_op_tags(4).tag_name);
END;
/ 

如果完全从查询填充集合,则实际上根本不需要游标或循环;您可以直接批量收集:

DECLARE
   p_op_tags     t_operation_tags;
BEGIN
   SELECT t_operation_tag(tag_name, tag_value)
   BULK COLLECT INTO p_op_tags
   FROM op_tags_table
   WHERE some_condition;

   ...
END;
/

如果完全从查询填充集合,则实际上根本不需要游标或循环;您可以直接批量收集:

DECLARE
   p_op_tags     t_operation_tags;
BEGIN
   SELECT t_operation_tag(tag_name, tag_value)
   BULK COLLECT INTO p_op_tags
   FROM op_tags_table
   WHERE some_condition;

   ...
END;
/

谢谢。第二个解决方案就是我一直在寻找的。非常感谢。第二个解决方案是我一直在寻找的。