使用此设置。
create table InsteadOf
(
ID int identity primary key,
Name varchar(10) not null
)
go
create view v_InsteadOf
as
select ID, Name
from InsteadOf
go
create trigger tr_InsteadOf on InsteadOf instead of insert
as
begin
insert into InsteadOf(Name)
select Name
from inserted
end
声明
insert into v_InsteadOf(Name)
output inserted.*
select 'Name1' union all
select 'Name2'
会给你一个错误。
Msg 334, Level 16, State 1, Line 4
DML 语句不能有任何启用的触发器,如果该语句
包含一个没有 INTO 子句的 OUTPUT 子句。
使用 INTO 子句代替插入。
declare @IDs table(ID int, Name varchar(10))
insert into v_InsteadOf(Name)
output inserted.* into @IDs
select 'Name1' union all
select 'Name2'
select *
from @IDs
将0 作为值而不是null 提供给您。
ID Name
----------- ----------
0 Name1
0 Name2
您可以将输出子句放在触发器中。
create trigger tr_InsteadOf on InsteadOf instead of insert
as
begin
insert into InsteadOf(Name)
output inserted.*
select Name
from inserted
end
当您进行插入时,将为您生成输出。
insert into v_InsteadOf(Name)
select 'Name1' union all
select 'Name2'
结果:
ID Name
----------- ----------
1 Name1
2 Name2
更新:
要捕获插入语句的输出,您可以使用insert into ... exec (...)
declare @T table
(
ID int,
Name varchar(10)
)
insert into @T
exec
(
'insert into v_InsteadOf(Name)
values (''Name1''),(''Name2'')'
)