日期:2014-05-17  浏览次数:20564 次

SQL insert
环境:批量插入
需求:碰到数据库存在的数据就跳过
          不同的数据就插入
                
             
求一存储过程

 谢谢!!! 

------解决方案--------------------
批量的单条insert 语句吗?

可以先创建一个临时表
把数据都插入到临时表里面。然后
insert into  tb
select * from #t where not exists(select 1 from tb where name=#t.name)
------解决方案--------------------
提个思路,你可以在程序端把数据放在一个集合中,然后再批量插入到临时表,最后用临时表的数据去跟原表数据比较,把不存在的插入

--我假设你的数据已经放在了一个临时表中


insert tb
select * from #tb a where not exists(select 1 from tb b where a.c1=b.c2 and a.c2=b.c2......)
------解决方案--------------------
不就一个简单的insert嘛
insert into 目的表(列名)
select * from 来源表 a where exists (select 1 from 目的表 b where a.主键<>b.主键)
------解决方案--------------------
你试一下改成这样:
declare @name nvarchar(128)
declare @school nvarchar(128)--这两个变量用来存放你程序传入的值
if exists(select 1 from 目的表 a where name<>@name and school <>@school) 
begin
   insert into ....
--判断有没有插入成功
   if @@rowcount>0
     select 1
   else 
     select 0
end
else 
begin
   select 0
end

然后C#获取里面的值,如果是1,就是成功,0就是不成功。不论成功与否循环依旧