@@IDENTITY,IDENT_CURRENT,SCOPE_IDENTITY,IDENT_INCR,IDENT_SEED详解

简介:

@@IDENTITY 返回最后插入的标识值的系统函数。


IDENT_CURRENT( 'table_name' ) 返回为指定的表或视图生成的最后一个标识值。所生成的最后一个标识值可以针对任何会话和任何作用域。


SCOPE_IDENTITY 返回插入到同一作用域中的标识列内的最后一个标识值。一个范围是一个模块:存储过程、触发器、函数或批处理。因此,如果两个语句处于同一个存储过程、函数或批处理中,则它们位于相同的作用域中。


IDENT_INCR ( 'table_or_view' ) 返回增量值(返回形式为 numeric (@@MAXPRECISION,0)),该值是在带有标识列的表或视图中创建标识列时指定的。

 

IDENT_CURRENT 类似于 SQL Server 2000 标识函数 SCOPE_IDENTITY 和 @@IDENTITY。这三个函数都返回最后生成的标识值。但是,上述每个函数中定义的“最后”的作用域和会话有所不同。

  • IDENT_CURRENT 返回为某个会话和用域中的指定表生成的最新标识值。

  • @@IDENTITY 返回为跨所有作用域的当前会话中的某个表生成的最新标识值。

  • SCOPE_IDENTITY 返回为当前会话和当前作用域中的某个表生成的最新标识值。

如果 IDENT_CURRENT 值为 NULL(因为表从未包含行或已被截断),IDENT_CURRENT 函数将返回种子值。

如果语句和事务失败,它们会更改表的当前标识,从而使标识列中的值出现不连贯现象。即使未提交试图向表中插入值的事务,也永远无法回滚标识值。例如,如果因 IGNORE_DUP_KEY 冲突而导致 INSERT 语句失败,表的当前标识值仍然会增加。

请谨慎使用 IDENT_CURRENT 来预报下一个生成的标识值。由于其他会话执行的插入,实际生成的值可能与 IDENT_CURRENT 加上 IDENTITY_INCR 不同。














本文转自cnn23711151CTO博客,原文链接:http://blog.51cto.com/cnn237111/476643 ,如需转载请自行联系原作者









相关文章
|
存储 关系型数据库 MySQL
超详细!Mysql错误1452 - Cannot add or update a child row: a foreign key constraint fails 原因及解决方法
超详细!Mysql错误1452 - Cannot add or update a child row: a foreign key constraint fails 原因及解决方法
1468 0
超详细!Mysql错误1452 - Cannot add or update a child row: a foreign key constraint fails 原因及解决方法
|
数据库
Incorrect table definition; there can be only one auto column and it must be defined as a key
Incorrect table definition; there can be only one auto column and it must be defined as a key
121 0
Incorrect table definition; there can be only one auto column and it must be defined as a key
When allowCredentials is true, allowedOrigins cannot contain the special value ___ since that cannot be set on the _Access-Contr
When allowCredentials is true, allowedOrigins cannot contain the special value ___ since that cannot be set on the _Access-Contr
335 1
IBASE Header change - access sequence
IBASE Header change - access sequence
100 0
IBASE Header change - access sequence
delete in ST05 trace - deletion will also lead to many DB access first
delete in ST05 trace - deletion will also lead to many DB access first
101 0
delete in ST05 trace - deletion will also lead to many DB access first
|
网络安全
RSA host key for xxx has changed and you have requested strict checking.
今天遇到了一个错误内容: RSA host key for x.x.x.x has changed and you have requested strict checking. Host key verification failed.
222 0
|
SQL 数据库 数据库管理
Replication-Replication Distribution Subsystem: agent xxxxxx failed. Column names in each table must be unique
原文:Replication-Replication Distribution Subsystem: agent xxxxxx failed. Column names in each table must be unique   最近遇到一个关于发布订阅(Replication)的奇葩问题,特此记录一下这个案例。
1019 0