Home  >  Article  >  Database  >  放弃SCOPE_Identity,使用OUTPUT代替

放弃SCOPE_Identity,使用OUTPUT代替

WBOY
WBOYOriginal
2016-06-07 17:42:43958browse

最近项目中使用了SCOPE_IDENTITY()来获取新增数据的自动递增ID号. 在运行过程中会不时的发生无法通过SCOPE_IDENTITY()来获取ID号的情况. 尝试着测试又发现不了问题. 今天在官网发现了OUTPUT可以代替使用. 先尝试一个例子: Create Table dbo.test( nid int Id

最近项目中使用了SCOPE_IDENTITY()来获取新增数据的自动递增ID号.

在运行过程中会不时的发生无法通过SCOPE_IDENTITY()来获取ID号的情况.

尝试着测试又发现不了问题.

今天在官网发现了OUTPUT可以代替使用.

先尝试一个例子:

Create Table dbo.test(

nid int Identity(1,1),

n varchar(20)

)

declare @tmptest Table (

nid int,

n varchar(20)

)

 

---新增

insert into dbo.test(n)

OUTPUT INSERTED.nid,INSERTED.n into @tmptest

values('你好')

select * from @tmptest

select * from dbo.test

有点触发器的感觉...

对更新和删除操作同理,详细介绍地址为:

(v=sql.90).aspx

,美国服务器,香港服务器租用,香港虚拟主机
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article: asp连接SQL数据库的代码Next article: Ado.net设计模式