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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With