IDENT_CURRENT 查看 表的递增字段的最后生成的ID

IDENT_CURRENT

返回为任何会话和任何作用域中的指定表最后生成的标识值。

语法

IDENT_CURRENT('table_name')

参数

table_name

是将要返回其标识值的表的名称。table_name 的数据类型为 varchar,没有默认值。

返回类型

sql_variant

注释

IDENT_CURRENT 类似于 Microsoft® SQL Server™ 2000 标识函数 SCOPE_IDENTITY 和 @@IDENTITY。这三个函数都返回最后生成的标识值。但是,它们在定义"最后"的作用域和会话上不同。

  • IDENT_CURRENT 返回为任何会话和任何作用域中的特定表最后生成的标识值。
  • @@IDENTITY 返回为当前会话的所有作用域中的任何表最后生成的标识值。
  • SCOPE_IDENTITY 返回为当前会话和当前作用域中的任何表最后生成的标识值。
示例

下面的示例说明由 IDENT_CURRENT、@@IDENTITY 和 SCOPE_IDENTITY 返回的不同的标识值。

USE pubs
DROP TABLE t6
DROP TABLE t7
GO
CREATE TABLE t6(id int IDENTITY)
CREATE TABLE t7(id int IDENTITY(100,1))
GO
CREATE TRIGGER t6ins ON t6 FOR INSERT 
AS
BEGIN
   INSERT t7 DEFAULT VALUES
END
GO
--end of trigger definition

SELECT   * FROM t6
--id is empty.

SELECT   * FROM t7
--id is empty.

--Do the following in Session 1
INSERT t6 DEFAULT VALUES
SELECT @@IDENTITY      
/*Returns the value 100, which was inserted by the trigger.*/

SELECT SCOPE_IDENTITY()   
/* Returns the value 1, which was inserted by the 
INSERT stmt 2 statements before this query.*/

SELECT IDENT_CURRENT('t7')
/* Returns value inserted into t7, i.e. in the trigger.*/

SELECT IDENT_CURRENT('t6')
/* Returns value inserted into t6, which was the INSERT statement 4 stmts before this query.*/

-- Do the following in Session 2
SELECT @@IDENTITY
/* Returns NULL since there has been no INSERT action 
so far in this session.*/

SELECT SCOPE_IDENTITY()
/* Returns NULL since there has been no INSERT action 
so far in this scope in this session.*/

SELECT IDENT_CURRENT('t7')
/* Returns the last value inserted into t7.*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lingxyd_0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值