在操作类里 
select name from 表 where name like %@name %,我要根据前台传入的参数name,来进行模糊查询,请问  like 后面应该怎么写?

解决方案 »

  1.   

    select name from 表 where charindex(@name,name)>0
      

  2.   

    select name from 表 where charindex(@name,name)>0
      

  3.   

    select name from 表 where patindex('%@name%',name)>0
      

  4.   


    --用like效率不高
    declare @a table(id varchar(20))
    insert @a select 
    '412hhhh44' union all select
    '412hhddh44' union all select
    '412b44' declare @str varchar(20)
    set @str='hh'select * from @a where id like '%'+@str+'%'id
    --------------------
    412hhhh44
    412hhddh44(2 行受影响)
      

  5.   

    EXEC('select name from 表 where name LIKE ''%'+@name+'%''')
      

  6.   

    exec('select name from 表 where name LIKE ''%'+@name+'%''')
      

  7.   

    --借小卒的数据
    declare @a table([name] varchar(20))
    insert @a select 
    '412hhhh44' union all select
    '412hhddh44' union all select
    '412b44' declare @name varchar(20)
    set @name='hh'
    select [name] from @a where charindex(@name,[name])>0
    /*name                 
    -------------------- 
    412hhhh44
    412hhddh44(所影响的行数为 2 行)*/