I have the following that sends an email to recipients that I get from a table in my database. What Im trying to do is send this email every hour. How could I do that?
This is what I have right now?
DECLARE @recipient VARCHAR(4000)
USE data
SELECT @recipient = STUFF((SELECT ';' + email
FROM dbo.email
FOR XML PATH('')
), 1, 1, '')
EXEC msdb.dbo.sp_send_dbmail
@profile_name='SQL Server Alerts System',
@recipients = @recipient,
@subject='Test message',
@body='This is开发者_如何学Python the body of the test message.
Congrates Database Mail Received By you Successfully.'
Create a scheduled SQL Server Agent Job
I know this post is long ago but just to add, you can actually do the following:
- Put the statement where you select email recipient and execute the mail sending into a stored procedure.
- Create a job
- At Schedules tab, set when you want the job to run. For me i usually run it 12.00am daily.
- Next in the 'Steps' tab click on 'New'.
- Put in a name for the step. Type should be Transact-SQL. For Run as it should be the database owner(or SA, dbo...)
- Include an execute statement of the stored procedure you created just now for mail sending in the command box area.
You can test it for yourself. Just wait patiently until the time for the job to run and see if you/recipient recipient receives any email
精彩评论