Using LIKE operator with stored procedure parameters(使用带有存储过程参数的 LIKE 运算符)
问题描述
我有一个存储过程,它使用 LIKE 运算符在其他一些参数中搜索卡车位置
I have a stored procedure that uses the LIKE operator to search for a truck location among some other parameters
@location nchar(20),
@time time,
@date date
AS
select
DonationsTruck.VechileId, Phone, Location, [Date], [Time]
from
Vechile, DonationsTruck
where
Vechile.VechileId = DonationsTruck.VechileId
and (((Location like '%'+@location+'%') or (Location like '%'+@location) or (Location like @location+'%') ) or [Date]=@date or [Time] = @time)
我将其他参数设为空并仅按位置搜索,但即使我使用了位置的全名,它也始终不返回任何结果
I null the other parameters and search by location only but it always returns no results even when I used the full name of the location
推荐答案
@location nchar(20) 的数据类型应该是 @location nvarchar(20),因为nChar 有固定长度(用空格填充).
如果 Location 也是 nchar,则您必须对其进行转换:
Your datatype for @location nchar(20) should be @location nvarchar(20), since nChar has a fixed length (filled with Spaces).
If Location is nchar too you will have to convert it:
... Cast(Location as nVarchar(200)) like '%'+@location+'%' ...
要使用和 AND 条件启用可为空参数,只需使用 IsNull 或 Coalesce 进行比较,这在您使用 OR 的示例中不需要.
To enable nullable parameters with and AND condition just use IsNull or Coalesce for comparison, which is not needed in your example using OR.
例如如果您想比较位置和日期和时间.
e.g. if you would like to compare for Location AND Date and Time.
@location nchar(20),
@time time,
@date date
as
select DonationsTruck.VechileId, Phone, Location, [Date], [Time]
from Vechile, DonationsTruck
where Vechile.VechileId = DonationsTruck.VechileId
and (((Location like '%'+IsNull(@location,Location)+'%')) and [Date]=IsNUll(@date,date) and [Time] = IsNull(@time,Time))
这篇关于使用带有存储过程参数的 LIKE 运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用带有存储过程参数的 LIKE 运算符
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- SQL 临时表问题 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
