在FORALL之后有插入语句和过程

时间:2013-09-08 12:40:44

标签: oracle plsql oracle10g forall

如何在plsql中的FORALL之后有一个insert语句和调用过程?

我在程序中有以下内容

FORALL indx IN p_product.FIRST .. p_product.LAST
        INSERT INTO   my_table
              VALUES   (p_product(indx),p_product_desc(indx),p_msg);

在插入之后,我想调用另一个将值插入另一个表的过程。

remove_dup_products(p_product(indx));

当我在插入语句后尝试调用上述过程时,我收到错误

INDX must be declared

1 个答案:

答案 0 :(得分:2)

FORALL声明就是这样;一份声明;你只能做一件事。你必须再次遍历你的类型。

forall indx in p_product.first .. p_product.last
   insert into my_table
   values (p_product(indx), p_product_desc(indx), p_msg);

for indx in p_product.first .. p_product.last loop
   remove_dup_products(p_product(indx));
end loop;

你没有做两个DML语句是没有价值的;你正在做一个并调用一个程序。因此,您不能两次使用FORALL,您必须使用常规for循环。

如果您只是 在第二个程序中执行DML,则可以传入整个集合,然后使用FORALL。您需要声明一个全局变量:

create or replace package something is

   type t__product is table of product.product%type;
   t_product t__product;

   ...

然后你可以在任何地方重复使用

create or replace package body something is

procedure current_proc is

begin

   forall indx in p_product.first .. p_product.last
      insert into my_table
      values (p_product(indx), p_product_desc(indx), p_msg);

   remove_dup_products(p_product);

end current_proc;

-------------------------------------------------------------

procedure remove_dup_products (p_product in t_product) is

begin

    forall in p_product.first .. p_product.last
       delete ...