The thousand-line stored procedure with nested cursors, temp tables, and twenty years of patches is the standard opening obstacle of every modernization project. Long-context LLMs are genuinely excellent at procedure archaeology — summarizing control flow, extracting business rules, and mapping dependencies — which turns days of manual tracing into hours of directed verification.
The discipline is treating every extracted "business rule" as a claim to be checked against the code and the data, not as documentation.
Use this before rewriting or migrating any stored procedure too large or too old to have a living owner. It is equally useful when an incident forces you to understand a procedure fast, or when scoping a migration and you need a defensible inventory of embedded logic.
Triggers:
Extract the complete source plus its neighborhood. Pull the procedure DDL, the DDL of every table it reads or writes, and any procs it calls. Incomplete context produces confident, wrong summaries — the model fills gaps with plausibility.
First pass — structure, not meaning.
Here is a legacy T-SQL stored procedure (1,100 lines) plus DDL for the
tables it touches. Do not summarize business meaning yet.
Produce:
1. Control-flow outline: sections, loops/cursors, branches, and what
each writes to (table or temp table).
2. Full dependency list: tables read, tables written, procs called,
with line numbers.
3. Every hard-coded literal (dates, IDs, magic numbers, statuses)
with line number and surrounding context.
4. Anything unreachable, or reachable only under conditions you can
identify (dead branches, commented-out logic still referenced).
[paste procedure + DDL]
The line numbers matter: they make every claim spot-checkable.
Verify the skeleton before going deeper. Spot-check five claims against the source — two dependencies, two literals, one branch condition. If any is wrong, fix the context (missing DDL? truncated paste?) and regenerate; a wrong skeleton poisons everything downstream.
Second pass — business-rule extraction.
Using the verified outline, extract every business rule as:
RULE-nn | plain-English statement | line numbers | inputs | outputs |
confidence (high/medium/low) | what would break downstream if this
rule were dropped.
Flag rules that contradict each other or that the branch structure
makes unreachable. Do not merge similar-looking rules — list them
separately with the difference noted.
Interrogate the low-confidence rules with data. For each rule the model marked medium/low, write a query that would show the rule operating. Rules the data contradicts get flagged for a human decision — the code may be wrong, dormant, or the summary may be.
-- RULE-07 claims: orders in status 'H' older than 90 days are
-- excluded from settlement. Does the output actually reflect that?
SELECT COUNT(*) AS should_be_zero
FROM settlement_output s
JOIN orders o ON o.order_id = s.order_id
WHERE o.status = 'H'
AND o.order_date < CURRENT_DATE - INTERVAL 90 DAY;
Produce the rewrite contract. Ask for a consolidated spec — rules table, dependency map, literals-to-parameterize list, open questions — and have the procedure's stakeholders review the open questions. This document, not the old code, is what the new dbt models get built against; every RULE-nn becomes a test in the rewrite.
The LLM's summary is a map drawn by a fast, occasionally overconfident cartographer. Line-number citations and rule-by-rule data checks are how you find where the map diverges from the territory before you build on it.
information_schema.routines / sys catalog views for referencing objects) — the model's list and the catalog's list must reconcile.#stage a later section reads, after conditional rebuilds, is exactly the detail summaries flatten. Trace one temp table by hand as a spot check.Legacy procedure code on client engagements is client IP, and MSAs usually prohibit pasting it into public tools. Run the identical two-pass workflow through Bedrock or Azure OpenAI provisioned in the client's tenancy, or a long-context local model via Ollama for smaller procedures. Where even tenancy-approved AI is unavailable, genericize first: a scripted rename of schema, table, and column identifiers to neutral tokens (kept in a local mapping) preserves the logic the analysis needs while removing client vocabulary. All verification queries run inside the client warehouse, and the rewrite contract is reviewed under the client's document controls.