{"record":{"id":"0b68341d68d5c5f6","repo":"grafana/grafana","slug":"error-when-executing-the-sql-query","errorCode":null,"errorMessage":"error when executing the sql query","messagePattern":"error when executing the sql query","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/grafana-sql/src/datasource/SqlDatasource.ts","lineNumber":219,"sourceCode":"    };\n\n    const rawSql = this.templateSrv.replace(query, scopedVars, this.interpolateVariable);\n\n    const interpolatedQuery: SQLQuery = {\n      refId: refId,\n      datasource: this.getRef(),\n      rawSql,\n      format: QueryFormat.Table,\n    };\n\n    // NOTE: we can remove this try-catch when https://github.com/grafana/grafana/issues/82250\n    // is fixed.\n    let response;\n    try {\n      response = await this.runMetaQuery(interpolatedQuery, range);\n    } catch (error) {\n      console.error(error);\n      throw new Error('error when executing the sql query');\n    }\n    return this.getResponseParser().transformMetricFindResponse(response);\n  }\n\n  // NOTE: this always runs with the `@grafana/data/getDefaultTimeRange` time range\n  async runSql<T extends object>(query: string, options?: RunSQLOptions) {\n    const range = getDefaultTimeRange();\n    const frame = await this.runMetaQuery({ rawSql: query, format: QueryFormat.Table, refId: options?.refId }, range);\n    return new DataFrameView<T>(frame);\n  }\n\n  private runMetaQuery(request: Partial<SQLQuery>, range: TimeRange): Promise<DataFrame> {\n    const refId = request.refId || 'meta';\n    const queries: DataQuery[] = [{ ...request, datasource: request.datasource || this.getRef(), refId }];\n\n    return lastValueFrom(\n      getBackendSrv()\n        .fetch<BackendDataSourceResponse>({","sourceCodeStart":201,"sourceCodeEnd":237,"githubUrl":"https://github.com/grafana/grafana/blob/ae3104e3690e28e72c8f0c97efc12647a70f75c3/packages/grafana-sql/src/datasource/SqlDatasource.ts#L201-L237","documentation":"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).","triggerScenarios":"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.","commonSituations":"Bad variable interpolation in rawSql; DB connectivity issues; permission errors on the target table; syntax errors from user-authored queries.","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."],"exampleFix":"// before: bad interpolation yields invalid SQL\nSELECT \"${columns}\" FROM t\n\n// after: use the variable syntax your plugin expects for identifiers/values\nSELECT * FROM t WHERE name = '${name}'","handlingStrategy":"try-catch","validationCode":"// Best-effort sanity check on interpolated SQL before sending\nfunction looksLikeValidSql(sql: string): boolean {\n  return sql.trim().length > 0 && !/\\$\\{[^}]*\\}/.test(sql); // no unresolved template vars\n}","typeGuard":null,"tryCatchPattern":"try {\n  const items = await ds.metricFindQuery(rawSql, { range });\n} catch (e) {\n  // thrown message is generic; the original error is logged to the console\n  showError('SQL query failed. See browser console for the original error.');\n}","preventionTips":["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."],"tags":["sql","query-execution","grafana-sql","database"],"backgroundTag":null,"analyzedSha":"ae3104e3690e28e72c8f0c97efc12647a70f75c3","analyzedAt":"2026-08-12T12:48:38.752Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}