实现序列自增,增量插入数据

create or replace package stu_pkg as
procedure stu_q;

procedure stu_p;

end stu_pkg;
/
create or replace package body stu_pkg as

/*========================================================================================
EFFECT:维表数据插入临时表
TARGET_TABLE:student_tmp
AUTHOR:Gorkor
DATE:2021-8-2
======================================================================================*/
procedure stu_q is

v_count number;
v_id number;

BEGIN

select count(*) into v_count from student_t;  

for i in 1 .. v_count-1 loop

select max(t.stu_id) into v_id from student t;  

  insert into student_tmp
    (stu_id, stu_name, update_date)
    select case
             when s.stu_name = t.stu_name then
              t.stu_id
             else
              v_id + 1
           end as stu_id,
           s.stu_name,
           sysdate
      from student_t s, student t
     where s.stu_name = t.stu_name(+);

end loop;

commit;

end stu_q;

/*========================================================================================
EFFECT:维表数据插入事实表
TARGET_TABLE:student
AUTHOR:Gorkor
DATE:2021-8-2
======================================================================================*/
procedure stu_p is

BEGIN

merge into student t

using (select s.stu_id, s.stu_name 
  from student_tmp s where s.rowid =
    (select max(s.rowid)from student_tmp t where s.stu_id =t.stu_id)) e

on (e.stu_name = t.stu_name)

when matched then
  update set t.stu_id = e.stu_id, t.update_date = sysdate
  
when not matched then
  
  insert
    (stu_id, stu_name, update_date)
  values
    (e.stu_id, e.stu_name, sysdate);

commit;

end stu_P;

end stu_pkg;
/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值