--- name: code-analysis description: "Use when the user asks to review, fix, explain or refactor T-SQL or a stored procedure without running it, incl. injection and SARGability. Has tested checks common answers miss, e.g. compile against the live schema with SET NOEXEC. For a cost-validated rewrite, use `query-rewrite`." user-invocable: true --- # T-SQL Code Analysis Use this skill when the user asks to refactor, fix, explain, document, or analyze a T-SQL script. ## Scope - Reviews code without executing it: correctness, performance anti-patterns (non-SARGable predicates, implicit conversions, cursors, `SELECT *`, row-by-row DML), SQL injection in dynamic SQL, refactoring, and documentation. - For a cost-validated rewrite of one query, use `query-rewrite`; for execution plan analysis, use `plan-analysis`. ## Required tools - A SQL query tool connected to the target server and database. Prefer, in order: the SQL Tools MCP server that ships with the `microsoft-sql-admin` plugin (`mssql_list_database_connections`, `mssql_connect_database`, then `mssql_execute_query`); any other SQL tool the agent host provides for running T-SQL and reading result sets; [`sqlcmd`](https://learn.microsoft.com/sql/tools/sqlcmd/sqlcmd-utility) from a terminal. It is used read-only (syntax validation and schema inspection), and to execute a schema change only when the user asks you to make it. If the SQL Tools MCP server is not connected or lists no connections, follow the `sql-tools-mcp-setup` skill, which asks before installing or configuring anything. If no SQL tool can be made available, give the user the T-SQL to run and ask for the results. ## Validating Syntax Validate a script in the database context without executing it. If the active tool catalog has `mssql_validate_syntax`, use it: it compiles every batch of a `GO` script in order on one connection, executes nothing, and reports a batch that needs an object an earlier batch creates as unverifiable. Otherwise send the `SET` statements and the script in **one** request, each `SET` in its own batch: ```sql SET NOEXEC ON; GO