读书人

存储过程模糊查询解决方法

发布时间: 2012-04-20 15:27:03 作者: rapoo

存储过程模糊查询

SQL code
ALTER proc proc_mohuchaxun@strs varchar(50)asselect * from Player where name like '%@strs%'


创建的时候说proc_mohuchaxun无效 什么原因?这样写有错吗

[解决办法]
探讨
SQL code

ALTER proc proc_mohuchaxun
@strs varchar(50)
as
select * from Player where name like '%@strs%'




创建的时候说proc_mohuchaxun无效 什么原因?这样写有错吗

[解决办法]
ALTER proc proc_mohuchaxun
@strs varchar(50)
as
set @strs='%'+@strs+'%'
exec sp_executesql N'select * from Player where name like @key',
N'@key nvarchar(100)',@key=@strs

拼接字符串,并利用sp_executesql进行参数化执行,以便重用执行计划。

读书人网 >SQL Server

热点推荐