在 FORALL 之后插入语句和程序



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

我在程序中有以下内容

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

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 ...

最新更新