在ORACLE中用A表中的某几列去更新B表的某几列(B表的这几列都不允许为空),如果B表中的记录数超过A表的记录数,按照如下UPDATE语句运行时ORACLE产生如下错误:无法将B表的列更新为NULL,是否在ORACLE中使用子查询进行UPDATE时B表的数量不能超过A表?
update B
set 
(serv_id, product_id )=
(select  serv_id, product_id  
 from A where khdjls = B.khdjls and zxxh = B.zxxh
);

解决方案 »

  1.   

    update B
    set 
    (serv_id, product_id )=
    (select  serv_id, product_id  
     from A where khdjls = B.khdjls and zxxh = B.zxxh
    ) where exists select 1 from A where A.khdjls = B.khdjls and A.zxxh = B.zxxh你那句的意思除了更新相同的记录外,还将B表中存在的,A表中不存在的主键的行全部置成空
    select id,name from a;
    id   , name
    1      aaa
    2      bbb
    3      cccselect id,name from b;
    1      aa
    2      bb
    3      cc
    4      ddupdate b set name = (select name from a where a.id = b.id);执行后
    select id, name from b;
    1       aaa
    2       bbb
    3       ccc
    4       
    你可以自己去试试看你这种更新最好是这么写,如果khdjls 和 zxxh 存在联合主键的话
    update (select a.serv_id serv_idA, a.product_id product_idA,b.serv_id serv_idB, b.product_id product_idB from A ,B where a.khdjls = B.khdjls and A.zxxh = B.zxxh ) set serv_idB,product_idB = serv_idA,product_idA; 
      

  2.   

    楼上正解,Oracle和Sql Server的这个语句有些不同