简体   繁体   English

Microsoft SQL Server JPA存储过程com.microsoft.sqlserver.jdbc.SQLServerException:'{'附近的语法不正确

[英]Microsoft SQL Server JPA Stored Procedure com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '{'

I have a Microsoft SQL Server TSQL stored procedure 我有一个Microsoft SQL Server TSQL存储过程

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_get_latest_ids]
@tblNm VARCHAR(50),
@ver VARCHAR(50)
AS
BEGIN
DECLARE @stmt VARCHAR(100)
SET @stmt = 'SELECT id FROM CHANGETABLE(CHANGES [DatabaseName].[dbo].[' +
@tblNm + '], ' + @ver + ') as c;';
EXEC (@stmt)
END
GO

This SP is working, I'm seeing rows returned when I run it like 此SP正在运行,运行时看到返回的行

exec  [dbo].[sp_get_latest_ids] @tblNm = 'Reports', @ver = '22379091'

Now I'm trying to use this Stored Proc with JPA as a StoredProcedureQuery in my DAO class 现在,我正在尝试将此JD的存储过程与我的DAO类中的StoredProcedureQuery一起使用

String selectStatement = "EXEC [dbo].[sp_get_latest_ids] @tblNm = 'Reports', @ver = '" + lastVer.toString()  + "'";
System.out.println(selectStatement);
StoredProcedureQuery q = em.createStoredProcedureQuery(selectStatement);
q.setMaxResults(100);

boolean isResultSet = q.execute(); // returns true when we have a result set from the proc
//List<Integer> reportIds2 =  q.getResultList();

This is not working, I get a stack trace that looks like below. 这不起作用,我得到如下所示的堆栈跟踪。 Need input on how to fix at this point, as '}' is not anywhere in my stored procedure code. 此时需要输入有关如何修复的信息,因为存储过程代码中的任何位置都不是'}'。 Thank you. 谢谢。

EXEC [dbo].[sp_get_latest_changetable_ids] @tblNm = 'Reports', @ver = '22379091'
2017-04-03 16:50:21.800  WARN 15436 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 102, SQL
State: S0001
2017-04-03 16:50:21.802 ERROR 15436 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : Incorrect syntax ne
ar '{'.
2017-04-03 16:50:21.809  INFO 15436 --- [           main] utoConfigurationReportLoggingInitializer :

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled
.
2017-04-03 16:50:21.826 ERROR 15436 --- [           main] o.s.boot.SpringApplication               : Application startup
 failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:779) [spring-boot-1.5.1.RELEASE.
jar!/:1.5.1.RELEASE]
        at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:760) [spring-boot-1.5.1.RELEASE
.jar!/:1.5.1.RELEASE]
        at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:747) [spring-boot-1.5.1.RELEAS
E.jar!/:1.5.1.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.5.1.RELEASE.jar!/:1
.5.1.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.1.RELEASE.jar!/:
1.5.1.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.1.RELEASE.jar!/:
1.5.1.RELEASE]
        at com.company.railagentmonitor.Application.main(Application.java:41) [classes!/:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_121]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_121]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_121]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [RailAgentMonitor-0.1.0.jar:na
]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [RailAgentMonitor-0.1.0.jar:na]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [RailAgentMonitor-0.1.0.jar:na]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [RailAgentMonitor-0.1.0.jar:na]
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: Error calling CallableSt
atement.getMoreResults
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692) ~[hibernate-enti
tymanager-5.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602) ~[hibernate-enti
tymanager-5.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.jpa.internal.StoredProcedureQueryImpl.execute(StoredProcedureQueryImpl.java:224) ~[hibernate-en
titymanager-5.0.11.Final.jar!/:5.0.11.Final]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_121]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_121]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_121]
        at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityMana
gerCreator.java:372) ~[spring-orm-4.3.6.RELEASE.jar!/:4.3.6.RELEASE]
        at com.sun.proxy.$Proxy77.execute(Unknown Source) ~[na:na]
        at com.company.railagentmonitor.dao.ChangetableVersionsDAOImpl.getLatestChangetableIds(ChangetableVersionsDAOImpl
.java:50) ~[classes!/:na]
        at com.company.railagentmonitor.service.ChangetableVersionsServiceImpl.getLatestChangetableIds(ChangetableVersion
sServiceImpl.java:24) ~[classes!/:na]
        at com.company.railagentmonitor.Application.run(Application.java:58) [classes!/:na]
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:776) [spring-boot-1.5.1.RELEASE.
jar!/:1.5.1.RELEASE]
        ... 14 common frames omitted
