T-SQL Programmability Syntax
This document provides a comprehensive guide to the syntax used in Transact-SQL (T-SQL) for programming objects within Microsoft SQL Server. Understanding these syntax rules is crucial for developing robust and efficient database solutions.
General Syntax Rules
T-SQL statements are generally composed of keywords, identifiers, operators, and literals. The following rules apply:
- Statements are typically terminated by a semicolon (
;), although it is often optional for single statements. - Keywords are not case-sensitive, but it is a common practice to write them in uppercase for readability.
- Identifiers (object names, variable names) are generally case-insensitive, depending on the server's collation settings.
- String literals are enclosed in single quotes (
'). - Numeric literals are written directly.
Example: Basic SELECT Statement
SELECT
<column_list>
FROM
<table_source>
WHERE
<search_condition>;
Delimiters and Special Characters
Several characters have special meaning in T-SQL:
;(Semicolon): Statement terminator.'...'(Single Quotes): Encloses string literals.-- ...(Double Hyphen): Starts a single-line comment./* ... */(Slash-Asterisk): Starts a multi-line comment.@(At sign): Precedes variable names (e.g.,@myVariable).#(Hash): Indicates a temporary table (e.g.,#tempTable).##(Double Hash): Indicates a global temporary table.[](Square Brackets): Used to delimit identifiers that contain special characters or reserved words..(Dot): Used to separate schema and object names (e.g.,dbo.myTable).
Standard Statement Structure
Most T-SQL statements follow a predictable structure:
- Keywords: Reserved words with specific meanings (e.g.,
SELECT,INSERT,CREATE,ALTER). - Identifiers: Names of database objects such as tables, views, stored procedures, and columns.
- Expressions: Combinations of values, operators, and functions that evaluate to a single value.
- Clauses: Optional parts of a statement that modify its behavior (e.g.,
WHERE,GROUP BY,ORDER BY).
Example: CREATE TABLE Syntax
CREATE TABLE <table_name> (
<column_name1> <data_type> [<constraints>],
<column_name2> <data_type> [<constraints>],
...
);
Here, <...> denotes placeholders for specific names or values.
Common Programmability Constructs
T-SQL offers rich features for programmability:
Stored Procedures
CREATE PROCEDURE <procedure_name>
[@parameter1 datatype [= default_value]]
[, @parameter2 datatype [= default_value]]
AS
BEGIN
-- T-SQL statements for the procedure body
-- ...
END;
Functions
CREATE FUNCTION <function_name>
(@parameter1 datatype, @parameter2 datatype)
RETURNS <return_data_type>
AS
BEGIN
-- T-SQL statements for the function body
DECLARE @result <return_data_type>;
-- ...
RETURN @result;
END;
Triggers
CREATE TRIGGER <trigger_name>
ON <table_name>
AFTER <INSERT|UPDATE|DELETE>
AS
BEGIN
-- T-SQL statements for the trigger body
-- Accessing inserted and deleted tables
-- ...
END;
Data Type Specification
When defining columns or variables, you must specify a valid T-SQL data type. Common types include:
INT,BIGINT,SMALLINT,TINYINT(Integer types)DECIMAL,NUMERIC,FLOAT,REAL(Numeric types)VARCHAR(n),NVARCHAR(n),CHAR(n),NCHAR(n)(String types)DATE,DATETIME,DATETIME2,TIME(Date and Time types)BIT(Boolean type)VARBINARY(n)(Binary data type)UNIQUEIDENTIFIER(GUID)
Refer to the T-SQL Data Types documentation for a complete list and detailed descriptions.