How to identify which process updated a table
CREATE TABLE YourLogTable(ModificationTime DATETIME, [Application] NVARCHAR(500), [SQL] NVARCHAR(MAX))
GO
CREATE TRIGGER [dbo].[trgUpdateLogger] ON [dbo].[YourTable]
FOR INSERT,UPDATE,DELETE
AS
--Get updating process info
CREATE TABLE #s(EventType VARCHAR(30), Parameters INT, SQL NVARCHAR(MAX))
INSERT INTO #s
EXEC('DBCC INPUTBUFFER(@@SPID) WITH NO_INFOMSGS')
--Write to log table
INSERT INTO YourLogTable (ModificationTime, [Application], [SQL])
SELECT GETDATE(), APP_NAME(), #s.SQL FROM #s
--Cleanup
DROP TABLE #s
GO
Note: I found it on LinkedIN... thought it might be useful
GO
CREATE TRIGGER [dbo].[trgUpdateLogger] ON [dbo].[YourTable]
FOR INSERT,UPDATE,DELETE
AS
--Get updating process info
CREATE TABLE #s(EventType VARCHAR(30), Parameters INT, SQL NVARCHAR(MAX))
INSERT INTO #s
EXEC('DBCC INPUTBUFFER(@@SPID) WITH NO_INFOMSGS')
--Write to log table
INSERT INTO YourLogTable (ModificationTime, [Application], [SQL])
SELECT GETDATE(), APP_NAME(), #s.SQL FROM #s
--Cleanup
DROP TABLE #s
GO
Note: I found it on LinkedIN... thought it might be useful
similar to my response on MSDN TSQL forum: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/346c2eab-2a3f-4ff1-a876-d29a67f9c670/how-to-identify-which-process-updated-a-table-resolved?forum=transactsql
ReplyDelete