@@identity、scope_identity()、IDENT_CURRENT('tablename')函数的区别
@@IDENTITY 和SCOPE_IDENTITY 返回在当前会话中的任何表内所生成的最后一个标识值。但是,SCOPE_IDENTITY 只返回插入到当前作用域中的值;@@IDENTITY 不受限于特定的作用域。
例如,有两个表 T1 和 T2,并且在 T1 上定义了 INSERT 触发器。当将某行插入 T1 时,触发器被激发,并在 T2 中插入一行。 该方案演示了两个作用域:在 T1 上的插入,以及在 T2 通过触发器的插入。
假设 T1 和 T2 都有标识列,@@IDENTITY 和 SCOPE_IDENTITY 将在 T1 上的 INSERT 语句的最后返回不同的值。@@IDENTITY 将返回在当前会话中的任何作用域内插入的最后一个标识列的值。这是在 T2 中插入的值。SCOPE_IDENTITY() 将返回在 T1 中插入的 IDENTITY 值。这是在同一个作用域内发生的最后的插入。如果在任何 INSERT 语句作用于作用域中的标识列之前调用 SCOPE_IDENTITY() 函数,则该函数将返回空值。
如果语句和事务失败,它们会更改表的当前标识,从而使标识列中的值出现不连贯现象。即使未提交试图向表中插入值的事务,也永远无法回滚标识值。例如,如果因 IGNORE_DUP_KEY 冲突而导致 INSERT 语句失败,表的当前标识值仍然会增加。
以下示列创建两个表,TZ
和 TY
,并在 TZ
中创建一个 INSERT 触发器。当将某行插入表 TZ
中时,触发器 (Ztrig
) 将激发并在 TY
中插入一行。
USE tempdb GO CREATE TABLE TZ ( Z_id int IDENTITY(1,1)PRIMARY KEY, Z_name varchar(20) NOT NULL) INSERT TZ VALUES ('Lisa') INSERT TZ VALUES ('Mike') INSERT TZ VALUES ('Carla') SELECT * FROM TZ --Result set: This is how table TZ looks. Z_id Z_name ------------- 1 Lisa 2 Mike 3 Carla CREATE TABLE TY ( Y_id int IDENTITY(100,5)PRIMARY KEY, Y_name varchar(20) NULL) INSERT TY (Y_name) VALUES ('boathouse') INSERT TY (Y_name) VALUES ('rocks') INSERT TY (Y_name) VALUES ('elevator') SELECT * FROM TY --Result set: This is how TY looks: Y_id Y_name --------------- 100 boathouse 105 rocks 110 elevator /*Create the trigger that inserts a row in table TY when a row is inserted in table TZ*/ CREATE TRIGGER Ztrig ON TZ FOR INSERT AS BEGIN INSERT TY VALUES ('') END /*FIRE the trigger and determine what identity values you obtain with the @@IDENTITY and SCOPE_IDENTITY functions.*/ INSERT TZ VALUES ('Rosalie') SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY] GO SELECT @@IDENTITY AS [@@IDENTITY] GO
下面是结果集:
SCOPE_IDENTITY 4 /*SCOPE_IDENTITY returned the last identity value in the same scope. This was the insert on table TZ.*/ @@IDENTITY 115 /*@@IDENTITY returned the last identity value inserted to TY by the trigger. This fired because of an earlier insert on TZ.*/
http://www.cnblogs.com/MingDe/archive/2011/10/12/2208749.html
USE [tempdb] GO CREATE TABLE [dbo].[MainTB]( [Id] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](50) NULL, CONSTRAINT [PK_MainTB] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO CREATE TABLE [dbo].[SubTB]( [Id] [int] IDENTITY(100,5) NOT NULL, [Memo] [nvarchar](50) NULL, [MainKeyId] [int] NOT NULL, CONSTRAINT [PK_SubTB] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO ALTER TABLE [dbo].[SubTB] WITH CHECK ADD CONSTRAINT [FK_SubTB_MainTB] FOREIGN KEY([MainKeyId]) REFERENCES [dbo].[MainTB] ([Id]) GO ALTER TABLE [dbo].[SubTB] CHECK CONSTRAINT [FK_SubTB_MainTB] GO ---------------------------------------------------- INSERT INTO dbo.MainTB(Name) SELECT '張三' SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY] SELECT @@IDENTITY AS [@@IDENTITY] SELECT IDENT_CURRENT('MainTB') SELECT IDENT_CURRENT('SubTB') ---------------------------------------------------- INSERT INTO dbo.MainTB(Name) SELECT '李四' UNION ALL SELECT '王五' SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY] SELECT @@IDENTITY AS [@@IDENTITY] SELECT IDENT_CURRENT('MainTB') SELECT IDENT_CURRENT('SubTB') ---------------------------------------------------- INSERT INTO dbo.MainTB(Name) SELECT '李四' UNION ALL SELECT '王五' INSERT INTO dbo.SubTB(Memo) SELECT '備註一' UNION ALL SELECT '備註二' SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY] SELECT @@IDENTITY AS [@@IDENTITY] SELECT IDENT_CURRENT('MainTB') SELECT IDENT_CURRENT('SubTB') ----------------------------------------------------------- INSERT INTO dbo.MainTB(Name) SELECT '李四' UNION ALL SELECT '王五' INSERT INTO dbo.SubTB(Memo,MainKeyId) SELECT '備註一',1 UNION ALL SELECT '備註二',1 SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY] SELECT @@IDENTITY AS [@@IDENTITY] SELECT IDENT_CURRENT('MainTB') SELECT IDENT_CURRENT('SubTB') ----------------------------------------------------------- CREATE TRIGGER trgTEST ON MainTB FOR INSERT AS BEGIN INSERT INTO dbo.SubTB(Memo,MainKeyId) SELECT '備註一',SCOPE_IDENTITY() END INSERT INTO dbo.MainTB(Name) SELECT '李四' SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY] SELECT @@IDENTITY AS [@@IDENTITY] SELECT IDENT_CURRENT('MainTB') SELECT IDENT_CURRENT('SubTB') ------------------------------------------------------ INSERT INTO dbo.MainTB(Name) SELECT '李四' WAITFOR DELAY '00:00:20' SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY] SELECT @@IDENTITY AS [@@IDENTITY] SELECT IDENT_CURRENT('MainTB') SELECT IDENT_CURRENT('SubTB') SELECT * FROM dbo.MainTB SELECT * FROM dbo.SubTB --新开查询窗同时执行以下Script INSERT INTO dbo.MainTB(Name) SELECT '李四' SELECT SCOPE_IDENTITY() AS [SCOPE_IDENTITY] SELECT @@IDENTITY AS [@@IDENTITY] SELECT IDENT_CURRENT('MainTB') SELECT IDENT_CURRENT('SubTB') SELECT * FROM dbo.MainTB SELECT * FROM dbo.SubTB
IDENT_CURRENT('MainTB'):取表的最后编号,注意如果在高并发下,可能会取的不是预期的结果。