patternsqlMinor
SQL Server audit query that fires a trigger
Viewed 0 times
triggersqlauditquerythatserverfires
Problem
Using SQL Server, does exist a way to audit from inside a trigger the sql that fires it?
I need to know the SQL query that fires a trigger over a database without a profiler.
Thanks
I need to know the SQL query that fires a trigger over a database without a profiler.
Thanks
Solution
I have a number of triggers that do this, and I find that
At the end of this,
DBCC INPUTBUFFER is generally the best way to do it. Caution: the output is limited to 4000 characters. Very long queries will be truncated.DECLARE @sql nvarchar(max)
SET @sql = 'DBCC INPUTBUFFER(' + CAST(@@SPID AS nvarchar(100)) + ')'
CREATE TABLE #SQL (
EventType varchar(100),
Parameters int,
EventInfo nvarchar(max)
)
INSERT INTO #SQL
EXEC sp_executesql @sql
SELECT @sql = EventInfo FROM #SQL
DROP TABLE #SQLAt the end of this,
@sql contains the query for the current request. Also, you could just as easily use a table variable instead of a temp table.Code Snippets
DECLARE @sql nvarchar(max)
SET @sql = 'DBCC INPUTBUFFER(' + CAST(@@SPID AS nvarchar(100)) + ')'
CREATE TABLE #SQL (
EventType varchar(100),
Parameters int,
EventInfo nvarchar(max)
)
INSERT INTO #SQL
EXEC sp_executesql @sql
SELECT @sql = EventInfo FROM #SQL
DROP TABLE #SQLContext
StackExchange Database Administrators Q#46508, answer score: 5
Revisions (0)
No revisions yet.