简体   繁体   English

将字符串评估为 Oracle 中的条件

[英]Evaluate a string as condition in Oracle

For istance, if I have a string like例如,如果我有一个字符串

my_string := ' ''a'' = ''a'' and 1 > 0 '

I can get it evaluated doing something like this in a procedure/function我可以在过程/函数中对其进行评估

execute immediate 'select CASE WHEN(' || my_string || ') THEN 1 ELSE 0 END from dual'

But is there a way to do that without using execute immediate?但是有没有办法在不使用立即执行的情况下做到这一点? Is there a way to evaluate a string like it was written in a query?有没有办法像在查询中一样评估字符串?

I want to this because I have generic conditions in a table like "COD1 like '%x%' OR COD2 = 'Z'".我想这样做是因为我在“COD1 like '%x%' OR COD2 = 'Z'”这样的表中有通用条件。 So I do some replace with this strings but then I would like to have them evaluated with the costraint to not use a user definied function, so no "execute immediate"所以我用这个字符串做了一些替换,但是我想让他们用 costraint 评估不使用用户定义的 function,所以没有“立即执行”

No way, as far as I can tell.没办法,据我所知。 That's what dynamic SQL (ie execute immediate ) is used for.这就是动态 SQL (即execute immediate )的用途。


For example, if you put just one condition (for simplicity) into a table:例如,如果您只将一个条件(为简单起见)放入表中:

SQL> select * from test;

MY_STRING
---------------------
 'a' = 'a' and 1 > 0

