MS SQL异常:'@ P0'附近的语法不正确

51 java sql jsp

我正在使用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);
}
Run Code Online (Sandbox Code Playgroud)

提前致谢!

And*_*mar 93

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

SELECT TOP (?)
Run Code Online (Sandbox Code Playgroud)

  • 对于语法错误绑定,在“ INSERT”查询中也会出现此错误。@P字符后的数字可以指示哪个参数会产生错误(第三个参数的@ P3是...)。 (2认同)

小智 19

在我们的应用程序中,我们扩展了一个沮丧SQLServerDialect.改变后SQLServer2008Dialect问题消失了.

  • 我在使用`PageRequest`时得到了这个.我猜测,根据@Andomar的答案,它是使用没有括号的"顶部"实现的.正如你所说,从`SQLServerDialect`(在我的情况下更新到2012)更新似乎解决了这个问题.显然,这是SQL Server 2005的一个变化:http://www.dotnetfunda.com/articles/show/59/difference-between-top-clause-in-sql-2000-and-sql-2005 (2认同)

Mar*_*ess 12

将hibernate升级到5.x版并遇到了这个问题.不得不将org.hibernate.dialect.SQLServerDialect中的"hibernate.dialect"配置更新为org.hibernate.dialect.SQLServer2012Dialect.修正了这个问题!

Hibernate Doc参考:https: //docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/session-configuration.html#configuration-programmatic

Hibernate Jira问题:https: //hibernate.atlassian.net/browse/HHH-10032