{"record":{"id":"ee922cc7aa38f1e7","repo":"pentaho/pentaho-kettle","slug":"error-opening-resultset-for-query-sql","errorCode":null,"errorMessage":"error opening resultset for query: \" + sql","messagePattern":"error opening resultset for query: \" \\+ sql","errorType":"exception","errorClass":"KettleDatabaseException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/org/pentaho/di/core/database/Database.java","lineNumber":3633,"sourceCode":"      if ( pstmt != null ) {\n        try {\n          pstmt.close();\n        } catch ( Exception e ) {\n          throw new KettleDatabaseException( \"Unable to close prepared statement pstmt\", e );\n        }\n        pstmt = null;\n      }\n      if ( selStmt != null ) {\n        try {\n          selStmt.close();\n        } catch ( Exception e ) {\n          throw new KettleDatabaseException( \"Unable to close prepared statement sel_stmt\", e );\n        }\n        selStmt = null;\n      }\n      return new RowMetaAndData( rowMeta, row );\n    } else {\n      throw new KettleDatabaseException( \"error opening resultset for query: \" + sql );\n    }\n  }\n\n  public RowMeta getMetaFromRow( Object[] row, ResultSetMetaData md ) throws SQLException, KettleDatabaseException {\n    RowMeta meta = new RowMeta();\n\n    for ( int i = 0; i < md.getColumnCount(); i++ ) {\n      ValueMetaInterface valueMeta = getValueFromSQLType( md, i + 1, true, false );\n      meta.addValueMeta( valueMeta );\n    }\n\n    return meta;\n  }\n\n  public RowMetaAndData getOneRow( String sql, RowMetaInterface param, Object[] data ) throws KettleDatabaseException {\n    ResultSet rs = openQuery( sql, param, data );\n    if ( rs != null ) {\n      Object[] row = getRow( rs ); // One value: a number;","sourceCodeStart":3615,"sourceCodeEnd":3651,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/core/src/main/java/org/pentaho/di/core/database/Database.java#L3615-L3651","documentation":"Database.openQuery() could not open a JDBC ResultSet for the given SQL string. The method returns a RowMetaAndData on success, but if the query produced no row or the execution failed, Kettle wraps the failure in a KettleDatabaseException that includes the offending SQL so the developer can see exactly which statement broke.","triggerScenarios":"Calling Database.openQuery(sql) with SQL that fails to execute (syntax error, missing table/column, bad connection) or that returns no rows, so the 'if (row != null)' branch is skipped and the else-branch throws.","commonSituations":"Typos in generated SQL, querying a table that does not exist on the target database, insufficient privileges, using a dialect-specific SQL that the connected DB rejects, or a closed/failed connection.","solutions":["Log or print the exact 'sql' string in the message and run it directly against the database to see the underlying driver error","Verify the table and column names exist in the connected database/schema","Check the database connection settings and test with Database.connect() before querying","Enable Kettle debug logging to capture the driver-level exception that preceded this throw"],"exampleFix":"// before\nRowMetaAndData r = db.openQuery(\"SELECT * FROM LOGG_TBL\");\n// after\nRowMetaAndData r = db.openQuery(\"SELECT * FROM LOG_TABLE\"); // corrected table name","handlingStrategy":"try-catch","validationCode":"if (db == null || !db.isAutoCommit() && db.getConnection() == null) throw new IllegalStateException(\"connect first\");\n// validate table exists:\nif (!db.checkTableExistsUnquoted(schema, table)) throw new IllegalStateException(\"table missing: \" + table);","typeGuard":"boolean isQueryReady(Database db) { try { return db.getConnection() != null && !db.getConnection().isClosed(); } catch (SQLException e) { return false; } }","tryCatchPattern":"try {\n  RowMetaAndData r = db.openQuery(sql);\n  // consume r\n} catch (KettleDatabaseException e) {\n  log.error(\"openQuery failed for SQL: \" + sql, e);\n  throw new RuntimeException(\"Query failed, SQL=\" + sql, e);\n}","preventionTips":["Run generated SQL against the target DB in a dev environment before deploying","Use database metadata (tableExists) to verify objects before querying","Always pass connection-tested Database instances to query methods","Enable Kettle SQL logging during development to spot bad SQL early"],"tags":["jdbc","sql","database"],"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"}