Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stored procedure returns varchar

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 
like image 706
rmdussa Avatar asked May 29 '09 05:05

rmdussa


People also ask

What does stored procedure return?

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.

Can a stored procedure return data?

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.

What is the return type of a procedure?

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.

How many values can be returned from a stored procedure?

A stored function can return only one value, unlike a stored procedure, which can return multiple values or an entire result set.


1 Answers

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

like image 191
Binoj Antony Avatar answered Sep 19 '22 13:09

Binoj Antony