T-SQL Syntax Reference

This section provides detailed information on the Transact-SQL (T-SQL) syntax used in Microsoft SQL Server. T-SQL is a powerful extension of SQL that adds procedural programming, local variables, string and date processing functions, and command-line editing.

T-SQL Statements

Statements are the fundamental building blocks of T-SQL. They are used to perform actions on the database, such as creating tables, inserting data, querying information, and managing the database.

Data Definition Language (DDL) Statements

DDL statements are used to define and manage database objects.

Data Manipulation Language (DML) Statements

DML statements are used to manage data within database objects.

Control-of-Flow Language Statements

These statements allow you to control the execution flow of T-SQL code.

T-SQL Expressions

Expressions are combinations of values, operators, and functions that evaluate to a single value.

Scalar Expressions

Scalar expressions return a single value.

Table Expressions

Table expressions return a result set (a table).

T-SQL Operators

Operators are special symbols or keywords that perform operations on one or more expressions.

Arithmetic Operators

Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo (Remainder)

Comparison Operators

Operator Description
= Equal to
!= or <> Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Logical Operators

Operator Description
AND True if both operands are true.
OR True if at least one operand is true.
NOT Reverses the logical state of its operand.

String Concatenation Operator

The + operator is used for string concatenation in T-SQL.

String Concatenation Example:

SELECT 'Microsoft' + ' ' + 'SQL Server';
            

T-SQL Data Types

T-SQL supports a wide range of data types to store different kinds of information.

Numeric Data Types

Date and Time Data Types

Character String Data Types

Binary Data Types

Other Data Types

Creating a table with various data types:


CREATE TABLE Products (
    ProductID INT PRIMARY KEY,
    ProductName VARCHAR(100) NOT NULL,
    Price DECIMAL(10, 2) DEFAULT 0.00,
    StockQuantity SMALLINT,
    ManufacturedDate DATE,
    Description NVARCHAR(MAX),
    IsActive BIT DEFAULT 1
);
            

This section covers the core syntax elements of T-SQL. For more advanced topics and specific function references, please refer to the relevant sub-sections.