Solved

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

Posted on 2014-10-14
9
150 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

708 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

13 Experts available now in Live!

Get 1:1 Help Now