【问题标题】:PLSQL: Assign array returned by one function into an array defined in another functionPLSQL:将一个函数返回的数组分配给另一个函数中定义的数组
【发布时间】:2018-10-12 10:02:42
【问题描述】:

我有两个函数——func1 和 func2。 func1 从表中选择一些值并将其分配给一个数组并返回该数组。 func2 调用 func1。 func2 使用 func1 返回的数组来执行一些操作。我的问题是:如何将 func1 返回的数组分配给 func2 中的数组。请在下面找到func1和func2的代码sn-ps。

function func1 (table1 varchar2, table2 varchar2) return j_list
  is
   type j_list is varray (10) of VARCHAR2(50);
   attr_list j_list := j_list(); 
   counter integer :=0;  
  begin   
for i in 
    (select  a.column_name from  all_tab_columns a) 

    LOOP 
      counter := counter + 1; 
      attr_list.extend; 
      attr_list(counter)  := i.column_name;
    END LOOP;
   return attr_list;
end func1;


function func2 (table1 varchar2, table2 varchar2) return varchar2
  is
   type new_j_list is varray (10) of VARCHAR2(50);
   new_attr_list j_list := j_list();
   new_attr_list.extend;
   new_attr_list() := func1 (table1, table2) /*does this assign the array                  
that is returned by func1 into the array new_attr_list ??? */
   jt varchar2(4000);
  begin  
  jt := /*some operations using the new_attr_list*/
  return jt;
end func2; 

【问题讨论】:

    标签: oracle plsql plsql-package


    【解决方案1】:

    您的代码有一些错误。你不能在不首先声明的情况下使用j_listj_list 的范围必须在使用前声明。请参阅下面如何做到这一点。此外,方法 extend 应该在 begin 块中使用,而不是在 declaration 块中。

    CREATE OR REPLACE TYPE j_list IS VARRAY (10) OF VARCHAR2(50);
    /
    
    CREATE OR REPLACE FUNCTION func1 (    table1 VARCHAR2,
                                          table2 VARCHAR2
                                      ) 
    RETURN j_list 
    IS    
        attr_list   j_list := j_list ();
        counter     INTEGER := 0;
    BEGIN
        FOR i IN ( SELECT a.column_name FROM all_tab_columns a) 
        LOOP
            counter := counter + 1;
            attr_list.extend;
            attr_list(counter) := i.column_name;
        END LOOP;
        RETURN attr_list;
    END func1;
    /
    
    CREATE OR REPLACE FUNCTION func2 ( table1 VARCHAR2,
                                      table2 VARCHAR2
    ) RETURN VARCHAR2 
    IS
        new_attr_list   j_list := j_list ();
        jt              VARCHAR2(4000);
    BEGIN
        new_attr_list.extend;
        new_attr_list:= func1(table1, table2 );
    
        jt := 'some operations using the new_attr_list';
        RETURN jt;
    END func2;
    

    【讨论】:

      猜你喜欢
      • 2015-06-04
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 2021-01-21
      • 2018-08-22
      相关资源
      最近更新 更多