在Oracle 21C中执行一个存储过程,该存储过程具有一个作为数组的输入参数

cunj1qz1  于 2023-10-16  发布在  Oracle
关注(0)|答案(1)|浏览(108)

在发布这个问题之前,我试图找到一些类似于我所拥有的东西,但没有成功。
在这里我有一切,类型的创建,表的创建,值的插入,过程的创建,一切都工作,除了当我试图调用过程的部分。

CREATE OR REPLACE TYPE array_serial AS TABLE OF VARCHAR(20)

CREATE OR REPLACE TYPE ITEM_DATA AS OBJECT
(
    ITEM_CODE VARCHAR2 (50),
    serial array_serial,
    notes VARCHAR2 (1000)
);

CREATE OR REPLACE TYPE obj_good_array AS VARRAY(500) OF ITEM_DATA;

CREATE  TABLE item_data_table 
(
    item_id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    item_data obj_good_array
);

插入数据

DECLARE
    item_data_obj obj_good_array;
BEGIN
    item_data_obj := obj_good_array();
    item_data_obj.extend;
    item_data_obj(1) := ITEM_DATA(
        'ABC123',
        array_serial('Serial1', 'Serial2', 'Serial3'),
        'Sample notes for item ABC123'
    );

    INSERT INTO item_data_table (item_data) VALUES (item_data_obj);
    COMMIT;
END;
/

DECLARE
    item_data_obj obj_good_array;
BEGIN
    item_data_obj := obj_good_array();
    item_data_obj.extend;
    item_data_obj(1) := ITEM_DATA(
        'XYZ789',
        array_serial('Serial4', 'Serial5', 'Serial6'),
        'Sample notes for item XYZ789'
    );
 
    INSERT INTO item_data_table (item_data) VALUES (item_data_obj);
    COMMIT;
END;
/

select * from item_data_table;

CREATE OR REPLACE PROCEDURE process_items (
    p_item_list IN obj_good_array
) AS
BEGIN
  
    FOR i IN 1..p_item_list.COUNT LOOP
        DBMS_OUTPUT.PUT_LINE('Item Code: ' || p_item_list(i).ITEM_CODE);
        DBMS_OUTPUT.PUT_LINE('Notes: ' || p_item_list(i).notes);
        
        FOR j IN 1..p_item_list(i).serial.COUNT LOOP
            DBMS_OUTPUT.PUT_LINE('Serial ' || j || ': ' || p_item_list(i).serial(j));
        END LOOP;
        
    END LOOP;
END process_items;
/

以这种方式调用过程是不起作用的。这是错误:
ORA-02315:默认构造函数的参数数量不正确
代码:

set serveroutput on
DECLARE
    item_data_variable obj_good_array; -- Declare the obj_good_array variable
BEGIN
 
    SELECT CAST(MULTISET(
        SELECT item_data
        FROM item_data_table
    ) AS obj_good_array) INTO item_data_variable FROM dual;

    process_items(item_data_variable);
END;
/

这应该是输出:

Item Code: ABC123
Notes: Sample notes for item ABC123
Serial 1: Serial1
Serial 2: Serial2
Serial 3: Serial3

Item Code: XYZ789
Notes: Sample notes for item XYZ789
Serial 1: Serial4
Serial 2: Serial5
Serial 3: Serial6

你能告诉我如何调用这个存储过程吗?
提前感谢您

vptzau2j

vptzau2j1#

首先,你没有一个嵌套表,你有一个VARRAY
其次,您可以用途:

DECLARE
  item_data_variable obj_good_array; -- Declare the obj_good_array variable
BEGIN
  DBMS_OUTPUT.ENABLE();

  FOR rw IN (SELECT * FROM item_data_table) LOOP
    process_items(rw.item_data);
  END LOOP;
END;
/

其输出:

Item Code: ABC123
Notes: Sample notes for item ABC123
Serial 1: Serial1
Serial 2: Serial2
Serial 3: Serial3
Item Code: XYZ789
Notes: Sample notes for item XYZ789
Serial 1: Serial4
Serial 2: Serial5
Serial 3: Serial6

fiddle

相关问题