Our requirement is that when any DBA team member creates or Alter or dropped a mail alert trigger to Team members.
With the help of DDL trigger, we can achieve.
Read Trigger: Trigger in SQL server
Read DDL Trigger: DDL Trigger in sql server
Let’s see the example
Here I am creating a DDL trigger on the server and with the help of sp_send_dbmail SP sending the email.
USE master
GO
CREATE TRIGGER [ddl_trig_database]
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() +' on ' + cast(GETDATE() as varchar(20))
SET @results =
(SELECT EVENTDATA().value('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]','nvarchar(max)'))
SET @databaseName = (SELECT EVENTDATA().value('(/EVENT_INSTANCE/DatabaseName)[1]', 'VARCHAR(255)'))
--sending mail
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Bagesh_Mail',
@recipients = 'bageshkumarbagimsbi@gmail.com',
@body = @results,
@subject = @subjectText,
@exclude_query_output = 1 --Suppress 'Mail Queued' message
GO
|
Read How to: Configure Database mail in SQL server
Now trigger is created successfully.
Whenever we will create a database a mail trigger to 'bageshkumarbagimsbi@gmail.com'.
Let’s see. Now I am creating a database on my server
CREATE DATABASE [Test_DDL_TRIGGER]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'Test_DDL_TRIGGER', FILENAME = N'D:\Test_DDL_TRIGGER.mdf' , SIZE = 5120KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'Test_DDL_TRIGGER_log', FILENAME = N'D:\Test_DDL_TRIGGER_log.ldf' , SIZE = 2048KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
|
When we created a database a mail trigger in the inbox.
See the database in the server.
Now I am seeing my Inbox
See the mail body
Writing a trigger when the database is dropped
USE master
GO
ALTER TRIGGER [ddl_trig_database_DROP]
ON ALL SERVER
FOR DROP_DATABASE
AS
declare @results varchar(max)
declare @subjectText varchar(max)
declare @databaseName VARCHAR(255)
SET @subjectText = 'DATABASE Droped on ' + @@SERVERNAME + ' by ' + SUSER_SNAME() +' on ' + cast(GETDATE() as varchar(20))
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 = 'DDL_Trigger_Test',
@recipients = 'bageshkumarbagimsbi@gmail.com',
@body = @results,
@subject = @subjectText,
@exclude_query_output = 1 --Suppress 'Mail Queued' message
GO
|
Trigger created successfully.
Let’s drop the database.
DROP DATABASE Test_DDL_TRIGGER
|
See the result.
See the body
Similarly, we can write the trigger on Alter database, Create any object on the database.
Hope this will be helpful. Please leave your comment.
It is very good blog and useful for students and developer ,
ReplyDeleteSql server DBA Online Course Bangalore
Great Article Artificial Intelligence Projects
DeleteProject Center in Chennai
JavaScript Training in Chennai
JavaScript Training in Chennai Project Centers in Chennai
Good
ReplyDeletePlease share the Data & log file growth alert like if reaches 80% out of max size
ReplyDeleteHello, Thanks for your Awesome post! I quite Satisfied reading it, you are a good author.I will Make sure to bookmark your blog and definitely will come back from now on. I want to encourage that you continue your great job, have a nice day.
ReplyDeletePower Bi Training In Hyderabad
Power Bi Online Training
Power Bi Training In Ameerpet
Through this post, I know that your good knowledge in playing with all the pieces was very helpful. I notify that this is the first place where I find issues I've been searching for. You have a clever yet attractive way of writing on Msbi online training
ReplyDeleteMsbi online training Hyderabad
Msbi online training India
Msbi online course
Msbi course
Msbi training
Msbi certification training
Very nice article,This blog looks awesome and Very Unique.
ReplyDeleteThank You,,,
Keep Updating,,,
Power BI Course
nice post.sql server developer training
ReplyDeletesql server developer online training
azure online training
ReplyDeletejava online training
salesforce online training
hadoop online training
Data Science online training
nice post.sql server developer training
ReplyDeletesql server developer online training
sql server developer online course
nice post.sql server developer training
ReplyDeletesql server developer online training
sql server developer online course
nice...........!
ReplyDeletemsbi course training
Thanks again for the article post.Really thank you! Fantastic.
ReplyDeletedot net online training
dotnet institute