首页 > 数据库 > mysql教程 > 动态执行存储过程条件处理

动态执行存储过程条件处理

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
发布: 2016-06-07 17:47:38
原创
954 人浏览过

动态执行条件处理

create procedure test(@where1 nvarchar(2000))
as
declare @sql nvarchar(2000)
set @sql= 'select * from tabletest where 1=1 '+ @where1
exec (@sql)

 

exec test 'and where1 = @where1  '
exec test 'and where1 = @where1 and where2 = @where2 '

//

create procedure test(@where1 int, @where2 int, @where3 int)
as
  if @where1 is not null and @where2 is null and @where3 is null
      select * from tabletest where where1 = @where1
  if @where1 is not null and @where2 is not null and   @where3 is null
      select * from tabletest where where1 = @where1 and where2 = @where2
  if @where1 is not null and @where2 is not null and @where3 is not null 
      select * from tabletest where where1 = @where1 and where2 = @where2 and where3 = @where3


  
//

select *
from tabletest
where (where1 = @where1 or @where1 is null)
and (where2 = @where2 or @where2 is null)
and (where3 = @where3 or @where3 is null);


//

select *
from tabletest
where     where1 = case when @where1 is not null then @where1 else where1 end and
      where2 = case when @where2 is not null then @where2 else where2 end and
      where3 = case when @where3 is not null then @where3 else where3 end

 

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
怎么用php+mysql做动态数据表格 , 急!!!
来自于 1970-01-01 08:00:00
0
0
0
动态渲染表格
来自于 1970-01-01 08:00:00
0
0
0
反应模态的滑动动画
来自于 1970-01-01 08:00:00
0
0
0
头部怎么动态修改标题呢
来自于 1970-01-01 08:00:00
0
0
0
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板