estou fazendo uma consulta no SQL Server 2005.
Quando coloco TOP ?
e configuro o valor pelo setInt ocorre o erro abaixo:
09:53:42,224 ERROR [STDERR] Caused by: java.sql.SQLException: Sintaxe incorreta próxima a '@P0'.
09:53:42,224 ERROR [STDERR] at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:368)
09:53:42,224 ERROR [STDERR] at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2820)
09:53:42,224 ERROR [STDERR] at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2258)
09:53:42,224 ERROR [STDERR] at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:632)
09:53:42,224 ERROR [STDERR] at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQLQuery(JtdsStatement.java:477)
09:53:42,224 ERROR [STDERR] at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeQuery(JtdsPreparedStatement.java:776)
09:53:42,224 ERROR [STDERR] at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeQuery(WrappedPreparedStatement.java:342)
09:53:42,224 ERROR [STDERR] at br.org.sc.pousada.business.dao.AcomodacaoDAO.getLimit(AcomodacaoDAO.java:155)
09:53:42,224 ERROR [STDERR] ... 23 more
Mas quando eu coloco TOP “+limit+” funcioana.
Esta certo as duas formas? e porque a primeira ocorre o erro?
List<Acomodacao> list = new ArrayList<Acomodacao>();
Connection conn = null;
try {
conn = getConnection();
PreparedStatement pst = getPreparedStatement(" SELECT TOP ? id,titulo,descricao,id_categoria,numero,valor_diaria,categoria_descricao FROM view_acomodacao ",conn);
pst.setInt(1, limit);
ResultSet rs = pst.executeQuery();
while(rs.next()){
list.add(createSimples(rs));
}
} catch (MsitecException e) {
throw e;
} catch (SQLException e) {
throw new MsitecException(e.getMessage(),e);
} finally{
close(conn);
}
return list;