SQL Server – msdb.dbo.sp_start_job Stuck in Stored Procedure

jobssql serversql-server-agentstored-procedures

I have a stored procedure USP_A with logic like following:

......
BEGIN TRY
exec dbo.usp_Log 'Start to run job job1'
Exec @intErrorCode = msdb.dbo.sp_start_job 'job1'   
  IF @intErrorCode <> 0 Goto errorHandling
exec dbo.usp_Log 'End to run job job1'
Return @intErrorCode
END TRY
BEGIN CATCH
  exec dbo.usp_Log 'Error......'
END CATCH
......

But when I run this stored procedure, it got stuck and when I check log I can only see message 'Start to run job job1'. And no error being caught in CATCH block. Also in the SQL Server Agent job monitor I can not see this job get triggered.

But if I manually run

Exec @intErrorCode = msdb.dbo.sp_start_job 'job1' 

it works fine.

The SQL Server is Microsoft SQL Server 2005 Enterprise Edition (version 9.00.5000.00)

Update

It seems that the sp is crashed rather than hungup. Because I used sp_WhoIsActive to find which query is waiting and no record returned.

I also tried to comment the start job logic and just

Return @intErrorCode

However the SP still hangs (or crashes).

Best Answer

It turns out to be some kind of SQL Server bug (Maybe the SQL Server 2005 is quite old). The stored procedure just ended unexpectedly and does not return any code.

So the problem is solved by moving all the logic before this msdb.dbo.sp_start_job procedure into a separate stored procedure.

Hope it can help anyone who got this same issue.