Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 7,2000
»
T-SQL
»
Trigger to capture TSQL statement
Trigger to capture TSQL statement
Rate Topic
Display Mode
Topic Options
Author
Message
RD-201664
RD-201664
Posted Tuesday, November 10, 2009 6:21 PM
SSC Rookie
Group: General Forum Members
Last Login: Wednesday, June 05, 2013 9:32 AM
Points: 31,
Visits: 488
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
Elliott Whitlow
Elliott Whitlow
Posted Tuesday, November 10, 2009 6:29 PM
SSCertifiable
Group: General Forum Members
Last Login: Yesterday @ 10:09 AM
Points: 5,863,
Visits: 4,886
You can probably use
fn_get_sql
if you have SP3 or higher.
http://support.microsoft.com/kb/325607
CEWII
Post #816939
Elliott Whitlow
Elliott Whitlow
Posted Tuesday, November 10, 2009 6:40 PM
SSCertifiable
Group: General Forum Members
Last Login: Yesterday @ 10:09 AM
Points: 5,863,
Visits: 4,886
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
RD-201664
RD-201664
Posted Tuesday, November 10, 2009 7:26 PM
SSC Rookie
Group: General Forum Members
Last Login: Wednesday, June 05, 2013 9:32 AM
Points: 31,
Visits: 488
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
Elliott Whitlow
Elliott Whitlow
Posted Tuesday, November 10, 2009 8:57 PM
SSCertifiable
Group: General Forum Members
Last Login: Yesterday @ 10:09 AM
Points: 5,863,
Visits: 4,886
Alright then no problem..
CEWII
Post #816955
Jack Corbett
Jack Corbett
Posted Wednesday, November 11, 2009 7:58 AM
SSChampion
Group: General Forum Members
Last Login: Yesterday @ 1:41 PM
Points: 10,613,
Visits: 11,952
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
GSquared
GSquared
Posted Wednesday, November 11, 2009 8:15 AM
SSCoach
Group: General Forum Members
Last Login: 2 days ago @ 1:45 PM
Points: 15,442,
Visits: 9,572
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
RD-201664
RD-201664
Posted Wednesday, November 11, 2009 3:17 PM
SSC Rookie
Group: General Forum Members
Last Login: Wednesday, June 05, 2013 9:32 AM
Points: 31,
Visits: 488
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 »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.