I have a SQL Server job that sends out a notification every time it fails. I run this job every 5 minutes. If something the job needs has its state changed at 10:00 PM at night and the problem isn't discovered until the next morning, there would be over 100 e-mails sent with the same failure notification.
Is there anyway for you to throttle the number of e-mails sent? For example, I would like SQL Server to send out an e-mail notification on the hour if any of the 12 scheduled runs during the hour fails, but no more than one e-mail should be sent (even if there are multiple failures).
You could create a simple table with the job name and a bit column indicating whether an email has been sent successfully, then check that column as part of the alert.
DECLARE Notifications TABLE
(
ID int IDENTITY (1, 1) PRIMARY KEY NOT NULL,
JobName varchar(50),
MailSent bit DEFAULT 0
)
In SSMS select Database Mail under Management, right-click and Configure Database Mail. After that's complete send a test mail using the new profile to confirm that works.
Next, open the properties of the SQL Server Agent. On the Alert System page enable the mail profile. Restart the SQL Server Agent for the change to take effect.
Under the SQL Server agent create Operators to send messages to.
Finally, in your job select Notifications and set the Operator to send to when the job completes.
Related
Last night due to some bad code, my SQL Server tried sending a million emails. After 10k the emails started going into queue. I immediately stopped the sysmail service using exec sysmail_stop_sp.
Then I deleted all entries from sysmail_allitems, sysmail_faileditems, sysmail_send_retries, sysmail_senditems and sysmail_unsentitems.
I also cleared sysmail_event_log.
Assuming that it would clear my mail queue I started the sysmail services using
exec sysmail_start_sp
But to my surprise the sysmail is still trying to send emails but it cannot find them in sysmail_allitems.
I checked the mail queue using msdb.dbo.sysmail_help_queue_sp and it shows queue with queue_type 'mail' showing those million emails queue.
I rechecked all the tables and they all are empty. Where is this queue is being fetched?
Though no email were sent but still the server tried each and every email which lasted for few hours.
Any idea where the queue is fetched from?
Stop SQL Server Agent
Set it to start manually (using SQL Server Configuration Manager)
Restart SQL Server when you can.
Try to execute:
DECLARE #GETDATE datetime
SET #GETDATE = GETDATE();
EXECUTE msdb.dbo.sysmail_delete_mailitems_sp #sent_before = #GETDATE;
GO
This should remove them all. The reason I mentioned stopping agent is so that it won't take a lock on them and stop you deleting them.
Set Agent back to starting auto.
Restart Agent
And hopefully there won't be a stream of emails coming out. You'll also have to check for any that are queued up in the mail server as well.
Hope that helps.
I have a report running against a Data Driven subscription in SSRS. The subscription runs a report and produces PDFs - about 1000 of them. The process takes about 2 minutes to complete.
I have been kicking this off manually using the following SQL:
EXEC msdb.dbo.sp_start_job #job_name = '<job_name>'
This works, but what I would like to know is when the job has finished. According to what I have read so far, I should be able to run:
exec msdb.dbo.sp_help_job
This lists my job, but it always has a status of 4 (Idle), even while I can see that reports are being produced.
How can I tell when the job has completed and all my reports have been produced?
MSDB shouldn't contain informtaion on the reporting server. The reporting server is seperate from Sql Server Management Server and will only tell you if the job ran or not not what happened in the job. If you have access to the DB I don't know how you have it set up but I have a subscriptions table that I can check with email sent and when it was sent. IF you don't have that you can go onto the reportserver web site and check the subscription and check the status and it should have a date of when it was last sent.
The only way you can access the information in Sql Server Management Studio is by queryng the DB and its tables assuming it is setup correctly.
I've created a Database Mail account (tested and sends with no problem), created an operator, set up an alert on the job I want to monitor, and restarted my SQL Server Agent. But I still am not alerted when the job in question completes. Is there a step that is missing?
Look at your Database Mail log and make sure you don't have any errors sending there. Make sure your internal relay mail servers allow sending from the SQL server's IP address. Also run EXEC msdb.dbo.sysmail_help_status_sp and make sure the dbmail queue status is "Started" or "Receives_Occuring". If the queue is in a Stopped status, restart it with EXEC msdb.dbo.sysmail_stop_sp and EXEC msdb.dbo.sysmail_start_sp
Open the job, go to notifications and select Email, choose the operator you created, and the condition "When the job fails". That should do it.
Was able to get notifications to start sending after a server restart
I have a problem setting up email notifications in SQL Server 2008.
I am using SQL Server 2008 to build a data cube. This process consists of several jobs, that are all scheduled to run each night at a specific time. To see whether a job was running without any failure, it is possible to set up email notification.
(source: smilinginthesun.de)
(source: smilinginthesun.de)
Unfortunately, no email is sent even though I think I have set up database mail correctly.
All guides I can found just explains, how to set up database mail (eg. http://blog.sqlauthority.com/2008/08/23/sql-server-2008-configure-database-mail-send-email-from-sql-database/), but thats it.
If I sent a mail to test functionality (via test email button or from T-SQL script) it works just fine and the email arrives. But when using the notification feature in a job, it doesn't even create a sysmail_mailitems or sysmail_log entry.
I have also send an email using sp_notify_operator, successfully. So the operator set up seems to work, too. (Thanks, Joe Stefanelli, for the hint.)
There is a great workaround written for SQL Server 2005, that works for me, too: http://www.howtogeek.com/howto/database/sending-automated-job-email-notifications-in-sql-server-with-smtp/, but why isn't it possible to just use that build-in functionality?
Does anyone knows if there is anything else to do, to make that email notification task work properly? Thanks in advise.
I am late but I saw your post having the same problem.
The solution is to enable a mail profile for alerts.
For this:
Right click on "SQL Server Agent"
Then "Properties".
Then go to "Alert System" section.
Tick the "Enable mail profile" box.
Then "OK".
Restart the SQL Server Agent service.
To send email notifications from agent jobs in SQL Server Agent, the alarm system must be activated. We did that. After restarting the agent, the Database Mail system should be used. Unfortunately, this does not work. The agent was still trying to reach a Mapi profile (as in SQL 2000).
The following registry value wasn't set: "UseDatabaseMail"=dword:00000001
After setting it to 1, the database mail system was used.
You can do that in SQL server, directly:
EXEC master.dbo.xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\SQLServerAgent', N'UseDatabaseMail', N'REG_DWORD', 1
I've tried what ZeuG. indicated and still didn't have success... then I restarted the SQL Agent. That's what made the job email notification work for me.
For me, SQL email worked fine when I right-clicked Database Mail and said Send Test Email. However, after setting up alerts for all severities and running RAISERROR (N'Test alert!', 20, 1) WITH LOG;, no email came through. The issue is the service account that SQL Server Agent runs under. Apparently, the virtual account NT Service\SQLSERVERAGENT (or whatever your default may be) does not have permissions to access objects on the network and this includes e-mail.
To fix, change the service to use a local account under the Users group, restart SQL Server agent, and then try running a command that throws an error like the RAISERROR statement above. You should now receive the email.
When I use the sp_send_dbmail stored procedure, I get a message saying that my mail was queued. However, it never seems to get delivered. I can see them in the queue if I run this SQL:
SELECT * FROM msdb..sysmail_allitems WHERE sent_status = 'unsent'
This SQL returns a 1:
SELECT is_broker_enabled FROM sys.databases WHERE name = 'msdb'
This stored procedure returns STARTED:
msdb.dbo.sysmail_help_status_sp
The appropriate accounts and profiles have been set up and the mail was functioning at one point. There are no errors in msdb.dbo.sysmail_event_log.
Have you tried
sysmail_stop_sp
then
sysmail_start_sp
I had the same problem and this is how I was able to resolve it.
Go to Sql Agent >> Properties >> Alert System >> Check the Enable box for DBMail and add a profile.
Restart Agent and it works since then.
Hope this helps,
_Ub
Could be oodles of things. For example, I've seen (yes, actually seen) this happen after:
Domain controller reboot
Exchange server reboot
Router outage
Service account changes
SQL Server running out of disk space
So until it happens again, I wouldn't freak out over it.