【发布时间】:2014-11-28 14:25:55
【问题描述】:
我有一张桌子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()
【问题讨论】:
-
最后一个查询返回了什么?
-
到目前为止我还没有创建触发器,但我想知道在上述情况下 scope_identity 会返回什么。我知道 scope_identity 返回最后一个值,但在批量插入的情况下,行为会改变吗?
标签: sql sql-server sql-server-2008