SQL 语法异常 - 无效字符,但查询工作正常

Posted

技术标签:

【中文标题】SQL 语法异常 - 无效字符,但查询工作正常【英文标题】:SQL Grammar Exception-Invalid Character, but query working fine 【发布时间】:2018-08-13 07:14:19 【问题描述】:

我写了一个查询,它获取员工的数量,我在 sql developer 中对其进行了测试,它准确且工作正常,但不知何故我在我的 IDE 日志中无缘无故地收到 BadSqlGrammeException

SELECT COUNT(DISTINCT emp_code)
FROM Xxmpcd_Salary_Detail_Table
WHERE Prayas_Erp_Org_Id LIKE '202-%'
AND Yyyymm='201803'
ORDER BY Emp_Name ASC;

IDE 日志

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT count(Distinct emp_code)  FROM Xxmpcd_Salary_Detail_Table WHERE Prayas_Erp_Org_Id LIKE '202-%' AND Yyyymm='201803'  Order by Emp_Name ASC;]; nested exception is java.sql.SQLException: ORA-00911: invalid character

    at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:99)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:645)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:680)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:707)
    at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:757)
    at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.queryForObject(NamedParameterJdbcTemplate.java:211)
    at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.queryForObject(NamedParameterJdbcTemplate.java:226)
    at com.awzpact.prayas.service.SalaryMigrateService.getNoOfRows(SalaryMigrateService.java:85)
    at com.awzpact.prayas.controller.SalaryMigrationEmpListController.preMigrationEmployeeList(SalaryMigrationEmpListController.java:80)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:222)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:417)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.sql.SQLException: ORA-00911: invalid character

    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
    at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:743)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:216)
    at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:799)
    at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1038)
    at oracle.jdbc.driver.T4CPreparedStatement.executeMaybeDescribe(T4CPreparedStatement.java:839)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1133)
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3285)
    at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3329)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:93)
    at org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:688)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:629)
    ... 46 more

JAVA 代码:

public int getNoOfRows(SalaryMigrateSearchParamDTO searchParam) 
    MapSqlParameterSource param = new MapSqlParameterSource();
    String query = "SELECT count(Distinct emp_code) "
            + " FROM Xxmpcd_Salary_Detail_Table ";
    if (searchParam.getCircle() != null && !"".equals(searchParam.getCircle())) 
        query += "WHERE Prayas_Erp_Org_Id LIKE '" + searchParam.getCircle() + "-%' ";
     else 
        query += "WHERE Prayas_Erp_Org_Id LIKE '" + searchParam.getRegion() + "-%' ";
    
    query += "AND Yyyymm='" + searchParam.getYearMonth() + "'  "
            + "Order by Emp_Name ASC ;";
    return getNamedParameterJdbcTemplate().queryForObject(query, param, Integer.class);


请提供解决方案,以便我识别问题,是服务器端出现的问题,我正在使用 Spring MVC 框架开发 Web 应用程序。

【问题讨论】:

请向我们展示发出此查询的 java 代码。 @Tamas 看到我的编辑,我也附加了 java 代码 你测试省略了吗?在 ASC 之后? 可能重复wheres-my-invalid-character-ora-00911 【参考方案1】:

尝试删除语句的最后一个分号(';')。

【讨论】:

哦..那是工作,我忘了删除最后一个分号,它提出了问题,非常感谢@finqq

以上是关于SQL 语法异常 - 无效字符,但查询工作正常的主要内容,如果未能解决你的问题,请参考以下文章

查询无法正常工作(可能是语法问题)

使用 SQLKorma 获取语法异常:使用 SQL 执行查询失败

SQL join 语句中“ON”字的语法无效[关闭]

使用python执行SQL查询时语法无效[关闭]

Vim语法文件:向iskeyword添加某些字符无效

为啥 Range 有效,但 Cells 无效?