November 10, 2009 at 6:21 pm
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.
November 10, 2009 at 6:29 pm
You can probably use fn_get_sql if you have SP3 or higher.
http://support.microsoft.com/kb/325607
CEWII
November 10, 2009 at 6:40 pm
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
November 10, 2009 at 7:26 pm
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.
November 10, 2009 at 8:57 pm
Alright then no problem..
CEWII
November 11, 2009 at 7:58 am
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
Consultant - Straight Path Solutions
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
November 11, 2009 at 8:15 am
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
November 11, 2009 at 3:17 pm
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
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply