Java-MS SQL异常:“ @ P0”附近的语法不正确

我正在使用MS SQL查询数据库,由于某种原因,我收到以下错误:com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '@P0',即使此'P0'在我的语法中不存在...

我已经读到有人遇到了同样的问题,但是他们使用的是存储过程,而我没有使用某些东西,所以我看不出他的解决方案对我有什么用。 (他的解决方案是在过程调用周围添加花括号{}。

无论如何,下面我已经粘贴了相关代码。 真希望有人能对此有所帮助,感到很沮丧。

PreparedStatement stmt = null;
Connection conn = null;    

String sqlQuery = "SELECT TOP ? \n"+
                              "z.bankAccountNo, \n"+
                              "z.statementNo, \n"+
                              "z.transactionDate, \n"+
                              "z.description, \n"+
                              "z.amount, \n"+
                              "z.guid \n"+
                              "FROM \n"+
                              "( \n"+
                              "select  \n"+
                              "ROW_NUMBER() OVER (ORDER BY x.transactionDate, x.statementNo) AS RowNumber, \n"+
                              "x.transactionDate, \n"+
                              "x.statementNo, \n"+
                              "x.description, \n"+
                              "x.amount, \n"+
                              "x.bankAccountNo, \n"+
                              "x.guid \n"+
                              "FROM \n"+
                              "( \n"+
                              "SELECT  \n"+
                              "a.bankAccountNo,  \n"+
                              "a.statementNo,  \n"+
                              "a.transactionDate, \n"+
                              "a.description,  \n"+
                              "a.amount,  \n"+
                              "a.guid  \n"+
                              "FROM BankTransactions as a  \n"+
                              "LEFT OUTER JOIN BankTransactionCategories as b  \n"+
                              "ON a.category = b.categoryCode  \n"+
                              "WHERE b.categoryCode is null \n"+
                              ") as x \n"+
                              ") as z \n"+
                              "WHERE (z.RowNumber >= ?)";

stmt = conn.prepareStatement(sqlQuery);
stmt.setInt(1, RowCountToDisplay);
stmt.setInt(2, StartIndex);
ResultSet rs = null;
try{
    rs = stmt.executeQuery();
} catch (Exception Error){
    System.out.println("Error: "+Error);
}

提前致谢!

VMAtm asked 2020-01-24T12:50:45Z
6个解决方案
93 votes

如果您传入变量,SQL Server要求您在top的参数周围加上括号:

SELECT TOP (?)
Andomar answered 2020-01-24T12:51:15Z
19 votes

在我们的应用程序中,我们扩展了废弃的SQLServerDialect。更改为SQLServer2008Dialect后,问题消失了。

Tomasz Godziński answered 2020-01-24T12:51:35Z
12 votes

将休眠升级到版本5.x,并遇到此问题。 必须将“ hibernate.dialect”配置从org.hibernate.dialect.SQLServerDialect更新为org.hibernate.dialect.SQLServer2012Dialect。 解决了这个问题!

Hibernate Doc参考:[https://docs.jboss.org/hibernate/orm/3.6/reference/zh-CN/html/session-configuration.html#configuration-programmatic]

休眠Jira问题:[https://hibernate.atlassian.net/browse/HHH-10032]

MarCrazyness answered 2020-01-24T12:52:04Z
1 votes

像我这样,也可能是由于您的SQL中的语法错误引起的

select * from drivel d where exists (select * from drivel where d.id = drivel.id and drivel.start_date < '2015-02-05' AND '2015-02-05' < drivel.end_date) OR exists (select * from drivel where d.id = drivel.id and drivel.start_date < '2015-02-05' AND '2015-02-05' < drivel.end_date) OR exists (select * from drivel where d.id = drivel.id and '2015-02-05' < drivel.start_date and drivel.end_date < '2015-02-05'

给了消息

com.microsoft.sqlserver.jdbc.SQLServerException:'@ P5'附近的语法不正确

问题实际上是末尾缺少平衡')',即正确的版本是

select * from drivel d where exists (select * from drivel where d.id = drivel.id and drivel.start_date < '2015-02-05' AND '2015-02-05' < drivel.end_date) OR exists (select * from drivel where d.id = drivel.id and drivel.start_date < '2015-02-05' AND '2015-02-05' < drivel.end_date) OR exists (select * from drivel where d.id = drivel.id and '2015-02-05' < drivel.start_date and drivel.end_date < '2015-02-05')
Nick answered 2020-01-24T12:52:37Z
0 votes

如果使用自定义数据源,请添加property:

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.SQLServer2012Dialect

在application.properties中将不起作用。

您必须将属性作为属性映射添加到数据源Bean中:

  @Bean
public LocalContainerEntityManagerFactoryBean sqlServerEntityManagerFactory() {
    HashMap<String, String> properties = new HashMap<>();
    properties.put("hibernate.dialect", "org.hibernate.dialect.SQLServer2012Dialect");

    LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
    factoryBean.setDataSource(sqlServerDataSource());
    factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    factoryBean.setJpaPropertyMap(properties);
    return factoryBean;
}
Prashant answered 2020-01-24T12:53:06Z
0 votes

如果在spring-boot应用程序中使用Hibernate,则可以使用以下配置属性设置hibernate.dialect

spring.jpa.database-platform=org.hibernate.dialect.SQLServer2008Dialect

Meysam answered 2020-01-24T12:53:30Z
translate from https://stackoverflow.com:/questions/7038818/ms-sql-exception-incorrect-syntax-near-p0