Skip to main content

Scheduling Email Using Sql Server

DECLARE
@out_desc VARCHAR(1000),
@out_mesg VARCHAR(10)

DECLARE @name VARCHAR(20),
@birthdate datetime,
@email NVARCHAR(50)

DECLARE @body NVARCHAR(1000)

DECLARE C1 CURSOR READ_ONLY
FOR
SELECT [name], [birthdate], [email]
FROM Customers

OPEN C1
FETCH NEXT FROM C1 INTO
@name, @birthdate, @email
WHILE @@FETCH_STATUS = 0
BEGIN
IF DATEPART(DAY,@birthdate) = DATEPART(DAY,GETDATE())
AND DATEPART(MONTH,@birthdate) = DATEPART(MONTH,GETDATE())
BEGIN
SET @body = 'Happy Birthday ' + @name +
'

Many happy returns of the day'
+ '

Customer Relationship Department'
EXEC sp_send_mail
sender@abc.com',
'xxxxxxx',
@email,
'Birthday Wishes',
@body,
'htmlbody',
@output_mesg = @out_mesg output,
@output_desc = @out_desc output

PRINT @out_mesg
PRINT @out_desc
END
FETCH NEXT FROM C1 INTO
@name, @birthdate, @email
END
CLOSE C1
DEALLOCATE C1



Now to make the above script automatically run daily we will need to schedule it to run daily using the SQL Server Job Scheduler.

Below I’ll explain how to schedule the script to run daily using Job Scheduler




Step 1

In the SQL Server Enterprise Manager expand the Management Tab and select SQL Server Agent Node.

Refer figure below.





Step 2

On the Right Panel Select Job, Right Click it and select New Job from context menu to open New Job Window.

Refer figure below.





Step 3

In the New Job Window, in General Tab enter the following details

1. Name - Name of the Job
2. Description – Description of the Job (Optional)
3. Enabled – Determines whether job is enabled or disabled

Refer figure below





Step 4

In the New Job Window, in Steps Tab click New Step to open a New Step Window

In the New Step Window enter the following details

1. Step Name - Name of the Step
2. Type – Select Transact SQL Script
3. Database – Select the database on which you want to run the script.
4. Command – Paste the SQL Script which you wish the Job Scheduler to run.


Refer figure below





Step 5

In the New Job Window, in Schedules Tab click New Schedule to open a New Schedule Window

In the New Schedule Window enter the following details

1. Name - Name of the Step
2. Enabled – Determines whether Schedule is enabled or disabled
3. Schedule Type – Select Recurring schedule type since we need to run it daily


Refer figure below





Step 6

Next click on Change button in the Schedule Window to set the schedule

In the Edit Schedule Window enter the following details

1. Occurs – Daily since we need to run it daily
2. Daily Frequency – Since we need to run once a day, select the time you wish to run
3. Start date – Select date from when you want the schedule to run.


Refer figure below





Step 7

That’s it and your job is created. You will see a new entry in the SQL Server agent -----> Jobs

To start the job right click the job and in the context menu click Start Job



This code is Taken From :
http://www.aspsnippets.com/Articles/Automated-Email-Notifications-using-SQL-Server-Job-Schedular.aspx

Comments

Popular posts from this blog

Accessing Enum in template–Angular with Typescript

By reading title of  this post, It  seems some what misleading. In Angular 2 (or higher version for that matter). We cannot directly access  Enum or any other type for that matter into template. so, for an example  consider following example now if we try to access Enum WeekDays in HTML template as follows It would be resulted in runtime error shown below To resolve this error we should be assigning enum to component property as shown below. Reason behind this is , Component is the execution context for an Angular application. We can reference only those properties in template which are defined in component or have scope in component.

Setting up Visual Studio code for Java Spring boot development

  Download Visual Studio Code to your machine by clicking here . You will be redirected to official download site for VS Code. You can download VS depending on your OS. When we open VS Code and click on explorer (Ctrl+Shft+E). There two options available by default. Open Folder Clone Repository Visual Studio code is an Editor not a full fledged IDE. But we can configure VS Code by installing relevant extensions available at  https://marketplace.visualstudio.com/vscode . To install extension directly from Visual Studio, click on Extension icon on Menu item at left panel. Alternatively we can use Ctrl+Shft+X keyboard short cut. First extension we going to search is "Extension Pack From Java" from Microsoft. This extension pack is collection of extensions which is needed to enabled Java development in General. Language Support for Java - Rad Hat Debugger for Java - Microsoft Maven for Java - Microsoft Test Runner for Java - Microsoft Project Manager for Java - Microsoft. Next ex...

Visual Studio code - Setting up Tomcat server

  This is the second post in series of Setting up Visual Studio code for Java Spring Boot development. First post of this series can be found here . In this article we will see how we can configure VS Code to run Apache Tomcat server via extension. So lets begin. Open your VS Code and click on Extensions menu item. Now search for "Tomcat For Java". Select extension From Wei Shen and click install. Once this extension is installed. we can see new tab panel enabled in Explorer window. When we expand it, its empty. Now we need to configure Tomcat. For that, we need to download Apache Tomcat from official site. To download Tomcat click here to redirect to official site. There are multiple version available to download. For this demo we will download Tomcat Version 9.  Once Tomcat is downloaded to our machine. We now navigate back to VS code. We can now configure Tomcat by click on "+" sign on "Tomcat Servers" panel. When we click on Add Tomcat Server ...