Friday, June 23, 2017

Insert Trigger to collect the spid information of a insert query



create table tempid(spid int,loginname varchar(50),hostname varchar(50))
create table temptrigger(spid int,loginname varchar(50),hostname varchar(50),lastbatch datetime)

/*declare @spid int
select @spid=@@spid
--print @spid
insert into temptrigger select spid,loginame,hostname,last_batch from sys.sysprocesses where spid=@spid
select * from #tempid
--drop table #tempid
*/
CREATE TRIGGER TR_INS_WhatYouWant
    ON tempid
    AFTER INSERT
    AS
    declare @spid int
select @spid=@@spid
--print @spid
insert into temptrigger select spid,loginame,hostname,last_batch from sys.sysprocesses where spid=@spid
    GO

insert into tempid values(1,'abc','bbc')
select * from temptrigger

No comments:

Post a Comment