我有一個Microsoft SQL Server TSQL存儲過程Microsoft SQL Server的JPA存儲過程com.microsoft.sqlserver.jdbc.SQLServerException:附近有語法錯誤 '{'
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
該SP是工作,我所看到的行返回當我運行它像
exec [dbo].[sp_get_latest_ids] @tblNm = 'Reports', @ver = '22379091'
現在,我想在我的DAO類中使用這個存儲過程使用JPA作爲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();
這不起作用,我得到一個如下所示的堆棧跟蹤。需要關於如何解決此問題的輸入,因爲'}'不在我的存儲過程代碼中的任何位置。謝謝。
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]
不應該'createStoredProcedureQuery'只採用存儲過程的**名稱**嗎?它可能轉換爲JDBC調用轉義,這會觸發語法錯誤。 –