Caused by: org.hibernate.exception.SQLGrammarException: Error calling CallableStatement.getMoreResults
        at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106) ~[hi
bernate-core-5.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
 ~[hibernate-core-5.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109) ~[hibern
ate-core-5.0.11
.Final.jar!/:5.0.11.Final]
        at org.hibernate.result.internal.OutputsImpl.convert(OutputsImpl.java:79) ~[hibernate-core-5.0.11.Final.jar!/:5.
0.11.Final]
        at org.hibernate.result.internal.OutputsImpl.<init>(OutputsImpl.java:56) ~[hibernate-core-5.0.11.Final.jar!/:5.0
.11.Final]
        at org.hibernate.procedure.internal.ProcedureOutputsImpl.<init>(ProcedureOutputsImpl.java:32) ~[hibernate-core-5
.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.procedure.internal.ProcedureCallImpl.buildOutputs(ProcedureCallImpl.java:411) ~[hibernate-core-
5.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.procedure.internal.ProcedureCallImpl.getOutputs(ProcedureCallImpl.java:363) ~[hibernate-core-5.
0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.jpa.internal.StoredProcedureQueryImpl.outputs(StoredProcedureQueryImpl.java:234) ~[hibernate-en
titymanager-5.0.11.Final.jar!/:5.0.11.Final]
        at org.hibernate.jpa.internal.StoredProcedureQueryImpl.execute(StoredProcedureQueryImpl.java:217) ~[hibernate-en
titymanager-5.0.11.Final.jar!/:5.0.11.Final]
        ... 24 common frames omitted
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '{'.
        at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:232) ~[mssql-jd
bc-6.1.0.jre8.jar!/:na]

Based on the docs at http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html the argument to createStoredProcedureQuery should be the name of the stored procedure, not a statement to execute the query. 根据http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html上的文档,createStoredProcedureQuery的参数应该是存储过程的名称,而不是执行查询的语句。 Try: 尝试:

StoredProcedureQuery q = em.createStoredProcedureQuery("sp_get_latest_ids");

Then bind your tblNm and ver parameters using the setParameter(...) methods of the resulting stored procedure query. 然后使用结果存储过程查询的setParameter(...)方法绑定tblNm和ver参数。

Regarding the "{" - my guess is that the q.execute() is building a query which has that character, but because your stored procedure 'name' is invalid, you get a syntax error after the character. 关于“ {”-我的猜测是q.execute()正在构建具有该字符的查询,但是由于存储过程“名称”无效,因此在该字符之后会出现语法错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 com.microsoft.sqlserver.jdbc.SQLServerexception:&#39;=&#39;附近的语法不正确 - com.microsoft.sqlserver.jdbc.SQLServerexception:incorrect syntax near'=' com.microsoft.sqlserver.jdbc.SQLServerException:&#39;GO&#39;附近的语法不正确 - com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near 'GO' com.microsoft.sqlserver.jdbc.SQLServerException: &#39;)&#39; 附近的语法不正确 - com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near ')' com.microsoft.sqlserver.jdbc.SQLServerException: &#39;=&#39; 附近的语法不正确 - com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '=' com.microsoft.sqlserver.jdbc.SQLServerException: &#39;|&#39; 附近的语法不正确 - com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '|' com.microsoft.sqlserver.jdbc.SQLServerException 与 jpa - com.microsoft.sqlserver.jdbc.SQLServerException with jpa com.microsoft.sqlserver.jdbc.SQLServerException:&#39;@ P0&#39;附近的语法不正确 - com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '@P0' Vaadin错误:com.microsoft.sqlserver.jdbc.SQLServerException:&#39;LIMIT&#39;附近的语法不正确 - Vaadin error : com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near 'LIMIT' 尝试执行存储过程时收到“ com.microsoft.sqlserver.jdbc.SQLServerException:索引1超出范围。” - Receiving “com.microsoft.sqlserver.jdbc.SQLServerException: The index 1 is out of range.” when trying to execute stored procedure “ com.microsoft.sqlserver.jdbc.SQLServerException:SQL Server未返回响应。” - “com.microsoft.sqlserver.jdbc.SQLServerException: SQL Server did not return a response.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM