Solved

Run task automatically, daily

Posted on 2010-09-14
17
505 Views
Last Modified: 2012-05-10
i would like to run a sql query and other actions automatically and on a daily basis. I need to check the sql database to see if any credit cards are expiring, for example. If so, I need to send an email to that user's email address. I think I can write the code to query the database, but I need to know how to schedule this to run automatically on a daily basis. I do not want to do this manually. I am using VB  and visual web developer 2010 express.

Thank you.
0
Comment
Question by:mlg101
  • 4
  • 3
  • 3
  • +6
17 Comments
 
LVL 20

Expert Comment

by:wolfcamel
ID: 33679505
write the vb script and make it a task in windows scheduler.
0
 
LVL 1

Author Comment

by:mlg101
ID: 33679524
What is windows scheduler and where do I find it? Is it on the server hosting my website? I have a windows server 2008.
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 33679662
SQL Server has all the capabilities to meet your requirements posted above:

>> I would like to run a sql query and other actions automatically and on a daily basis.

Just create a SQL Agent job to run daily

>> I need to check the sql database to see if any credit cards are expiring, for example.

Handle this logic in TSQL scripts and make it a step in your job.

>> If so, I need to send an email to that user's email address. I think I can write the code to query the database, but I need to know how to schedule this to run automatically on a daily basis.

Configure Database mail so that you can easily send email appropriately and include this as a another step of your job..
0
 
LVL 4

Expert Comment

by:senthilkumarsb
ID: 33679673
This you can do it in sql server job it self. Have a look in to below will help you.

http://msdn.microsoft.com/en-us/library/ms191439.aspx
0
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33679891
If you dont want to do it using SQL Job, you can also create a Windows Service, and using timer control, you can set its time to 24 hours. so it will run daily once a day.

Here you can manage any code level going (if you want to make it on .net code) also you can handle SQL Queries also.
0
 
LVL 3

Expert Comment

by:deepusreedhar
ID: 33680878
I will also go with the Windows Service option, as I did one like this to work in every 24 hrs.
0
 
LVL 1

Expert Comment

by:funazonki
ID: 33681180
You could also consider the windows task scheduler - go to Control Panel - Administrative Tools - start the applet Task Scheduler.

It will schedule pretty much anything you can run at pretty much any frequency you can imagine
0
 
LVL 1

Author Comment

by:mlg101
ID: 33683032
What I would like is just a little bit of an example to get me started in any one of these ideas. I've never done this automatic script thing before and need help.
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33683177
0
 
LVL 1

Expert Comment

by:funazonki
ID: 33683189
There are lots of examples here

http://msdn.microsoft.com/en-us/library/aa384006%28v=VS.85%29.aspx

including sending emails and other stuff - the examples have detailed instructions for registering tasks via a command line app, or there is a GUI interface for the scheduler.
0
 
LVL 1

Author Comment

by:mlg101
ID: 33683911
My version of visual studio does not allow me to create a windows service
0
 
LVL 8

Accepted Solution

by:
Mohit Vijay earned 250 total points
ID: 33684555
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 33687478
>> My version of visual studio does not allow me to create a windows service

Have you tried the approach that can be done in SQL Server which I and some other experts have suggested above..
0
 
LVL 3

Expert Comment

by:xiong8086
ID: 33689767
follow the example step by step if you are using sql server:

http://www.quackit.com/sql_server/tutorial/sql_server_agent_jobs.cfm

and don't forget to start "sql server agent" services.

0
 
LVL 1

Author Comment

by:mlg101
ID: 33693750
rrjegan17 - I made a query in sql server that I got to work in a test window, but when i created a new job in sql server agent, I put that code in the "steps" section and the "command" window. It didn't work there, so I'm not sure how to implement the code in the command area. The code I put there was this as one step:

Declare @Emails varchar(526)
Select @Emails=isnull(@Emails+';','') + a.Email From Student As a
Where a.Email>''
and exists (SELECT * From CreditCard As b WHERE a.StudentID = b.StudentID And b.CCMonth = month(DATEADD("m",-1,GETDATE())) And b.CCYear = DATEPART(year,GETDATE()))

print '@Emails='+ isnull(@Emails, 'empty')

EXEC msdb..sp_send_dbmail @profile_name='MyProfile',
@recipients= @Emails,
@subject='Update billing information',
@body='Hello, your billing information is about to expire. Please update as soon as possible. Thank you.'

And yes, sql server agent is running.
0
 

Expert Comment

by:ed_collen
ID: 33697359
mlq101,

Another suggestion which might save you some time, you can use the at command on windows to schedule tasks quite easily, so if you write the program you could schedule it to run every weekday at 10am using:

at 10:00 /every:M,T,W,Th,F C:\SQL_Email_Program.exe
0
 
LVL 57

Assisted Solution

by:Raja Jegan R
Raja Jegan R earned 250 total points
ID: 33698857
Try executing this in Job step.
Can you post the error you are obtaining so that it can be fixed accordingly:

Declare @Emails varchar(526);
Select @Emails=isnull(@Emails+';','') + a.Email From Student As a
Where a.Email>''
and exists (SELECT * From CreditCard As b WHERE a.StudentID = b.StudentID And b.CCMonth = month(DATEADD("m",-1,GETDATE())) And b.CCYear = DATEPART(year,GETDATE()));

EXEC msdb..sp_send_dbmail @profile_name='MyProfile',
@recipients= @Emails,
@subject='Update billing information',
@body='Hello, your billing information is about to expire. Please update as soon as possible. Thank you.';
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

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

758 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

21 Experts available now in Live!

Get 1:1 Help Now