SQLException: SELECT TOP number

| No Comments | No TrackBacks

Today, I found a little issue in Microsoft SQL Server handling the 'TOP' SQL statement when you pass it a number to 'SELECT TOP' by. The code worked in the previous version of MS SQL, before the upgrade.

 

The error received is "java.sql.SQLException: Incorrect syntax near '@P0'." 'P0' is parameter zero.

 

For example:

        String sqlGetMyData =

            "SELECT TOP ? * FROM TEMP_UPLOAD "

                + "WHERE TRANSACTION_TYPE = ? AND TRANSACTION_SUB_TYPE = ? AND TRANSFORMID > ? ORDER BY TRANSFORMID";

 

The number that 'TOP' uses must be in parenthesis, as detailed below.


        String sqlGetMyData =

            "SELECT TOP (?) * FROM TEMP_UPLOAD "

                + "WHERE TRANSACTION_TYPE = ? AND TRANSACTION_SUB_TYPE = ? AND TRANSFORMID > ? ORDER BY TRANSFORMID";

 

Related Entries

No TrackBacks

TrackBack URL: http://jenikya.com/cgi-bin/mt5/mt-tb.cgi/201

Leave a comment

Archives

OpenID accepted here Learn more about OpenID