In testing output values from procs, why does the final select @TestValOut return 0 instead of null or an empty string?
I understand the correct way to do this is by using OUTPUT parameters, so the question really becomes: Why is the datatype of the set value of @TestValOut, at execution, an integer?
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'Custom.test') AND type in (N'P', N'PC'))
DROP PROCEDURE Custom.test
GO
CREATE PROCEDURE Custom.test (
@CurrentUserID INT = 1
)
As
Declare @TestValIn varchar(max)
select @TestValIn='asdf'
GO
BEGIN TRAN
Declare @TestValOut varchar(max)
set @TestValOut='ffff'
Exec @TestValOut=Custom.test @CurrentUserID=1
select @TestValOut
ROLLBACK
A return value in a stored procedure is always an integer, as a matter of fact you can only use an integer with a return value. The fact that you see 0 means the proc executed correctly, this is the return value that SQL Server returns telling you what the result of the proc execution is
For fun do a
select 1/0in the proc and you will see it won’t be 0 anymoreSee here
Is a return value of 0 always a success in stored procedures?
here are the examples from that answer
Now run it
Now let’s do it again without the return statement
Better to use an
output parameterto pass back statuses and or messages