{"record":{"id":"283c844d78e258d2","repo":"pentaho/pentaho-kettle","slug":"an-error-occurred-executing-sql","errorCode":null,"errorMessage":"An error occurred executing SQL: ","messagePattern":"An error occurred executing SQL: ","errorType":"exception","errorClass":"KettleDatabaseException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/pentaho/di/core/database/Database.java","lineNumber":2057,"sourceCode":"            selStmt.setFetchSize( fs );\n          }\n          selStmt.setFetchDirection( fetchMode );\n        }\n        if ( rowlimit > 0 && databaseMeta.supportsSetMaxRows() ) {\n          selStmt.setMaxRows( rowlimit );\n        }\n\n        log.snap( Metrics.METRIC_DATABASE_EXECUTE_SQL_START, databaseMeta.getName() );\n        res = selStmt.executeQuery( databaseMeta.stripCR( sql ) );\n        log.snap( Metrics.METRIC_DATABASE_EXECUTE_SQL_STOP, databaseMeta.getName() );\n      }\n\n      // MySQL Hack only. It seems too much for the cursor type of operation on MySQL, to have another cursor opened\n      // to get the length of a String field. So, on MySQL, we ignore the length of Strings in result rows.\n      //\n      rowMeta = getRowInfo( res.getMetaData(), databaseMeta.isMySQLVariant(), lazyConversion );\n    } catch ( SQLException ex ) {\n      throw new KettleDatabaseException( \"An error occurred executing SQL: \" + Const.CR + sql, ex );\n    } catch ( Exception e ) {\n      throw new KettleDatabaseException( \"An error occurred executing SQL:\" + Const.CR + sql, e );\n    } finally {\n      log.snap( Metrics.METRIC_DATABASE_OPEN_QUERY_STOP, databaseMeta.getName() );\n    }\n\n    return res;\n  }\n\n  private boolean canWeSetFetchSize( Statement statement ) throws SQLException {\n    return databaseMeta.isFetchSizeSupported()\n      && ( statement.getMaxRows() > 0\n      || databaseMeta.getDatabaseInterface() instanceof PostgreSQLDatabaseMeta\n      || ( databaseMeta.isMySQLVariant() && databaseMeta.isStreamingResults() ) );\n  }\n\n  public ResultSet openQuery( PreparedStatement ps, RowMetaInterface params, Object[] data )\n    throws KettleDatabaseException {","sourceCodeStart":2039,"sourceCodeEnd":2075,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/core/src/main/java/org/pentaho/di/core/database/Database.java#L2039-L2075","documentation":"Thrown by Database.openQuery(...) when a SQLException occurs while executing a SELECT and creating the ResultSet (prepareStatement/executeQuery or reading metadata). The failed SQL is appended after Const.CR. The query result never becomes available.","triggerScenarios":"Database.openQuery(sql) (and overloads) when executeQuery() or res.getMetaData() throws a SQLException — invalid SQL, table doesn't exist, connection broken, or query cancelled.","commonSituations":"Table lookup steps pointing at non-existent tables; WHERE clauses with wrong column names; connections dropped during long-running SELECTs; read-only replicas rejecting the query.","solutions":["Check the wrapped SQLException cause for the server-side error message","Validate the SQL and referenced table/column names against the target database","Confirm the connection is still alive; reconnect if the query ran long or the network is flaky","Wrap openQuery in try/catch with a fallback query or abort path"],"exampleFix":"// before\nResultSet rs = db.openQuery(\"SELECT id FROM customer WHERE nmae = ?\", ...);\n// after\nResultSet rs = db.openQuery(\"SELECT id FROM customer WHERE name = ?\", ...);","handlingStrategy":"try-catch","validationCode":"if (sql == null || sql.trim().isEmpty()) throw new IllegalArgumentException(\"Query is empty\");","typeGuard":null,"tryCatchPattern":"try {\n  ResultSet rs = database.openQuery(sql);\n} catch (KettleDatabaseException e) {\n  Throwable cause = e.getCause();\n  logError(\"openQuery failed for [\" + sql + \"]: \" + (cause != null ? cause.getMessage() : \"\"), e);\n  throw e;\n}","preventionTips":["Validate SQL and object names against the target schema before executing","Keep result-set consumption close to openQuery to avoid timeouts","Use correct, current JDBC drivers","Handle query cancellation/connection loss with retry logic"],"tags":["database","jdbc","sql","select"],"backgroundTag":"sql-query-failed","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}