sql server 如何过滤向另一个表中插入重复主键的问题

来源:百度知道 编辑:UC知道 时间:2024/05/31 00:20:08
我有一个企业信息表,其中注册号是主键,分批次要插入一次数据,这样的话就有可能有主键重复的数据。如何能把那些重复的数据在插入的时候过滤掉,但不能影响其他数据的插入,最好是能把那些重复主键的数据还能放到另一个表中。
帮帮小弟!谢谢。
谢谢,现在主要的问题是,在插入新数据时怎么过滤有主键重复的记录。其他数据还的照常插入。

做个存储过程。
1条条的插入。
插入之前判断主键是否已经存在。如存在就不操作。不存在则插入

declare @count int
decalre cur_test cursor for select * from ………………
open cur_test
fetch cur_test into 变量1,变量2....
while(@@fetch_status=0)
begin
select @count = count(*) from 源表 where 主键 = @主键的变量
if @count = 0
begin
insert into 企业信息表
values(变量1,变量2...)

commit
fetch next from cur_test into 变量1,变量2....
end
close cur_test
deallocate cur_test
end

union 是去掉重复值,insert to

把该表的主建设为另一个表的外建。