How to format PL/SQL and T-SQL code without breaking it

By Hammad Rasool · November 11, 2026

Quick answer: tell the formatter which database the code is for. A formatter set to "standard SQL" rejects everyday Oracle syntax such as %TYPE, q'[...]' strings and :bind variables, and everyday SQL Server syntax such as [bracketed names], #temp tables and @variables. Use the PL/SQL formatter for Oracle and the T-SQL formatter for SQL Server, then check the two things a formatter can get wrong in procedural code: ranges such as 1..3 and the layout of BEGIN/END blocks.

How this was tested: on 11 November 2026, 7 PL/SQL and 7 T-SQL snippets were run through sql-formatter 15.8.2 (the open-source library ToolStash's formatter uses, with the same settings) in three modes each: standard SQL, Oracle and SQL Server. The results were checked again on sql-formatter 15.9.0, the latest release. All output on this page is copied from those runs. The formatted code was not executed on an Oracle or SQL Server database, and Oracle SQL Developer and SSMS were not tested, so this page doesn't describe their built-in formatting.

What breaks when you pick the wrong dialect

Each row is one snippet formatted in the wrong mode. The error messages are copied from the runs.

CodeWrong modeResult
employees.last_name%TYPE (PL/SQL)Standard SQLParse error: Unexpected "%TYPE; beg"
customer_id = :cust (Oracle bind variable)Standard SQLParse error: Unexpected ":cust orde"
q'[It's done]' (Oracle quoting)Standard SQL and SQL ServerParse error: Unexpected "]' as msg,"
[Order ID] (T-SQL name with a space)Standard SQL and OracleParse error: Unexpected "[Order ID]"
create table #t (T-SQL temp table)Standard SQL and OracleParse error: Unexpected "#t (id int"
declare @total money (T-SQL variable)Standard SQLParse error: Unexpected "@total mon"

A parse error is the good outcome, because nothing gets changed. In standard SQL mode the message even adds This likely happens because you're using the default "sql" dialect. The dangerous case is code that formats without an error but comes out different, which is what happened with PL/SQL ranges.

Formatting PL/SQL (Oracle)

The range bug: 1..3 became 1. .3

This loop was formatted in Oracle mode:

begin <<outer>> for i in 1..3 loop exit outer when i = 2; dbms_output.put_line(i); end loop; end;

sql-formatter 15.8.2 and 15.9.0 both returned:

BEGIN << outer >> FOR i IN 1. .3
LOOP EXIT outer WHEN i = 2;

.. is PL/SQL's range operator. Split into 1. .3, it reads as the two numbers 1. and .3, so the loop no longer means "1 to 3". With variables instead of numbers, for i in v_lo..v_hi, the library stopped with a parse error instead. The same run also wrote %TYPE as % TYPE and the label <<outer>> as << outer >>.

ToolStash's PL/SQL formatter now protects .. before formatting and tidies %TYPE, %ROWTYPE, cursor attributes such as SQL%NOTFOUND and labels afterwards, without touching string literals, quoted names or comments. The same input now gives:

BEGIN <<outer>> FOR i IN 1..3
LOOP EXIT outer WHEN i = 2;

dbms_output.put_line (i);

END
LOOP;

END;

If you use sql-formatter directly, in an editor plugin or a build step, check FOR loops in the output until the library fixes this. Searching the result for . . finds the broken ones.

Blocks are split, but not indented

A DECLARE block in Oracle mode, after the fix above:

DECLARE v_name employees.last_name%TYPE;

BEGIN v_name := 'King';

dbms_output.put_line ('Name: ' || v_name);

END;

Statements are separated and keywords uppercased, but the body of BEGIN ... END is not indented, and in the loop example END LOOP was split over two lines. The code still means the same thing, because PL/SQL ignores line breaks between keywords. For long packages, a formatter built for procedural code will give a more readable layout; for queries, anonymous blocks and short procedures, this is usually enough.

Oracle syntax that formatted correctly

  • Alternative quoting kept intact, including the apostrophe inside:
    SELECT
      q'[It's done]' AS msg,
      'plain' AS other
    FROM
      dual;
  • Hierarchical queries with START WITH and CONNECT BY PRIOR:
    SELECT
      id,
      parent_id,
      LEVEL
    FROM
      org
    START WITH parent_id IS NULL
    CONNECT BY PRIOR id = parent_id;
  • The old outer-join operator: d.id(+) came out as d.id (+), which Oracle reads the same way.
  • Bind variables and row limiting: :cust and FETCH FIRST 10 rows ONLY were kept.
  • The SQL*Plus slash. A / on its own line, which runs a PL/SQL block in SQL*Plus and SQL Developer scripts, stayed on its own line:
    CREATE
    OR replace PROCEDURE touch_row AS
    BEGIN
    UPDATE t
    SET
      ts = sysdate
    WHERE
      id = 1;
    
    END;
    
    /
    SELECT
      count(*)
    FROM
      t;
    Note the line break inside CREATE OR REPLACE and the lowercase replace: harmless, but worth tidying by hand.

