-- 表1.BBB与表2.CCC为关联字段,根据关联字段从表2查出N条记录,取第二条记录Update表1.AAA字段
-- 此SQL运行至where b.CCC = 表1.BBB 报错,无法识别表1.BBB。
update 表1 
   set 表1.AAA =
       (select b2.DDD
          from (select b.*, rownum as brn
                  from 表2 b
                 where b.CCC = 表1.BBB
                   and rownum < 3) b2
         where b2.brn > 1);怎么实现?
多谢!

解决方案 »

  1.   


    没有看明白你 rownum < 3 的逻辑是什么..为什么呢? 
      

  2.   

    merge into 表1 t1
    using (select ccc, ddd
             from (select ccc, ddd, rownum rn from 表2 where rownum < 3)
            where rn > 1) t2
    on (t1.bbb = t2.ccc)
    when matched then
      update set t1.aaa = t2.ddd;
      

  3.   

    为了取查询出来的第二条数据,用的是rownum>1 然后再 rownum<3
      

  4.   


    恩,使用merge也是一个好的方法.