存储过程遇到的问题

ding0623 2011-03-12 10:56:38
在存储过程中将"where ..."条件判断作为参数@Filter

执行时如果@Filter是where name like '%王%'这种形式时该怎么写? 查了好多都没效果..
...全文
67 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
gw6328 2011-03-12
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 fredrickhu 的回复:]
多转义一下就OK了
[/Quote]
+1
--小F-- 2011-03-12
  • 打赏
  • 举报
回复
多转义一下就OK了
--小F-- 2011-03-12
  • 打赏
  • 举报
回复
'where name like ''%王%'''
AcHerat 2011-03-12
  • 打赏
  • 举报
回复

set nocount on
create table tb(id int,num numeric(18,8),ar varchar(10))
insert into tb select 1,12.12000000,'a'
insert into tb select 3,125.13500000,'a'
insert into tb select 4,4821.00000000,'baa'
insert into tb select 5,1245.78950000,'vav'
insert into tb select 6,120.14579210,'d'
insert into tb select 7,15478.00000000,'d'
insert into tb select 8,334200.00000000,'d'
go

create proc result(@wh varchar(100))
as
exec('select * from tb ' + @wh)
go

exec result 'where ar like ''%a%'''

drop proc result
drop table tb
set nocount off


/*
id num ar
----------- --------------------------------------- ----------
1 12.12000000 a
3 125.13500000 a
4 4821.00000000 baa
5 1245.78950000 vav
ding0623 2011-03-12
  • 打赏
  • 举报
回复
exec [dbo].[a] 'where name like ''%王%'''

好像这种形式就不行?
快溜 2011-03-12
  • 打赏
  • 举报
回复

declare @Filter varchar(50),@str varchar(1000)
set @Filter='where name like ''%王%'''
set @str='select * from tb '+@Filter
exec(@str)
dawugui 2011-03-12
  • 打赏
  • 举报
回复
where charindex(@filter,name) > 0

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