Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Run SQL Job from Stored Procedure

Author  Topic 

cblash
Starting Member

7 Posts

Posted - 2009-11-17 : 15:07:55
Is there a way to run a job from a stored procedure? I am programming something in .NET and need to be able to automate scheduling a task and pass in the name of the task, the start and end times, and the frequency of the task as parameters. It seems that the best option would be to run a job from a stored procedure if it is possible.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-17 : 15:18:58
You can start a job via sp_start_job.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-11-17 : 15:26:35
should know that even if the job fails the sp will succeed though.
Go to Top of Page

cblash
Starting Member

7 Posts

Posted - 2009-11-17 : 15:38:27
Can you give me an example or give me a link I can refer to?
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-11-17 : 15:44:36
[code]
Create Proc startAJob
AS
Exec sp_start_job 'my sql agent job';
GO
[/code]

http://msdn.microsoft.com/en-us/library/ms186757(SQL.90).aspx
Go to Top of Page

cblash
Starting Member

7 Posts

Posted - 2009-11-17 : 15:51:13
quote:
Originally posted by russell


Create Proc startAJob
AS
Exec sp_start_job 'my sql agent job';
GO


http://msdn.microsoft.com/en-us/library/ms186757(SQL.90).aspx




Thank you
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-11-17 : 15:54:11
You're welcome. Happy to help
Go to Top of Page

cblash
Starting Member

7 Posts

Posted - 2009-11-17 : 16:36:00
quote:
Originally posted by russell

You're welcome. Happy to help



Would I be able to pass in values from the stored procedure to the job? Sorry to ask so many questions but I have never created job before.
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-11-17 : 16:56:30
what is this job going to do? may be better to have the SP do the whole thing...?

this might not be helpful but have a look here: http://msdn.microsoft.com/en-us/library/ms175575.aspx
Go to Top of Page

cblash
Starting Member

7 Posts

Posted - 2009-11-17 : 17:44:44
quote:
Originally posted by russell

what is this job going to do? may be better to have the SP do the whole thing...?

this might not be helpful but have a look here: http://msdn.microsoft.com/en-us/library/ms175575.aspx



Its a vb.net app that allows for scheduling an event to be run daily, weekly, monthly, or yearly with start date, end date, and how often to be scheduled passed in.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-11-17 : 17:50:23
I'm confused. Are you trying to start a job or create a job?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-11-17 : 19:46:16
me too. seems op wants to create one
Go to Top of Page

cblash
Starting Member

7 Posts

Posted - 2009-11-18 : 08:18:46
quote:
Originally posted by tkizer

I'm confused. Are you trying to start a job or create a job?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."



I want to create a stored procedure that creates a job.
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2009-11-18 : 10:31:12
have a look at sp_add_job

somehow though, i suspect you're taking the wrong approach. if you want the front end to schedule jobs, you can have it store the parameters in a table, and have a sql agent job look at this table at some set interval
Go to Top of Page

cblash
Starting Member

7 Posts

Posted - 2009-11-18 : 10:45:19
quote:
Originally posted by russell

have a look at sp_add_job

somehow though, i suspect you're taking the wrong approach. if you want the front end to schedule jobs, you can have it store the parameters in a table, and have a sql agent job look at this table at some set interval



That sounds more like what I am trying to do. There is a part of my vb.net app that allows the user to input how often they want an event to be scheduled. The event will only show up on or after the day it has been scheduled for.
Go to Top of Page
   

- Advertisement -