在oralce中求一sql 分别用case 与 decode 实现

快跑蜗牛哥 2016-08-08 06:41:34
三个表简单条件关联,
页面上 参数 传到 sql ,
根据不用参数,sql条件不同。(直接在sql中实现,不在页面上判断)
获得两个参数。
当 第1个参数值为空 / "" 与 第2个参数为 空 / ""时,条件 rownum<1
当 第1个参数值为空 / "" 第2个参数不为空 / ""时, 条件 instr('adcdsdd',bb.btype)>0
其中 adcdsdd 是获得的第2个参数 值
当 第2个参数值为空 / "" 第1个参数不为空 / ""时, 条件 instr('fffdsssdddd',aa.atype)>0
其中 fffdsssdddd 是获得的第1个参数 值
当 第1个参数值不为空 / "" 第2个参数不为空 / ""时, 条件 instr('adcdsdd',bb.btype)>0 or instr('fffdsssdddd',aa.atype)>0
其中 adcdsdd 是获得的第2个参数 值 fffdsssdddd 是获得的第1个参数 值

求一个sql
...全文
212 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnwz 2016-08-18
  • 打赏
  • 举报
回复
这样?
SELECT  a.ty,a.itemno,a.qty
FROM  a,b
WHERE a.atype=b.btype
and a.packno<>'0' 
  and ((para1 is null and para2 is null and rownum<1)
       or (para1 is null and para2 is not null and instr(para2,b.btype)>0)
       or (para1 is not null and para2 is null and instr(para1,a.atype)>0)
       or (para1 is not null and para2 is not null and (instr(para2,b.btype)>0 or instr(para1,a.atype)>0))
ORDER BY a.ty ASC;
大表的话性能不好的,如果可以还是拼接动态SQL吧
快跑蜗牛哥 2016-08-09
  • 打赏
  • 举报
回复
引用 1 楼 ghx287524027 的回复:
select 
case when para1 is null     and para2 is null     then ' rownum<1'
	 when para1 is null     and para2 is not null then ' instr(para2,bb.btype)>0'
	 when para1 is not null and para2 is null     then ' instr(para1,aa.atype)>0'
	 else '  instr(para2,bb.btype)>0 or  instr(para1,aa.atype)>0'
end tj into v_tj
from dual;

v_where:=v_where||' and '||v_tj
大概意思这样
如下 sql 该如何修改?
SELECT  a.ty,a.itemno,a.qty
   FROM  a,b
  WHERE a.atype=b.btype
and a.packno<>'0' and (CASE para1 is null
                THEN ' rownum<1'
              ELSE 'instr(para1,atype)>0' 
           END)  ORDER BY a.ty ASC;
快跑蜗牛哥 2016-08-09
  • 打赏
  • 举报
回复
引用 1 楼 ghx287524027 的回复:
select 
case when para1 is null     and para2 is null     then ' rownum<1'
	 when para1 is null     and para2 is not null then ' instr(para2,bb.btype)>0'
	 when para1 is not null and para2 is null     then ' instr(para1,aa.atype)>0'
	 else '  instr(para2,bb.btype)>0 or  instr(para1,aa.atype)>0'
end tj into v_tj
from dual;

v_where:=v_where||' and '||v_tj
大概意思这样
你好! case 可以写在 where 条件中吗? 如何修改
快跑蜗牛哥 2016-08-09
  • 打赏
  • 举报
回复
引用 2 楼 wmxcn2000 的回复:
这两个参数,前台传过来的是什么 呢?
字符串
卖水果的net 2016-08-08
  • 打赏
  • 举报
回复
这两个参数,前台传过来的是什么 呢?
ghx287524027 2016-08-08
  • 打赏
  • 举报
回复
select 
case when para1 is null     and para2 is null     then ' rownum<1'
	 when para1 is null     and para2 is not null then ' instr(para2,bb.btype)>0'
	 when para1 is not null and para2 is null     then ' instr(para1,aa.atype)>0'
	 else '  instr(para2,bb.btype)>0 or  instr(para1,aa.atype)>0'
end tj into v_tj
from dual;

v_where:=v_where||' and '||v_tj
大概意思这样
第ⅰ部分 关键的数据库概念  第1章 oracle database 11g 体系结构  第2章 安装oracle database 11g 和创建数据库  第3章 升级到oracle database 11g  第4章 规划oracle 应用程序——方法、风险和标准 第ⅱ部分 sqlsql*plus  第5章 sql 中的基本语法  第6章 基本的sql*plus 报表和命令  第7章 文本信息的收集与更改  第8章 正则表达式搜索  第9章 数值处理  第10章 日期:过去、现在及日期的差  第11章 转换函数与变换函数  第12章 分组函数  第13章 当一个查询依赖于另一个查询时  第14章 一些复杂的技术  第15章 更改数据:插入﹑更新﹑合并和删除  第16章 decodecasesql中的if-then-else  第17章 创建和管理表、视图、索引、群集和序列  第18章 分区 . 第19章 oracle 基本安全 第ⅲ部分 高 级 主 题  第20章 高级安全性——虚拟专用数据库  第21章 高级安全性:透明数据加密  第22章 使用表空间  第23章 用sql*loader 加载数据  第24章 使用data pump export 和data pump import  第25章 访问远程数据  第26章 使用物化视图  第27章 使用oracle text 进行文本搜索  第28章 使用外部表  第29章 使用闪回查询  第30章 闪回:表和数据库  第31章 sql 重放 第ⅳ部分 pl/sql  第32章 pl/sql 简介  第33章 应用程序在线升级  第34章 触发器  第35章 过程、函数与程序包  第36章 使用本地动态sql 和dbms_sql  第37章 pl/sql 调整 第ⅴ部分 对象关系数据库  第38章 实现对象类型、对象视图和方法  第39章 收集器(嵌套表和可变数组)  第40章 使用大对象  第41章 面向对象的高级概念 第ⅵ部分 oracle 中的java  第42章 java 简介  第43章 jdbc 程序设计  第44章 java 存储过程 第ⅶ部分 指 南  第45章 oracle 数据字典指南  第46章 应用程序和sql 调整指南  第47章 sql 结果缓存和客户端查询缓存  第48章 关于调整的示例分析  第49章 高级体系结构选项—— db保险库、内容db 和记录db  第50章 oracle 实时应用群集  第51章 数据库管理指南  第52章 oracle 中的xml 指南 第ⅷ部分 附 录 附录a 命令和术语参考

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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