AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • Início
  • system&network
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • Início
  • system&network
    • Recentes
    • Highest score
    • tags
  • Ubuntu
    • Recentes
    • Highest score
    • tags
  • Unix
    • Recentes
    • tags
  • DBA
    • Recentes
    • tags
  • Computer
    • Recentes
    • tags
  • Coding
    • Recentes
    • tags
Início / coding / Perguntas / 77297141
Accepted
Florin
Florin
Asked: 2023-10-15 22:47:03 +0800 CST2023-10-15 22:47:03 +0800 CST 2023-10-15 22:47:03 +0800 CST

Execute um procedimento armazenado no Oracle 21C tendo um parâmetro de entrada como um array

  • 772

Antes de postar a pergunta, tentei encontrar algo parecido com o que tenho, mas sem sucesso.

Aqui tenho tudo, a criação dos tipos, a criação da tabela, a inserção dos valores, a criação da procedure, tudo funciona menos a parte quando tento chamar a procedure.

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
);

Inserindo os dados

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;
/

Chamar o procedimento dessa forma não está funcionando. Este é o erro:

ORA-02315: número incorreto de argumentos para o construtor padrão

Código:

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;
/

Esta deve ser a saída:

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

Você pode ajudar como chamar esse procedimento armazenado?

Agradeço antecipadamente

sql
  • 1 1 respostas
  • 30 Views

1 respostas

  • Voted
  1. Best Answer
    MT0
    2023-10-16T04:54:00+08:002023-10-16T04:54:00+08:00

    Primeiramente, você não tem uma tabela aninhada, você tem um arquivo VARRAY.

    Em segundo lugar, você pode usar:

    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;
    /
    

    Quais saídas:

    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
    

    violino

    • 1

relate perguntas

  • Atualizando todas as linhas, exceto uma que tenha os mesmos valores em determinadas colunas

  • Existe uma maneira de inverter apenas os números quando eu retornar uma coluna sql? (hebraico)

  • SQL menor/maior comparação entre booleanos produz resultados inesperados

  • Como atualizar valores na tabela Postgres com base em uma correspondência em uma matriz

  • Como somar colunas no sql server

Sidebar

Stats

  • Perguntas 205573
  • respostas 270741
  • best respostas 135370
  • utilizador 68524
  • Highest score
  • respostas
  • Marko Smith

    destaque o código em HTML usando <font color="#xxx">

    • 2 respostas
  • Marko Smith

    Por que a resolução de sobrecarga prefere std::nullptr_t a uma classe ao passar {}?

    • 1 respostas
  • Marko Smith

    Você pode usar uma lista de inicialização com chaves como argumento de modelo (padrão)?

    • 2 respostas
  • Marko Smith

    Por que as compreensões de lista criam uma função internamente?

    • 1 respostas
  • Marko Smith

    Estou tentando fazer o jogo pacman usando apenas o módulo Turtle Random e Math

    • 1 respostas
  • Marko Smith

    java.lang.NoSuchMethodError: 'void org.openqa.selenium.remote.http.ClientConfig.<init>(java.net.URI, java.time.Duration, java.time.Duratio

    • 3 respostas
  • Marko Smith

    Por que 'char -> int' é promoção, mas 'char -> short' é conversão (mas não promoção)?

    • 4 respostas
  • Marko Smith

    Por que o construtor de uma variável global não é chamado em uma biblioteca?

    • 1 respostas
  • Marko Smith

    Comportamento inconsistente de std::common_reference_with em tuplas. Qual é correto?

    • 1 respostas
  • Marko Smith

    Somente operações bit a bit para std::byte em C++ 17?

    • 1 respostas
  • Martin Hope
    fbrereto Por que a resolução de sobrecarga prefere std::nullptr_t a uma classe ao passar {}? 2023-12-21 00:31:04 +0800 CST
  • Martin Hope
    比尔盖子 Você pode usar uma lista de inicialização com chaves como argumento de modelo (padrão)? 2023-12-17 10:02:06 +0800 CST
  • Martin Hope
    Amir reza Riahi Por que as compreensões de lista criam uma função internamente? 2023-11-16 20:53:19 +0800 CST
  • Martin Hope
    Michael A formato fmt %H:%M:%S sem decimais 2023-11-11 01:13:05 +0800 CST
  • Martin Hope
    God I Hate Python std::views::filter do C++20 não filtrando a visualização corretamente 2023-08-27 18:40:35 +0800 CST
  • Martin Hope
    LiDa Cute Por que 'char -> int' é promoção, mas 'char -> short' é conversão (mas não promoção)? 2023-08-24 20:46:59 +0800 CST
  • Martin Hope
    jabaa Por que o construtor de uma variável global não é chamado em uma biblioteca? 2023-08-18 07:15:20 +0800 CST
  • Martin Hope
    Panagiotis Syskakis Comportamento inconsistente de std::common_reference_with em tuplas. Qual é correto? 2023-08-17 21:24:06 +0800 CST
  • Martin Hope
    Alex Guteniev Por que os compiladores perdem a vetorização aqui? 2023-08-17 18:58:07 +0800 CST
  • Martin Hope
    wimalopaan Somente operações bit a bit para std::byte em C++ 17? 2023-08-17 17:13:58 +0800 CST

Hot tag

python javascript c++ c# java typescript sql reactjs html

Explore

  • Início
  • Perguntas
    • Recentes
    • Highest score
  • tag
  • help

Footer

AskOverflow.Dev

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve