1.三者的区别:
1)IDENT_CURRENT 返回为
任何会话和任何作用域中的特定表最后生成的标识值。
2)@@IDENTITY 返回为
当前会话的所有作用域中的任何表最后生成的标识值。
3)SCOPE_IDENTITY 返回为
当前会话和当前作用域中的任何表最后生成的标识值。(防止返回触发器中的insert的IDENTITY值)
2.详细的说明:
引用
SCOPE_IDENTITY, IDENT_CURRENT, and @@IDENTITY are similar functions because they return values that are inserted into identity columns.
IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the value generated for a specific table in any session and any scope. For more information, see IDENT_CURRENT (Transact-SQL).
SCOPE_IDENTITY and @@IDENTITY return the last identity values that are generated in any table in the current session. However, SCOPE_IDENTITY returns values inserted only within the current scope; @@IDENTITY is not limited to a specific scope.
For example, there are two tables, T1 and T2, and an INSERT trigger is defined on T1. When a row is inserted to T1, the trigger fires and inserts a row in T2. This scenario illustrates two scopes: the insert on T1, and the insert on T2 by the trigger.
Assuming that both T1 and T2 have identity columns, @@IDENTITY and SCOPE_IDENTITY will return different values at the end of an INSERT statement on T1. @@IDENTITY will return the last identity column value inserted across any scope in the current session. This is the value inserted in T2. SCOPE_IDENTITY() will return the IDENTITY value inserted in T1. This was the last insert that occurred in the same scope. The SCOPE_IDENTITY() function will return the null value if the function is invoked before any INSERT statements into an identity column occur in the scope.
详细参考:
http://msdn.microsoft.com/en-us/library/ms190315.aspx
分享到:
相关推荐
在SQL Server中,`@@IDENTITY`和`SCOPE_IDENTITY()`都是用来获取最近一次插入操作产生的标识列的值,但它们之间存在重要的区别。标识列通常是在表中自动生成的唯一序列号,用于记录每条记录的顺序。 `@@IDENTITY`是...
正确理解和使用 `Identity` 及其相关的函数(如 `@@IDENTITY`、`SCOPE_IDENTITY()` 和 `IDENT_CURRENT()`),可以帮助开发人员更好地管理数据库中的数据。同时,在实际应用中还需注意并发控制、数据迁移和性能优化等...
- 如果需要跨表引用`Identity`值,建议使用`SCOPE_IDENTITY()`或者`IDENT_CURRENT()`,以避免不必要的错误。 通过以上介绍可以看出,在处理数据库中的复杂数据关系时,合理运用`Identity`属性及其相关的全局变量...
`@@IDENTITY`可能返回任何会话和任何作用域中的最后一个标识值,`SCOPE_IDENTITY`仅限于当前作用域,而`IDENT_CURRENT`则不受作用域和会话限制,但需要指定表名。 在进行多表操作时,尤其是在涉及触发器的情况下,...
总结来说,理解并正确使用 `@@IDENTITY`、`SCOPE_IDENTITY()` 和 `IDENT_CURRENT()` 这三个函数对于在SQL Server中处理自增ID至关重要,特别是当数据库设计涉及复杂的触发器和多表操作时。选择合适的函数能够确保你...
而在.NET框架中,获取自递增ID的方法更加多样,可以使用SCOPE_IDENTITY()、@@IDENTITY、IDENT_CURRENT函数或者OUTPUT参数。每种方法都有其适用场景,选择合适的方法可以帮助开发者有效避免并发和作用域范围的问题。 ...
- 使用`SCOPE_IDENTITY()`、`@@IDENTITY`或`IDENT_CURRENT('table_name')`函数,但请注意它们的差异,例如`SCOPE_IDENTITY()`仅返回当前作用域内的最后生成的ID。 9. 通用分页存储过程 - 创建存储过程,接收页码...
- `IDENT_CURRENT('tableName')`:返回特定表的最后一个自增ID。 3. **使用动态SQL** 要排除自增列并处理非固定数据类型,你可以编写一个存储过程,生成动态的INSERT语句。首先,你需要获取表结构,然后构造...
- 使用`SCOPE_IDENTITY()`、`IDENT_CURRENT()`或`@@IDENTITY`函数可以获取最近插入的自增字段的值。`SCOPE_IDENTITY()`返回同一事务中最后一个语句生成的标识符,是最安全的选择。 9. **通用分页存储过程** - ...
4. **自增主键的获取**:Insert后立即使用SCOPE_IDENTITY()、IDENT_CURRENT()或@@IDENTITY获取最新插入的自增主键。 5. **性能分析优化**:使用SQL Server Profiler、Database Engine Tuning Advisor等工具。 **XML...
在其他数据库系统中,如SQL Server,类似的功能有 `SCOPE_IDENTITY()` 和 `IDENT_CURRENT()`。`SCOPE_IDENTITY()` 类似于MySQL的 `LAST_INSERT_ID()`,返回当前会话和事务中的最后一个自增ID,而 `IDENT_CURRENT()` ...
其实在MSSQL中SCOPE_IDENTITY()和IDENT_CURRENT()的区别和这里是类似的。使用SCOPE_IDENTITY()可以获得插入某个IDENTITY字段的当前会话的值,而使用IDENT_CURRENT()会获得在某个IDENTITY字段上插入的最大值,而不...
1. 使用`IDENT_CURRENT('TBName')`,它返回最后一次对指定表(`TBName`)的任何会话的INSERT操作产生的ID值,不受作用域限制。 2. 使用`@@IDENTITY`,它返回最后一条INSERT语句在任何表中生成的ID值,但可能受到多个...
在ASP.NET或C#中,可以通过数据库自增列或使用`SCOPE_IDENTITY()`、`IDENT_CURRENT()`等函数来获取刚插入记录的ID。 #### Xhtml的三种DOCTYPE及其意义 - **Strict**:禁止使用表现层标记和属性,如`<font>`。 - **...