循环将一张表的数据添加到另一张表,重复数据不添加

1、循环将一张表的数据添加到另一张表,重复数据不添加(a表必须存在)
create or replace procedure sp_import
as
  i_count int;  
  begin
  for r in (select * from A) loop
    --判断会员卡
    select count(*) into i_count from B where b1= r.a1;

    if i_count = 0 then
      insert into B(b1,b2,b3) select a1,a2,a3 from A where  a1=r.a1;
    end if;    
  end loop;
exception
  when others then
    --异常处理,自己写想要写什么吧
end;
--基本思路就是这样了,自己再调调试试

或者

Insert into a(a1,a2,a3) select b1,b2,b3 from b where b1 not in (select a1 from a)

 2、复制表数据和结构,但是表字段的默认值设置会丢(a表必须不存在)

create table a as select * from b

3、复制表结构。

create table 表1 as select * from  表2 where 1<>1 ;

当然,关于where字句,也可以是:1=2,1=3等等

4、存储过程异常捕捉

create or replace procedure sp_import
as
  i_count int;  
  strID varchar2(10); --新的ID
  i_userid int;       --UserID 
  strComp varchar2(10); --公司ID
  strsql varchar2(4000);
begin
  for r in (select * from A) loop
    --判断会员卡
    select count(*) into i_count from B where cardID = r.拥有会员卡号;

    if i_count = 0 then
      strID := to_char(to_number(r.编号) + 10000);
      select userid into i_userid from table where name = r.人员; --这个表你没提供,自己对着改
       select company into strComp from tableCompany where name = r.公司; --这个表你也没提供,自己对着改
       strsql := 'insert into B(ID,userid,address,company,cardID) ' ||
                 ' values (''' || strID || ''',' || i_userid || ',''' || r.地址 || 
                 ''',' || strComp || ',' || r.拥有会员卡号 || ')';
      execute immediate strsql;
    end if;    
  end loop;
  commit;
exception
  when others then
    --异常处理,自己写想要写什么吧
end;
--基本思路就是这样了,自己再调调试试

plsql 右击存储过程 -> 测试->弹出测试窗口->按f9调试

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值