Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Trigger to capture TSQL statement Expand / Collapse
Author
Message
Posted Tuesday, November 10, 2009 6:21 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, April 15, 2014 11:35 AM
Points: 31, Visits: 509
Hi all,
I am creating an update trigger to capture when a specific table.field is updated. I have the trigger created and functioning properly but would like to see the actual TSQL update statement that performed the update. Profiler has the "Textdata" column. Where does this come from and can it be captured in a trigger?

This particular case is specifc to SQL 2000.

Thanks.
Post #816937
Posted Tuesday, November 10, 2009 6:29 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, January 03, 2014 3:52 PM
Points: 6,066, Visits: 5,277
You can probably use fn_get_sql if you have SP3 or higher.

http://support.microsoft.com/kb/325607

CEWII
Post #816939
Posted Tuesday, November 10, 2009 6:40 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, January 03, 2014 3:52 PM
Points: 6,066, Visits: 5,277
I would be sure to test the performance penalty to do this.

I'm curious what is so important about the statement instead of the results of the statement..

CEWII
Post #816940
Posted Tuesday, November 10, 2009 7:26 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, April 15, 2014 11:35 AM
Points: 31, Visits: 509
Thanks for the info.
I am trying to catch somebody with their hand in the cookie jar
There is some rogue app or custom code somewhere causing an undesired update.
This does not happen frequent enough to be concerned about performance. If it happened more often I would use Profiler.

Post #816943
Posted Tuesday, November 10, 2009 8:57 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Friday, January 03, 2014 3:52 PM
Points: 6,066, Visits: 5,277
Alright then no problem..

CEWII
Post #816955
Posted Wednesday, November 11, 2009 7:58 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 12:06 PM
Points: 10,910, Visits: 12,546
I'd still consider doing a server-side trace limited to that database. I think that it will have less impact than a trigger.





Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Post #817173
Posted Wednesday, November 11, 2009 8:15 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Monday, April 14, 2014 1:34 PM
Points: 15,442, Visits: 9,588
I'd definitely set up a server-side trace. Minimal impact, will get you the data you need, and has a number of other benefits as well.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Post #817198
Posted Wednesday, November 11, 2009 3:17 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, April 15, 2014 11:35 AM
Points: 31, Visits: 509
Do you guys have any articles or links you can point me to on server side traces? I can't seem to find how to trace a change to a specific table.column.

Here is an example of my trigger using Northwind.Orders as an example. Feel free to critique this as well. Does my code to get hostname, username, etc seem valid?

--Northwind - Trigger to track changes to Orders.OrderDate

--Create table store log data
CREATE TABLE [Orders_Log] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[OrderID] [int] NOT NULL ,
[orig_OrderDate] [datetime] NULL ,
[new_OrderDate] [datetime] NULL ,
[Computer_Name] [varchar] (80) NULL ,
[Windows_user] [varchar] (80) NULL ,
[sql_login] [varchar] (80) NULL ,
[program_Name] [varchar] (80) NULL ,
[command] [varchar] (80) NULL ,
[MAC_address] [varchar] (80) NULL ,
[log_date] [datetime] NOT NULL DEFAULT (getdate())
) ON [PRIMARY]
GO



--Trigger to track changes to OrderDate
--drop trigger [track_OrderDate_change]
CREATE TRIGGER [track_OrderDate_change] ON [dbo].[Orders]
For UPDATE
AS
If Update(OrderDate)
Begin
Set Nocount On
insert into [Orders_Log]
(OrderID, orig_OrderDate, new_OrderDate,
Computer_Name, Windows_User, sql_login, program_name, command, mac_address)
select d.OrderID, d.OrderDate, i.OrderDate,
(SELECT TOP 1 hostname FROM [master].[dbo].[sysprocesses] WHERE spid = @@spid ),
(SELECT TOP 1 nt_username FROM [master].[dbo].[sysprocesses] WHERE spid = @@spid ),
(SELECT TOP 1 loginame FROM [master].[dbo].[sysprocesses] WHERE spid = @@spid ),
(SELECT TOP 1 program_name FROM [master].[dbo].[sysprocesses] WHERE spid = @@spid ),
(SELECT TOP 1 cmd FROM [master].[dbo].[sysprocesses] WHERE spid = @@spid ),
(SELECT TOP 1 net_address FROM [master].[dbo].[sysprocesses] WHERE spid = @@spid )
from [inserted] i, [deleted] d
Where i.OrderID = d.OrderID

End
Post #817512
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse