{"record":{"id":"474b068d3240b3b7","repo":"googleapis/mcp-toolbox","slug":"unable-to-execute-query-w-474b06","errorCode":null,"errorMessage":"unable to execute query: %w","messagePattern":"unable to execute query: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sources/cloudsqlmssql/cloud_sql_mssql.go","lineNumber":118,"sourceCode":"\nfunc (s *Source) SourceType() string {\n\t// Returns Cloud SQL MSSQL source type\n\treturn SourceType\n}\n\nfunc (s *Source) ToConfig() sources.SourceConfig {\n\treturn s.Config\n}\n\nfunc (s *Source) MSSQLDB() *sql.DB {\n\t// Returns a Cloud SQL MSSQL database connection pool\n\treturn s.Db\n}\n\nfunc (s *Source) RunSQL(ctx context.Context, statement string, params []any) (any, error) {\n\tresults, err := s.MSSQLDB().QueryContext(ctx, statement, params...)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"unable to execute query: %w\", err)\n\t}\n\tdefer results.Close()\n\n\tcols, err := results.Columns()\n\t// If Columns() errors, it might be a DDL/DML without an OUTPUT clause.\n\t// We proceed, and results.Err() will catch actual query execution errors.\n\t// 'out' will remain an empty slice if cols is empty or err is not nil here.\n\tout := []any{}\n\tif err == nil && len(cols) > 0 {\n\t\t// create an array of values for each column, which can be re-used to scan each row\n\t\trawValues := make([]any, len(cols))\n\t\tvalues := make([]any, len(cols))\n\t\tfor i := range rawValues {\n\t\t\tvalues[i] = &rawValues[i]\n\t\t}\n\n\t\tfor results.Next() {\n\t\t\tscanErr := results.Scan(values...)","sourceCodeStart":100,"sourceCodeEnd":136,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/cloudsqlmssql/cloud_sql_mssql.go#L100-L136","documentation":"Returned by the cloud-sql-mssql source's RunSQL when s.MSSQLDB().QueryContext fails to execute the supplied statement. This wraps any driver-level execution error: SQL syntax errors, missing objects, permission denials, timeouts, or dropped connections. The error occurs before any rows are read, so results.Close() is never deferred in this path.","triggerScenarios":"Any tool invocation that reaches Source.RunSQL (e.g. execute-sql tool) with a statement the SQL Server rejects or cannot run: invalid T-SQL syntax, referencing a non-existent table/column, insufficient permissions, parameter count/type mismatch, context deadline exceeded, or connection already closed.","commonSituations":"LLM-generated SQL with syntax errors, querying tables the configured user lacks SELECT rights on, very long queries hitting a context timeout, stale connections after an instance restart, placeholders (!) param style mismatches with sqlserver driver.","solutions":["Read the wrapped driver error for the exact SQL Server message (line number, error code)","Run the statement manually in SSMS/sqlcmd with the same user to reproduce","Fix SQL syntax or object names in the tool's statement/parameters","Grant the configured user the needed SELECT/EXEC permissions","Increase the request context timeout if the query is long-running"],"exampleFix":"// before\nSELECT * FORM users WHERE id = @p1;\n// after\nSELECT * FROM users WHERE id = @p1;","handlingStrategy":"validation","validationCode":"// Validate statement non-empty and table names quoted before invoking RunSQL\nif strings.TrimSpace(statement) == \"\" {\n    return errors.New(\"statement must not be empty\")\n}","typeGuard":null,"tryCatchPattern":"results, err := src.RunSQL(ctx, statement, params)\nif err != nil {\n    if strings.Contains(err.Error(), \"unable to execute query\") {\n        // surface wrapped driver error to the user and let the LLM retry with corrected SQL\n        return fmt.Errorf(\"SQL rejected by SQL Server: %w\", err)\n    }\n    return err\n}","preventionTips":["Test all configured tool statements manually in SSMS/sqlcmd before deployment","Grant the configured SQL Server user only the specific permissions the tools need","Use parameterized statements rather than string interpolation","Set sensible context timeouts matching expected query durations"],"tags":["mssql","sql","query-execution","cloudsql"],"backgroundTag":"query-execution-failed","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}