为集合中的记录赋值时出错

Getting error while assigning values to records inside collection

我需要遍历 plsql 代码中的一些值。 所以试图创建一个记录集合。 如下所述出现错误。 请帮我解决

-- please see the code below  


    declare 
    type tab_name is record  (t_name varchar2(30),col_name varchar2(30));
    type tab_list is table of tab_name;
    table_names tab_list:=tab_list(tab_name('ABC','abc'),tab_name('XYZ','xyz'));
    begin
    for i in table_names.first..table_names.last loop
    dbms_output.put_line(table_names(i).t_name||'-'||table_names(i).col_name);
    end loop;
    end;
    /




    Error report -
    ORA-06550: line 4, column 32:
    PLS-00222: no function with name 'TAB_NAME' exists in this scope
    ORA-06550: line 4, column 13:
    PL/SQL: Item ignored
    ORA-06550: line 6, column 10:
    PLS-00320: the declaration of the type of this expression is incomplete or malformed
    ORA-06550: line 6, column 1:
    PL/SQL: Statement ignored
    06550. 00000 -  "line %s, column %s:\n%s"
    *Cause:    Usually a PL/SQL compilation error.
    *Action:

记录类型的名称不是它的初始值设定项。 事实上,oracle中根本就没有类似初始化记录类型的函数:

Documentation

Note that values are assigned separately to each field of a record in Example 5-47. You cannot assign a list of values to a record using an assignment statement. There is no constructor-like notation for records.

您必须分别创建每个对象,然后用它们创建集合:

declare 
    type tab_name is record  (t_name varchar2(30),col_name varchar2(30));
    type tab_list is table of tab_name;

    name_1 tab_name;

    table_names tab_list;
begin
   name_1.t_name := 'ABC';
   name_1.col_name := 'abc';
   table_names := tab_list();
   table_names.extend;
   table_names(table_names.last) := name_1;
   name_1.t_name := 'XYZ';
   name_1.col_name := 'xyz';
   table_names.extend;
   table_names(table_names.last) := name_1;

    for i in table_names.first..table_names.last 
    loop
        dbms_output.put_line(table_names(i).t_name||'-'||table_names(i).col_name);
    end loop;
end;

集合确实有初始化器。但是,在您的情况下,您有一组记录类型,因此您必须预先创建记录才能使用它。