If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Running agent jobs from command line

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-13-03, 06:59
damgog damgog is offline
Registered User
 
Join Date: Jul 2003
Posts: 16
Running agent jobs from command line

Hi,

How can i run a SQL Server Agent Job from the command line on the same box? I am using SQL Server 2000.

Thanks
Reply With Quote
  #2 (permalink)  
Old 11-13-03, 07:37
mtracey mtracey is offline
Registered User
 
Join Date: Oct 2003
Location: Ireland
Posts: 54
Check out the SQL Books Online for Command Line execution of SQL Agent

The dtsrun utility executes a package created using Data Transformation Services (DTS). The DTS package can be stored in the Microsoft® SQL Server™ msdb database, a COM-structured storage file, or SQL Server Meta Data Services.

Syntax
dtsrun
[/?] |
[
[
/[~]S server_name[\instance_name]
{ {/[~]U user_name [/[~]P password]} | /E }
]
{
{/[~]N package_name }
| {/[~]G package_guid_string}
| {/[~]V package_version_guid_string}
}
[/[~]M package_password]
[/[~]F filename]
[/[~]R repository_database_name]
[/A global_variable_name:typeid=value]
[/L log_file_name]
[/W NT_event_log_completion_status]
[/Z] [/!X] [/!D] [/!Y] [/!C]
]



dtsrun /Sserver_name /Uuser_nName /Ppassword /Npackage_name /Mpackage_password
Reply With Quote
  #3 (permalink)  
Old 11-13-03, 07:59
damgog damgog is offline
Registered User
 
Join Date: Jul 2003
Posts: 16
So i have to create a DTS package to run my job and then use dtsrun to run that package?
Reply With Quote
  #4 (permalink)  
Old 11-13-03, 08:01
mtracey mtracey is offline
Registered User
 
Join Date: Oct 2003
Location: Ireland
Posts: 54
Sorry I sent you the wrong script. I'll be back with the correct one shortly. Mark
Reply With Quote
  #5 (permalink)  
Old 11-13-03, 08:13
mtracey mtracey is offline
Registered User
 
Join Date: Oct 2003
Location: Ireland
Posts: 54
You could create a stored procedure to run the job. Example:
(You may be able to pass the name through as a parameter ??)

CREATE PROCEDURE sp_RunJob
AS

BEGIN

EXEC msdb..sp_start_job @job_name = 'The job name'

END

You can also use the job id etc... do a search in the Books Online for sp_start_job and you'll get the syntax.

Then from the command line, create a .bat file and put the following isql command synax into the .bat file:


isql /U username -n -E /d "database name" -Q "stored procedure name" /S servername -oC:\returncode.txt

This will actually produce some output into the returncode.txt file, which might be useful. I'm not sure whether you'll have to return a value back out of the stored procedure or not. I've not tried. My sample above it a cut down version. But anyhow there should be enough above to give you the idea in terms of syntax etc.

You would then just execute the .bat file on your server/pc etc, perhaps via a scheduler etc...


Anyhow a search on SQL Books Online will give you all the details. Search for isql , isqlw etc..
Reply With Quote
  #6 (permalink)  
Old 11-13-03, 08:56
damgog damgog is offline
Registered User
 
Join Date: Jul 2003
Posts: 16
Cheers Mark.
Reply With Quote
  #7 (permalink)  
Old 06-30-09, 00:26
LimaCharlie LimaCharlie is offline
Registered User
 
Join Date: Oct 2005
Posts: 119
Quote:
Originally Posted by mtracey
You could create a stored procedure to run the job. Example:
(You may be able to pass the name through as a parameter ??)

CREATE PROCEDURE sp_RunJob
AS

BEGIN

EXEC msdb..sp_start_job @job_name = 'The job name'

END

You can also use the job id etc... do a search in the Books Online for sp_start_job and you'll get the syntax.

Then from the command line, create a .bat file and put the following isql command synax into the .bat file:


isql /U username -n -E /d "database name" -Q "stored procedure name" /S servername -oC:\returncode.txt

This will actually produce some output into the returncode.txt file, which might be useful. I'm not sure whether you'll have to return a value back out of the stored procedure or not. I've not tried. My sample above it a cut down version. But anyhow there should be enough above to give you the idea in terms of syntax etc.

You would then just execute the .bat file on your server/pc etc, perhaps via a scheduler etc...


Anyhow a search on SQL Books Online will give you all the details. Search for isql , isqlw etc..
can we pass a parameter to this stored procedure when running from a command line?

how then is the syntax?

thanks.
Reply With Quote
  #8 (permalink)  
Old 06-30-09, 03:23
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,309
Sorry LimaCharlie, after six years I doubt that the thread has much life left in it!

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #9 (permalink)  
Old 07-01-09, 14:51
PMASchmed PMASchmed is offline
Registered User
 
Join Date: Jun 2004
Location: Long Island
Posts: 696
Oh it does, just used it for info.
Reply With Quote
  #10 (permalink)  
Old 12-01-09, 13:12
chrismilleruk chrismilleruk is offline
Registered User
 
Join Date: Dec 2009
Posts: 1
Update

I thought I'd just update this thread with the latest info as came up number 1 in Google and didn't quite answer the question for me.

You can run a SQL server agent job from the command line with the following:
Code:
osql -E -d MSDB -Q "sp_start_job 'MySQLJob'"
-E means use use Integrated (Windows) security. If you need to use a SQL username and password, you can do this instead:
Code:
osql -U user1 -P password -d MSDB -Q "sp_start_job 'MySQLJob'"
Either of these statements can be placed inside a batch file for convenience but it's important to note that if you use a % character in your command line then it needs to be escaped to %% when used from inside the batch file.

The following links may also be useful:
sp_start_job docs (note the required permissions)
osql Utility
Reply With Quote
  #11 (permalink)  
Old 12-01-09, 14:24
Brett Kaiser Brett Kaiser is offline
SQLTeam Scrub
 
Join Date: Nov 2002
Location: Jersey
Posts: 9,984
Quote:
Originally Posted by LimaCharlie View Post
can we pass a parameter to this stored procedure when running from a command line?

how then is the syntax?

thanks.
Yes

This massage is too short
__________________
Brett
8-)

It's a Great Day for America everybody!
My Blog
My SQL Blog dbforums Yak CorralRadio 'Rita
dbForums Member List SQLTeam Member List
It's 5:00 Somewhere Pearls
The physical order of data in a database has no meaning.
Reply With Quote
  #12 (permalink)  
Old 12-02-09, 01:01
jezemine jezemine is offline
another indirection layer
 
Join Date: May 2004
Location: Seattle
Posts: 1,311
osql is deprecated in 2005 and beyond. use sqlcmd instead.
__________________
elsasoft.org
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On