Transact-SQL (T-SQL) Statements
This section provides detailed documentation for Transact-SQL (T-SQL) statements, which are the fundamental building blocks for interacting with and managing Microsoft SQL Server. T-SQL is an extension of SQL that adds procedural programming, local variables, string and date processing functions, and other features.
Statement Categories
Data Manipulation Language (DML) Statements
DML statements are used to retrieve, insert, update, and delete data in your database.
SELECT- Retrieves data from one or more tables.INSERT- Adds new rows of data to a table.UPDATE- Modifies existing data in a table.DELETE- Removes rows from a table.MERGE- Performs INSERT, UPDATE, or DELETE operations on a target table based on a join with a source table.
Data Definition Language (DDL) Statements
DDL statements are used to define, modify, and delete database objects such as tables, indexes, views, and stored procedures.
CREATE TABLE- Creates a new table.ALTER TABLE- Modifies an existing table.DROP TABLE- Deletes a table.CREATE INDEX- Creates an index on a table.CREATE VIEW- Creates a virtual table based on the result set of a query.CREATE PROCEDURE- Creates a stored procedure.
Data Control Language (DCL) Statements
DCL statements are used to manage permissions and access to database objects.
GRANT- Grants permissions to users or roles.REVOKE- Revokes previously granted permissions.DENY- Denies permissions to users or roles.
Transaction Control Language (TCL) Statements
TCL statements manage transactions within a database.
BEGIN TRANSACTION- Starts a transaction.COMMIT TRANSACTION- Saves all changes made within the current transaction.ROLLBACK TRANSACTION- Undoes all changes made within the current transaction.SAVE TRANSACTION- Sets a savepoint within a transaction.
Control Flow Statements
Control flow statements enable you to manage the execution path of T-SQL code.
IF...ELSE- Executes statements conditionally.WHILE- Repeats statements as long as a condition is true.TRY...CATCH- Implements error handling.WAITFOR- Pauses execution.
Example: Simple SELECT Statement
Here's a basic example of a SELECT statement to retrieve customer names and email addresses from a Customers table:
SELECT
FirstName,
LastName,
Email
FROM
Sales.Customers
WHERE
CountryRegionCode = 'US';
For more detailed information on specific statements, including syntax, parameters, and examples, please refer to the individual documentation pages linked above.