【问题标题】:Call procedure with nested table as parameter以嵌套表为参数调用过程
【发布时间】:2021-03-22 21:23:01
【问题描述】:

如何用嵌套表参数编写程序?在表测试中,我需要从循环中插入数据,例如。 1,2,3...

plsql

Declare
      TYPE code_nt is table of varchar2(10);
      l_codes code_nt := code_nt();
    begin    ​
    
      ​FOR i IN 1..APEX_APPLICATION.G_F01.COUNT LOOP  
        l_codes.extend;
        l_codes(i) := to_char(i);
      ​END LOOP;
      
      //here call procedure
      //PKG_EMP.INSERT_EMP(PAR_1); 
end;

包装:

create or replace PACKAGE PKG_EMP AS
    
    TYPE code_nt is table of varchar2(10);
    l_codes code_nt := code_nt();
    
    procedure INSERT_EMP (PAR_1  code_nt);
END;
    
    
create or replace PACKAGE BODY PKG_EMP AS
      
    procedure INSERT_EMP (PAR_1  code_nt) AS
    BEGIN
         INSERT INTO test (ID) VALUES (value from code_nt);
    END;  
end;

【问题讨论】:

  • @MT0 给了你答案。请注意,在您的包中声明包变量l_codes 可能没有意义。只需在此处声明类型并在需要的地方声明该类型的变量即可。
  • @JustinCave 你能在回答中描述一下吗? Tnx!

标签: oracle plsql oracle-apex


【解决方案1】:

您的代码将无法工作,因为 code_nt 在您的 PL/SQL 函数和 PL/SQL 匿名块中都是本地定义的类型,尽管它们具有相同的名称和签名,但它们是不同的数据类型。

您需要在两者中使用相同的数据类型:

Declare
  l_codes PKG_EMP.code_nt := PKG_EMP.code_nt();
begin    ​
  FOR i IN 1..APEX_APPLICATION.G_F01.COUNT LOOP  
    l_codes.extend;
    l_codes(i) := to_char(i);
    -- or
    -- l_codes(i) := TO_CHAR( APEX_APPLICATION.G_F01(i) );
  ​END LOOP;
      
  PKG_EMP.INSERT_EMP(l_codes); 
END;
/

您可以将您的包声明为:

CREATE PACKAGE PKG_EMP AS
  TYPE code_nt is table of varchar2(10);

  PROCEDURE INSERT_EMP (PAR_1 code_nt);
END;
/

CREATE PACKAGE BODY PKG_EMP AS
  procedure INSERT_EMP (PAR_1 code_nt) AS
  BEGIN
    FORALL i IN 1 .. par_1.COUNT
      INSERT INTO test (ID) VALUES ( par_1(i) );
  END;  
END;
/

db小提琴here

【讨论】:

    猜你喜欢
    • 2014-01-24
    • 1970-01-01
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    相关资源
    最近更新 更多