开发者社区> 问答> 正文

异常syntax error, expect RPAREN, actual FOR FOR

环境jdk1.6 + tomcat6.0+sqlserver2005

执行的插入语句INSERT INTO MMS_SETTLEMENT_COM(handler,handleTime,MID,MERCHANTNAME,TOTALAMT,ACTUALAMT,paymentMoney,STATUS,SERIAL_NUM,REMARKS)SELECT 'admin',getdate(),MID,MERCHANTNAME,SUM(CONVERT(DECIMAL(18,2),isnull(TOTALAMT,0))) TOTALAMT,SUM(CONVERT(DECIMAL(18,2),isnull(ACTUALAMT,0))) ACTUALAMT,SUM(CONVERT(DECIMAL(18,2),isnull(paymentMoney,0))) paymentMoney,2,126,(SELECT REMARKS+'' FROM MMS_SETTLEMENT_COM WHERE MID=A.MID FOR XML PATH('')) AS REMARKS FROM MMS_SETTLEMENT_COM A WHERE ID IN (304,305,306,297,108,184) GROUP BY MID ,MERCHANTNAME

能够插入成功,但是后台druid会报异常。 经过测试,是插入语句中的(SELECT REMARKS+'' FROM MMS_SETTLEMENT_COM WHERE MID=A.MID FOR XML PATH('')) AS REMARKS这一部分出错,是不是druid不支持使用for xml path?

com.alibaba.druid.sql.parser.ParserException: syntax error, expect RPAREN, actual FOR FOR at com.alibaba.druid.sql.parser.SQLExprParser.accept(SQLExprParser.java:1382) at com.alibaba.druid.sql.parser.SQLExprParser.primary(SQLExprParser.java:194) at com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerExprParser.primary(SQLServerExprParser.java:54) at com.alibaba.druid.sql.parser.SQLExprParser.expr(SQLExprParser.java:109) at com.alibaba.druid.sql.parser.SQLSelectParser.expr(SQLSelectParser.java:434) at com.alibaba.druid.sql.parser.SQLSelectParser.parseSelectItem(SQLSelectParser.java:295) at com.alibaba.druid.sql.parser.SQLSelectParser.parseSelectList(SQLSelectParser.java:273) at com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerSelectParser.query(SQLServerSelectParser.java:89) at com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerSelectParser.select(SQLServerSelectParser.java:47) at com.alibaba.druid.sql.parser.SQLExprParser.primary(SQLExprParser.java:373) at com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerExprParser.primary(SQLServerExprParser.java:54) at com.alibaba.druid.sql.parser.SQLExprParser.expr(SQLExprParser.java:109) at com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser.parseInsert0(SQLServerStatementParser.java:134) at com.alibaba.druid.sql.parser.SQLStatementParser.parseInsert0(SQLStatementParser.java:1035) at com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser.parseInsert(SQLServerStatementParser.java:85) at com.alibaba.druid.sql.parser.SQLStatementParser.parseStatementList(SQLStatementParser.java:144) at com.alibaba.druid.sql.parser.SQLStatementParser.parseStatementList(SQLStatementParser.java:108) at com.alibaba.druid.sql.parser.SQLStatementParser.parseStatementList(SQLStatementParser.java:103) at com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils.parameterize(ParameterizedOutputVisitorUtils.java:53) at com.alibaba.druid.filter.stat.StatFilter.mergeSql(StatFilter.java:145) at com.alibaba.druid.filter.stat.StatFilter.createSqlStat(StatFilter.java:629) at com.alibaba.druid.filter.stat.StatFilter.statementPrepareAfter(StatFilter.java:305) at com.alibaba.druid.filter.FilterEventAdapter.connection_prepareStatement(FilterEventAdapter.java:124) at com.alibaba.druid.filter.FilterChainImpl.connection_prepareStatement(FilterChainImpl.java:448) at com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl.prepareStatement(ConnectionProxyImpl.java:342) at com.alibaba.druid.pool.DruidPooledConnection.prepareStatement(DruidPooledConnection.java:311) at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:505) at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:94) at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:87) at org.hibernate.engine.query.NativeSQLQueryPlan.performExecuteUpdate(NativeSQLQueryPlan.java:157) at org.hibernate.impl.SessionImpl.executeNativeUpdate(SessionImpl.java:1163) at org.hibernate.impl.SQLQueryImpl.executeUpdate(SQLQueryImpl.java:334) at hsir.settlement.service.MmsSettlementSummaryService.doMerge(MmsSettlementSummaryService.java:939) at hsir.settlement.service.MmsSettlementSummaryService$$FastClassByCGLIB$$d80e3b47.invoke() at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149) at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:696) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:631) at hsir.settlement.service.MmsSettlementSummaryService$$EnhancerByCGLIB$$5d5afacc.doMerge() at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149) at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:50) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) at $Proxy48.doMerge(Unknown Source) at hsir.settlement.litebean.Settlement1Bean.btn_merge(Settlement1Bean.java:342) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.operamasks.faces.binding.ModelBean.invokeMethod(ModelBean.java:319) at org.operamasks.faces.binding.ModelBean.invokeAction(ModelBean.java:394) at org.operamasks.faces.binding.impl.ActionMethodAdapter.invoke(ActionMethodAdapter.java:66) at org.operamasks.faces.binding.impl.CompositeMethodAdapter.invoke(CompositeMethodAdapter.java:62) at org.operamasks.faces.application.impl.ActionListenerImpl.processAction(ActionListenerImpl.java:109) at javax.faces.component.UICommand.broadcast(UICommand.java:386) at javax.faces.component.UIViewRoot.broadcastEvent(UIViewRoot.java:491) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:451) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:794) at org.operamasks.faces.lifecycle.InvokeApplication.execute(InvokeApplication.java:42) at org.operamasks.faces.lifecycle.LifecycleImpl.doPhase(LifecycleImpl.java:135) at org.operamasks.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:83) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:244) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at filter.UserLoginFilter.doFilter(UserLoginFilter.java:35) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:124) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.operamasks.faces.webapp.filter.DefaultSuffixRedirector.doFilter(DefaultSuffixRedirector.java:73) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:619) 31 三月 14 12:45:06,72102 [http-8080-1] INFO hsir.settlement.service.MmsSettlementSummaryService

