SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Server level trigger


Server level trigger

Author
Message
Oracle_91
Oracle_91
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1174 Visits: 1742
Hi,

I wanted to a create server level trigger which prevents change of recovery model
I got some resources from internet and here is the code.

There are couple of problems with this code.
1. When I issue ALTER DATABASE COMMAND , it says Mail queued and because of the below ROLLBACK statement it is getting rollbacked and I dont receive any kind of email.
2. Other thing is that, I am able to see the RAISERROR() msg in management studio, but the ALTER DATABASE statement somehow getting auto-commit which allows the recovery model to be changed.

Can anybody help me in acheiving this task. what necessary changes/logic should i need to incorporate in the below code to make the mail functionality as well prevent ALTER DATABASE statement getting executed.
Other alternative is Policy Based Management but I wanted to implement this using trigger.

Thanks in Advance.

Use master
go
create database testdb
go
ALTER DATABASE testdb SET RECOVERY FULL;
go
create trigger [usp_Restrict_RecoveryModel_Changes]
ON ALL SERVER
FOR ALTER_DATABASE, DROP_DATABASE
AS
BEGIN
DECLARE @data xml
DECLARE @trigger_name sysname, @LoginName sysname, @UserName sysname, @dbname sysname
SET @data = EVENTDATA()
DECLARE @Str nvarchar(max)
SELECT @Str=cast(@data as nvarchar(max));

/*
<EVENT_INSTANCE>
<EventType>ALTER_DATABASE</EventType>
<PostTime>2007-01-12T20:05:27.527</PostTime>
<SPID>65</SPID>
<ServerName>NAME</ServerName>
<LoginName>sa</LoginName>
<DatabaseName>TestSecurity</DatabaseName>
<TSQLCommand>
<SetOptions ANSI_NULLS="ON" ANSI_NULL_DEFAULT="ON" ANSI_PADDING="ON" QUOTED_IDENTIFIER="ON" ENCRYPTED="FALSE" />
<CommandText>ALTER DATABASE [TestSecurity] SET RECOVERY SIMPLE WITH NO_WAIT
</CommandText>
</TSQLCommand>
</EVENT_INSTANCE>
*/

-- send email to self before you will raise error
EXEC msdb.dbo.sp_send_dbmail
@recipients='test@gmail.com',
@subject = '!!!****Attempt to alter database***!!!',
@body = @Str,
@body_format = 'HTML',
@profile_name ='Test profile'

RAISERROR ('ALTER DATABASE DISABLED!',10, 1)
ROLLBACK
END
GO
---trying to change recovery model to 'SIMPLE', we need to prevent this happening!!!!
ALTER DATABASE testdb SET RECOVERY FULL;
go


Thanks in Advance.
Matthew Darwin
Matthew Darwin
SSC-Addicted
SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)

Group: General Forum Members
Points: 459 Visits: 875
I think you need to move the ROLLBACK to before the mail send and raiserror, this will ensure that the ALTER DATABASE statement is rolled back but then allow the mail to be sent.

Follow me on twitter @EvoDBA

Check out my blog Natural Selection DBA
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