JSp

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

发布于 2021-02-01 16:29:57

我正在使用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);
}

提前致谢!

关注者
0
被浏览
148
1 个回答
  • 面试哥
    面试哥 2021-02-01
    为面试而生,有面试问题,就找面试哥。

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

    SELECT TOP (?)
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看