一个查询中包含两个变量-PL / SQL过程

时间:2019-04-09 10:54:39

标签: sql variables plsql

我需要使用一个查询或pl / sql过程更新多行

手动,我需要对每一行执行以下操作:

update item_product set product_no in ('8948061100060064024') where id in (1319180455);

对于不同的ID,请更新不同的product_no

我尝试了pl / sql过程

DECLARE 
   type ordersIDarray IS VARRAY(5) OF VARCHAR2(10); 
   type simArray IS VARRAY(5) OF VARCHAR2(20); 
   orders ordersIDarray;
   simNo simArray;

BEGIN 
   orders := ordersIDarray('1319180455,1319182309'); 
   simNo := simArray('8948061100060064024','8948061100060064055');

   for i in 1 .. total LOOP
   update item_product set product_no in (simNo(i)) where id in (orders(i));
   end loop;
END;

有什么想法如何在一个查询中连接两个变量?

1 个答案:

答案 0 :(得分:0)

如果您只有几个产品ID,则可以使用:

update item_product 
    set product_no = case id 
                        when 1 then '8948061100060064024'
                        when 2 then '1234'
                        when 3 then '7890'
                      end
where id in (1,2,3);

这样做的缺点是您需要重复ID列的值。

您可以通过使用带有ID列表的MERGE语句来解决此问题

merge into item_product 
using (
   select 1 as id, '8948061100060064024' as product_no from dual union all
   select 2 as id, '1234' from dual union all
   select 3 as id, '7890' from dual
) t on (t.id = item_product.id)
when matched then update 
    set product_no = t.product_no;
相关问题