中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 数据库开发 > 数据库综合
使用Oracle的Instr()与decode()函数进行多条件组合查询
作者:未知 时间:2005-07-27 23:40 出处:CSDN 责编:chinaitpower
              摘要:使用Oracle的Instr()与decode()函数进行多条件组合查询

系统中遇到了要处理多条件组合查询的情况,好比说有4个独立的条件,那么组合出的查询条件便有24种,不可能写24条Sql语句再分别指定给24种组合情况吧,条件数再多点呢,人都会搞疯的。。。。于是我求助于高手,获得了使用instr()和decode()函数的实现方法。

下面先说明一下instr()的功能和语法:(函数的语法是从http://www.techonthenet.com/oracle/functions/index.htm处得到的,相当清晰明了:)

In Oracle/PLSQL, the instr function returns the location of a substring in a string.

 

 

 

 

The syntax for the instr function is:

 

 

 

 

instr (string1, string2, [start_position], [nth_appearance])

 

 

 

 

string1 is the string to search.

 

 

 

 

string2 is the substring to search for in string1.

 

 

 

 

start_position is the position in string1 where the search will start.  This argument is optional.  If omitted, it defaults to 1.  The first position in the string is 1.  If the start_position is negative, the function counts back start_position number of characters from the end of string1 and then searches towards the beginning of string1.

 

 

 

 

nth_appearance is the nth appearance of string2.  This is optional.  If omiited, it defaults to 1.

 

 

 

 

再说明一下decode()的功能和语法:

In Oracle/PLSQL, the decode function has the functionality of an IF-THEN-ELSE statement.

 

 

 

 

The syntax for the decode function is:

 

 

 

 

decode ( expression , search , result [, search , result]... [, default] )

 

 

 

 

expression is the value to compare.

 

 

 

 

search is the value that is compared against expression.

 

 

 

 

result is the value returned, if expression is equal to search.

 

 

 

 

default is optional.  If no matches are found, the decode will return default.  If default is omitted, then the decode statement will return null (if no matches are found).

综合使用得到的SQL语句如下:

select e.到达日期,
       e.角色名,
       d.单据标题,
       d.单据编号,
       e.节点编号,
       e.处理动作,
       e.处理日期,
       b.流程实例编号

  from gzl_流程类型     a,
       gzl_流程实例     b
,
       gzl_流程定义     c,
       dj_单据          d,
       gzl_流程流转状态 e
 where a.流程类型编号 = c.流程类型编号 and e.处理标记 = '是'
       and   e.用户id = 'tetdmis' and b.流程定义编号 = c.流程定义编号    
       and b.活动编号 = d.单据编号 and c.流程定义编号 = b.流程定义编号
       and  e.流程实例编号 = b.流程实例编号
      and instr(decode(:流程类型条件, '-1', a.流程类型编号, :流程类型条件), a.流程类型编号) > 0
      and (to_char(e.处理日期, 'yyyy-mm-dd') between :开始日期 and :结束日期)      
       and instr(decode(:节点名称条件, '-1', e.处理动作, :节点名称条件),
                 e.处理动作) > 0

最后得到的界面如下:

查询功能界面


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有