有两个表的结构及数据分别如下,其中ID是对应的:----------------------------------------------------------------------------------------
表A:
 ID                Name                 Sex
2007001             李三                     F
2007002             王四                     M
2007003             赵五             F
2007004             钱六                    M
-------------------------------------------------------------------------
表B:
 ID            Salary
 2007001        100
 2007002        (上一条记录的Salary加上50) 
 ...
----------------------------------------------------------------------------------------说明,A表中有四条记录(也可能有更多条),B表中只有两条记录(也可能更多条),现在要求写个SQL语句或存储过程,将B表中的数据依据A表的ID补全,即把2007003和2007004的数据补到B表中,而Salary一列的规则是:从第二条记录开始,每项Salary等于上条记录加50,即补全后的B表数据应该如下:
 2007001        100
 2007002        150
 2007003        200
 2007004        250求这样的一条SQL语句或存储过程。

解决方案 »

  1.   

    create or replace procedure insert_ouxuan is
    cou number(9);
    sql1 varchar2(99);
    sql2 varchar2(99);
    cursor cur1 is select num1 from ouxuan2 ta where ta.num1 not in(select tb.num1 from ouxuan tb);
    begin
       sql1:='select num2 from (select * from ouxuan order by num1 desc) where rownum=1';
       execute immediate sql1 into cou;
      for c1 in cur1 loop
      cou:=cou+50;
      sql2:= 'insert into ouxuan values ('||c1.num1||','||cou||')';
      execute immediate sql2;
      end loop;
    end insert_ouxuan;
      

  2.   

    insert into b 
    (
    (select a.id,(select min(Salary) from b)+50*(rownum-1) salary from a, b
    where
    a.id=b.id(+))
    minus 
    (select * from b)
    )先算出所有的记录,然后减去b里已有的,再插入b表中
      

  3.   

    如果A表的第一条记录在B表不存在的话怎么处理?
    如果B表现有记录的salary值不符合你的定义规律的话是不是要更新现有数据?
    以B表第一条记录的salary为基准?