Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the stored procedure name from a trigger when an update operation was performed? [duplicate]

Tags:

sql-server

Possible Duplicate:
Is it possible for a trigger to find the name of the stored procedure that modified data?

I have a table with a trigger (on update) on it. When the trigger is executed I would like to know the name of the stored procecure which updated the table in question.

like image 883
Anthony Avatar asked Jan 22 '13 21:01

Anthony


People also ask

Can a stored procedure be called from a trigger?

MySQL allows you to call a stored procedure from a trigger by using the CALL statement. By doing this, you can reuse the same stored procedure in several triggers. However, the trigger cannot call a stored procedure that has OUT or INOUT parameters or a stored procedure that uses dynamic SQL.

Can a trigger and procedure have the same name in the same schema?

If you are not using (or even familiar with) schemas then the short answer is no. Object names within a schema must be unique, so if you only have the one (default) schema then object names must be unique full stop. Save this answer.

Can a trigger and procedure have the same name?

Triggers exist in a separate namespace from procedure, package, tables (that share the same namespace), which means that a trigger can have the same name as a table or procedure. Row-level triggers execute once for each row in a transaction.

How do you find where a stored procedure is used?

Using SQL Server Management Studio Expand Databases, expand the database in which the procedure belongs, and then expand Programmability. Expand Stored Procedures, right-click the procedure and then click View Dependencies. View the list of objects that depend on the procedure.


1 Answers

This won't always be 100% reliable, and it sometimes will capture the outer procedure call even if that procedure called an inner one. But you can at least get some idea of what the user called that ended them up in the trigger.

ALTER TRIGGER dbo.whatever
ON dbo.something
FOR UPDATE
AS
BEGIN
    ... other trigger logic

    DECLARE @ExecStr varchar(50), @Qry nvarchar(255)

    CREATE TABLE #inputbuffer 
    (
      EventType nvarchar(30), 
      Parameters int, 
      EventInfo nvarchar(255)
    )

    SET @ExecStr = 'DBCC INPUTBUFFER(' + STR(@@SPID) + ')'

    INSERT INTO #inputbuffer 
    EXEC (@ExecStr)

    SET @Qry = (SELECT EventInfo FROM #inputbuffer)

    SELECT @Qry AS 'Query that fired the trigger', 
     SYSTEM_USER as LoginName, 
     USER AS UserName, 
     CURRENT_TIMESTAMP AS CurrentTime

   -- of course you can store this somewhere instead of select
END

Stolen from Vyas K: http://vyaskn.tripod.com/tracking_sql_statements_by_triggers.htm

You may also want to check out this question, which has an answer relating to using sys.dm_exec_query_stats - you can track back to procedure name using the object id exposed by sys.dm_exec_sql_text, and you can limit to very recent invocations using GETDATE() as a guide for "recent." There is also a lengthy example of using Extended Events to do it.

Create Trigger to log SQL that affected table?

Or these ones which use CONTEXT_INFO (but this requires changing all of the stored procedures that update the table):

Find query that fired a trigger

Is it possible for a trigger to find the name of the stored procedure that modified data?

Finally, you may check out these posts for some ideas:

http://www.thecodepage.com/post/TIP-A-Call-Stack-in-SQL-Server.aspx

like image 75
Aaron Bertrand Avatar answered Oct 12 '22 12:10

Aaron Bertrand