Transact-SQL Syntax Conventions

Microsoft SQL Server Documentation

Introduction

This document outlines the syntax conventions used in Transact-SQL (T-SQL) to ensure consistency and readability across SQL Server documentation.

General Syntax Conventions

The following conventions are used to describe T-SQL syntax:

Keywords: All T-SQL keywords, such as SELECT, FROM, WHERE, are displayed in uppercase.
Identifiers: Object names (tables, columns, stored procedures, etc.) are represented in a generic form, often using placeholders like table_name, column_name, or object_name. These are typically shown in lowercase and italics or are self-explanatory.
Syntax Elements:
  • Brackets []: Optional syntax elements are enclosed in square brackets.
  • Braces {}: Used for choices where one option must be selected. For example, { UNION | UNION ALL }.
  • Pipe |: Separates alternative syntax options.
  • Ellipsis ...: Indicates that a list of items can be repeated.
  • Parentheses (): Used to group elements or to enclose arguments for functions.
Variables: User-defined variables typically start with the @ symbol, e.g., @variable_name. System variables are also prefixed with @@, e.g., @@ROWCOUNT.
Comments: Single-line comments start with --. Multi-line comments are enclosed in /* ... */.

Example Syntax Block

A typical syntax block might look like this:


CREATE PROCEDURE procedure_name
    [ @parameter1 datatype [ = default_value ] [ OUTPUT ] ]
    [ , [ @parameter2 datatype [ = default_value ] [ OUTPUT ] ] ]
[ AS ]
{ BEGIN
    -- T-SQL statements
    RETURN { integer_expression | @@ERROR }
END
}
                

Reserved Keywords

A comprehensive list of reserved keywords can be found in the SQL Server Books Online. It's good practice to avoid using these as identifiers.

Note: While many keywords are case-insensitive, it is a strong convention to write them in uppercase for clarity.

Best Practices

Tip: Always use proper indentation and formatting for your T-SQL code to improve maintainability and reduce errors. Use comments to explain complex logic.

Further Reading