T-SQL Formatter

Format T-SQL queries online for SQL Server and Azure SQL. Beautify Transact-SQL with proper indentation, consistent keyword casing, and readable line breaks โ€” copy-ready output for SSMS, Azure Data Studio, or code reviews.

Why Format T-SQL Queries

T-SQL stored procedures, triggers, and ad-hoc queries written under time pressure accumulate inconsistent indentation, mixed keyword casing, and cramped clause formatting. Poorly formatted T-SQL is hard to review in pull requests, difficult to debug, and tricky to hand off to other developers. This formatter applies consistent SQL Server conventions: uppercase keywords, aligned clauses, and proper line breaks around BEGIN...END blocks, CASE expressions, and WITH CTEs โ€” the output matches SSMS auto-format expectations.

  • T-SQL syntax aware: Handles TOP, WITH (NOLOCK), OUTPUT, MERGE, and TRY...CATCH
  • CTE formatting: WITH cte AS (...) blocks indented and structured clearly
  • Uppercase keywords: Standard SQL Server convention applied consistently
  • Block indentation: BEGIN...END, IF...ELSE, and WHILE blocks properly indented
  • Browser-based: Queries never leave your device

Choose the Right Variant

Step-by-Step Tutorial

  1. Paste your T-SQL query or stored procedure body โ€” minified, one-liner, or inconsistently indented
  2. Example input: select top 10 u.id,u.name,sum(o.amount) as total from users u inner join orders o on u.id=o.user_id where o.created_at >= '2024-01-01' group by u.id,u.name order by total desc
  3. Click Format SQL
  4. Review the output โ€” keywords uppercased, clauses on new lines, proper alignment
  5. Click Copy and paste into SSMS, Azure Data Studio, or your application code

T-SQL Specific Syntax Reference

  • TOP clause: SELECT TOP (10) col FROM table โ€” use parentheses for compatibility
  • Table hints: FROM table WITH (NOLOCK) โ€” reduces locking for read-heavy queries
  • OUTPUT clause: INSERT INTO t OUTPUT inserted.id VALUES (...) โ€” returns affected rows
  • MERGE: Upsert syntax โ€” MERGE target USING source ON condition WHEN MATCHED THEN UPDATE...
  • TRY...CATCH: BEGIN TRY ... END TRY BEGIN CATCH ... END CATCH โ€” T-SQL error handling
  • Window functions: ROW_NUMBER() OVER (PARTITION BY col ORDER BY col2)
  • CTEs: WITH cte_name AS (SELECT ...) SELECT * FROM cte_name

Privacy and Data Handling

All SQL formatting runs locally in your browser. T-SQL queries โ€” including table names, stored procedure logic, and any inline values โ€” never leave your device. Safe for internal database queries and business-sensitive SQL.

Frequently Asked Questions

What is the difference between T-SQL and standard SQL?

T-SQL (Transact-SQL) is Microsoft's extension of SQL used in SQL Server and Azure SQL. It adds procedural programming features absent from standard SQL: local variables (DECLARE @var INT), control flow (IF...ELSE, WHILE, BREAK, CONTINUE), error handling (TRY...CATCH, RAISERROR, THROW), and proprietary functions like GETDATE(), ISNULL(), COALESCE(), and TOP. T-SQL also uses different string quoting (N'unicode string') and date handling than MySQL or PostgreSQL.

Should I use WITH (NOLOCK) in my T-SQL queries?

WITH (NOLOCK) (equivalent to READ UNCOMMITTED) allows reads of uncommitted data โ€” it reduces blocking on busy OLTP tables but risks reading "dirty" data from transactions that later roll back. Use it only for approximate reporting queries where slight inaccuracies are acceptable (dashboard counts, non-critical analytics). Never use NOLOCK on queries driving financial calculations, inventory systems, or any write operations. For better alternatives with fewer risks, consider snapshot isolation (SET TRANSACTION ISOLATION LEVEL SNAPSHOT) which provides consistent reads without dirty reads.

How do I write efficient CTEs in T-SQL?

CTEs in T-SQL are not materialised by default โ€” the query optimizer may inline the CTE definition at each reference point, potentially executing it multiple times. For CTEs referenced more than once in a query, consider using a temporary table (#temp) instead, which is computed once and stored in tempdb. Use OPTION (RECOMPILE) on queries with CTEs containing parameter-sniffing issues. Recursive CTEs are powerful for hierarchical data but add MAXRECURSION hint for safety: OPTION (MAXRECURSION 100) to prevent infinite loops on bad data.