I would like to know if in SQL is it possible to return a varchar
value from a stored procedure, most of the examples I have seen the return value is an int.
Example within a procedure:
declare @ErrorMessage varchar(255)
if @TestFlag = 0
set @ErrorMessage = 'Test'
return @ErrorMessage
The RETURN statement is used to unconditionally and immediately terminate an SQL procedure by returning the flow of control to the caller of the stored procedure. It is mandatory that when the RETURN statement is executed that it return an integer value. If the return value is not provided, the default is 0.
You can use one or more RETURN statements in a stored procedure. The RETURN statement can be used anywhere after the declaration blocks within the SQL-procedure-body. To return multiple output values, parameters can be used instead. Parameter values must be set prior to the RETURN statement being executed.
Return Value in SQL Server Stored Procedure In default, when we execute a stored procedure in SQL Server, it returns an integer value and this value indicates the execution status of the stored procedure. The 0 value indicates, the procedure is completed successfully and the non-zero values indicate an error.
A stored function can return only one value, unlike a stored procedure, which can return multiple values or an entire result set.
You can use out parameter or the resulset to return any data type.
Return values should always be integer
CREATE PROCEDURE GetImmediateManager
@employeeID INT,
@managerName VARCHAR OUTPUT
AS
BEGIN
SELECT @managerName = ManagerName
FROM HumanResources.Employee
WHERE EmployeeID = @employeeID
END
Taken from here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With