繁体   English   中英

一个查询中的两个变量 - pl / sql过程

[英]Two variables in one query - pl/sql procedure

我需要使用一个查询或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;

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

如果您只有几个产品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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM