首页 文章

将数据类型varchar转换为int时出错

提问于
浏览
2

我'm trying to return ' ItemId ' column value of ' item ' table using stored procedure based on the input value of '名称'列是varchar类型的,但每当我将任何值传递给存储过程时,它都会返回一个错误: Error converting data type varchar to int.

create procedure RetrieveId
(@itemId int output,@Name varchar(30))
As
Begin
If exists(Select  * from item where [Name] = @Name)
Begin
 Select @itemId = itemid from item 
  where [Name] = @Name
 return @itemId
End
Else
return 1
End

这就是我所说的:

RetrieveId 'asf'

1 回答

  • 4

    您必须匹配参数:RETURN不会填充OUTPUT参数:您对@itemid的分配就是这样做的 .

    DECLARE @item int
    EXEC RetrieveId @item OUTPUT, 'asf'
    

    此外,您的存储过程太复杂 . RETURN不是从存储过程返回数据的好选择,并且EXISTS是不必要的 . 在这种情况下,如果找不到@itemId将为NULL

    create procedure RetrieveId
       @itemId int output,
       @Name varchar(30)
    As
       Select @itemId = itemid
       from item 
       where [Name] = @Name
    GO
    

相关问题