MS SQL 异常:'@P0' 附近的语法不正确 [英] MS SQL Exception: Incorrect syntax near '@P0'

查看:19
本文介绍了MS SQL 异常:'@P0' 附近的语法不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm querying a DB using MS SQL and for some reason I get the following error: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near '@P0' even though this 'P0' isn't anywhere in my syntax...

我读到有人遇到了同样的问题,但他们使用的是存储过程,我没有使用它,所以我不知道他的解决方案对我有用.(他的解决方案是在过程调用周围添加大括号 {}.

I've read that someone has had a same issue but they were using a stored proc, something which I am not using so I don't see how his solution will work for me. (His solution being asomething about adding braces {} around the procedure call.

无论如何,下面我已经粘贴了相关代码.真的希望有人能帮我解决这个问题,我很沮丧.

Anyways, below I have pasted the relevant code. Really hope someone can help me with this, getting quite frustrated.

PreparedStatement stmt = null;
Connection conn = null;    

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

提前致谢!

推荐答案

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

SQL Server requires you to place parenthesis around the argument to top if you pass in a variable:

SELECT TOP (?)

这篇关于MS SQL 异常:'@P0' 附近的语法不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