views:

29

answers:

2

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 proc

declare @ErrorMessage varchar(255) if @TestFlag = 0 set @ErrorMessage = 'Test' 
return @ErrorMessage 

calling on asp.net Updated:

Error: Conversion failed when converting the varchar value 'Development' to data type int.

   using (DataContextDataContext dc = conn.GetContext())
{
    string serverName = ""
    var result = dc.spGetServerName(ref serverName);                    
    return result.ToString();

}
A: 

No. Please see: http://msdn.microsoft.com/en-us/library/ms174998.aspx

ngroot
+4  A: 

No, you can't return anything but an int using the RETURN statement. You'd need to use OUTPUT parameters instead. They are more flexible.

e.g.

CREATE PROCEDURE dbo.ExampleSproc 
    @OutParam VARCHAR(10) OUTPUT
AS
SET @OutParam = 'Test'
...
GO

Update:
For an example on how to retrieve the output param, see this article.

AdaTheDev
how do i call the storproc to get the output?
Abu Hamzah
i have update my question.
Abu Hamzah
@Nisar Khan - see the link in the update in my question
AdaTheDev
@Ada: please see my updated question, the link you gave me is talk about int output what i am doing is returning string.
Abu Hamzah
@Nisar Khan - it gives you the exact approach you need, it's just that in that example, the output parameter is an INTEGER. You just define your OUTPUT parameter as a VARCHAR, like my example, and tweak the .NET code to define the .NET variable as a string instead of an int like the example.
AdaTheDev