Conversion failed when converting the nvarchar value 'Internet Explorer 3 original' to data type int

前端 未结 2 687
一整个雨季
一整个雨季 2021-01-15 02:34

In SQL Server 2008 (TSQL), I\'ve created a stored procedure like this:

CREATE PROCEDURE SP_1_10_2
AS
declare @mostValuableBook nvarchar(255)
SELECT @mostValu         


        
2条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-15 03:07

    RETURN cannot be used to return nvarchar / varchar such as you have. RETURN is used to return an integer, this can be expressed as some sort of status code 1=True / 0=False. Read more about return here: http://msdn.microsoft.com/en-us/library/ms174998.aspx

    In your case, you simply need to use OUTPUT variables which is similiar to pass-by-ref in C# or C++. You pass the variable to the sproc, the sproc modifies it, and you get the expected results after a SELECT....

    Change it so that your parameters becomes an output parameter:

    CREATE PROCEDURE SP_1_10_2
    @mostValueableBook nvarchar(255) output
    AS
    SELECT @mostValuableBook = Name
    FROM books
    WHERE price =
        ( SELECT MAX(price)
          FROM books
          WHERE izd LIKE '%BHV%' );
    SELECT @mostValuableBook
    GO
    

    Call it like so:

    DECLARE @theValBook nvarchar(255)
    EXECUTE SP_1_10_2 @mostValuableBook = @theValBook output
    

    Then you can say:

    SELECT 'Most expensive book is', @theValBook

提交回复
热议问题