0

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
4

1 回答 1

0

你得到什么样的结果?在快速查看您的查询后,我的第一个想法是您得到的结果是由于

select * from #inserted

您拥有的行(从底部开始的第三行)。这条线告诉我您正在请求结果,这可能是您得到一些结果的原因?

如果您确实需要从表中选择值来修改或自定义选择,也许 CTE 会帮助您?(公用表表达式

于 2012-08-01T03:45:10.067 回答