Getting a Return Value From EXEC | Database Journal

Getting a Return Value From EXEC

Written By
Alan Enderby
Alan Enderby
Oct 4, 2000
1 minute read

— 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

Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.