Bonjour,

Je travaille généralement avec MySQL et dans le cadre d'un nouveau projet je dois utiliser MSSQL.
Je cherche donc à "imiter" le LIMIT de MySQL.
Lorsque sous l'exécuteur de requête je rentre ma commande MSSQL, cela fonctionne, par contre quand j'utilise un PreparedStatement j'ai l'exception suivante :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
 
%com.microsoft.sqlserver.jdbc.SQLServerException: Line 1: Incorrect syntax near '@P0'.
	at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source)
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(Unknown Source)
	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(Unknown Source)
	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(Unknown Source)
	at com.microsoft.sqlserver.jdbc.TDSCommand.execute(Unknown Source)
	at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(Unknown Source)
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(Unknown Source)
	at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(Unknown Source)
	at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(Unknown Source)
Requête sue l'analyseur :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
SELECT TOP 10 * FROM (
	SELECT TOP 20 * FROM maTable WHERE INTITULE LIKE '%toto%' ORDER BY INTITULE DESC
	) AS tmp ORDER BY tmp.INTITULE ASC
Requête en java :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
 
protected static final String INTITULE = "SELECT TOP ? * FROM (" +
	" SELECT TOP ? * FROM "+TABLE+" WHERE INTITULE LIKE ? ORDER BY INTITULE DESC" +
	") AS tmp ORDER BY tmp.INTITULE ASC;"; 
...
PreparedStatement ps = dbConnection.prepareStatement(INTITULE);
ps.setInt(1, offset);
ps.setInt(2, offset+rows);
ps.setString(3, "%"+intitule+"%");
ResultSet rs = ps.executeQuery(); <-- Erreur ici
Voila, si quelqu'un est inspiré... parce que là je n'ai vraiment aucune idée.

Merci d'avance
Manu