简体   繁体   English

在 SQL 服务器存储过程中将 varchar 值 '%' 转换为数据类型 int 时转换失败

[英]Conversion failed when converting the varchar value '%' to data type int in SQL Server stored procedure

I am working on a SQL Server stored procedure.我正在处理 SQL 服务器存储过程。 I have table with these columns我有这些列的表

BusName(NVarchar(100) not null)
BusNumber(int, null) 

in my main Bus table.在我的主Bus表中。

I am trying to implement search functionality where user can either pass BusName or BusNumber to get all the results stored in main Bus table我正在尝试实现搜索功能,用户可以通过BusNameBusNumber获取存储在主Bus表中的所有结果

I have implemented this stored procedure to handle the search functionality:我已经实现了这个存储过程来处理搜索功能:

CREATE PROCEDURE [dbo].[spGetAllBus]
    @BusName VARCHAR(512),
    @BusNo INT
AS
    SET NOCOUNT ON;  
BEGIN
     SELECT 
         [BusName], 
         [ManagingOwner],
         [Operator],
         [CurrentService], [CurrentRate],
         [BusNumber],
         [MarketRate],
         [EarliestRdel]
     FROM 
         [dbo].[Bus] 
     WHERE 
         [BusName] LIKE '%' + @BusName + '%' 
         OR [BusNumber] LIKE '%' + @BusNo + '%' 
END

When I execute this stored procedure like this:当我像这样执行这个存储过程时:

DECLARE @return_value int

EXEC    @return_value = [dbo].[spGetAllBus]
        @BusName = N'emma',
        @BusNo = NULL
SELECT  'Return Value' = @return_value

I get an error我得到一个错误

Conversion failed when converting the varchar value '%' to data type int.将 varchar 值“%”转换为数据类型 int 时转换失败。

I am struggling to find the root cause as I am not having any conversions in the code.我正在努力寻找根本原因,因为我没有在代码中进行任何转换。 Can someone help me here.有人可以帮我吗。 Any help would be appreciated.任何帮助,将不胜感激。 Thanks谢谢

You can alter your store procedure like below if your BusNumber column's type is INT.如果您的 BusNumber 列的类型是 INT,您可以像下面这样更改您的存储过程。

ALTER PROCEDURE [dbo].[spGetAllBus]
   @BusName VARCHAR(512),
   @BusNo INT
AS
   SET NOCOUNT ON;  
BEGIN
 SELECT 
     [BusName], 
     [ManagingOwner],
     [Operator],
     [CurrentService], [CurrentRate],
     [BusNumber],
     [MarketRate],
     [EarliestRdel]
 FROM 
     [dbo].[Bus] 
 WHERE 
     [BusName] LIKE '%' + @BusName + '%' 
     OR [BusNumber] = @BusNo 
END

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在存储过程中将varchar值'SVGFPPL PONDY'转换为数据类型int时转换失败 - Conversion failed when converting the varchar value 'SVGFPPL PONDY' to data type int in stored procedure 将varchar值'%'转换为数据类型int SQL时转换失败 - Conversion failed when converting the varchar value '%' to data type int SQL 在SQL中将varchar值转换为数据类型int时转换失败 - Conversion failed when converting varchar value to data type int in SQL 将varchar值'%%'转换为数据类型int时,经典SQL形式的SQL Server转换失败 - Classic ASP form giving SQL Server Conversion failed when converting the varchar value '%%' to data type int 将varchar值转换为SQL Server中的数据类型int时转换失败 - Conversion failed when converting the varchar value to data type int in SQL Server 将varchar值int转换为数据类型int时转换失败 - Conversion failed when converting the varchar value int to data type int 将varchar值'%'转换为数据类型int时转换失败 - Conversion failed when converting the varchar value '%' to data type int Error 将varchar值'Id'转换为数据类型int时转换失败 - Conversion failed when converting the varchar value 'Id' to data type int 将varchar值'1 * 2 * 3'转换为数据类型int时转换失败 - Conversion failed when converting the varchar value '1*2*3' to data type int 将varchar值'tblSQLAdminInventory'转换为数据类型int时转换失败 - Conversion failed when converting the varchar value 'tblSQLAdminInventory' to data type int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM