T-SQL Introduction
Master Microsoft T-SQL — what Transact-SQL is, how it extends ANSI SQL with procedural features (variables, control flow, error handling, transactions, triggers), comparison vs standard SQL, brief history, and tooling overview (SSMS, Azure Data Studio).
T-SQL (Transact-SQL) is Microsoft's proprietary extension to the standard SQL language, designed specifically for Microsoft SQL Server (and Azure SQL Database). It takes everything ANSI SQL gives you — SELECT, INSERT, UPDATE, DELETE, JOIN — and adds a complete procedural programming layer on top: variables, control flow (IF/ELSE/WHILE), error handling (TRY...CATCH), stored procedures, triggers, and more.
If you've worked with Oracle's PL/SQL, you'll find T-SQL fills the same role for SQL Server: it lets you build complex logic that lives inside the database engine, without having to ship every row out to an application server.
Why T-SQL Matters
- Performance. Code that runs in T-SQL avoids round-trips between application and database.
- Encapsulation. Stored procedures and functions hide complexity from callers.
- Security. Grant
EXECUTEon a procedure instead ofSELECTon every underlying table. - Atomicity. Wrap multi-statement work in transactions to ensure consistency.
SQL was born at IBM in the early 1970s as SEQUEL (Structured English Query Language). After a trademark dispute the name was shortened to SQL, but the pronunciation "sequel" stuck. Two key milestones turned SQL into the universal language of relational databases:
| Year | Event |
|---|---|
| 1986 | ANSI standardizes SQL (SQL-86) |
| 1987 | ISO ratifies the same standard |
| 1989 | Sybase & Microsoft release the first version of T-SQL with SQL Server |
| 1992 | SQL-92 — the version most modern databases still align with |
| 1995→ | Microsoft takes sole ownership of SQL Server (and T-SQL) |
Every major RDBMS vendor extended ANSI SQL with their own procedural dialect — Oracle made PL/SQL, IBM made SQL PL, PostgreSQL made PL/pgSQL, and Microsoft (originally with Sybase) made T-SQL. They all do similar things; they're just not portable to each other.
The two are closely related but solve different problems. Standard SQL is a query language; T-SQL is a full programming environment built around it.
| Aspect | Standard SQL | T-SQL |
|---|---|---|
| Style | Declarative — describe what you want | Declarative + procedural — also describe how step-by-step |
| Standardization | ANSI/ISO standard, portable across vendors | Microsoft proprietary; specific to SQL Server & Azure SQL |
| Variables | Not part of the core standard | DECLARE @v INT; — first-class |
| Control flow | None (CASE expressions only) | IF / ELSE, WHILE, BREAK, CONTINUE, GOTO |
| Error handling | Not in the language | TRY / CATCH, THROW, RAISERROR |
| Procedures & functions | Limited — varies by vendor | CREATE PROCEDURE, CREATE FUNCTION, parameters, return values |
| Transactions | COMMIT, ROLLBACK — basic |
Named & nested transactions, savepoints, @@TRANCOUNT |
| Built-in functions | Small standard set | Hundreds — string, date, math, system, window, JSON, etc. |
The smallest meaningful T-SQL example uses a variable, an arithmetic operation, and a PRINT:
DECLARE @greeting VARCHAR(50) = 'Welcome to T-SQL';
DECLARE @year INT = YEAR(GETDATE());
PRINT @greeting + N' (' + CAST(@year AS VARCHAR(4)) + N')';
That tiny script already uses three core T-SQL features: a typed local variable (@greeting), a built-in function (YEAR(GETDATE())), and the PRINT statement. Standard SQL has none of these.
The pages that follow walk through every major area of T-SQL programming, with worked examples and output traces:
| Topic | Covers |
|---|---|
| Data types | INT, BIGINT, VARCHAR, NVARCHAR, DECIMAL, DATE/DATETIME family, BIT, table variables, user-defined types |
| Variables | DECLARE syntax, SET vs SELECT for assignment, scope rules, compound operators |
| Control flow | IF/ELSE, WHILE, CASE, BREAK, CONTINUE, GOTO, batch boundaries |
| Cursors | Declaring and iterating, cursor types and options, when (not) to use them |
| Error handling | TRY / CATCH, THROW, RAISERROR, ERROR_MESSAGE() and friends |
| Stored procedures & functions | Creating, parameters (IN/OUT/default), return values, scalar vs table-valued functions |
| Transactions | BEGIN TRAN, COMMIT, ROLLBACK, savepoints, @@TRANCOUNT |
| Triggers | DML (AFTER / INSTEAD OF) and DDL triggers, the inserted and deleted pseudo-tables |
- SQL Server — any recent edition. SQL Server 2022 Developer Edition is free for non-production use; SQL Server Express is free with size limits and good enough for learning.
- Client tool — SQL Server Management Studio (SSMS) on Windows, or Azure Data Studio cross-platform. The output cards in this tutorial mirror what SSMS shows in its Results and Messages tabs.
- A sample database — Microsoft's AdventureWorks or WideWorldImporters are useful, but every example here creates its own small tables so you can follow along on a fresh database.
- T-SQL = ANSI SQL + Microsoft's procedural extensions for SQL Server (and Azure SQL).
- Adds variables, control flow, error handling, stored procedures, transactions, and triggers — the things that turn a query language into a programming environment.
- It's proprietary: code written in T-SQL won't run unchanged on Oracle, PostgreSQL, or MySQL, but standard SELECT/INSERT/UPDATE/DELETE statements you know already are valid T-SQL.
- Tooling: SQL Server engine + SSMS or Azure Data Studio — both free for development.
- The next pages dive into the core building blocks, starting with data types and variables.