首页 新闻 会员 周边

sqlserver批量插入

0
悬赏园豆:20 [已关闭问题] 关闭于 2013-08-02 17:45

sqlserver批量插入 怎么得到刚刚插入的所有的主键

问题补充:

declare @t table (id int)
if(exists(select name from tempdb..sysobjects where name like'%test%' and type='U'))
   drop table #test
Create Table #test(ids int )
insert into 192.168.1.10.数据库.表1(name,password) output inserted.id into @t
select name ,password
from [user]
where not exists(select b.name from 192.168.1.10.数据库.表1 b where  name= b.name)
insert into  #test(ids) SELECT id FROM @t
insert into insert into 192.168.1.10.数据库.表2(truename,userid)
select username,ids from #test

output不能远程 有没有别的办法 能获取到刚刚插入的所有的主键ID  插入到 表2中

cnns的主页 cnns | 初学一级 | 园豆:1
提问于:2013-08-02 11:02
< >
分享
所有回答(2)
0

你是怎么批量插入的?单条循环的话,可以用

select @@identity

如果是SqlBulkCopy等其他方式,可以在插入前纪录最大ID(maxBeforeId),插入后纪录最大ID(maxAfterId),然后去查询主键>maxBeforeId and <=maxAfterId的Id即可。多用户同时插入需要注意下~

幻天芒 | 园豆:37175 (高人七级) | 2013-08-02 11:08
select @@identity 远程跨服务器 返回的是NULL 而且不能保证我在倒数据的时候 有人在手动插入 
支持(0) 反对(0) cnns | 园豆:1 (初学一级) | 2013-08-02 11:14

@cnns: 在其他表加状态标识,如果正在导入,禁止手动插入,然后取插入前id和插入后id的区间,这样比较好~

支持(0) 反对(0) 幻天芒 | 园豆:37175 (高人七级) | 2013-08-02 12:54
0

有什么办法?导数据的时候记下唯一区分信息,再查一次

滴答的雨 | 园豆:3660 (老鸟四级) | 2013-08-02 11:50
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册