create or replace procedure test is cursor c_cursor is select sell_no,order_no from A where code1 is null or code2 is null; begin for t_cursor in c_cursor loop if t_cursor.order_no is null then update A set code1 = (select cd1 from B where A.sell_no = B.sell_no), code2 = (select cd2 from B where A.sell_no = B.sell_no) where sell_no = t_cursor.sell_no; else update A set code1 = (select no1 from C where A.order_no = C.order_no and C.index = '1'), code2 = (select no2 from C where A.order_no = C.order_no and C.index = '1') where sell_no = t_cursor.sell_no; end if; end loop; end test;
UPDATE a
SET (code1, code2) =
(SELECT DECODE (a.order_no, NULL, b.cd1, c.no1) code1,
DECODE (a.order_no, NULL, b.cd2, c.no2) code1
FROM b, c
WHERE a.sell_no = b.sell_no(+) AND a.order_no = c.order_no(+)
AND c.index='1')
WHERE a.code1 ISNULLAND a.code2 ISNULL
update a set (code1,code2)=(select code1,code2 from(
select a.sell_no nos,b.cd1 code1,b.cd2 code2 from a,b where a.order_no is null and a.sell_no=b.sell_no union select a.sell_no nos,c.no1 code1,c.no2 code2 from a,c where a.order_no is not null and a.order_no=c.order_no and c.indexs=1) d where a.sell_no=d.nos ) ;
测试了下,的确能正常执行 但少了个条件,在最后加上a.code1 is null and a.code2 is null 就行
SQL code
UPDATE
A SET ( CODE1 ,
CODE2 ) = (
SELECT
CODE1 ,
CODE2
FROM
(
SELECT
A.SELL_NO NOS ,
B.CD1 CODE1 ,
B.CD2 CODE2
FROM
A ,
B
WHERE
A.ORDER_NO ISNULLAND A.SELL_NO=B.SELL_NO
UNIONSELECT
A.SELL_NO NOS ,
C.NO1 CODE1 ,
C.NO2 CODE2
FROM
A ,
C
WHERE
A.ORDER_NO ISNOTNULLAND A.ORDER_NO=C.ORDER_NO
AND C.INDEXS=1 ) D
WHERE
A.SELL_NO=D.NOS )
WHERE
A.CODE1 ISNULLAND A.CODE2 ISNULL