rt:
table: Temp1 Temp2
Temp1有四个字段 A1,B1,C1,D1
Temp2也有这四个字段A1,B1,C1,D1
现在要求是先判断Temp1中的D1的值是否为0,如果为0的话就用Temp2中的D2值覆盖Temp1中的D1值,
同时两张表需要用A1=A2,B1=B2,C1=C2来定位两张表中的记录:我自己的写法:
update temp1 set temp1.D1 = ( select temp2.D1 from temp2 
where temp1.A1 = temp2.A1 and temp1.B1 = temp2.B1 and 
temp1.C1 = temp2.C1 ) where temp1.A1 in (select temp2.A1 
from temp2) and temp1.B1 in (select temp2.B1 from temp2) 
and temp1.C1 in ( select temp2.C1 from temp2) and temp1.D1 = 0 会报错说:ORA-01407: cannot update ("temp1"."D1") to NULL怎么解决呢?请各位大侠指正。。多谢啦另,工具:plsql developer数据:
select a1,b1,c1,d1 from temp2 where d1 !=0
1 7102 A9 A1 100000.00select A1,B1,C1,D1 from temp1 where A1= 7102 and dept_code = 'A9' and sta_code = 'A1' and D1 = 01 7102 A9 A1 0.00
2 7102 A9 A1 0.00
3 7102 A9 A1 0.00
4 7102 A9 A1 0.00按道理说temp1中的0.00都应该为100000.00的

解决方案 »

  1.   

    update temp1 a=(select b.d1 from temp2 b where a.a1=b.a1 and a.b1=b.b1 and a.c1=b.c1)
     where a.d1=0 and exists(select 1 from temp2 c where a.a1=c.a1 and a.b1=c.b1 and a.c1=c.c1)
      

  2.   

    update temp1 a set a.d1=(select b.d1 from temp2 b where a.a1=b.a1 and a.b1=b.b1 and a.c1=b.c1)
     where a.d1=0 and exists(select 1 from temp2 c where a.a1=c.a1 and a.b1=c.b1 and a.c1=c.c1)
      

  3.   

    这样也是可以的.
    update temp1 a=(select b.d1 from temp2 b where a.a1=b.a1 and a.b1=b.b1 and a.c1=b.c1)
     where a.d1=0 and (a.a1,a.b1,a.c1)in (select a1,b1,c1 from temp2)