Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-10-14
9
Medium Priority
?
181 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 40

Accepted Solution

by:
lcohan earned 2000 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 70

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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

926 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