Getting a Return Value From EXEC

— The procedure

drop proc exec_with_return_code
go
create proc exec_with_return_code @tsql varchar(255)
as
set nocount on
create table #return (code int null)
insert into #return EXEC (‘DECLARE @rc int exec @rc=’ + @tsql + ‘
select code=@rc’)
declare @rc int select @rc=isnull(code,0) from #return
return @rc
go

— A test procedure

drop proc return_me
go
create proc return_me @rc int=0
as
return
@rc
go

–Test

declare @rc int
exec @rc=exec_with_return_code ‘return_me 12’
select ‘The return code is’=@rc

Get the Free Newsletter!

Subscribe to Cloud Insider for top news, trends & analysis

Latest Articles