I am trying to set up my first trigger, and it's mostly there, but I'm having a little trouble. The trigger will log all deletes on a table, along with the SQL statement that did the delete. But I'm having trouble getting the SQL statement that triggered the delete correctly. When I use DBCC INPUTBUFFER(@@SPID), it returns the correct statement, but only the first 255 characters, which isn't long enough. When I use fn_get_sql, it returns the SQL of the trigger creation instead of the SQL that triggered the delete. Here is what my trigger looks like:create trigger deletion_trigger on mytablefor deleteasbeginset nocount ondeclare @DelDate char(8),@DelTime char(12),@Handle binary(20),@Qry nvarchar(4000)select @Handle = sql_handle FROM master..sysprocesses WHERE spid = @@SPIDSELECT @Qry = convert(nvarchar(4000),[text]) FROM ::fn_get_sql(@Handle)set @DelDate = convert(varchar(8), getdate(), 112)set @DelTime = convert(varchar(12), getdate(), 114)insert into deletelog values (@DelDate, @DelTime, @Qry)end
I've read several similar questions to this on the net, but none of them seemed to ever be answered. Does anyone have any ideas on this?Thanks,Andy