Boas…
Eu estou tendo um problema, pois estou tendo a necessidade de Quebrar um SELECT em duas ou + vezes, devido ao IN suportar uma lista de 1000 itens…no meu caso essa lista tem 1158…Aí eu pego os 1000 primeiros, execulto e armazeno em um resultSet “rsAux”, jogo no rsFull(esse deverá armazenar todos rsAux)… o código segue abaixo::
public static RSModel getRelatorio(java.util.Date de, java.util.Date ate, int idFilial) throws BioException {
//inicializaMeses();
try {
String sql= "";
List id_lancto = new ArrayList();
String numero_nota="", numero_nota_ant ="";
sql = "" +
"SELECT n.numero_nota, l.id_lancamento " +
"FROM nota_fiscal n, " +
" lancamento l " +
"WHERE TRUNC(n.data_emissao) BETWEEN TRUNC("+ DateUtil.toSQL(de) +") AND TRUNC("+ DateUtil.toSQL(ate) +") " +
" AND n.id_lancamento = l.id_lancamento " +
"ORDER BY n.numero_nota " ;
Statement st = ConnectionPool.getConnection().createStatement();
ResultSet rs = st.executeQuery(sql);
while(rs.next()){
numero_nota = rs.getString(1);
if(!numero_nota.equals(numero_nota_ant)){
id_lancto.add(""+rs.getInt(2));
}
numero_nota_ant = numero_nota;
}
String tmp = "";
int vezes = 0;
if (id_lancto.size() > 1000){
double size = id_lancto.size();
double cont = id_lancto.size() / 1000;
vezes = Math.ceil(cont);
}
ResultSet rsFull = null;
double j = 1;
while(j <= vezes){
if (id_lancto.size() > 999){
for(int aux = 0; aux < 999; aux++){
tmp += id_lancto.get(aux) + ", ";
}
for(int auxT = 999; auxT > 0; auxT--){
id_lancto.remove(auxT);
}
tmp = tmp.toString().substring(1, tmp.toString().length()-1);
}
else{
tmp = id_lancto.toString().substring(1, id_lancto.toString().length()-1);
}
sql ="" +
"SELECT compra.id_compra, cliente.cpf, serie_nota, numero_nota, " +
"valor_nota, data_emissao, nota_fiscal.cancelado " +
"FROM cliente, nota_fiscal, lancamento, compra " +
"WHERE TRUNC(data_emissao) >= TRUNC("+ DateUtil.toSQL(de) +") " +
" AND TRUNC(data_emissao) <= TRUNC("+ DateUtil.toSQL(ate) +") " +
" AND cliente.id_cliente = compra.id_cliente " +
" AND lancamento.id_lancamento IN (SELECT id_lancamento " +
" FROM lancamento l " +
" WHERE l.id_compra = compra.id_compra" +
" AND l.id_lancamento IN ("+ tmp +"))" +
" AND nota_fiscal.id_lancamento = lancamento.id_lancamento " +
" AND lancamento.id_caixa IN (SELECT id_caixa " +
" FROM caixa " +
" WHERE id_centro_custo IN (SELECT id_centro_custo " +
" FROM centro_custo " +
" WHERE id_filial = "+ idFilial + ")) " +
"ORDER BY numero_nota " ;
Statement stAux = ConnectionPool.getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rsAux = stAux.executeQuery(sql);
rsFull += rsAux; /* ------------------ O ERRO OCORRE NESSA LINHA */
j++;
}
return new RSModel(rsFull);
}
catch(SQLException e) {
e.printStackTrace(System.err);
BioException ex = new BioException("Erro ao selecionar relatório de débito automático.");
ex.fillInStackTrace();
throw (ex);
}
}