Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Alerting the DBA on New DB Creation and the same should be backed up.

$
0
0

Hello,

I am not good at t-sql and hence need your suggestions here. I have a requirement like, whenever the new Database got created, the database should get backup in next sec/min. Do we have any ways to achieve this? Kindly let me know. Appreciate your help.

I have this code which will alert me on new database creation. I need to backup the newly created database next.

Create TRIGGER [ddl_trig_NewDatabase]
ON ALL SERVER
FOR CREATE_DATABASE
AS

declare @results varchar(max)
declare @subjectText varchar(max)
declare @databaseName VARCHAR(255)
SET @subjectText = 'DATABASE Created on ' + @@SERVERNAME + ' by ' + SUSER_SNAME() 
SET @results = 
  (SELECT EVENTDATA().value('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]','nvarchar(max)'))

SET @databaseName = (SELECT EVENTDATA().value('(/EVENT_INSTANCE/DatabaseName)[1]', 'VARCHAR(255)'))

EXEC msdb.dbo.sp_send_dbmail
 @profile_name = 'Mailer',
 @recipients = '4heaven@domain.com',
 @body = @results,
 @subject = @subjectText,
 @exclude_query_output = 1 --Suppress 'Mail Queued' message
Go


Viewing all articles
Browse latest Browse all 23857

Trending Articles