Gets the value of the self-growing field after SQL Server inserts data

Source: Internet
Author: User

?
12345678910111213141516171819202122232425 insert into Tb_People(uname,era,amount) values( ‘兆周‘,‘老年‘,‘10000‘)  select @@identity  --当运行完插入语句后,执行select   @@identity就可得到自动生成的id  --如果是sql server 最好用select SCOPE_IDENTITY() as id--因为@@identity全局的select SCOPE_IDENTITY() as id select IDENT_CURRENT (‘Tb_People‘)select MAX(id) from Tb_People--SCOPE_IDENTITY--返回插入到同一作用域中的 IDENTITY 列内的最后一个 IDENTITY 值。一个作用域就是一个模块——存储过程、触发器、函数或批处理。因此,如果两个语句处于同一个存储过程、函数或批处理中,则它们位于相同的作用域中。--IDENT_CURRENT--返回为任何会话和任何作用域中的指定表最后生成的标识值。这个函数需要一个以表名为值的变量,也就是说虽然不受会话和作用域的限制,却会受到表的限制。[email protected]@IDENTITY--返回最后插入的标识值。--体会:加上事务处理,两个函数一个变量没有本质区别。不加事务处理两个函数一个变量受到其他会话、作用域的影响不一样。--一直以来都是使用@@identity来获得最后一个插入到表的记录的identity值,最近发现这种方法在某种情况是不可靠的,先来看看两个概念--作用域:在SQL SERVER作用域就是一个模块-存储过程,触发器,函数或批处理--会话: 一个用户连接产生的所有上下文信息--相同点:都是返回最后插入的标识值--不同点:[email protected]@identity:返回当前会话最后一个标识值,不限于特定的作用域;--ident_current(‘tablename‘):返回任何会话,任何作用域中的指定表中生成的最后一个标识值;--scope_identity:返回当前会话当前作用域任何表生成的最后一个标识值 。

Gets the value of the self-growing field after SQL Server inserts data

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.