Solved

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

Posted on 2014-10-14
9
162 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

840 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