Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
23.3k views
in Technique[技术] by (71.8m points)

java - MS SQL Exception: Incorrect syntax near '@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);
}

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

SELECT TOP (?)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...