RAISERROR / THROW and sp_start_job Termination Behaviour
2
votes
1
answer
258
views
Given the following SQL Server Agent job
USE [msdb]
GO
BEGIN TRANSACTION
DECLARE @ReturnCode INT
SELECT @ReturnCode = 0
IF NOT EXISTS (SELECT name FROM msdb.dbo.syscategories WHERE name=N'[Uncategorized (Local)]' AND category_class=1)
BEGIN
EXEC @ReturnCode = msdb.dbo.sp_add_category @class=N'JOB', @type=N'LOCAL', @name=N'[Uncategorized (Local)]'
IF (@@ERROR 0 OR @ReturnCode 0) GOTO QuitWithRollback
END
DECLARE @jobId BINARY(16)
EXEC @ReturnCode = msdb.dbo.sp_add_job @job_name=N'Do Nothing',
@enabled=1,
@notify_level_eventlog=0,
@notify_level_email=0,
@notify_level_netsend=0,
@notify_level_page=0,
@delete_level=0,
@description=N'No description available.',
@category_name=N'[Uncategorized (Local)]',
@owner_login_name=N'Me', @job_id = @jobId OUTPUT
IF (@@ERROR 0 OR @ReturnCode 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'Wait',
@step_id=1,
@cmdexec_success_code=0,
@on_success_action=1,
@on_success_step_id=0,
@on_fail_action=2,
@on_fail_step_id=0,
@retry_attempts=0,
@retry_interval=0,
@os_run_priority=0, @subsystem=N'TSQL',
@command=N'WAITFOR DELAY ''00:00:10''',
@database_name=N'master',
@flags=0
IF (@@ERROR 0 OR @ReturnCode 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_update_job @job_id = @jobId, @start_step_id = 1
IF (@@ERROR 0 OR @ReturnCode 0) GOTO QuitWithRollback
EXEC @ReturnCode = msdb.dbo.sp_add_jobserver @job_id = @jobId, @server_name = N'(local)'
IF (@@ERROR 0 OR @ReturnCode 0) GOTO QuitWithRollback
COMMIT TRANSACTION
GOTO EndSave
QuitWithRollback:
IF (@@TRANCOUNT > 0) ROLLBACK TRANSACTION
EndSave:
GO
And the following calling code
EXEC msdb.dbo.sp_start_job @job_name = 'Do nothing' -- waits for 10 seconds
WAITFOR DELAY '00:00:05'
EXEC msdb.dbo.sp_start_job @job_name = 'Do nothing' -- fails, job still running
WAITFOR DELAY '00:00:10'
EXEC msdb.dbo.sp_start_job @job_name = 'Do nothing' -- should succeed
I get the following error:
Job 'Do nothing' started successfully. Msg 22022, Level 16, State 1, Line 25 SQLServerAgent Error: Request to run job Do Nothing (from User Me) refused because the job is already running from a request by User Me. Job 'Do nothing' started successfully.
So we can see the second
sp_start_job
call failed because the job was still running, then SQL Server continued execution
the failure is a Level 16 and when I do the following
PRINT 'hello'
RAISERROR ('error',16,1)
PRINT 'hello'
I get the same "fail and continue" behaviour
hello
Msg 50000, Level 16, State 1, Line 50
error
hello
however, if I do the following, which also raises a Level 16 error, the second print is not run
PRINT 'hello'
;THROW 51000, 'error', 1;
PRINT 'hello'
hello
Msg 51000, Level 16, State 1, Line 50
error
The docs for THROW state
> Any error that occurs in a THROW statement causes the statement batch to be terminated.
The docs for RAISERROR state
> The error is returned to the caller if RAISERROR is run:
>Outside the scope of any TRY block.
>With a severity of 10 or lower in a TRY block.
>With a severity of 20 or higher that terminates the database connection.
My questions is, is sp_start_job
using RAISERROR rather than throw - I have looked into
the definition and can't see if anywhere
Asked by SE1986
(2182 rep)
Dec 16, 2024, 12:00 PM
Last activity: Dec 16, 2024, 09:06 PM
Last activity: Dec 16, 2024, 09:06 PM