sqlsql-serversql-server-2008triggersextended-events

Create Trigger to log SQL that affected table?


I'm trying to find out what is updating the value of a column and i have very little knowledge of the application. At a quick glance I've noticed about 90% of the applications business logic is handled on the database. Needless to say the depth of SP's, functions, and triggers is crazy.

I'd like to create a trigger on the table in question that will log the SQL that affected the table. What SQL could be used to grab the executed SQL in the context of the table being updated?

Details: MS SQL Server 2008

Thanks!!


Solution

  • My Solution

    I added a trigger on the table in question that logged information i narrowed down via timestamps from sys.dm_exec_sql_text AND sys.dm_exec_query_stats. This quickly nailed down what i was looking for. Turns out there were a few triggers i didn't know about that were updating data after a UPDATE.

    SELECT 
        qStats.last_execution_time AS [ExecutedAt],
        qTxt.[text] AS [Query], qTxt.number
    FROM
        sys.dm_exec_query_stats AS qStats
        CROSS APPLY
        sys.dm_exec_sql_text(qStats.sql_handle) AS qTxt
    WHERE
        qTxt.[dbid] = @DbId
        AND qTxt.[text] like '%UPDATE%'
        AND qStats.last_execution_time between @StartExecutionSearchTime and @EndExecutionSearchTime
    ORDER BY
        qStats.last_execution_time DESC