系統(tǒng)中碰到了要處理多條件組合查詢的情況,好比說有4個(gè)獨(dú)立的條件,那么組合出的查詢條件便有24種,不可能寫24條Sql語(yǔ)句再分別指定給24種組合情況吧,條件數(shù)再多點(diǎn)呢,人都會(huì)搞瘋的。。。。于是我求助于高手,獲得了使用instr()和decode()函數(shù)的實(shí)現(xiàn)方法。 下面先說明一下instr()的功能和語(yǔ)法:(函數(shù)的語(yǔ)法是從http://www.techonthenet.com/Oracle/functions/index.htm處得到的,相當(dāng)清楚明了:)
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()的功能和語(yǔ)法: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語(yǔ)句如下:select e.到達(dá)日期,
e.角色名,
d.單據(jù)標(biāo)題,
d.單據(jù)編號(hào),
e.節(jié)點(diǎn)編號(hào),
e.處理動(dòng)作,
e.處理日期,
b.流程實(shí)例編號(hào) from gzl_流程類型 a,
gzl_流程實(shí)例 b,
gzl_流程定義 c,
dj_單據(jù) d,
gzl_流程流轉(zhuǎn)狀態(tài) e
where a.流程類型編號(hào) = c.流程類型編號(hào) and e.處理標(biāo)記 = '是'
and e.用戶id = 'tetdmis' and b.流程定義編號(hào) = c.流程定義編號(hào)
and b.活動(dòng)編號(hào) = d.單據(jù)編號(hào) and c.流程定義編號(hào) = b.流程定義編號(hào)
and e.流程實(shí)例編號(hào) = b.流程實(shí)例編號(hào)
and instr(decode(:流程類型條件, '-1', a.流程類型編號(hào), :流程類型條件), a.流程類型編號(hào)) > 0
and (to_char(e.處理日期, 'yyyy-mm-dd') between :開始日期 and :結(jié)束日期)
and instr(decode(:節(jié)點(diǎn)名稱條件, '-1', e.處理動(dòng)作, :節(jié)點(diǎn)名稱條件),
e.處理動(dòng)作) > 0