googleapis/mcp-toolbox · error
unable to execute query: %w
Error message
unable to execute query: %w
What it means
This error wraps a failure from `QueryContext` in the MSSQL source's `RunSQL`. It means the SQL statement itself failed to execute — syntax errors, missing tables/columns, permission denials, timeouts, or connection drops during execution.
Source
Thrown at internal/sources/mssql/mssql.go:116
func (s *Source) SourceType() string {
// Returns Cloud SQL MSSQL source type
return SourceType
}
func (s *Source) ToConfig() sources.SourceConfig {
return s.Config
}
func (s *Source) MSSQLDB() *sql.DB {
// Returns a Cloud SQL MSSQL database connection pool
return s.Db
}
func (s *Source) RunSQL(ctx context.Context, statement string, params []any) (any, error) {
results, err := s.MSSQLDB().QueryContext(ctx, statement, params...)
if err != nil {
return nil, fmt.Errorf("unable to execute query: %w", err)
}
defer results.Close()
cols, err := results.Columns()
// If Columns() errors, it might be a DDL/DML without an OUTPUT clause.
// We proceed, and results.Err() will catch actual query execution errors.
// 'out' will remain an empty slice if cols is empty or err is not nil here.
out := []any{}
if err == nil && len(cols) > 0 {
// create an array of values for each column, which can be re-used to scan each row
rawValues := make([]any, len(cols))
values := make([]any, len(cols))
for i := range rawValues {
values[i] = &rawValues[i]
}
for results.Next() {
scanErr := results.Scan(values...)View on GitHub (pinned to 8cc6e09de2)
Solutions
- Run the statement in SSMS/Azure Data Studio to reproduce the exact server error
- Unwrap the error and inspect mssql.Error number/state for the server-side cause
- Check user permissions on the target database/objects
- Increase the context timeout for long-running queries
- Validate parameter count and types match the statement's placeholders
Example fix
// before (wrong schema) stmt := "SELECT * FROM users" // after stmt := "SELECT * FROM dbo.users"
Defensive patterns
Strategy: try-catch
Type guard
func isSqlServerErr(err error) bool {
var se mssql.Error
return errors.As(err, &se)
} Try / catch
res, err := src.RunSQL(ctx, stmt, nil)
if err != nil {
var se mssql.Error
if errors.As(err, &se) {
return fmt.Errorf("SQL Server error %d: %s", se.Number, se.Message)
}
if errors.Is(err, context.DeadlineExceeded) {
return fmt.Errorf("query timed out: %w", err)
}
return err
} Prevention
- Test statements in a SQL client first
- Use fully qualified schema.table names
- Grant least-privilege permissions
- Set realistic context deadlines
When it happens
Trigger: Calling RunSQL with a syntactically invalid statement; referencing non-existent tables or columns; selecting from a database the user lacks rights on; query timeout via context cancellation; parameters count/type mismatch.
Common situations: Typos in T-SQL; querying a table that exists in another database/schema; running DDL without permissions; context deadline exceeded on long queries; stale connections after server failover.
Related errors
- unable to execute query: %w
- unable to execute query: %w
- unable to execute query: %w
- errors encountered by results.Scan: %w
- unable to connect successfully: %w
AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05).
Data as JSON: /api/errors/ea3f68d7403a8cac.
Report an issue: GitHub.