2012-07-19 35 views
2

我需要在報告工具中爲提示寫一個sql。我得到了用' - '分隔的變量中的多個值的列表,並且這些值的數目可以變化(例如,「abc-def」eg2。「abc-def-xyz」)。動態數字在哪裏條件在oracle中sql

現在我需要寫這種形式(邏輯)的的Oracle SQL

select something 
    from somewhere 
where someColumn in 'abc' -- use substr to extract abc 
    or someColumn in 'def' -- use substr to extract def 
    or ...till we don't find '-'. 

我不能使用PLSQL的查詢。要麼我可能不知道要使用plsql中選擇的變量,或者可能是報告工具不支持該變量。

在此先感謝。

回答

6

嘗試

select something 
    from somewhere 
where someColumn in (select regexp_substr('abc-def-xyz','[^-]+', 1, level) from dual 
        connect by regexp_substr('abc-def-xyz', '[^-]+', 1, level) is not null); 

爲了概括(考慮您的字段由 「 - 」 分隔)

select something 
    from somewhere 
where someColumn in (select regexp_substr(variable,'[^-]+', 1, level) from dual 
        connect by regexp_substr(variable, '[^-]+', 1, level) is not null); 

基本上子查詢的輸出如下所示: -

SQL> select regexp_substr('abc-def-xyz','[^-]+', 1, level) value from dual 
     connect by regexp_substr('abc-def-xyz', '[^-]+', 1, level) is not null; 

VALUE        
-------------------------------- 
abc        
def        
xyz 
0

如果你只是想包含一個結果列表「 - 」,你可能只是做類似

選擇一些東西從什麼地方WHERE SOMECOLUMN LIKE(「% - %」);

+0

我編輯了問題。你介意看看那個嗎? – chemicalkt 2012-07-19 03:30:11

1

首先使用Oracle的regexp_substr()函數將字符串拆分爲其部分。見regexp_substr function(如果你可以訪問生成的字符串,原來,只是使用)

其次,把這個臨時表,然後只是有:

select something 
    from somewhere 
where someColumn in (select parts from tmpTable) 
+0

不需要臨時表,只需內聯視圖即可。另外,在報告工具中使用PLSQL存儲過程(無論如何都可以完成)來填充臨時表(帶有用戶輸入)似乎是不必要的。 – Annjawn 2012-07-19 04:14:21

+0

@Annjawn,我同意你關於臨時表的看法,但他對我來說是如此的新穎,我希望儘可能簡單和可測試。 – JBrooks 2012-07-19 16:07:19

1
select something 
    from somewhere 
where INSTR('-'||'abc-def-ghi'||'-', '-'||someColumn||'-') > 0;