从插入触发器中抑制结果

时间:2012-08-01 02:39:21

标签: sql sql-server triggers nocount

MS SQL插入触发器会在返回结果时导致一些悲伤。有没有办法禁止发送任何结果?我已经使用'set nocount on',但这似乎并不多...

create trigger tr_insert_raw_data
on raw_data
instead of insert
as
begin
 set nocount on

 declare @query varchar(max);
 declare @rev int;
 declare @id int;
 declare @oldrev int;
 declare @contextinfo VARBINARY(128)  

 select @contextinfo = context_info()
 if @contextinfo = 0x00001
 begin
  insert into raw_data select * from inserted
  return
 end

 select * into #query from inserted
 set @id = (select count(distinct id) from raw_data);
 set @id += 1;
 insert into revisions (username, hostname, ts) values (SYSTEM_USER, HOST_NAME(), GETDATE())
 set @rev = (select max(id) from revisions);

 select @oldrev = revision_id from inserted;
 if @oldrev is null set @oldrev=@rev-1;

 select * into #inserted from inserted
 update #inserted set revision_id = @rev, id = @id
 select * from #inserted
 insert into raw_data select * from #inserted
 insert into edges (a, b) values (@oldrev, @rev)
end

1 个答案:

答案 0 :(得分:0)

你得到了什么样的结果? 在快速审核您的查询后,我首先想到的是,由于

,您将获得结果
select * from #inserted

你有线(从底部开始的第三行)。这一行告诉我你要求的结果可能就是为什么你会得到一些?

如果您确实需要从表格中选择的值进行修改或自定义选择,CTE会帮您解决吗? (Common Table Expressions