首页 > 解决方案 > ORA-00907: 在 Java 中使用 % 通配符时缺少右括号

问题描述

在我在子句中使用%通配符之前,我遇到问题的查询工作正常。LIKE:-

这很好(从 Java 打印):-

select acc.*, service.* 
    from com_accounts acc 
    left join com_subscriptions service on acc.cust_id = service.cust_id
    where 1=1 and  UPPER(acc.first_name) LIKE UPPER(?)

这引发了一个错误(从 Java 代码打印):-

select acc.*, service.* 
    from com_accounts acc 
    left join com_subscriptions service on acc.cust_id = service.cust_id 
    where 1=1 and  UPPER(acc.first_name) LIKE UPPER('%'?'%')

java.sql.SQLSyntaxErrorException: ORA-00907: 缺少右括号

标签: javasqloracleselectsyntax-error

解决方案


使用此查询,您将只有三个连续的字符串,它们之间没有运算符,正如您所见,这不是 Oracle 中的合法语法。使用运算符连接它们||应该可以解决问题:

select acc.*, service.* 
from com_accounts acc 
left join com_subscriptions service on acc.cust_id = service.cust_id 
where 1=1 and  UPPER(acc.first_name) LIKE UPPER('%' || ? || '%')
-- Here --------------------------------------------^----^

推荐阅读