Install
openclaw skills install @dennisrongo/sql-reviewPre-commit SQL code review for uncommitted database changes. Detects antipatterns that cause real production incidents — sp_send_dbmail in CATCH blocks (masks the real error as a misleading permission denial), broken retry patterns (@retry declared without a surrounding WHILE loop), swallowing CATCH blocks (no THROW/RAISERROR/log), new tables created without a primary key or any index (silent perf-then-deadlock killer), parameter-vs-column type mismatches (8152 truncation risk), EXEC() string concatenation without sp_executesql parameters (SQL injection), NOLOCK in write paths, UPDATE/DELETE without WHERE, cursors without READ_ONLY FORWARD_ONLY LOCAL, hardcoded env values (emails, server names, paths), cross-DB references like msdb.dbo.*, missing SET NOCOUNT ON, missing GRANT EXECUTE on CREATE PROC, BEGIN TRANSACTION outside TRY/CATCH, and vestigial control-flow comments hinting at refactor leftovers (e.g. -- end while loop with no WHILE). Reports findings as BLOCKER / WARN / INFO with file:line citations and per-finding fix recommendations. **Never edits SQL** — surfaces findings for human review. Use this skill whenever the user says "/sql-review", "review my SQL", "review the SQL diff", "lint the SQL", "check my SQL changes", "SQL pre-commit check", "audit my stored proc", or "any SQL antipatterns in this diff" — even if they don't explicitly say "SQL review skill". Distinct from code-review (general best-practice review) — this carries SQL-specific patterns that surface DB-layer production incidents.
openclaw skills install @dennisrongo/sql-reviewReview uncommitted SQL changes against a catalog of patterns that cause real production incidents — masked errors, unindexed new tables, silent truncation, SQL injection, deadlock-prone CATCH blocks — and surface findings as recommendations the user can act on, without editing SQL unprompted.
.sql change touching a stored procedure, function, or tableDo not auto-trigger when the user is asking for a general code review on a mixed diff — defer to code-review for that. This skill is specifically for SQL-heavy diffs where the antipattern catalog applies.
If you find issues, do not start editing the SQL. Produce the findings report first. After delivering it, ask the user per finding: "Want me to fix #N?" — and wait for an explicit yes. Pre-authorization ("fix them all") proceeds through the list; absent that, default to ask-per-fix.
SQL changes are particularly sensitive because they often run against production data in a single non-reversible deployment. A drive-by refactor mid-review is more dangerous here than in application code.
git status --short — see what's modified, staged, untracked.git diff -- '*.sql' — unstaged SQL changes.git diff --staged -- '*.sql' — staged SQL changes.main..HEAD), use that instead: git diff <range> -- '*.sql'.A or ??) — review the entire file. The whole file is new code; pre-existing antipatterns elsewhere don't apply.M) — review only the changed hunks. The rest of the file may have legacy antipatterns the user isn't introducing (e.g. existing repos commonly have dozens of SPs with the sp_send_dbmail-in-CATCH pattern; flagging all of them on an unrelated edit creates noise).BLOCKER / WARN / INFO (see Categories).file:line for every finding. Line numbers come from the diff or the current file content.BLOCKER — would cause a production incident: silent data loss, masked errors, SQL injection, unindexed table that will grow, broken transaction handling. Fix before commit.WARN — likely a bug or maintenance hazard: NOLOCK in write paths, hardcoded environment values, missing SET NOCOUNT ON, cross-DB references. Should fix but won't necessarily incident.INFO — style / convention / hygiene: vestigial comments, cursor defaults, missing GRANT EXECUTE where the project's pattern exists.Lead each finding with the why. "This swallows the original error and surfaces a misleading permission denial" beats "add ;THROW;".
The 17 checks are pattern matches. A ripgrep hit is a candidate — before reporting it, Read the whole procedure/batch it sits in; severity depends on context the pattern can't see. Concrete demotions:
WITH (NOLOCK) hit, but the proc is a read-only reporting proc with no INSERT/UPDATE/DELETE → WARN at most, not BLOCKER (check #13 blocks only when the same proc writes the tables it dirty-reads).DECLARE @retry with no WHILE @retry in sight → check for a GOTO-label retry loop consuming it before flagging; only report when nothing reads the variable.CREATE TABLE with no index, but it's a static lookup/config table seeded with a handful of rows in the same diff → WARN with a one-line reason, not BLOCKER (the deadlock incident needs growth under concurrent writes).Every reported finding must:
BLOCKER / WARN: state the concrete production incident in one sentence (this state → this outcome, e.g. "first deadlock under load → SP silently gives up, rows never written"). If you can't write that sentence after reading the full proc, demote one level.Zero findings is a valid outcome — a clean diff gets a clean report. Never stretch INFO items into WARNs to fill sections; thoroughness is the 17 checks you ran, which the report shows.
Use ripgrep for detection. The patterns below are starting points — adapt to the actual diff text.
sp_send_dbmail inside CATCH — masks the real exception with a permission/configuration error when the executing login lacks EXECUTE on msdb.dbo.sp_send_dbmail. The real underlying error is silently lost.
rg -n -B 20 'sp_send_dbmail' <file> → check whether the surrounding context is a BEGIN CATCH.;THROW; to re-raise, or write the error info to a local log table. Never sp_send_dbmail from inside operational SPs.Broken retry pattern — DECLARE @retry INT = N; exists, but no WHILE @retry loop wraps the TRY/CATCH. The retry variable is decremented in the CATCH but never read, so the SP gives up on the first deadlock instead of retrying.
rg -n '@retry' <file> → confirm there's a matching WHILE.*@retry somewhere in the same SP body.WHILE @retry > 0 BEGIN ... END around the TRY/CATCH, or remove the vestigial variable entirely.Empty / swallowing CATCH blocks — BEGIN CATCH ... END CATCH with no THROW, no RAISERROR, and no INSERT INTO <error_log_table>. Silent failure → silent data loss.
rg -n -A 30 'BEGIN CATCH' <file> → check each match for at least one of THROW, RAISERROR, or INSERT INTO.;THROW; unless there's a deliberate reason to suppress (and document that reason).BEGIN TRANSACTION without TRY/CATCH + XACT_STATE handling — an error mid-transaction leaves an open transaction on the connection.
rg -n 'BEGIN TRAN' <file> → confirm the same SP has BEGIN TRY and XACT_STATE() checks in CATCH.BEGIN TRY / BEGIN TRANSACTION ... COMMIT TRANSACTION / END TRY / BEGIN CATCH / IF XACT_STATE() = -1 ROLLBACK; IF XACT_STATE() = 1 COMMIT; ;THROW; / END CATCH.Vestigial control-flow comments — -- end while loop with no WHILE keyword, -- retry on deadlock with no retry loop, etc. Strong signal that a refactor left dead code behind.
rg -n -- '-- end (while|for|repeat)' <file> → check for matching opening keyword.New CREATE TABLE without a primary key, clustered index, or any index — heap tables will deadlock-and-table-scan once they grow. This is the exact pattern that caused real production deadlocks.
rg -n -A 50 'CREATE TABLE' <file> → confirm at least one of PRIMARY KEY, CLUSTERED INDEX, or CREATE INDEX exists on that table within the diff or in a sibling file.PRIMARY KEY CLUSTERED on the natural ID column. If the table is append-mostly with no natural ID, add a clustered index on the column the populating SPs filter by in WHERE.Parameter-to-column type-width mismatches — passing @x VARCHAR(50) into a column declared VARCHAR(3) causes silent truncation (or runtime error 8152 depending on ANSI_WARNINGS).
@param VARCHAR(N) declarations against the target table's column widths. Flag any narrowing.LEFT(@x, 3) truncation at the boundary so it's intentional and visible.Implicit type conversions in JOIN / WHERE — WHERE intCol = @varcharParam defeats indexes and produces table scans.
rg -n -i 'WHERE\s+\w+\s*=\s*@\w+' <file> → cross-check parameter type vs. column type.Dynamic SQL via string concatenation passed to EXEC() without sp_executesql @stmt, @params parameterization — SQL injection.
rg -n 'EXEC\s*\(' <file> → confirm any string-concat dynamic SQL nearby uses sp_executesql with parameters, not EXEC(@strSQL) with values inlined.EXEC sp_executesql @stmt = @strSQL, @params = N'@p1 INT, @p2 VARCHAR(50)', @p1 = @value1, @p2 = @value2;.Hardcoded environment-specific values — email addresses, server names, file paths, IP addresses, connection strings.
rg -nE "'[\w.-]+@[\w.-]+\.\w+'|@server_name\s*=\s*'\w+'|\\\\\\\\[\\w.-]+\\\\" <file> (emails, linked servers, UNC paths).Cross-database references — msdb.dbo.*, master.dbo.*, linked server [srv].db.dbo.*. These require permissions that may not exist in all environments (the canonical example: a SP calling msdb.dbo.sp_send_dbmail from a login that lacks EXECUTE on it).
rg -n 'msdb\.|master\.dbo\.|\[\w+\]\.\w+\.\w+\.' <file>.UPDATE or DELETE without a WHERE clause — almost always a bug; will affect every row.
rg -n -B 0 -A 20 'UPDATE\s+\w+' <file> → confirm a WHERE clause is present and not commented out.rg -n -B 0 -A 10 'DELETE\s+FROM' <file> → same.WHERE clause. If "every row" really is intended (TRUNCATE-style), use TRUNCATE TABLE and add a comment explaining why.NOLOCK / READ UNCOMMITTED inside transactional write paths — WITH (NOLOCK) is fine for ad-hoc reporting reads, but in an UPDATE/INSERT/DELETE SP it can read uncommitted data and corrupt the write.
rg -n -i 'NOLOCK|READ UNCOMMITTED' <file> → check whether the same SP has INSERT, UPDATE, or DELETE against the same tables.SNAPSHOT isolation if the goal is to avoid blocking.TRUNCATE TABLE or DROP without IF EXISTS in deploy scripts that are supposed to be idempotent (re-runnable). Many repos use flat idempotent Deploy_*.sql files where re-runs must not fail on missing objects.
rg -n -i 'DROP (TABLE|PROC|FUNCTION|VIEW)' <file> → confirm IF EXISTS or OBJECT_ID(...) IS NOT NULL guard.IF OBJECT_ID('dbo.X', 'U') IS NOT NULL DROP TABLE dbo.X; or DROP TABLE IF EXISTS dbo.X; (SQL 2016+).Missing SET NOCOUNT ON at the top of a new CREATE PROC — causes extra round-trips to the client and breaks some ORM drivers that don't handle row-count messages.
rg -n -A 5 'CREATE PROC' <file> → confirm SET NOCOUNT ON appears within the first ~10 lines of the procedure body.SET NOCOUNT ON; as the first statement after AS BEGIN.Cursors without READ_ONLY FORWARD_ONLY LOCAL — default cursors are slow and persist beyond the current batch.
rg -n 'DECLARE\s+\w+\s+CURSOR\b' <file> → confirm READ_ONLY FORWARD_ONLY LOCAL (or FAST_FORWARD) is specified.DECLARE c CURSOR READ_ONLY FORWARD_ONLY LOCAL FOR SELECT ....Missing GRANT EXECUTE at end of CREATE PROC — only flag when the rest of the repo has a consistent GRANT EXECUTE ON ... TO <role> pattern at the end of every SP file. Detect the pattern by sampling a few neighboring .sql files; if every existing SP grants to the same role and this new SP doesn't, flag it.
rg -n 'GRANT\s+EXECUTE' <file> → confirm present if the repo convention requires it.GRANT EXECUTE ON [dbo].[<proc>] TO [<app_role>] GO matching the role the rest of the repo grants to.# SQL review — uncommitted changes
**SQL files changed:** <N> (<S new>, <M modified>)
---
## Verdict
**Ship / Fix blockers first**
<one-paragraph summary of the change and overall state>
---
## Blockers (<N>)
### B1. <short title> — `path/to/file.sql:<line>`
**Evidence:** `<offending SQL fragment, quoted verbatim>`
**Why:** <root cause / production consequence — one line>
**Fix:** <concrete recommendation — one line>
### B2. ...
## Warnings (<N>)
### W1. <short title> — `path/to/file.sql:<line>`
**Evidence:** `<offending SQL fragment, quoted verbatim>`
**Why:** ...
**Fix:** ...
## Info (<N>)
- `path:line` — <one-liner>
---
## Summary
- BLOCKER: <count>
- WARN: <count>
- INFO: <count>
## Fixes I can apply
If you want, I can apply any of: B1, B2, W1. Which? (or "all", or "none")
End with the offer. Wait for the user's choice.
User: "/sql-review"
Claude:
git diff --staged -- '*.sql' and git diff -- '*.sql' in parallel.Stored Procedures/dbo.usp_NewSave.sql.B1 — BEGIN CATCH without ;THROW; (line 88) — swallowing.B2 — calls EXEC msdb.dbo.sp_send_dbmail inside that CATCH (line 92) — masking risk.W1 — missing SET NOCOUNT ON at line 6.W2 — WITH (NOLOCK) on the table being UPDATEd (line 45).I1 — missing GRANT EXECUTE at end of file (the repo's other SPs all end with it).User: "review the SQL diff before I push"
Claude:
Tables/dbo.NewAuditLog.sql (new file) — CREATE TABLE declared but no PRIMARY KEY, no CLUSTERED INDEX, no CREATE INDEX.B1 — "Heap table will table-scan on every read/UPDATE. Once the table grows past a few hundred thousand rows under concurrent writes, SQL Server's optimizer picks parallel plans and the workers deadlock on heap pages — a recurring production incident pattern."PRIMARY KEY CLUSTERED (id) or a clustered index on the lookup column."B2 — implicit type conversion WHERE recordID = @recordID where the new table's recordID is INT but the param is declared VARCHAR).User: "lint the SQL — just the staged hunks"
Claude:
usp_UpdateBigOldSP.sql is modified but huge. The user's diff added a 20-line block that does UPDATE dbo.Customers SET ... without a WHERE clause.UPDATE without WHERE as B1 — does NOT flag the legacy sp_send_dbmail call elsewhere in the file (which was already there before the user's edit).BLOCKER items without asking. SQL deploys to production; user confirmation gates the change.nit when there's a BLOCKER next to it. Lead with severity.file:line.WITH (NOLOCK) vs READPAST, @retry vs @retryCount — the exact token is the finding.BLOCKER with no one-sentence production-incident scenario. Can't name the incident? It's a WARN.write-a-skill change, not a per-run improvisation..sql-scoped; mixed diffs route to code-review for the non-SQL parts.Deploy_*.sql files (every deploy is a full re-run), check #14 (DROP / TRUNCATE without IF EXISTS) is critical — the deploy will fail on second run otherwise. For repos that use forward-only migrations, this check is lower priority.DECLARE @retry INT = 5; followed by SET @retry = @retry - 1; in CATCH but no WHILE loop looks like deadlock handling. It isn't. Multiple SPs in real codebases have this bug from removed-loop refactors.code-review — on a mixed diff (.cs + .sql), run code-review for the application code and sql-review for the database changes.