How can i exit in the middle of a stored procedure?
i have a stored procedure where i want to bail out early (while trying to debug it). i've tried calling RETURN
and RAISERROR
, and the sp keeps on running:
CREATE PROCEDURE dbo.Archive_Session @SessionGUID uniqueidentifier AS
print 'before raiserror'
raiserror('this is a raised error', 18, 1)
print 'before return'
return -1
print 'after return'
[snip]
i know it keeps running because i encounter an error further down. i don't see any of my prints. If i comment out the bulk of the stored procedure:
CREATE PROCEDURE dbo.Archive_Session @SessionGUID uniqueidentifier AS
print 'before raiserror'
raiserror('this is a raised error', 18, 1)
print 'before return'
return -1
print 'after return'
/*
[snip]
*/
Then i don't get my error, and i see the results:
before raiserror
Server: Msg 50000, Level 18, State 1, Procedure Archive_Session, Line 5
this is a raised error
before return
So the question is: how do i bail out of a stored procedure in SQL Server?