简体   繁体   English

在where子句中使用带“ in”的解码

[英]Using decode in where clause, with “in”

Have a tricky situation in witch I belive you guys can help me out. 女巫有一个棘手的情况,我相信你们可以帮助我。 I want to use decode in my cursors where-clause. 我想在游标的子句中使用解码。 I am using "IN()" but I belive the program thinks the comma belongs to the decode and not as a separator between values. 我正在使用“ IN()”,但我相信程序认为逗号属于解码,而不是作为值之间的分隔符。

I think maybe using CASE might solve this, am I right? 我认为也许使用CASE可以解决这个问题,对吗?

 CURSOR order_cur (
  cur_ao    VARCHAR2) IS 
  SELECT t1.nr, t1.status$nr
      FROM eh_order_t@NGEXT_DBLINK t1
     WHERE     t1.status$nr IN (3, 6)
           AND t1.ao IN (DECODE (
                            cur_ao,
                            'ALLA', Argus_ehandel_pkg.get_ehorder_ao (
                                       t1.nr),
                            SUBSTR (cur_ao, 1, 2), SUBSTR (cur_ao, 3, 2)))

CASE variant 案例变体

CURSOR order_cur (
  cur_ao    VARCHAR2) IS
    SELECT t1.nr, t1.status$nr
      FROM eh_order_t@NGEXT_DBLINK t1
     WHERE     t1.status$nr IN (3, 6)
           AND t1.ao IN (CASE
                            WHEN cur_ao = 'ALLA'
                            THEN
                               Argus_ehandel_pkg.get_ehorder_ao (t1.nr)
                            ELSE
                               SUBSTR (cur_ao, 1, 2), SUBSTR (cur_ao, 3, 2)
                         END)                -- SUBSTR (cur_ao, 3, 2) END)
  --AND t1.nr = DECODE (order_in, NULL, t1.nr, order_in)
  ORDER BY t1.skapad_dat ASC; 

DECODE is an expression, and it will work in the IN list along with other values in the list, you just need to take care of the syntax and parenthesis . DECODE是一个表达式,它将与列表中的其他值一起在IN列表中工作,您只需要注意语法括号即可

What matters is the final value that you get after evaluating the DECODE expression. 重要的是评估DECODE表达式后获得的最终值。 Comma separated values inside DECODE will be evaluated as part of the DECODE expression. DECODE内用逗号分隔的值将作为DECODE表达式的一部分进行求值。 And the values after closing the parenthesis of DECODE expression, but inside the IN list will be considered in the IN list. 并且关闭DECODE表达式括号后但在IN列表内部的值将在IN列表中考虑。

For example, 例如,

SQL> SELECT deptno FROM emp
  2  WHERE deptno IN(decode(ename, 'KING', 20, 10), 30);

    DEPTNO
----------
        30
        30
        30
        30
        10
        30
        30
        10

8 rows selected.

SQL>

There are two comma separated values in the IN list. IN列表中有两个逗号分隔的值。 DECODE expression is evaluated as 10 , and 20 is in the IN list. DECODE表达式的计算结果为10 ,而IN列表中为20 So, the IN list is finally evaluated as: 因此,IN列表最终评估为:

WHERE deptno = 10 OR deptno = 20

10 is the result of DECODE , and 30 is hardcoded in the IN list . 10是DECODE的结果,而30是硬编码在IN列表中的


Update : 更新:

OP posted a CASE expression and the same could written using DECODE . OP发布了一个CASE表达式,同样可以使用DECODE编写。

Your CASE expression will throw an error because you are trying to return two values from the CASE expression which is not possible. 您的CASE表达式将引发错误,因为您试图从CASE表达式返回两个值,这是不可能的。 You can at most return a single value from the expression, just like a function . 您最多可以从表达式中返回一个 ,就像function一样

It could be written using DECODE in the IN list : 可以使用IN列表中的 DECODE编写它:

IN(  
DECODE(cur_ao, 'ALLA', Argus_ehandel_pkg.get_ehorder_ao (t1.nr), SUBSTR (cur_ao, 1, 2))), 
SUBSTR (cur_ao, 1, 2)
)

In the above IN list , there are two values, the first is evaluated by the DECODE expression , and the other is evaluated by SUBSTR (cur_ao, 1, 2) . 在上面的IN列表中 ,有两个值, 一个值由DECODE表达式求值,另一个值由SUBSTR (cur_ao, 1, 2)求值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM