是只有插入当前记录在里面(给你个例子就清楚了!) --测试 create table T1(name varchar(10)) insert into T1 select 'a' union all select 'b'create table T2(name varchar(10))--建触发器 create trigger T_T1 on T1 for insert as insert into T2 select * from inserted--插入测试数据 insert into T1 select 'z' insert into T1 select 'l' insert into T1 select 'p'--查询T2 结果 select * from T2name ---------- z l p
在你建立触发器后,运行插入语句是所插入的数据都会在inserted里面的。
create table T1(name varchar(10)) go create trigger T_T1 on T1 for insert as select * from inserted go insert into t1 values('aa') insert into t1 values('bb') insert into t1 values('cc') go insert into t1 select 'aa' union all select 'bb' union all select 'cc' --执行结果 name ---------- aa(所影响的行数为 1 行)name ---------- bb(所影响的行数为 1 行)name ---------- cc(所影响的行数为 1 行)name ---------- aa bb cc(所影响的行数为 3 行) 上面的结果说明了,如果你是一条记录一条记录的插入的话,那么在inserted中实际上是你当前插入的记录的;如果你是一批记录的插入的话,那么在inserted中实际上是你这一批记录的。
会,批量插入的话会出现这种情况.例如:insert into tablename select .. from table where ..这种情况如果满足条件的是多条记录就会出现这种情况
--测试
create table T1(name varchar(10))
insert into T1 select 'a'
union all select 'b'create table T2(name varchar(10))--建触发器
create trigger T_T1 on T1
for insert
as
insert into T2 select * from inserted--插入测试数据
insert into T1 select 'z'
insert into T1 select 'l'
insert into T1 select 'p'--查询T2 结果
select * from T2name
----------
z
l
p
go
create trigger T_T1 on T1
for insert
as
select * from inserted
go
insert into t1 values('aa')
insert into t1 values('bb')
insert into t1 values('cc')
go
insert into t1 select 'aa'
union all select 'bb'
union all select 'cc'
--执行结果
name
----------
aa(所影响的行数为 1 行)name
----------
bb(所影响的行数为 1 行)name
----------
cc(所影响的行数为 1 行)name
----------
aa
bb
cc(所影响的行数为 3 行)
上面的结果说明了,如果你是一条记录一条记录的插入的话,那么在inserted中实际上是你当前插入的记录的;如果你是一批记录的插入的话,那么在inserted中实际上是你这一批记录的。