Somewhere in your estate sits a 2,400-line stored procedure nobody dares touch, written by someone who left in 2014. Long-context AI can read the whole thing at once and hand back control flow, business rules, dead branches and a dependency map — the archaeology that must precede any rewrite. (T-145)
Tier: Core · Workflow area: Migration & Modernization · ~40 minutes
Rewriting a legacy procedure without understanding it is how migrations lose the business rules that only existed in code. This playbook is the excavation pass: you feed the model the complete source plus its call graph, and extract a structured dossier — what the procedure does, what it depends on, what is dead, and which behaviors look like bugs preserved as features. The output is a review document, not a rewrite; the rewrite comes later, and only after a human confirms the dossier.
Extract the complete source plus everything it touches. Partial context produces confident wrong summaries. Pull the procedure and its direct dependencies:
-- SQL Server: procedure source
SELECT OBJECT_DEFINITION(OBJECT_ID('dbo.usp_ProcessMonthlyBilling'));
-- and its dependency edges
SELECT referenced_schema_name, referenced_entity_name, referenced_minor_name
FROM sys.dm_sql_referenced_entities('dbo.usp_ProcessMonthlyBilling', 'OBJECT');
-- Oracle: package body plus dependencies
SELECT text FROM all_source
WHERE name = 'PKG_BILLING' AND type = 'PACKAGE BODY' ORDER BY line;
SELECT referenced_owner, referenced_name, referenced_type
FROM all_dependencies WHERE name = 'PKG_BILLING';
Also grab the DDL of every table the dependency query names — column types and constraints are half the business logic.
Ask for a structured dossier, not a summary. A free-form "explain this code" prompt yields a book report. Demand sections with line references:
You are analyzing a legacy T-SQL stored procedure (source and table DDL
below). Produce an excavation dossier with EXACTLY these sections:
1. PURPOSE — one paragraph: what business process this implements.
2. CONTROL FLOW — the major branches as a numbered outline, each with
line ranges and the condition that selects it.
3. BUSINESS RULES — every rule encoded in code (thresholds, statuses,
date cutoffs, rounding), each with its line number and the literal
values involved.
4. SIDE EFFECTS — every table written, message sent, file touched, or
transaction controlled, with line numbers.
5. DEAD OR SUSPECT CODE — branches that appear unreachable, variables
never read, comparisons that can never be true. Mark each SUSPECT
unless you can prove it dead from this source alone.
6. DEPENDENCY MAP — objects called and objects calling in, as a list.
7. QUESTIONS FOR THE OWNER — everything you could not determine.
Cite line numbers for every claim. If a section is empty, say so.
Do not propose a rewrite.
[paste source + DDL + dependency list]
The line-number requirement is the verification hook: every claim becomes checkable in seconds.
Spot-check the dossier against the source — ten claims minimum. Pick three business rules, three side effects, two dead-code claims and two control-flow branches, and read the cited lines yourself. A typical finding worth catching: the model reports "orders under 100 are exempt" when line 412 actually reads @total <= 100 — boundary semantics matter in billing code.
Interrogate the suspect branches with data. Dead-code claims are hypotheses. Test them against production reality:
-- Model claims the status 'H' branch (lines 610-680) is dead.
-- Does 'H' still occur?
SELECT status, COUNT(*), MAX(updated_at)
FROM dbo.billing_queue GROUP BY status;
Sample output that settles it: H | 4,210 | 2019-03-31 — the branch is dormant, not dead, and the 2019 date is a migration clue worth chasing.
Map the blast radius from the database's side. The model only sees the code you pasted. Confirm inbound callers independently — jobs, triggers and app code the dossier cannot know about:
-- SQL Server Agent jobs that call it
SELECT j.name, s.step_name FROM msdb.dbo.sysjobs j
JOIN msdb.dbo.sysjobsteps s ON s.job_id = j.job_id
WHERE s.command LIKE '%usp_ProcessMonthlyBilling%';
Then grep the application repos for the procedure name. Every caller found goes into the dossier's dependency map by hand.
Publish the dossier with a verification ledger. The document that enters the migration wiki carries a header: AI-drafted 2026-08-06; 10 claims spot-checked, 1 corrected (boundary at line 412); suspect branches tested against data; inbound callers verified independently. Now it is evidence, not folklore.
WHEN OTHERS THEN NULL handlers as first-class findings.pg_depend with a grep across dump files, and have the model flag dynamic SQL (EXECUTE format(...)) whose targets metadata cannot see.Legacy procedures are where hardcoded credentials and embedded customer data go to hide. Scan the source for passwords, connection strings and literal PII before it leaves your machine — or keep the whole exercise on a tenant-approved model.
The dossier's value is not that AI read the code fast — it is that the line-cited structure makes a human verification pass fast. Excavation before rewrite, citations before trust, data before dead-code verdicts.