12

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 

3 Answers 3

23

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

1
  • 1
    And to call: DECLARE @ManagerNameOutput VARCHAR; EXEC GetImmediateManager 1, @ManagerNameOutput OUTPUT
    – Sal
    Commented Sep 4, 2018 at 19:40
8

You will need to create a stored function for that:

create function dbo.GetLookupValue(@value INT)
returns varchar(100)
as begin
  declare @result varchar(100)

  select
    @result = somefield
  from 
    yourtable
  where 
    ID = @value;

  return @result
end

You can then use this stored function like this:

select dbo.GetLookupValue(4) 

Marc

5

A stored procedure's return code is always integer, but you can have OUTPUT parameters that are any desired type -- see http://msdn.microsoft.com/en-us/library/aa174792.aspx .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.