oracle 不插入重复数据

一小科技 2017-09-26 08:54:34
请问下 我有个存储过程每天要向表里插入数据,使用insert如何只插入新的数据,重复的数据不插入。

代码如下;
create or replace procedure cdr5 (v_date in out varchar2 )is


v_sql varchar2(100);

cursor shuju is select zhangh,zhhuye,kehhao from aghfh order by zhangh ;
c_shuju shuju%rowtype;
begin
v_date := to_char(sysdate,'yyyymmdd');

for c_shuju in shuju loop

insert into cdr (kehhao,zhangh,zhhuye,shij) values (c_shuju.kehhao,c_shuju.zhangh,c_shuju.zhhuye,v_date);
--目前使用的是基本的插入方式,会插入重复的数据,如何加判断不插入重复的数据
commit;
end loop;
v_sql :='update cdr set day'||to_char(sysdate,'dd')||'= zhhuye';
execute immediate v_sql;
commit;

end cdr5;
...全文
1012 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
一小科技 2017-09-26
  • 打赏
  • 举报
回复
引用 6 楼 qq646748739 的回复:
用这个:
	   
create or replace procedure cdr5(v_date out varchar2)
is
v_sql varchar2(100);
begin
    v_date := to_char(sysdate,'yyyymmdd');
    insert into cdr (kehhao,zhangh,zhhuye,shij)
	  select distinct kehhao,zhangh,zhhuye,v_date as shij
	    from aghfh t1
	   where not exists(select null from cdr t2 where t2.kehhao = t1.kehhao and t2.zhangh = t1.zhangh and t2.zhhuye =t1.zhhuye);  
	   
    commit;
	
    v_sql :='update cdr set day'||to_char(sysdate,'dd')||'= zhhuye';
    execute immediate v_sql;
    commit;
    
end cdr5;
照葫芦画瓢搞定了,谢谢 能否注释下这个句子的用法
碧水幽幽泉 2017-09-26
  • 打赏
  • 举报
回复
用这个:
	   
create or replace procedure cdr5(v_date out varchar2)
is
v_sql varchar2(100);
begin
v_date := to_char(sysdate,'yyyymmdd');
insert into cdr (kehhao,zhangh,zhhuye,shij)
select distinct kehhao,zhangh,zhhuye,v_date as shij
from aghfh t1
where not exists(select null from cdr t2 where t2.kehhao = t1.kehhao and t2.zhangh = t1.zhangh and t2.zhhuye =t1.zhhuye);

commit;

v_sql :='update cdr set day'||to_char(sysdate,'dd')||'= zhhuye';
execute immediate v_sql;
commit;

end cdr5;
碧水幽幽泉 2017-09-26
  • 打赏
  • 举报
回复
存储过程改下就OK了。
	   
create or replace procedure cdr5(v_date out varchar2)
is
v_sql varchar2(100);
begin
v_date := to_char(sysdate,'yyyymmdd');
insert into cdr (kehhao,zhangh,zhhuye,shij)
select kehhao,zhangh,zhhuye,v_date as shij
from aghfh t1
where not exists(select null from cdr t2 where t2.kehhao = t1.kehhao and t2.zhangh = t1.zhangh and t2.zhhuye =t1.zhhuye);

commit;

v_sql :='update cdr set day'||to_char(sysdate,'dd')||'= zhhuye';
execute immediate v_sql;
commit;

end cdr5;
一小科技 2017-09-26
  • 打赏
  • 举报
回复
引用 3 楼 acen_chen 的回复:
首先要搞清为什么会有重复的数据,光保证不重复很简单,只需要先判断一下有没有主键一样的数据,有用更新,没有用插入
我的数据是将每天系统里备份的数据提取需要的字段到表里,但是每天都会有之前重复的数据,目的是为了排除之前已有的数据,只将新增的数据插入表里 就是你这个意思,但是应该怎写呢?
acen_chen 2017-09-26
  • 打赏
  • 举报
回复
首先要搞清为什么会有重复的数据,光保证不重复很简单,只需要先判断一下有没有主键一样的数据,有用更新,没有用插入
一小科技 2017-09-26
  • 打赏
  • 举报
回复
引用 1 楼 qq646748739 的回复:
加个distinct去重看看! cursor shuju is select distinct zhangh,zhhuye,kehhao from aghfh order by zhangh ;
不行哦,我的意思是比如我把同样的数据连续插入两次,他不会重复。应该是要在inset into 那里曾家判断条件把??
碧水幽幽泉 2017-09-26
  • 打赏
  • 举报
回复
加个distinct去重看看!
cursor shuju is select distinct zhangh,zhhuye,kehhao from aghfh order by zhangh ;

17,382

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