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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Status Code from Db Job
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

37 Posts

Posted - 10/08/2007 :  06:09:31  Show Profile  Reply with Quote

Below is the piece of code:

EXECUTE @ReturnCode = msdb.dbo.sp_add_jobstep @job_id = @JobID,
@step_id = 1,
@step_name = N'Add Task to TCM Schedule',
@command = @strCommand,
@database_name = N'master',
@server = N'',
@database_user_name = N'',
@subsystem = N'TSQL',--run operating system command
@cmdexec_success_code = 0,
@flags = 2, --used for over writing log file
@retry_attempts = 5,
@retry_interval = 15, --retry at every 15 mins
@on_success_action = 4,
@on_success_action = 4,
@on_fail_action = 4,
@on_fail_step_id = 4

Here the @strCommand is the TSQL executing a stored procedure. Even though i'm returning status code = -1001 when stored procedure fails, the job ends successfully and deletes itself as set in the conditions while creating the job.
Is there any condition where in we can set the return code of stored procedure = return code of the job?

Or is there any other solution to this problem?

Any help will be highly appreciated.


Flowing Fount of Yak Knowledge

7266 Posts

Posted - 10/08/2007 :  21:38:56  Show Profile  Reply with Quote
What do you mean return code of the job? You have to run the job to get job return code.
Go to Top of Page

Starting Member

37 Posts

Posted - 10/09/2007 :  00:43:07  Show Profile  Reply with Quote
When job runs it should return some code if it has passed, failed or hanged...
Go to Top of Page

Almighty SQL Goddess

38200 Posts

Posted - 10/09/2007 :  00:51:07  Show Profile  Visit tkizer's Homepage  Reply with Quote
This should all be handled inside the job step by T-SQL code and not by the job scheduler. Use variables and IF statements to determine the status of your stored procedure. Then decide whether or not to fail the job or let it succeed.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000