Formatting T-SQL (SQL Server)

In SQL Server mode all seven T-SQL snippets formatted without an error.

Bracketed names and Unicode strings

SELECT
  [Order ID],
  [Customer Name]
FROM
  [dbo].[Orders]
WHERE
  [Status] = N'Open';

Square brackets are SQL Server's way of quoting names with spaces or reserved words. Oracle and standard SQL use double quotes for this, which is why the other two modes rejected the brackets. The N prefix on N'Open' was kept.

GO stays on its own line

CREATE TABLE #t (id int)
GO
INSERT INTO
  #t
VALUES
  (1)
GO

GO is not a T-SQL statement. It is a batch separator understood by SSMS and sqlcmd, so it must be alone on its line, which the formatter kept.

Variables, TOP and table hints

DECLARE @total money = 0;

SELECT
  @total = sum(amount)
FROM
  dbo.payments
WHERE
  paid = 1;

PRINT @total;
SELECT
  TOP (10) *
FROM
  dbo.orders
WITH
  (nolock)
ORDER BY
  created_at DESC;

The table hint WITH (NOLOCK) ended up on its own lines as if it were the start of a CTE. It still runs, because the line break doesn't change the meaning, but a reviewer may misread it. Join it back to the table name: FROM dbo.orders WITH (NOLOCK).

The semicolon before WITH

In SQL Server, the statement before a common table expression must end with a semicolon, so people often write ;WITH. The formatter split that into a lone ; followed by the CTE:

;

WITH
  recent AS (
    SELECT
      *
    FROM
      dbo.orders
    WHERE
      created_at > dateadd(day, -7, getdate())
  )
SELECT
  count(*)
FROM
  recent;

That still works. The cleaner habit is to end every statement with ;, so the CTE can start with a plain WITH.

TRY...CATCH

BEGIN try BEGIN TRAN;

UPDATE dbo.accounts
SET
  balance = balance - 100
WHERE
  id = 1;

COMMIT;

END try BEGIN catch ROLLBACK;

throw;

END catch

The structure is right, but try, catch and throw stayed lowercase while the other keywords were uppercased, and the BEGIN TRY body is not indented. As with PL/SQL, expect to tidy long procedures by hand.

PL/SQL vs T-SQL: differences that matter when formatting

Oracle PL/SQLSQL Server T-SQL
Quoting names"Order ID"[Order ID] (or double quotes with QUOTED_IDENTIFIER ON)
Variablesv_total NUMBER; in DECLARE, bind variables as :nameDECLARE @total money;
Assignmentv_total := 0;SET @total = 0;
String concatenation||+ or CONCAT()
Running a script/ after each block in SQL*Plus or SQL DeveloperGO between batches in SSMS or sqlcmd
First N rowsFETCH FIRST n ROWS ONLYTOP (n), or OFFSET ... FETCH
ErrorsEXCEPTION WHEN ... THENBEGIN TRY ... END TRY BEGIN CATCH ... END CATCH

The general layout rules are the same for both: uppercase keywords, one clause per line, consistent indentation and aliases. They are covered in SQL formatting best practices.

A checklist after formatting procedural code

  1. Search the output for . . (a split range) and % TYPE. The ToolStash PL/SQL formatter fixes both; other tools built on the same library may not.
  2. Check that / and GO are still alone on their lines.
  3. Rejoin table hints and other fragments that were moved onto their own line.
  4. Compile or run the formatted code once in a development database before committing it.
  5. To check a long procedure without reformatting it, compare the old and new versions in the diff checker: changes should be whitespace and keyword case only.

FAQ

Can I use a normal SQL formatter for PL/SQL?

For plain SELECT, INSERT and UPDATE statements, usually yes. As soon as the code uses %TYPE, bind variables or Oracle's q' quoting, a standard SQL formatter stops with a parse error. Choose Oracle mode.

Why does my T-SQL fail to format with [brackets]?

The formatter is in standard SQL or another dialect's mode, where brackets aren't name quotes. Switch to SQL Server (T-SQL).

Does formatting change what the code does?

It shouldn't, since it only changes whitespace and keyword case. The PL/SQL range bug described above is the exception found in this test, which is why running the code once after formatting is worth it.

Is my code uploaded when I use the formatter?

No. The SQL formatters run in your browser; nothing is sent to a server.

How do I make a long query fit on one line instead?

Use the SQL minifier, which removes comments and extra whitespace.

Sources