Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 need help with capturing ad-hoc stmt in trigger sp

Author  Topic 

SQLIsTheDevil
Posting Yak Master

177 Posts

Posted - 2009-10-27 : 10:39:47
Hello,

I want to write a trigger which monitors when a row is deleted from a table, with specific conditions (i.e. where column=xx). But I want to capture what ad-hoc sql statement (from server-side code) is doing it and throw that into a separate table. Is there a way to do this?

Thank you!

Jeff

SQLIsTheDevil
Posting Yak Master

177 Posts

Posted - 2009-10-27 : 11:38:23
I wrote the trigger; it's not long at all, but all the trigger does is "select * from deleted where column1=xx"

This tells me what was deleted, but it doesn't tell me what the specific ad-hoc query was which was executed. Is there a way I can retrieve that? Maybe I can make use of "INSTEAD OF" somehow?
Go to Top of Page

YellowBug
Aged Yak Warrior

616 Posts

Posted - 2009-10-27 : 12:31:25
Maybe you can use the new SQL Server 2008 Audit feature:
http://msdn.microsoft.com/en-us/library/dd392015.aspx

I've not used this feature personally, but from what I read it may be useful to you.
Go to Top of Page

YellowBug
Aged Yak Warrior

616 Posts

Posted - 2009-10-27 : 12:44:28
I found this article that seems to illustrate better:
http://www.sqldbatips.com/showarticle.asp?ID=136


The audit feature seems to be in SQL Server 2008 Enterprise edition only.
Go to Top of Page

SQLIsTheDevil
Posting Yak Master

177 Posts

Posted - 2009-10-27 : 14:08:52
Thank you so much!

This will be of great help. Thank you!

I'm also looking for a solution in sql 2000 but I imagine I'd have to employ a similar feat to the one you described.
Go to Top of Page

YellowBug
Aged Yak Warrior

616 Posts

Posted - 2009-10-27 : 16:07:56
You're welcome. Glad to help.
Go to Top of Page
   

- Advertisement -