Solved

How can I alert several users when a database table has more than 200 rows added?

Posted on 2014-10-14
9
171 Views
Last Modified: 2014-10-20
How can I alert several users when a database table has more than 200 rows added?
0
Comment
Question by:TexanDonnaP
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 40

Expert Comment

by:lcohan
ID: 40380364
You could add a trigger on insert to send mail notification if rowcount in that table is > 200
0
 
LVL 40

Expert Comment

by:lcohan
ID: 40380379
--Like in the code below on a table called "Clients"


CREATE TRIGGER [ClientsInsert] ON  [dbo].[Clients]
   AFTER INSERT
AS
BEGIN

IF (SELECT SUM (row_count) FROM sys.dm_db_partition_stats
            WHERE object_id=OBJECT_ID('Clients')  AND (index_id=0 or index_id=1)) > 200
                  BEGIN                                      
                        --send mail code here
                        DECLARE @sqlstr varchar(1000)
                        SET @sqlstr = 'The table Clients has now more than 200 rows.'+CHAR(13)+ 'Sent at: '+char(13)+cast(getdate() as sysname);
                        EXEC msdb.dbo.sp_send_dbmail
                              @profile_name = 'yourmailprofile',
                              @recipients = 'you@email.com',
                              @subject = 'CallTime alert',
                              @body =  @sqlstr
                  END
END
0
 

Author Comment

by:TexanDonnaP
ID: 40380538
Icohan

The new of the table is called "base_flights"? Would the below be correct? Would I insert this on the database trigger folder if im running SQL management studio? thank you for your help

CREATE TRIGGER [ClientsInsert] ON  [dbo].[base_flights]
   AFTER INSERT
AS
BEGIN

IF (SELECT SUM (row_count) FROM sys.dm_db_partition_stats
            WHERE object_id=OBJECT_ID('base_flights')  AND (index_id=0 or index_id=1)) > 200
                  BEGIN                                      
                        --send mail code here
                        DECLARE @sqlstr varchar(1000)
                        SET @sqlstr = 'The table Clients has now more than 200 rows.'+CHAR(13)+ 'Sent at: '+char(13)+cast(getdate() as sysname);
                        EXEC msdb.dbo.sp_send_dbmail
                              @profile_name = 'texans@hotmail.com',
                              @recipients = 'support@htomail.com',
                              @subject = 'CallTime alert',
                              @body =  @sqlstr
                  END
END
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 40

Accepted Solution

by:
lcohan earned 500 total points
ID: 40380579
If you are in SQL Management Studio then connect (expand Databases and click) to the database where base_flights table exists and indeed just execute the code you posted above - maybe update the trigger name to reflect your table name like: CREATE TRIGGER [base_flights_Insert]
Now...once the trigger is created it will send out 1 email EVERY TIME a new row is inserted into the base_flights table after it reached 200 rows so if you need to stop that you can use DISABLE TRIGGER or DROP TRIGGER commands to stop that. If you need more users to get notified just add them with semicolon to the mail list like:
'support@htomail.com;email1@mail.com;email2@mail.com'

Please note that @profile_name IS 'yourmailprofile' from that SQL Server Database Mail configuration and NOT your FROM email. To find the mail profiles on that server please run a command like below:
EXEC msdb.dbo.sysmail_help_profile_sp;
EXEC msdb.dbo.sysmail_help_profileaccount_sp;



CREATE TRIGGER [base_flights_Insert] ON  [dbo].[base_flights]
    AFTER INSERT
 AS
 BEGIN

 IF (SELECT SUM (row_count) FROM sys.dm_db_partition_stats
             WHERE object_id=OBJECT_ID('base_flights')  AND (index_id=0 or index_id=1)) > 200
                   BEGIN                                      
                         --send mail code here
                         DECLARE @sqlstr varchar(1000)
                         SET @sqlstr = 'The table Clients has now more than 200 rows.'+CHAR(13)+ 'Sent at: '+char(13)+cast(getdate() as sysname);
                         EXEC msdb.dbo.sp_send_dbmail
                               @profile_name = 'yourmailprofile'--'texans@hotmail.com',
                               @recipients = 'support@htomail.com',
                               @subject = 'base_flights table alert',
                               @body =  @sqlstr
                   END
 END
 GO
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 40392217
You may want to re-consider sending an email using a TRIGGER.  A TRIGGER is meant to be very lightweight, so doing anything like using a CURSOR or sending an email is never a good idea.  A better approach is to add an entry in a table in the TRIGGER and periodically poll the table and send out an email.
0
 

Author Comment

by:TexanDonnaP
ID: 40392313
Anthony,

I thought what Icohan recommended was a trigger, correct? I tested it and it worked. Can you please advice?
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 40392337
I tested it and it worked.
I am merely suggesting that sometimes a screw is better than a nail.  If you are happy with a nail, then all is well with the world.
0
 
LVL 40

Expert Comment

by:lcohan
ID: 40392387
I believe at low level volumes and to do this action only once and/or when certain threshold was reached then disable/drop the trigger would be definitely fine.

If indeed you would need to send out mails and alert people/email groups of certain things in your database then you can have some trigger populating a queue table then a SQL job for instance to process/clear the queue periodically rather than on-line and I believe that's what Anthony wanted to explain you.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40392452
Triggers should always be as efficient as possible.  Thus I would recommend tweaking the trigger code to:

CREATE TRIGGER [base_flights_Insert]
ON [dbo].[base_flights]
AFTER INSERT
AS
SET NOCOUNT ON;
IF (SELECT SUM (row_count) FROM sys.dm_db_partition_stats
    WHERE object_id=OBJECT_ID('base_flights') AND (index_id=0 or index_id=1)) > 200
    BEGIN
          EXEC msdb.dbo.sp_send_dbmail
                @profile_name = 'yourmailprofile', --'texans@hotmail.com',
                @recipients = 'support@hotmail.com',
                @importance = 'High',
                @subject = 'ALERT: Base_Flights Table has more than 200 Rows!',
                @body = N'The Base_Flights table has more than 200 rows.'
    END; --IF
GO
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question