create or replace procedure v_csdn is
v_id varchar2(20);
num number :=0;
v_year number;
v_month number;
v_value number;
v_value1 number;
v_sort number :=1;
v_b number := 1;
v_y number;
cursor cur is select t.id,t.year,t.month,t.value,t.sort from v_tb_test t order by t.sort,t.year,t.month;
begin
open cur;
loop
fetch cur into v_id,v_year,v_month,v_value,v_sort;
exit when cur%notfound;
if (v_sort = v_b) and (v_year = v_y) then
insert into v_tb_test1 values(v_id,v_year,v_month,v_sort,v_value-num);
commit;
num :=v_value;
else
insert into v_tb_test1 values(v_id,v_year,v_month,v_sort,v_value);
commit;
v_b := v_sort;
v_y := v_year;
num :=v_value;
end if;
end loop;
close cur;
exception when others then
dbms_output.put_line(sqlerrm);
rollback;
end;