<legend id='BjYmM'><style id='BjYmM'><dir id='BjYmM'><q id='BjYmM'></q></dir></style></legend>

    <small id='BjYmM'></small><noframes id='BjYmM'>

        <bdo id='BjYmM'></bdo><ul id='BjYmM'></ul>
      <tfoot id='BjYmM'></tfoot>
      <i id='BjYmM'><tr id='BjYmM'><dt id='BjYmM'><q id='BjYmM'><span id='BjYmM'><b id='BjYmM'><form id='BjYmM'><ins id='BjYmM'></ins><ul id='BjYmM'></ul><sub id='BjYmM'></sub></form><legend id='BjYmM'></legend><bdo id='BjYmM'><pre id='BjYmM'><center id='BjYmM'></center></pre></bdo></b><th id='BjYmM'></th></span></q></dt></tr></i><div id='BjYmM'><tfoot id='BjYmM'></tfoot><dl id='BjYmM'><fieldset id='BjYmM'></fieldset></dl></div>

      使用带有存储过程参数的 LIKE 运算符

      时间:2023-10-26
      <legend id='ckJb7'><style id='ckJb7'><dir id='ckJb7'><q id='ckJb7'></q></dir></style></legend>

          <small id='ckJb7'></small><noframes id='ckJb7'>

            <tbody id='ckJb7'></tbody>

          <tfoot id='ckJb7'></tfoot>

          • <bdo id='ckJb7'></bdo><ul id='ckJb7'></ul>
            <i id='ckJb7'><tr id='ckJb7'><dt id='ckJb7'><q id='ckJb7'><span id='ckJb7'><b id='ckJb7'><form id='ckJb7'><ins id='ckJb7'></ins><ul id='ckJb7'></ul><sub id='ckJb7'></sub></form><legend id='ckJb7'></legend><bdo id='ckJb7'><pre id='ckJb7'><center id='ckJb7'></center></pre></bdo></b><th id='ckJb7'></th></span></q></dt></tr></i><div id='ckJb7'><tfoot id='ckJb7'></tfoot><dl id='ckJb7'><fieldset id='ckJb7'></fieldset></dl></div>
                本文介绍了使用带有存储过程参数的 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 运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:需要一个工具来自动缩进和格式化 SQL Server 存储过程 下一篇:在 MySQL 中:如何将表名作为存储过程和/或函数参数传递?

                相关文章

              • <legend id='i99RM'><style id='i99RM'><dir id='i99RM'><q id='i99RM'></q></dir></style></legend>
                • <bdo id='i99RM'></bdo><ul id='i99RM'></ul>
                <i id='i99RM'><tr id='i99RM'><dt id='i99RM'><q id='i99RM'><span id='i99RM'><b id='i99RM'><form id='i99RM'><ins id='i99RM'></ins><ul id='i99RM'></ul><sub id='i99RM'></sub></form><legend id='i99RM'></legend><bdo id='i99RM'><pre id='i99RM'><center id='i99RM'></center></pre></bdo></b><th id='i99RM'></th></span></q></dt></tr></i><div id='i99RM'><tfoot id='i99RM'></tfoot><dl id='i99RM'><fieldset id='i99RM'></fieldset></dl></div>

              • <tfoot id='i99RM'></tfoot>
                1. <small id='i99RM'></small><noframes id='i99RM'>