原提问者GitHub用户tfgzs

展开
收起
山海行 2023-07-05 21:56:03 428 0
4 条回答
写回答
取消 提交回答
  • 北京阿里云ACE会长

    这个异常通常是因为SQL语句中的语法错误导致的。

    根据异常信息,可以看到是期望右括号,实际上出现了FOR FOR。这提示我们在SQL语句中可能存在括号不匹配的问题,或者FOR语句书写错误。

    可以尝试以下几个解决方案:

    检查SQL语句的语法
    需要仔细检查SQL语句的语法,确保SQL语句中的关键字、括号、运算符等符号使用正确,不要存在语法错误。可以使用SQL编辑器或者调试工具来检查SQL语句的语法,找出错误并进行修复。

    检查括号匹配
    需要检查SQL语句中的括号是否匹配。例如,如果SQL语句中存在左括号,就必须存在右括号与之匹配。可以使用文本编辑器或者SQL编辑器的括号匹配功能,检查SQL语句中的括号是否匹配。

    检查FOR语句
    需要检查SQL语句中的FOR语句是否书写正确。例如,如果使用了嵌套FOR循环,需要确保内外循环的变量名称不同,避免出现变量名冲突的情况。同时,还需要确保FOR循环的语法正确,例如FOR循环的开始和结束条件是否正确设置。

    尝试化简SQL语句
    如果SQL语句过于复杂,可以尝试将SQL语句进行化简,将其分解为多个简单的SQL语句,从而更容易发现和修复语法错误。可以使用SQL编辑器或者调试工具,逐步分解SQL语句,将其化简为多个简单的SQL语句。

    2023-07-30 09:36:08
    赞同 展开评论 打赏
  • 值得去的地方都没有捷径

    根据异常信息,确实是因为Druid不支持使用"FOR XML PATH"这样的语法。"FOR XML"是SQL Server的特有语法,用于将查询结果以XML格式返回。在Druid中,无法正确解析这样的语法,导致报错。

    如果你想在插入语句中使用XML格式,请考虑以下解决方案:

    将查询结果转换为XML格式,然后手动将XML字符串插入到REMARKS字段中。你可以在Java代码中使用XML库(如dom4j或JAXB)来生成XML字符串。
    如果REMARKS字段是数据库中的XML类型,则可以在应用程序中先生成XML字符串,然后使用JDBC的setSQLXML方法将XML字符串插入到REMARKS字段中。
    请根据你的具体情况选择适合你的解决方案。希望能对你有所帮助!如果有任何其他问题,请随时提问。

    2023-07-11 16:10:02
    赞同 展开评论 打赏
  • 问题已修复,请用新版本 https://github.com/alibaba/druid/releases/tag/1.1.14

    原回答者GitHub用户wenshao

    2023-07-06 12:30:19
    赞同 展开评论 打赏
  • 根据异常信息,Druid不支持在SQL语句中使用FOR XML PATH语法。由于您的环境使用的是JDK 1.6和Tomcat 6.0,可能导致无法使用较新版本的Druid驱动程序。

    解决此问题的一种方法是将查询结果作为子查询,并在外部查询中连接到主查询。例如,您可以将以下查询:

    sql (SELECT REMARKS+'' FROM MMS_SETTLEMENT_COM WHERE MID=A.MID FOR XML PATH('')) AS REMARKS 更改为:

    sql (SELECT (SELECT REMARKS+'' FOR XML PATH('')) FROM MMS_SETTLEMENT_COM WHERE MID=A.MID) AS REMARKS

    这样修改后,Druid应该能够正确解析和执行SQL语句。

    2023-07-06 09:18:04
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载