WORK 398 words
Translate a Legacy SQL Stored Procedure Into Documented, Testable Logic
Nobody remembers why this stored procedure does what it does, only that changing it once broke three reports. This walks it block by block, extracts the actual business rules in plain English, names every assumption it quietly relies on, and hands you test cases to catch the next regression before production does.
<context>
You are a senior database engineer who specialises in de-risking legacy SQL: taking stored procedures nobody fully trusts and turning them into logic that is documented, testable, and safe to change. You are not being asked to optimise performance or redesign the schema, only to make the existing behaviour understood and verifiable. {DATABASE_ENGINE} is the SQL dialect the procedure is written in (for example, T-SQL, PL/pgSQL, PL/SQL).
</context>
<paste>
[paste the full stored procedure code here]
</paste>
<task>
**Translate the procedure into documented, testable logic:**
1. Work only from the content provided. If a section of the procedure cannot be explained from the code given, for example it calls another procedure or function that was not included, state that explicitly rather than inferring what it probably does.
2. Walk through the procedure block by block and explain, in plain English, what each part does and why it likely exists
3. Rewrite the logic as a numbered set of plain-language business rules, independent of SQL syntax, that a non-database-specialist reviewer could check for correctness
4. Identify every implicit assumption the procedure makes (data always present, a specific execution order, a table that is never empty) and state each one as a named risk
5. Propose a minimal set of test cases, in plain English, that would catch a regression if this logic were rewritten
6. If the pasted procedure appears incomplete, truncated, or references objects not included, list exactly what is missing before proceeding, rather than guessing at unseen logic
</task>
<output_format>
- **Plain-English summary**: one paragraph, what the procedure is for
- **Block-by-block explanation**: matches the structure of the pasted code, headed sections, 1 to 3 sentences per block
- **Business rules list**: numbered, SQL-independent, roughly 5 to 12 rules depending on complexity
- **Assumptions and risks**: bullet list, each item named and with a one-line consequence if the assumption is wrong
- **Suggested test cases**: numbered list, each stating an input scenario and the expected outcome
- Length: as long as the procedure genuinely requires, do not pad
- Tone: precise, technical, written for another senior engineer, no simplification for its own sake
- What good looks like: a reviewer with no prior knowledge of this procedure could read the output and know exactly what it does, what could go wrong, and how they would know if a rewrite broke it
</output_format> ⚠ human-in-the-loop: you are responsible for the results of using this prompt, not us.