会话(Session):当客户端应用程序连接到SQL Server时,双方建立一个“会话”来交换信息。严格地说,会话与底层物理连接不同,它是连接的SQL Server逻辑表示。比如说,在 SQL Server Management Studio 新建一个查询、打开了一个对话框,即表示新建了一个”会话“;在代码中使用 SqlConnection,new了一个连接,即表示新建了一个”会话“。
作用域(Scope): 是一个模块:存储过程、触发器、函数或批处理。因此,如果两条语句位于相同的存储过程、函数或批处理中,则它们处于相同的作用域。
方法 | 描述 | 作用域 | 会话 |
---|---|---|---|
SCOPE_IDENTITY() | 返回在当前活动连接中的任何表的最后的ID | 同一作用域 | 同一会话 |
@@IDENTITY | 返回在当前活动连接中的任何表的最后的ID | 任何作用域 | 同一会话 |
IDENT_CURRENT(table_name) | 返回指定表的最后的ID | 任何作用域 | 任何会话 |
OUTPUT 子句 | 返回插入数据的任何指定的列信息(触发器必须禁用) | / | / |
有表 Tb1 及其两个 Insert 触发器(为了模拟“作用域”):Tr1、Tr1_2
有表 Tb2 及其一个 Insert 触发器(为了模拟“作用域”):Tr2
CREATE TABLE [dbo].[Tb1]( [ID] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](8) NULL, CONSTRAINT [PK_Tb1] 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]
CREATE TRIGGER [dbo].[Tr1] ON [dbo].[Tb1] AFTER INSERT AS BEGIN declare @name sysname select @name=Name from inserted insert into dbo.Tb1(Name) values(@name+'byTr1'); END
CREATE TRIGGER [dbo].[Tr1_2] ON [dbo].[Tb1] AFTER INSERT AS BEGIN declare @name sysname select @name=Name from inserted insert into dbo.Tb2(Name) values(@name+'_byTr1_2'); END
CREATE TABLE [dbo].[Tb2]( [ID] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](8) NOT NULL, CONSTRAINT [PK_Tb2] 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]
CREATE TRIGGER [dbo].[Tr2] ON [dbo].[Tb2] AFTER INSERT AS BEGIN declare @name sysname select @name=Name from inserted insert into dbo.Tb2(Name) values(@name+'_byTr2'); END
注:Tb1 的触发器及代表了“作用域”,已包括在如下个测试过程中
insert into dbo.Tb1(Name) values('User1') select SCOPE_IDENTITY() as [SCOPE_IDENTITY] select @@IDENTITY as [@@IDENTITY] select IDENT_CURRENT('dbo.Tb1') as [IDENT_CURRENT('dbo.Tb1')] select IDENT_CURRENT('dbo.Tb2') as [IDENT_CURRENT('dbo.Tb2')] select ID as [Tb1.ID], Name as [Tb1.Name] from dbo.Tb1 select ID as [Tb2.ID], Name as [Tb2.Name] from dbo.Tb2 /* truncate table dbo.Tb1 truncate table dbo.Tb2 */
输出:
(使用程序模拟并发易实现)
insert into dbo.Tb1(Name) output inserted.ID as [Tb1.output inserted.ID] values('User1') insert into dbo.Tb2(Name) values('User2') select SCOPE_IDENTITY() as [SCOPE_IDENTITY()] select @@IDENTITY as [@@IDENTITY] select IDENT_CURRENT('dbo.Tb1') as [IDENT_CURRENT('dbo.Tb1')] select IDENT_CURRENT('dbo.Tb2') as [IDENT_CURRENT('dbo.Tb2')] select ID as [Tb1.ID], Name as [Tb1.Name] from dbo.Tb1 select ID as [Tb2.ID], Name as [Tb2.Name] from dbo.Tb2 /* truncate table dbo.Tb1 truncate table dbo.Tb2 */
输出:
(使用程序模拟并发易实现)
若获取当前首次插入的记录ID,推荐使用 SCOPE_IDENTITY()