Query text to get in insert/update/delete trigger

  • All,

    I want to get the text of query that is just inserted/updated/delete (DML Statements). Query text that causes the trigger to fire.

    For e.g. I have an after insert trigger trig1 on table table1

    If insert query executed for table1 say "insert into table1 values ('AnyValue')" that will cause trig1 to fire. Inside that trigger I want to get the query text/insert statement that caused the trigger to fire.

    How can I?

  • [font="Verdana"]Turn on SQL Profiler then.

    Mahesh[/font]

    MH-09-AM-8694

  • i want to get the query inside that trigger! (programatically)

  • I'm not sure there is a way. You can get the currently executing statement from the sys.dm_exec_sql_text funtion, getting the sql_handle from sys.dm_exec_connections or sys.dm_exec_sessions, but that will just show you the definition of the trigger.

    The SQL engine doesn't keep track of what statements a user executed in the past, just the ones it's running now.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • I have tried using the following tables but still not able to get the result

    SELECT deqs.last_execution_time AS [Time], dest.TEXT AS [Query]

    FROM sys.dm_exec_query_stats AS deqs

    CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest

    ORDER BY deqs.last_execution_time DESC

    but this shows the information related to Create procedures and select, no DML records found

    even if I can get the last executed DML statement then that will be fine!

  • Query stats has no user-related info in. If you query that for the most recent statement, you'll get the most recent statement by any user on the server, not by your connection.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • That is not the problem for me as I am doing all this for auditing purpose rather thats good if I am getting user information too. Simply I want to log whole query that raise the trigger and thats it. How?

  • I don't think you can, unless you have some profiler or a server-side trace running.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Why do you want the query text in the trigger.

    "Keep Trying"

  • For generic auditing purpose!

  • Select definition

    from sys.all_sql_modules m

    join sys.objects o on m.object_id = o.object_id

    where o.type in ('TR','TA')

    and o.name = 'YourTriggerName'

  • Try: DBCC INPUTBUFFER (@@SPID)

Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply