当嵌套表属于记录类型时,如何将数据填充到 Oracle 中的嵌套表中
Posted
技术标签:
【中文标题】当嵌套表属于记录类型时,如何将数据填充到 Oracle 中的嵌套表中【英文标题】:How to populate data into a nested table in Oracle when the nested table is within a record type 【发布时间】:2013-08-02 12:42:31 【问题描述】:我需要能够从一个过程以游标变量的形式返回一个值列表。但在列表中,一些字段可以有多个值
例如一个产品的描述字段中可以有多个描述行(从不同的表中获取)。
我正在考虑在记录类型中创建一个嵌套表并将其与游标相关联。
TYPE N_TYPE IS TABLE OF VARCHAR2(350);
TYPE TYPE1 IS RECORD ( FIELD_1 VARCHAR2(100)
, FIELD_2 VARCHAR2(30)
, FIELD_3 N_TYPE);
TYPE T_CUR IS REF CURSOR RETURN TYPE1;
Procedure p_proc (p_1 IN VARCHAR2, p_2 OUT t_cur) is
-- processing input parameter and passing out a cursor to host application
end p_proc;
在此过程中,我需要将p_1
传递到表中,并使用显式游标将数据检索到Field_1
和Field_2
。
然后我需要从另一个表中将多条记录分配给Field_3
。
当表是记录中数据类型的一部分时,谁能告诉我如何将数据填充到嵌套表中?以及如何在填充后进行检查。以及如何将其分配给 out 参数的游标变量?
【问题讨论】:
【参考方案1】:本文档:http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/composites.htm#CIHIEBJC 描述了如何在 PL/SQL 中使用集合类型: 基本示例:
DECLARE
TYPE N_TYPE IS TABLE OF VARCHAR2(350);
TYPE TYPE1 IS RECORD ( FIELD_1 VARCHAR2(100)
, FIELD_2 VARCHAR2(30)
, FIELD_3 N_TYPE);
v_n n_type;
v_type1 type1;
BEGIN
v_n := n_type(); -- initialize an empty collection
v_n.extend( 3 ); -- add 3 elements to the table
v_n( 1 ) := 'First string ';
v_n( 2 ) := 'Second string ';
v_n( 3 ) := 'Third string ';
v_n.extend; -- add 1 element at the end of the table
v_n( v_n.last ) := 'Next string';
--assign the table to the field_3 of the record
v_type1.field_3 := v_n;
-- check values
FOR i in v_type1.field_3.first .. v_type1.field_3.last LOOP
DBMS_OUTPUT.PUT_LINE( v_type1.field_3( i ) );
END LOOP;
END;
/
--- DBMS_OUTPUT -------
First string
Second string
Third string
Next string
【讨论】:
感谢您的帮助。我希望能够在主机应用程序中处理记录集 v_type1。如果记录集要从表函数返回,是否可以操作 field_3 中的单个数据项?此外,如果主机应用程序要处理游标变量,是否可以将 v_type1 作为游标变量返回?如果是这样,那么如何再次操作 field_3 中的各个项目?以上是关于当嵌套表属于记录类型时,如何将数据填充到 Oracle 中的嵌套表中的主要内容,如果未能解决你的问题,请参考以下文章
带有表类型参数的存储过程返回数据但 SqlDataAdapter 不会填充