Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 519
  • Last Modified:

Run task automatically, daily

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
mlg101
Asked:
mlg101
  • 4
  • 3
  • 3
  • +6
2 Solutions
 
wolfcamelCommented:
write the vb script and make it a task in windows scheduler.
0
 
mlg101Author Commented:
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
 
Raja Jegan RSQL Server DBA & ArchitectCommented:
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
Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

 
Senthil BCommented:
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
 
Mohit VijayCommented:
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
 
Deepu SreedharBI Software EngineerCommented:
I will also go with the Windows Service option, as I did one like this to work in every 24 hrs.
0
 
funazonkiCommented:
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
 
mlg101Author Commented:
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
 
funazonkiCommented:
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
 
mlg101Author Commented:
My version of visual studio does not allow me to create a windows service
0
 
Raja Jegan RSQL Server DBA & ArchitectCommented:
>> 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
 
xiong8086Commented:
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
 
mlg101Author Commented:
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
 
ed_collenCommented:
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
 
Raja Jegan RSQL Server DBA & ArchitectCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

  • 4
  • 3
  • 3
  • +6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now