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
 General SQL Server Forums
 New to SQL Server Administration
 Trigger for modifications in database

Author  Topic 

Ciupaz
Posting Yak Master

232 Posts

Posted - 2011-08-24 : 08:50:24
Hello,
how can I write a trigger that save me (in a table, for example) modifications at the table, stored procs and funcionts in my DB?
The information I need to save is: user, dateOfModification and (if possible) the modification itself.

Is it possible write a trigger like this?

I'm using SQL Server 2008.

Thanks in advance.

Luigi

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-08-24 : 10:15:45
You can do this with DDL Triggers
Go to Top of Page

Ciupaz
Posting Yak Master

232 Posts

Posted - 2011-08-24 : 10:26:51
Something like this could be ok?

CREATE TRIGGER safety
ON DATABASE
FOR DROP_TABLE, ALTER_TABLE, ALTER_PROCEDURE, ALTER_FUNCTION, DROP_PROCEDURE,DROP_FUNCTION
AS
INSERT dbo.LogTrigger(Description,DateExecute)
SELECT EVENTDATA().value('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]','nvarchar(max)'),GETDATE()

Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2011-08-24 : 11:52:50
looks good. I'll also capture

('(/EVENT_INSTANCE/LoginName)[1]',sysname')
Go to Top of Page

gwilson67
Starting Member

42 Posts

Posted - 2011-08-29 : 00:39:01
You may also want to look into Change Data Capture.

Greg
http://www.freewebstore.org/tsqlcoderepository
Powerful tool for SQL Server development
Go to Top of Page
   

- Advertisement -