SQL里怎么用变量实现模糊查询?(难)

来源:百度知道 编辑:UC知道 时间:2024/06/07 07:20:10
bbsTopic表(主帖表)中的Ttopic列是标题列,我现在需要利用局部变量传值进行模糊查询某张帖子的信息,原因是因为用户不可能输入完整的标题全称,所以得需要模糊查询才行。现在假设表中某一标题是“学习JSP”。
但是。。我写的。。。。
declare @name varchar(10)
set @name='JSP' --假设模糊搜索“JSP”关键字,
select * from bbsTopic where Ttopic like @a 查不到任何结果
select * from bbsTopic where Ttopic like %@a% 语法错误
select * from bbsTopic where Ttopic like '%@a%' 还是查不到

--->>>请教高手怎么实现哇!!!!想不出来了

首先要保证变量类型与你的列名类型相同!!

你测试看看!
declare @test varchar(20)

set @test='a'

select * from biao where data2 like '%'+@test+'%'

select * from bbsTopic where Ttopic like '%'||@name||'%';

select * from bbsTopic where Ttopic like @name
select * from bbsTopic where Ttopic like '%'+@name+'%'