Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stored Procedure returning a string? [duplicate]

Possible Duplicate:
Conversion failed when converting the nvarchar value ‘Internet Explorer 3 original’ to data type int

So I created this following simple stored procedure:

CREATE PROCEDURE test
AS
BEGIN
    RETURN 'works!'
END
GO

I then wanted to execute it by firing the following statement:

EXEC test

Why am I getting the following error all the time?

Conversion failed when converting the varchar value 'works!' to data type int.

Oh and btw, (when) is it necessary to have the GO-statement in the end of a stored procedure? I guess it takes no effect here since it will jump out of the procedure when I am returing my @out String.

like image 964
Pascal Weber Avatar asked Nov 30 '12 16:11

Pascal Weber


People also ask

Can procedure return multiple values?

Multiple values will be returned from Stored Procedure by returning comma separated (delimited) values using Output Parameter. Output Parameter is supported in Stored Procedures of all SQL Server versions i.e. 2000, 2005, 2008, 2008R2, 2012 and 2014.

How do you return a successful message in SQL stored 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.

Can stored procedures return value?

A stored procedure does not have a return value but can optionally take input, output, or input-output parameters. A stored procedure can return output through any output or input-output parameter.


1 Answers

Using RETURN only allows you to return an INTEGER code

You either need to SELECT the value

   CREATE PROCEDURE test
   AS
   BEGIN
       SELECT 'works!'
   END

or if you want to assign into a variable in the caller, use an OUTPUT parameter

CREATE PROCEDURE test
    @outputVal VARCHAR(20) OUTPUT
AS
BEGIN
    SET @outputVal = 'works!'
END

DECLARE @val VARCHAR(20)
EXECUTE test @val OUTPUT
like image 188
AdaTheDev Avatar answered Sep 30 '22 14:09

AdaTheDev