10

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.

Community
  • 1
  • 1
Pascal Weber
  • 557
  • 3
  • 11
  • 19
  • 3
    Have a look at [Return](http://msdn.microsoft.com/en-us/library/ms174998.aspx). You probably want to be doing `Select 'works!'` – Manatherin Nov 30 '12 at 16:37
  • 1
    And regarding your quesion about `GO` see http://stackoverflow.com/questions/2299249/what-is-the-use-of-go-in-sql-server-management-studio – Martin Smith Nov 30 '12 at 16:39

1 Answers1

24

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
Aleksandr Fedorenko
  • 16,594
  • 6
  • 37
  • 44
AdaTheDev
  • 142,592
  • 28
  • 206
  • 200