Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Trigger to capture TSQL statement


Trigger to capture TSQL statement

Author
Message
RD-201664
RD-201664
Valued Member
Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)

Group: General Forum Members
Points: 52 Visits: 575
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.
Elliott Whitlow
Elliott Whitlow
SSCertifiable
SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)

Group: General Forum Members
Points: 7336 Visits: 5314
You can probably use fn_get_sql if you have SP3 or higher.

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

CEWII
Elliott Whitlow
Elliott Whitlow
SSCertifiable
SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)

Group: General Forum Members
Points: 7336 Visits: 5314
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
RD-201664
RD-201664
Valued Member
Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)

Group: General Forum Members
Points: 52 Visits: 575
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.
Elliott Whitlow
Elliott Whitlow
SSCertifiable
SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)SSCertifiable (7.3K reputation)

Group: General Forum Members
Points: 7336 Visits: 5314
Alright then no problem..

CEWII
Jack Corbett
  Jack Corbett
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12942 Visits: 14863
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
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

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
GSquared
GSquared
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16925 Visits: 9729
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
RD-201664
RD-201664
Valued Member
Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)Valued Member (52 reputation)

Group: General Forum Members
Points: 52 Visits: 575
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search