select name from 表 where patindex('%@name%',name)>0
--用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 行受影响)
EXEC('select name from 表 where name LIKE ''%'+@name+'%''')
exec('select name from 表 where name LIKE ''%'+@name+'%''')
--借小卒的数据 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 行)*/
--用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 行受影响)
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 行)*/