SCOPE_IDENTITY对批量插入的值

Par*_*h J 2 sql sql-server sql-server-2008

我有表MyTable,我有一After trigger(On Insert)对表.

当我运行批量插入语句时MyTable,它会触发触发器以捕获插入的数据(即用于审计日志目的).

我们截断了表格MyTable,现在在表格中插入5条记录.此外,该Auditlog表还有现有数据.假设审计日志表中已有500条记录.

我的问题是,当表有一个触发器时,在表SCOPE_IDENTITY()BULK INSERT语句中会返回什么?

--Create table 
Create table MyTable 
(
    FirstCol int identity(1,1) primary key,
    SecondCol varchar(10)
)

Create table AuditLog
(
    AID int identity(1,1) primary key,
    Comments varchar(50)
)

--Insert data to MyTable
INSERT INTO MyTable (SecondCol)
VALUES ('First'), ('Second'), ('Third'), ('Fourth'), ('Fifth')

Select * from MyTable
Select Scope_identity()  
Run Code Online (Sandbox Code Playgroud)

Guf*_*ffa 6

scope_identity回报率在同一范围内最后创建的ID.触发器在一个单独的作用域中运行,因此该函数将返回与触发器不存在时相同的内容.