and cross-join it to another table (because, I'd expect everything to be returned as that condition is always met), you get an error:并将其交叉连接到另一个表(因为,我希望返回所有内容,因为始终满足该条件),您会收到错误消息:

SQL> select *
  2  from dept d cross join test t
  3  where t.mystring;
where t.mystring
               *
ERROR at line 3:
ORA-00920: invalid relational operator

while - if condition is literally put into the where clause, it works : while - 如果将条件从字面上放入where子句,它可以工作

SQL> select *
  2  from dept d cross join test t
  3  where 'a' = 'a' and 1 > 0;

    DEPTNO DNAME          LOC           MY_STRING
---------- -------------- ------------- ---------------------
        10 ACCOUNTING     NEW YORK       'a' = 'a' and 1 > 0
        20 RESEARCH       DALLAS         'a' = 'a' and 1 > 0
        30 SALES          CHICAGO        'a' = 'a' and 1 > 0
        40 OPERATIONS     BOSTON         'a' = 'a' and 1 > 0

SQL>

So, dynamic SQL it is, I'm afraid.所以,恐怕是动态的 SQL。

is there a way to do that without using execute immediate有没有办法在不使用立即执行的情况下做到这一点

You can use a substitution variable as an alternative method such as您可以使用替代变量作为替代方法,例如

SQL> SELECT CASE WHEN(&str) THEN 1 ELSE 0 END
  2    FROM dual; 

CASEWHEN('A'='A'AND1>0)THEN1EL
------------------------------
                             1

where 'a' = 'a' and 1 > 0 entered for &str whenever prompted其中'a' = 'a' and 1 > 0在出现提示时为&str输入

Yes, but... you effectively have to write your own expression parser:是的,但是...您实际上必须编写自己的表达式解析器:

If you have the tables:如果你有桌子:

CREATE TABLE table_name (a, b, c, d) AS
SELECT 'x', 'x', 'x', 'x' FROM DUAL UNION ALL
SELECT 'w', 'x', 'y', 'z' FROM DUAL;

CREATE TABLE filters (filter) AS
SELECT 'a = b AND c <= d' FROM DUAL UNION ALL
SELECT 'a < b AND b < c AND c < d' FROM DUAL UNION ALL
SELECT 'a < ''y''' FROM DUAL UNION ALL
SELECT 'c LIKE ''%y%''' FROM DUAL;

and you want to apply the filters to table_name then, from Oracle 12, you can use:并且您想将filters应用于table_name然后,从 Oracle 12 开始,您可以使用:

WITH split_filters ( id, filter, left_operand, operator, right_operand, expr, num_expr ) AS (
  SELECT ROWID,
         filter,
         REGEXP_SUBSTR(
           filter,
           '(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)'       -- left_operand
           || '\s*([<>!]?=|[<>]|LIKE)'                 -- operator
           || '\s*(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)' -- right_operand
           || '\s*($|\sAND\s+)',                       -- expression concatenator
           1,
           1,
           'i',
           1
         ),
         REGEXP_SUBSTR(
           filter,
           '(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)'       -- left_operand
           || '\s*([<>!]?=|[<>]|LIKE)'                 -- operator
           || '\s*(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)' -- right_operand
           || '\s*($|\sAND\s+)',                       -- expression concatenator
           1,
           1,
           'i',
           3
         ),
         REGEXP_SUBSTR(
           filter,
           '(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)'       -- left_operand
           || '\s*([<>!]?=|[<>]|LIKE)'                 -- operator
           || '\s*(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)' -- right_operand
           || '\s*($|\sAND\s+)',                       -- expression concatenator
           1,
           1,
           'i',
           4
         ),
         1,
         REGEXP_COUNT(
           filter,
           '(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)'       -- left_operand
           || '\s*([<>!]?=|[<>]|LIKE)'                 -- operator
           || '\s*(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)' -- right_operand
           || '\s*($|\sAND\s+)',                       -- expression concatenator
           1,
           'i'
         )
  FROM   filters
UNION ALL
  SELECT id,
         filter,
         REGEXP_SUBSTR(
           filter,
           '(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)'       -- left_operand
           || '\s*([<>!]?=|[<>]|LIKE)'                 -- operator
           || '\s*(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)' -- right_operand
           || '\s*($|\sAND\s+)',                       -- expression concatenator
           1,
           expr + 1,
           'i',
           1
         ),
         REGEXP_SUBSTR(
           filter,
           '(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)'       -- left_operand
           || '\s*([<>!]?=|[<>]|LIKE)'                 -- operator
           || '\s*(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)' -- right_operand
           || '\s*($|\sAND\s+)',                       -- expression concatenator
           1,
           expr + 1,
           'i',
           3
         ),
         REGEXP_SUBSTR(
           filter,
           '(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)'       -- left_operand
           || '\s*([<>!]?=|[<>]|LIKE)'                 -- operator
           || '\s*(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)' -- right_operand
           || '\s*($|\sAND\s+)',                       -- expression concatenator
           1,
           expr + 1,
           'i',
           4
         ),
         expr + 1,
         num_expr
  FROM   split_filters
  WHERE  expr < num_expr
)
SELECT *
FROM   table_name t
       CROSS JOIN LATERAL (
         SELECT MAX(filter) AS filter
         FROM   (
           SELECT id,
                  filter,
                  CASE 
                  WHEN UPPER(left_operand) = 'A' THEN t.a
                  WHEN UPPER(left_operand) = 'B' THEN t.b
                  WHEN UPPER(left_operand) = 'C' THEN t.c
                  WHEN UPPER(left_operand) = 'D' THEN t.d
                  WHEN left_operand LIKE '''%''' THEN REPLACE(SUBSTR(left_operand, 2, LENGTH(left_operand) - 2), '''''', '''')
                  END AS l_op,
                  operator AS op,
                  CASE 
                  WHEN UPPER(right_operand) = 'A' THEN t.a
                  WHEN UPPER(right_operand) = 'B' THEN t.b
                  WHEN UPPER(right_operand) = 'C' THEN t.c
                  WHEN UPPER(right_operand) = 'D' THEN t.d
                  WHEN right_operand LIKE '''%''' THEN REPLACE(SUBSTR(right_operand, 2, LENGTH(right_operand) - 2), '''''', '''')
                  END AS r_op,
                  num_expr
           FROM   split_filters
         )
         WHERE CASE 
               WHEN op = '='    AND l_op =  r_op THEN 1
               WHEN op = '!='   AND l_op != r_op THEN 1
               WHEN op = '<'    AND l_op <  r_op THEN 1
               WHEN op = '>'    AND l_op >  r_op THEN 1
               WHEN op = '<='   AND l_op <= r_op THEN 1
               WHEN op = '>='   AND l_op >= r_op THEN 1
               WHEN op = 'LIKE' AND l_op LIKE r_op THEN 1
               END = 1
          GROUP BY id
          HAVING COUNT(*) = MAX(num_expr)
       );

Which outputs:哪个输出:

A一个 B C C D D FILTER筛选
x X x X x X x X a = b AND c <= d a = b AND c <= d
x X x X x X x X a < 'y'一个<'y'
w w x X y是的 z z a < b AND b < c AND c < d a < b 和 b < c 和 c < d
w w x X y是的 z z a < 'y'一个<'y'
w w x X y是的 z z c LIKE '%y%' c LIKE '%y%'

db<>fiddle here db<> 在这里摆弄


In Oracle 11g, you could re-write it as:在 Oracle 11g 中,您可以将其重写为:

WITH split_filters ( id, filter, left_operand, operator, right_operand, expr, num_expr ) AS (
  SELECT ROWID,
         filter,
         REGEXP_SUBSTR(
           filter,
           '(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)'       -- left_operand
           || '\s*([<>!]?=|[<>]|LIKE)'                 -- operator
           || '\s*(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)' -- right_operand
           || '\s*($|\sAND\s+)',                       -- expression concatenator
           1,
           1,
           'i',
           1
         ),
         REGEXP_SUBSTR(
           filter,
           '(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)'       -- left_operand
           || '\s*([<>!]?=|[<>]|LIKE)'                 -- operator
           || '\s*(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)' -- right_operand
           || '\s*($|\sAND\s+)',                       -- expression concatenator
           1,
           1,
           'i',
           3
         ),
         REGEXP_SUBSTR(
           filter,
           '(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)'       -- left_operand
           || '\s*([<>!]?=|[<>]|LIKE)'                 -- operator
           || '\s*(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)' -- right_operand
           || '\s*($|\sAND\s+)',                       -- expression concatenator
           1,
           1,
           'i',
           4
         ),
         1,
         REGEXP_COUNT(
           filter,
           '(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)'       -- left_operand
           || '\s*([<>!]?=|[<>]|LIKE)'                 -- operator
           || '\s*(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)' -- right_operand
           || '\s*($|\sAND\s+)',                       -- expression concatenator
           1,
           'i'
         )
  FROM   filters
UNION ALL
  SELECT id,
         filter,
         REGEXP_SUBSTR(
           filter,
           '(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)'       -- left_operand
           || '\s*([<>!]?=|[<>]|LIKE)'                 -- operator
           || '\s*(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)' -- right_operand
           || '\s*($|\sAND\s+)',                       -- expression concatenator
           1,
           expr + 1,
           'i',
           1
         ),
         REGEXP_SUBSTR(
           filter,
           '(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)'       -- left_operand
           || '\s*([<>!]?=|[<>]|LIKE)'                 -- operator
           || '\s*(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)' -- right_operand
           || '\s*($|\sAND\s+)',                       -- expression concatenator
           1,
           expr + 1,
           'i',
           3
         ),
         REGEXP_SUBSTR(
           filter,
           '(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)'       -- left_operand
           || '\s*([<>!]?=|[<>]|LIKE)'                 -- operator
           || '\s*(''([^'']|'''')*''|[A-Z][A-Z0-9_]*)' -- right_operand
           || '\s*($|\sAND\s+)',                       -- expression concatenator
           1,
           expr + 1,
           'i',
           4
         ),
         expr + 1,
         num_expr
  FROM   split_filters
  WHERE  expr < num_expr
),
operand_substitutions (t_id, f_id, a, b, c, d, filter, l_op, op, r_op, num_expr) AS (
  SELECT t.ROWID,
         f.id,
         t.a,
         t.b,
         t.c,
         t.d,
         filter,
         CASE 
         WHEN UPPER(left_operand) = 'A' THEN t.a
         WHEN UPPER(left_operand) = 'B' THEN t.b
         WHEN UPPER(left_operand) = 'C' THEN t.c
         WHEN UPPER(left_operand) = 'D' THEN t.d
         WHEN left_operand LIKE '''%''' THEN REPLACE(SUBSTR(left_operand, 2, LENGTH(left_operand) - 2), '''''', '''')
         END,
         operator,
         CASE 
         WHEN UPPER(right_operand) = 'A' THEN t.a
         WHEN UPPER(right_operand) = 'B' THEN t.b
         WHEN UPPER(right_operand) = 'C' THEN t.c
         WHEN UPPER(right_operand) = 'D' THEN t.d
         WHEN right_operand LIKE '''%''' THEN REPLACE(SUBSTR(right_operand, 2, LENGTH(right_operand) - 2), '''''', '''')
         END,
         num_expr
  FROM   split_filters f
         CROSS JOIN table_name t
)
SELECT MAX(a) AS a,
       MAX(b) AS b,
       MAX(c) AS c,
       MAX(d) AS d,
       MAX(filter) AS filter
FROM   operand_substitutions
WHERE  CASE 
       WHEN op = '='    AND l_op =  r_op THEN 1
       WHEN op = '!='   AND l_op != r_op THEN 1
       WHEN op = '<'    AND l_op <  r_op THEN 1
       WHEN op = '>'    AND l_op >  r_op THEN 1
       WHEN op = '<='   AND l_op <= r_op THEN 1
       WHEN op = '>='   AND l_op >= r_op THEN 1
       WHEN op = 'LIKE' AND l_op LIKE r_op THEN 1
       END = 1
GROUP BY t_id, f_id
HAVING COUNT(*) = MAX(num_expr);

db<>fiddle here db<> 在这里摆弄

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

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