如何检查数据库中是否已存在数据 [英] How do I check whether data is already present in database
问题描述
我在c#中导入了数据库中的excel文件,但是如果我再次导入同一个文件,则会重复记录。
我想避免这种重复。我的数据库结构没有任何主键。
导入我使用的是web服务并存储数据我使用的是sqlbulk
请任何人帮帮我
我尝试了什么:
我试图创建一个临时表但是什么是要解雇的确切查询,以避免重复。
I have import excel file in database in c# but if i import the same file again the record is get repeated.
I want to avoid such duplication. My database structure do not posses any primary key.
For importing I am using web service and storing the data I am using sqlbulk
please any one help me
What I have tried:
I tried to create a temporary table but what is the exact query to fired so as duplication will avoided.
推荐答案
这取决于你的数据。
没有主键的事实一般是坏事,但它不会影响这一点。
您需要知道哪些数据是唯一的。你如何识别自己的复制品?如果A,B和F列必须是唯一的,那么在插入中使用这些列(如果使用批量插入,则必须使用临时表和程序)
It depends on your data.
The fact that there is no primary key is a bad thing generally, but it won't affect this.
You need to know what data is is unique. How do you identify a duplicate yourself? If it's the case that column A, B and F must be unique, then use those columns in an insert (you will have to use a temp table and procedure for this if you use bulk insert)
--bulk insert to #temptable
insert into table (A,B,C,D,E,F,G)
select A,B,C,D,E,F,G
from #temptable tt
where not exists(
select * from table t on t.A = tt.A and t.B = tt.B and tt.F = t.F)
< b r />
当在存在(或不存在)子句中使用时,子选择实际上非常有效。
注意:我在MS SQL中写道样式,但同样适用于其他sql服务器
The sub-select is actually pretty efficient when used in an exists (or not exists) clause.
NB: I wrote in MS SQL style, but the same should apply to other sql servers
这篇关于如何检查数据库中是否已存在数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!