Catch SQL statement ?

  • Hi there,

    anybody knows if it's possisble to catch the latest sql statement fired on a certain server ?

    I have this trigger that logs all deletes on a crucial tablem but i would be nice to catch the delete statement as well...

    any (other) ideas ?

    tnx a lot !

  • DBCC INPUTBUFFER(spid) will display the last statement executed for a connection (spid).

    Profiler will show all sql statements executing on the server.


    When in doubt - test, test, test!

    Wayne

  • Figured it out meantime, thanks for the help!

  • What if the statement you're trying to catch is within a stored proc and you want to catch it within the same stored proc and log it to a "tracking table"?

  • Are you trying to capture a record of dynamic SQL? If so, it ought to be fairly easy. Rewrite your SP to build the SQL string in a variable, instead of directly in the argument of the EXEC function. Then, write the value of the variable to your tracking table, either before or after you run the EXEC, depending on what you want to capture.

    If you just want to know when an SP runs, you can just add an extra INSERT statement to the SP to add the log entry to your tracking table. Note that this can negatively impact performance, especially if you want to capture a great deal of information (e.g. spid, user, variable values).

  • Also, check out fn_get_sql in the BOL. It is better than DBCC INPUTBUFFER.

    You might also want to consider using a trigger (ON DELETE) to help capture what you want. Maybe set up the trigger to run fn_get_sql when someone does a delete.

    -SQLBill

Viewing 6 posts - 1 through 5 (of 5 total)

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