公司表       A   id name
用户表       B   id name a_id
tbale表      C   id b_id a_id
oracle 查c表,根据表里的用户ID,找到用户所在的公司,把公司ID,插到c表中
请教!麻烦加上注释。

解决方案 »

  1.   

    create or replace procedure goalgoalgoal
    isbegin
       for temp_cur in (select distinct b_id from c)
       loop
          update c set a_id=
             (select a.id from a,b where a.id=b.a_id and b.id=temp_cur.b_id)
          where c.bid=temp_cur.b_id;
       commit;end goalgoalgoal;
      

  2.   

    create or replace procedure goalgoalgoal 
    is begin 
      for temp_cur in (select distinct b_id from c) --取出c里的所有b_id,即用户
      loop    --循环更新该b_id所对应的a_id
          update c set a_id= 
            (select distinct a.id from a,b where a.id=b.a_id and b.id=temp_cur.b_id) 
          where c.bid=temp_cur.b_id; 
      commit; 
      end loop;end goalgoalgoal;上面少了end loop,呵呵。
      

  3.   

    update c set a_id=(select distinct a_id from b where id = b.b_id)
      

  4.   

    这个不需要用到a表
    update c set a_id=(select a_id from b where id = c.b_id and rownum<2)
      

  5.   

    update c set a_id= 
            (select a.id from a,b where a.id=b.a_id and b.id=temp_cur.b_id) 
          where c.bid=temp_cur.b_id;直接执行就可以了!
      

  6.   

    6楼的意思是在(select a.id from a,b where a.id=b.a_id and b.id=temp_cur.b_id) 不用distinctA表id应该不会出现重复的情况,不用去重。