March 9, 2010 at 12:41 pm
I'm trying to track down how a specific column in a table is being updated. I'm looking into adding a trigger on the table, and inserting before/after data into a logging (audit) table.
I need to also track what procedure is performing the update. If I use Object_Name(@@PROCID), I end up with the name of the trigger, not the calling procedure.
Is it possible to get the procedure that caused a trigger to fire from within the trigger?
Thanks,
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
March 9, 2010 at 1:53 pm
I believe the answer is no. There is an active MS Connect item requesting it at:
Which further indicates it isn't available..
CEWII
March 9, 2010 at 2:22 pm
In the absence of the smart solution have you considered an ugly process of a trigger updating a logging table everytime the column gets updated, and running a profiler tracking all stored procs that are starting. When the column get logged (throw in a trace stop?) and then check which stored procs were running at the time?
Like I say...Ugly. I'm sure you've probably thought of something like this. Also it may prove time consuming trudging through the stored procs that were running on a very large and active database.
March 9, 2010 at 2:39 pm
Off the top of my head that sounds like an ok solution. I don't have a better one. Ugly, but functional..
If I would add something I would probably build the trace internally and not use profiler, using the sprocs directly, this is considered a server side trace..
CEWII
March 9, 2010 at 3:30 pm
Shark Energy (3/9/2010)
In the absence of the smart solution have you considered an ugly process of a trigger updating a logging table everytime the column gets updated, and running a profiler tracking all stored procs that are starting. When the column get logged (throw in a trace stop?) and then check which stored procs were running at the time?Like I say...Ugly. I'm sure you've probably thought of something like this. Also it may prove time consuming trudging through the stored procs that were running on a very large and active database.
While this sounds doable, it is definitely uglier than I'd like to do at the time.
The connect item definitely looks better...
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
March 9, 2010 at 3:32 pm
I need to also track what procedure is performing the update
Assuming (and you know what that makes of me = SELECT SUBSTRING('assuming',1,3))
The following code will return all stored procedures that access the table in question.
CREATE TABLE #Temp(TableName VARCHAR(50), Spname VARCHAR(3000))
INSERT INTO #Temp
SELECT so.name, sob.name as 'Stored Procedure name' FROM sysobjects so
LEFT OUTER JOIN (sysobjects sob left outer join sysdepends on sob.id = sysdepends.id)
on sysdepends.depid = so.id
WHERE so.xtype = 'u' AND sob.xtype = 'p'AND NOT so.name = 'dtproperties'
AND so.name = 'yourtablesname'
From the above (with a bit more code) this will return the text of each stored procedure that contains the "UPDATE" command
select ROUTINE_SCHEMA, ROUTINE_NAME, ROUTINE_DEFINITION
From INFORMATION_SCHEMA.ROUTINES where ROUTINE_TYPE='PROCEDURE'
and ROUTINE_NAME = 'yourroutinenamefrom#temp ' AND ROUTINE_DEFINITION LIKE '%update%'
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply