Home > Database > Mysql Tutorial > body text

放弃SCOPE_Identity,使用OUTPUT代替

WBOY
Release: 2016-06-07 17:42:43
Original
957 people have browsed it

最近项目中使用了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

,美国服务器,香港服务器租用,香港虚拟主机
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!