patternsqlMinor
SqlServer output and return value in procedure
Viewed 0 times
returnsqlserveroutputvalueprocedureand
Problem
I'm new to sqlserver, and i have this error that i was not able to fix:
Msg 245, Level 16, State 1, Procedure info, Line 11 Conversion failed when converting the varchar value 'Steven' to data type int
Can you please show me where is the error, and how can i fix it? The code is as follow thank you.
I have noticed that: If instead of using "return" I use "print" to visualize the "@name", the procedure works, and i see "@name". It also works well if instead of "return" I put "@name" as an output parameter. It seems that the error is only when using "return" to return @name. Why is this? What i'm missing to understand?
Msg 245, Level 16, State 1, Procedure info, Line 11 Conversion failed when converting the varchar value 'Steven' to data type int
Can you please show me where is the error, and how can i fix it? The code is as follow thank you.
CREATE PROCEDURE info (@id int , @howMuch int OUTPUT) AS
select * from emp where empno=@id;
select @howMuch =count (*) from emp;
declare @name varchar(100)
select @name=ename from emp where empno=@id
return @name --with return doesn't work!!
--select @name with select it works !!
--@name as an output parameter it works!!
GO
-- execute the procedure. It gives me error.
DECLARE @num_rows int, @who varchar(100)
EXECUTE @who=info 100, @num_rows OUTPUT
select @num_rows,@whoI have noticed that: If instead of using "return" I use "print" to visualize the "@name", the procedure works, and i see "@name". It also works well if instead of "return" I put "@name" as an output parameter. It seems that the error is only when using "return" to return @name. Why is this? What i'm missing to understand?
Solution
It looks like you are attempting to give the value of
Better off declaring
Quote from Ref. https://msdn.microsoft.com/en-us/library/ms174998.aspx
RETURN
Exits unconditionally from a query or procedure. RETURN is immediate
and complete and can be used at any point to exit from a procedure,
batch, or statement block. Statements that follow RETURN are not
executed.
@name back to the caller, but that is not the purpose of RETURN. When you give an argument after RETURN it must be an integer which indicates the status of the procedure.Better off declaring
@name as an output parameter or SELECT @name.Quote from Ref. https://msdn.microsoft.com/en-us/library/ms174998.aspx
RETURN
Exits unconditionally from a query or procedure. RETURN is immediate
and complete and can be used at any point to exit from a procedure,
batch, or statement block. Statements that follow RETURN are not
executed.
Context
StackExchange Database Administrators Q#154424, answer score: 4
Revisions (0)
No revisions yet.