反向SQL就像在Oracle 12c中不起作用

时间:2019-06-18 14:05:15

标签: oracle oracle12c

我需要在Oracle 12c中进行类似查询的反向操作。虽然可以在H2上正常工作,但在Oracle12c中却无法使用。

表格:

CREATE TABLE t
    ("id" int, "code" varchar(100))
;

INSERT ALL 
    INTO t ("id", "code")
         VALUES (1, 'london')
    INTO t ("id", "code")
         VALUES (2, 'amsterdam')
    INTO t ("id", "code")
         VALUES (3, 'Oslo')
    INTO t ("id", "code")
         VALUES (4, 'Brussels')
SELECT * FROM dual
;

查询:

 select * from t where 'london south' like concat('%',code, '%');

它给出错误:ORA-00909:参数数目无效

我应该如何查询它的结果是(1,london)?

2 个答案:

答案 0 :(得分:3)

  1. 请勿在DDL中使用小写的列名引起双引号,除非您确实要强制Oracle以小写形式存储列名,这也意味着您需要在查询中使用双引号:

    select *  from t where 'london south' like '%'||"code"||'%' ;
    

    将值london当作输出的单行。

  2. 为什么在使用||时更灵活地使用concat函数?

答案 1 :(得分:1)

这是一个更简单的测试用例:

select concat('a', 'b', 'c')
from dual;

  

ORA-00909:参数数量无效

CONCAT() function恰好需要两个参数。您必须嵌套多个呼叫或使用|| operator

select *
from t
where 'london south' like '%' || code || '%';