2012-07-19 48 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;