What is the difference between Scope_Identity(), Identity(), @@Identity, and Ident_Current()?(Scope_Identity()、Identity()、@@Identity 和 Ident_Current() 有什么区别?)
问题描述
我知道 Scope_Identity()、Identity()、@@Identity 和 Ident_Current() 所有获取标识列的值,但我很想知道其中的区别.
I know Scope_Identity(), Identity(), @@Identity, and Ident_Current() all get the value of the identity column, but I would love to know the difference.
我遇到的部分争议是适用于上述这些功能的范围是什么意思?
Part of the controversy I'm having is what do they mean by scope as applied to these functions above?
我也想要一个使用它们的不同场景的简单示例?
I would also love a simple example of different scenarios of using them?
推荐答案
@@identity函数返回在同一会话中创建的最后一个身份.scope_identity()函数返回在同一会话和同一范围内创建的最后一个身份.ident_current(name)返回在任何会话中为特定表或视图创建的最后一个身份.identity()函数不用于获取身份,它用于在select...into查询中创建身份.- The
@@identityfunction returns the last identity created in the same session. - The
scope_identity()function returns the last identity created in the same session and the same scope. - The
ident_current(name)returns the last identity created for a specific table or view in any session. - The
identity()function is not used to get an identity, it's used to create an identity in aselect...intoquery.
会话是数据库连接.范围是当前查询或当前存储过程.
The session is the database connection. The scope is the current query or the current stored procedure.
scope_identity() 和 @@identity 函数不同的情况是,如果您在桌子上有触发器.如果您有一个插入记录的查询,导致触发器在某处插入另一条记录,则 scope_identity() 函数将返回查询创建的标识,而 @@identity 函数将返回触发器创建的标识.
A situation where the scope_identity() and the @@identity functions differ, is if you have a trigger on the table. If you have a query that inserts a record, causing the trigger to insert another record somewhere, the scope_identity() function will return the identity created by the query, while the @@identity function will return the identity created by the trigger.
所以,通常你会使用 scope_identity() 函数.
So, normally you would use the scope_identity() function.
这篇关于Scope_Identity()、Identity()、@@Identity 和 Ident_Current() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Scope_Identity()、Identity()、@@Identity 和 Ident_Current() 有什么区别?
基础教程推荐
- 无法解决整理冲突 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
