查询刚刚插入数据的自增id

insert into Users (username) output inserted.id values('aaa')

这个最靠谱。

 

网上说的大部是

select @@IDENTITY

这个非常不靠谱,其他库的操作也会导致这个值得变化

相对靠谱的是

select ident_current('table_name') 

这个限制了表,至少其他库的,其他表的不会影响。

 

 

output在update中也可以使用

update table set strs='dddd' output inserted.strs,deleted.strs where id=1 
posted @ 2012-12-12 18:21  czcz1024  阅读(238)  评论(0编辑  收藏  举报