static String buildSelectChunkSql ( String databaseType , String table , long start , int length , String columns , String [ ] columnNames ) { String sqlText = "SELECT " + columns + " FROM " + table ; switch ( databaseType ) { case SQL_SERVER_DB_TYPE : // requires ORDER BY clause with OFFSET/FETCH NEXT clauses, syntax supported since SQLServer 2012 sqlText += " ORDER BY ROW_NUMBER() OVER (ORDER BY (SELECT 0))" ; sqlText += " OFFSET " + start + " ROWS FETCH NEXT " + length + " ROWS ONLY" ; break ; case ORACLE_DB_TYPE : sqlText += " OFFSET " + start + " ROWS FETCH NEXT " + length + " ROWS ONLY" ; break ; case TERADATA_DB_TYPE : sqlText += " QUALIFY ROW_NUMBER() OVER (ORDER BY " + columnNames [ 0 ] + ") BETWEEN " + ( start + 1 ) + " AND " + ( start + length ) ; break ; default : sqlText += " LIMIT " + length + " OFFSET " + start ; } return sqlText ; }
Builds SQL SELECT to retrieve chunk of rows from a table based on row offset and number of rows in a chunk .