I have a stored procedure that is called by another stored procedure
ALTER PROCEDURE [dbo].[usp_Test]
AS
begin
declare @errorCode int
declare @lastIdentity int
select @errorCode = @@ERROR
if @errorCode=0
begin
update Vehicle set model='1996----------'
where Make='MERC'
select @errorCode = @@ERROR
select @lastIdentity = @@IDENTITY
end
print 'usp_test lastIdentity=' + convert(varchar(10), isnull(@lastIdentity,0))
print 'usp_test errorCode=' + convert(varchar(10), @errorCode)
end
If I call the stored procedure like this
declare @RetVal int
exec @RetVal=usp_Test
print 'return value is ' + convert(varchar(10), @RetVal)
I get the following messages
Msg 8152, Level 16, State 14, Procedure usp_Test, Line 14
String or binary data would be truncated.
The statement has been terminated.
usp_test lastIdentity=0
usp_test errorCode=8152
return value is -6
By adding RETURN 0 at the end and RETURN @errorCode after the select @errorCode… I will have a nice clean way of returning the error and subsequently handle it. I am surprise that without any RETURN’s I get a return value of -6. Can anyone explain why this is the case?
copied from this answer