grafana/grafana · error
error when executing the sql query
Error message
error when executing the sql query
What it means
SqlDatasource.metricFindQuery runs a meta query via runMetaQuery; any failure is caught, logged via console.error, and rethrown as the generic 'error when executing the sql query'. The original error is in the console, not the thrown message (a deliberate wrap linked to grafana/grafana#82250).
Source
Thrown at packages/grafana-sql/src/datasource/SqlDatasource.ts:219
};
const rawSql = this.templateSrv.replace(query, scopedVars, this.interpolateVariable);
const interpolatedQuery: SQLQuery = {
refId: refId,
datasource: this.getRef(),
rawSql,
format: QueryFormat.Table,
};
// NOTE: we can remove this try-catch when https://github.com/grafana/grafana/issues/82250
// is fixed.
let response;
try {
response = await this.runMetaQuery(interpolatedQuery, range);
} catch (error) {
console.error(error);
throw new Error('error when executing the sql query');
}
return this.getResponseParser().transformMetricFindResponse(response);
}
// NOTE: this always runs with the `@grafana/data/getDefaultTimeRange` time range
async runSql<T extends object>(query: string, options?: RunSQLOptions) {
const range = getDefaultTimeRange();
const frame = await this.runMetaQuery({ rawSql: query, format: QueryFormat.Table, refId: options?.refId }, range);
return new DataFrameView<T>(frame);
}
private runMetaQuery(request: Partial<SQLQuery>, range: TimeRange): Promise<DataFrame> {
const refId = request.refId || 'meta';
const queries: DataQuery[] = [{ ...request, datasource: request.datasource || this.getRef(), refId }];
return lastValueFrom(
getBackendSrv()
.fetch<BackendDataSourceResponse>({View on GitHub (pinned to ae3104e369)
Solutions
- Check the browser console for the original error logged before the throw.
- Test the interpolated rawSql directly against the database.
- Verify DB connection, credentials, and table/column existence.
- Fix variable interpolation that yields invalid SQL.
Example fix
// before: bad interpolation yields invalid SQL
SELECT "${columns}" FROM t
// after: use the variable syntax your plugin expects for identifiers/values
SELECT * FROM t WHERE name = '${name}' Defensive patterns
Strategy: try-catch
Validate before calling
// Best-effort sanity check on interpolated SQL before sending
function looksLikeValidSql(sql: string): boolean {
return sql.trim().length > 0 && !/\$\{[^}]*\}/.test(sql); // no unresolved template vars
} Try / catch
try {
const items = await ds.metricFindQuery(rawSql, { range });
} catch (e) {
// thrown message is generic; the original error is logged to the console
showError('SQL query failed. See browser console for the original error.');
} Prevention
- Inspect the console for the wrapped original error.
- Test interpolated rawSql directly against the DB before wiring to a variable.
- Validate that all template variables resolved before execution.
When it happens
Trigger: Variable/query interpolation (${variable}) that produces invalid SQL; the database is unreachable; the query references a missing table or column; connection or credentials are wrong.
Common situations: Bad variable interpolation in rawSql; DB connectivity issues; permission errors on the target table; syntax errors from user-authored queries.
Related errors
AI-assisted analysis of grafana/grafana@ae3104e369 (2026-08-12).
Data as JSON: /api/errors/0b68341d68d5c5f6.
Report an issue: GitHub.