有两个表 a、b ,其中表a中有字段a1、a2 ,表b中有字段b1、b2要求实现当a2=b2时 把b1的值赋给a1在pl/sql中执行语句:update a set a.a1 = (select b.b1 from b  where a.a2=b.b2) 报错:单行子查询返回对于一个行请求帮助

解决方案 »

  1.   

    update a  set a.a1=( 
    select b.b1  from b  where  b.b2=a.a2)
      

  2.   

    语句没错 在b.b2=a.a2 可能出现了两个以上的值
      

  3.   

    楼上的 ,你写的和我的一样呀,不行呀!这个和ORACLE的版本有关系没?
      

  4.   

    update a  set a.a1=
    (select b1 from 
    (select b.b1, b.b2
      from b where b.b2 in
           (select b2 from b group by b2 having count(b2) = 1)
                      
                         union all
    select b.b1, b.b2
      from b where b.b2 in
           (select b2 from b group by b2 having count(b2) > 1)  
       and rowid  in (select min(rowid)
                           from b group by b2
                         having count(b2) > 1)) c where c.b1=a.a1) //查询出不重复的数据,如果重复就取一条
      

  5.   


    update a set a.a1 = (select b.b1 from b where a.a2=b.b2)中的select b.b1 from b where a.a2=b.b2 有可能是多个字段,你把多个字段赋给了a.al
      

  6.   


    产生问题的原因是表A的一条记录对应表B中的多条记录,在执行更新语句时数据库无法判断应当使用B中哪条记录的值去更新表A的数据。所以需要更改SQL在其中加上挑选值的操作,例句如下:-- 如果有多条记录,使用最小的值
    update a set a.a1 = (select min(b.b1) from b where a.a2=b.b2);-- 如果有多条记录,使用最大的值
    update a set a.a1 = (select max(b.b1) from b where a.a2=b.b2);-- 如果有多条记录,使用平均值
    update a set a.a1 = (select adv(b.b1) from b where a.a2=b.b2);-- 如果有多条记录,使用第1条记录的值
    update a set a.a1 = (select b.b1 from b where a.a2=b.b2 and rownum = 1)