查询以处理 sql 中的单引号对象

Query to handle single quoted objects in sql

我在 jdbc

中使用 sql 查询
SELECT COUNT (COST_CENTER) AS count FROM IMDB1_FINANCE_BUDGET where COST_CENTER='object name'

这工作正常,但我有一些测试对象,如“0654603”? SSG Accounting with a single quote in beginning now query will be

SELECT COUNT (COST_CENTER) AS count FROM IMDB1_FINANCE_BUDGET where COST_CENTER=''0654603 ? SSG '

现在抛出异常

java.sql.SQLException: ORA-00933: SQL command not properly ended

如何处理此类对象

在这种情况下处理单引号的最好方法是使用Quoting string literal technique

例如,q'['SCOTT]'

SQL> WITH DATA AS(
  2  SELECT '''SCOTT' nm FROM dual
  3  )
  4  SELECT * FROM DATA WHERE nm = q'['SCOTT]';

NM
------
'SCOTT

SQL>

当然,用单引号括起来的传统方式 '''SCOTT' 仍然有效 -

SQL> WITH DATA AS(
  2  SELECT '''SCOTT' nm FROM dual
  3  )
  4  SELECT * FROM DATA WHERE nm = '''SCOTT';

NM
------
'SCOTT

SQL>