A stored procedure usually reaches you in the worst possible shape for editing: copied out of a database tool as a dense block, keywords in whatever case the original author felt like, several statements sharing lines, and a condition buried somewhere in the middle. You are expected to change one value in that block without disturbing anything else. Editing text you cannot properly read is how a safe change turns into a broken procedure, so the first job is not the edit. The first job is making the procedure readable.

This guide walks through a read-first workflow using the free SQL Formatter on Ovatly: copy the procedure out, format it so its statement blocks, conditions and nested queries line up, read the formatted version end to end, and only then start changing values.

Contents

  • Quick answer
  • Why a copied procedure is hard to edit safely
  • Read first, edit second
  • How to format a stored procedure, step by step
  • Worked example: a shipping procedure, before and after
  • Limitations and alternatives
  • Common mistakes
  • FAQs
  • Conclusion

Quick answer

Copy the procedure's SQL out of your database tool and paste it into the SQL Formatter. Choose the dialect your database uses, press Format SQL, and the text comes back with keywords in upper case and clauses on their own lines, with multiple statements formatted in sequence and separated by blank lines. Read that version fully before touching a value. The formatter works only on the text you paste: it does not connect to your database, validate the procedure or tell you whether it will run.

Why a copied procedure is hard to edit safely

A single query copied from application code is awkward enough. A procedure multiplies the problem, because it is several statements and at least one condition acting as a single unit. In compressed form, the boundaries between those pieces are invisible. You cannot see where one statement ends and the next begins, which rows a condition guards, or whether a nested query belongs to the update above it or the insert below it.

Case makes it worse. Mixed-case keywords hide the skeleton of each statement, so your eye has to work to find the where clause that decides which rows change. In an edit, that hidden clause is exactly the dangerous part: change a value in the wrong statement, or miss that a condition restricts the update, and the procedure still looks plausible while behaving differently.

Formatting attacks precisely this problem. Upper-cased keywords and one clause per line expose the skeleton of every statement, so blocks, conditions and nested queries sit in a visible structure you can scan top to bottom.

Read first, edit second

The workflow has a discipline to it: the formatted copy is for reading before it is for editing. Read the whole procedure in its new shape first, without changing anything. Confirm that you can account for every statement, that each condition sits where you expected it, and that any nested query is attached to the statement you thought it was. Surprises at this stage are cheap. Surprises after the edit are not.

Then keep both versions side by side while you work. Edit the formatted copy, where the structure stays visible as you type, and keep the original untouched as your reference and your fallback. Because formatting changes layout and keyword case only, and leaves identifiers, values and string literals exactly as written, the formatted copy is a faithful reading of the original, not a reinterpretation of it.

How to format a stored procedure, step by step

  1. Open the procedure in your database tool and copy its full SQL text.
  2. Open the SQL Formatter and paste the SQL into the input box.
  3. Choose the dialect your database uses. The tool covers eight dialects, including PostgreSQL, MySQL and SQL Server, and some syntax only parses correctly in its own dialect.
  4. Press Format SQL.
  5. Read the formatted procedure end to end before editing anything.
  6. Copy or download the formatted query and keep it beside the original while you make your changes.

Worked example: a shipping procedure, before and after

Here is a small MySQL-style procedure as it might come out of a database tool, complete and correct but compressed into a single block:

create procedure mark_order_shipped(in p_order_id int) begin update orders set status = 'shipped', shipped_at = current_timestamp where id = p_order_id and status = 'paid'; if row_count() = 1 then insert into order_events (order_id, event) values (p_order_id, 'shipped'); end if; end

Pasted into the formatter with the MySQL dialect chosen, the two working statements inside get the formatter's usual treatment: keywords in upper case, clauses on their own lines, the statements separated:

CREATE PROCEDURE mark_order_shipped(IN p_order_id INT)
BEGIN
UPDATE
  orders
SET
  status = 'shipped',
  shipped_at = CURRENT_TIMESTAMP
WHERE
  id = p_order_id
  AND status = 'paid';

IF ROW_COUNT() = 1 THEN
INSERT INTO
  order_events (order_id, event)
VALUES
  (p_order_id, 'shipped');
END IF;
END

Read honestly, that output tells you two things. The plain statements are now genuinely readable: you can see that the update only touches orders still marked as paid, and that the event insert happens inside the condition, guarded by the update having changed a row. The procedural scaffolding around them, the begin, if and end if lines, comes back plainer than the statements and may need a quick hand-tidy to indent the way you like. Formatting got you a procedure you can read and reason about; it did not rewrite the procedure, and every identifier, value and string literal is exactly as it was.

Limitations and alternatives

The formatter formats the SQL text you paste, nothing more. It does not connect to any database, so it cannot fetch the procedure for you, check it against your schema, or tell you whether it will run or what it will affect. A beautifully laid-out procedure can still contain a logic error, and formatting will not find it.

Dialect choice matters, and it has limits. Pick the wrong dialect and a paste may fail to format, because syntax such as MySQL backticks or T-SQL brackets only parses correctly in its own dialect. Beyond that, dialect-specific procedural syntax that wraps or extends plain statements may not lay out as neatly as the statements themselves. Expect the statements to come back clean and the scaffolding to sometimes need a hand.

For very large procedures, your database tool or editor may offer its own formatter with project-wide settings. The online tool earns its place when you are working outside that environment, reviewing SQL someone sent you, or preparing a procedure for a code review, where a formatted query is easier to diff and discuss.

Common mistakes

  • Editing the compressed original. The whole point is to read a laid-out version first. Changing values in the one-block copy skips the step that catches misunderstandings.
  • Choosing the wrong dialect. A mismatched dialect is the usual reason a paste fails to format or lays out oddly. Match the selector to the database the procedure came from.
  • Treating a clean layout as validation. Formatting proves nothing about correctness. The tool cannot say whether the procedure runs, only whether you can read it.
  • Reformatting after every small edit. Format once, read, then edit the formatted copy. Repeatedly pasting back and forth invites copy errors between versions.
  • Dropping the original. Keep the untouched original until the edited procedure is tested. It is your reference for what changed and your way back.

FAQs

Does the formatter connect to my database to fetch the procedure? No. You copy the procedure's SQL out of your database tool and paste it in. The formatter never connects to a database, and the formatting runs locally in your browser.

Will formatting change what the procedure does? No. It changes layout and keyword case only. Your identifiers, values and string literals are left exactly as written, and the statements stay equivalent.

Which dialect should I pick for a procedure? The one your database uses. Some syntax only parses correctly in its own dialect, such as MySQL backticks or T-SQL brackets, so a wrong dialect choice is the usual reason a paste fails to format.

Why did part of my procedure format less neatly than the rest? The formatter is built around SQL statements. Plain statements get the full treatment of upper-cased keywords and clauses on their own lines, while procedural scaffolding around them can come back plainer and may need a quick hand-tidy.

Can it format the whole procedure in one paste? Yes. Multiple statements pasted together are formatted in sequence, separated by blank lines, so a multi-statement procedure comes back as one readable document.

Conclusion

Procedures fail edits at the reading stage, not the typing stage. Copy the SQL out, run it through the SQL Formatter, and read the laid-out version until every statement and condition makes sense; then edit the formatted copy with the original beside you. For the related problem of queries nested inside queries, the companion guide on formatting nested subqueries without losing track goes deeper, and the rest of this series lives in the Developer Tools guides archive.