Solved

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

Posted on 2014-10-14
9
152 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
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 39

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 39

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
 
LVL 39

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 39

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:ScottPletcher
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

930 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now