1

我需要从过程中的集合中将一些值插入到表中,但出现ORA-00902: invalid datatype错误。

这是一个目标表:

create table t_test (col01 number, col02 number);

我在包中定义了一种集合和流水线函数:

create or replace package p_test is 
  type t_num is table of number;
  function rtn(arg_tn t_num) return t_num PIPELINED;
end p_test;
/
create or replace package body p_test is 
  function rtn(arg_tn t_num) return t_num PIPELINED is 
      tn_row number;
    begin 
      for i in arg_tn.first .. arg_tn.last loop
        tn_row := arg_tn(i);
        pipe row(tn_row);
      end loop;
      return;
  end;
end p_test;

这是我的 PL/SQL 程序:

declare
  tn_test p_test.t_num := p_test.t_num(10,20,30);
  n_num number := 69;
begin
  insert into T_TEST(col01, col02) select n_num, column_value from table(tn_test);
end;

结果表如下所示:

 col01 | col02
-------|-------
 69    | 10
 69    | 20
 69    | 30

这是我得到的错误:

错误 6550、642、306

我究竟做错了什么?如何解决?我本可以在一个for循环中完成此操作,但是对于所需目的来说效率是否太低了?

4

3 回答 3

2

我究竟做错了什么?如何解决?我本可以在 for 循环中完成此操作,但对于所需目的来说,它不是太低效了吗?

如果您正确检查错误,您可能会看到错误。错误说:

SQL 语句中不允许使用本地集合类型

这意味着在您的执行块中:

插入 T_TEST(col01, col02) select n_num, column_value from table(tn_test);

上面的说法是NOT ALLOWED

在 Oracle 11g 之前,您不能直接在块内使用的语句下使用Type在范围内声明的 if块。您需要更改范围之外的声明范围。这意味着,您需要PLSQLSQLTypePLSQLREMOVE

type t_num is table of number;从包规范中创建一个TYPESQL范围之外的。

所以你可以这样做:

Create or replace type t_num is table of number; 

请看下面的演示:

create table t_test (col01 number, col02 number);

-- Moving the type decalration under the scope of SQL.
Create or replace type t_num is table of number;

create or replace package p_test is
--  type t_num is table of number; --<-- Commenting the declaration since this is not allowed until 11g.
  function rtn(arg_tn t_num) 
    return t_num  PIPELINED;
end p_test;
/

create or replace package body p_test is
  function rtn(arg_tn t_num) 
    return t_num PIPELINED 
   is
    tn_row number;
  begin
    for i in arg_tn.first .. arg_tn.last
    loop
      tn_row := arg_tn(i);
      pipe row(tn_row);
    end loop;
    return;
  end;
end p_test;

执行:

declare
  tn_test t_num := t_num(10, 20, 30);
  n_num   number := 69;
begin
  insert into T_TEST
    (col01,
     col02)
    select n_num,
           column_value
    from   table(tn_test);
    commit;
end;

测试:

 SQL> Select * from T_TEST;

     COL01      COL02
---------- ----------
        69         10
        69         20
        69         30
于 2019-05-02T11:43:52.050 回答
2

本地定义的 PL/SQL 集合类型不能在非查询 DML 语句中使用(例如,作为表函数的参数)。只需将测试数据的初始化移至表函数即可。考虑以下示例:

create or replace package p_test is 
    type t_num is table of number;
    function rtn return t_num pipelined;
end p_test;
/
create or replace package body p_test is 
    function rtn return t_num pipelined is 
        tn_test t_num := t_num (10, 20, 30);
    begin 
        for i in 1..tn_test.count loop
            pipe row (tn_test (i));
        end loop;
        return;
    end;
end p_test;
/

begin
    insert into t_test (col01, col02) 
    select rownum, column_value from table (p_test.rtn ())
    ;
    dbms_output.put_line (sql%rowcount||' rows inserted.');  
end;
/
select * from t_test;     

3 rows inserted.

     COL01      COL02
---------- ----------
         1         10
         2         20
         3         30

如果集合类型的参数是必要的,请使用@XING 答案FORALL中建议的语句或 SQL 数据类型。

使用问题中的软件包进行演示,无需任何更改:

declare 
    sources p_test.t_num := p_test.t_num (10,20,30);
    targets p_test.t_num; 
    retrows p_test.t_num; 
    n_num number := 69;
begin
    select * bulk collect into targets
    from table (p_test.rtn (sources))
    ;
    forall i in indices of targets
    insert into t_test (col01, col02) values (n_num, targets (i)) 
    returning col01 bulk collect into retrows
    ;
    dbms_output.put_line (retrows.count||' rows inserted.'); 
end;
/

3 rows inserted.

select * from t_test;

     COL01      COL02
---------- ----------
        69         10
        69         20
        69         30
于 2019-05-07T13:26:22.223 回答
0

因为该INSERT语句在col02和之间存在类型不匹配column_value,它们的类型分别为number和。one-dimensional array如果对该数组应用迭代,则为每个迭代步骤导出单独的数字。它可以通过使用 a 来管理cursor

declare
  tn_test p_test.t_num := p_test.t_num(10,20,30);
  n_num number := 69;
begin
  for c in ( select row_number() over (order by 0) rn, column_value from table(tn_test) t)
  loop
    insert into t_test(col01,col02) values(n_num, tn_test(c.rn));
  end loop;
end;

这样,你会得到

select * from t_test;

+------+------+
|col01 |col02 |
+------+------+
|  69  | 10   |
|  69  | 20   |
|  69  | 30   |
+------+------+
于 2019-05-01T19:33:40.113 回答