[英]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我正在尝试实现搜索功能,用户可以通过
BusName
或BusNumber
获取存储在主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.