下面是一个查询信息的存储过程:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
goALTER PROCEDURE [dbo].[selectsw]  @bedate  datetime,
@enddate  datetime,
@bc   nvarchar(8),
@sgdw   nvarchar(20),
@zrr  nvarchar(8),
@ccr  nvarchar(8),
@swlb  nvarchar(8)
AS
select * from swxxb  where rq>=@bedate and rq<=@enddate  and wzlx like @bc+'%' and sgdw  like @sgdw+'%'   and zrrxm like  '%'+@zrr+'%'  and swfxr like  '%'+@ccr+'%' and swlb like @swlb+'%'在下知道用“%”是为了实现模糊查询,但是为什么要写成“ swlb like @swlb+'%' ”?直接写成“swlb = @swlb”不可以吗?这样写有什么好处呢?

解决方案 »

  1.   

    swlb like @swlb+'%' --只要最前面等于@swlb就符合
    --不等于
    swlb=@swlb --要完全等于
      

  2.   

    declare @t table(col varchar(10))
    insert @t select 'abc'
    insert @t select 'abcde'
    select * from @t where col like 'abc%'
    /*
    col
    ----------
    abc
    abcde
    */
    select * from @t where col ='abc'
    /*
    col
    ----------
    abc
    */
      

  3.   

     swlb like @swlb+'%'是模糊查询,即字符串的前五位只要是@swlb就行了
    swlb=@swlb是精确查询,字符串只能等于=@swlb
      

  4.   

    swlb like @swlb+'%' 最前面等于@swlbswlb=@swlb 完全等于
      

  5.   


    可是这样做是不是有什么好处?只是“swlb like @swlb+'%' 最前面等于@swlb”那么简单吗?
      

  6.   

    当然也可以用charindex
    charindex(@swlb,swlb)=1 --第一个位置找到