Lesson 8
Basic Elements of a Transact-SQL Statement
In this module, you learned about the basics of Transact-SQL, the programming language used with SQL Server 7. As I mentioned at the beginning of the module, Transact-SQL is really Microsoft's version of SQL, containing specific keywords and statements that only SQL-Server 7 understands.Having completed this module, you should be able to:
- Define what SQL does for you and explain how it applies to SQL Server 2022
- Describe the basic features of Transact-SQL, Microsoft's version of SQL
- Explain how SQL Server 2022processes queries
- Use variables to write a Transact-SQL statement
- Use control-of-flow logic in Transact-SQL statements
Transact-SQL (T-SQL) is Microsoft's proprietary extension to SQL, used in SQL Server and Azure SQL Database. T-SQL enhances the SQL standard with additional features for procedural programming, error handling, and transaction control, providing more powerful tools for managing and manipulating data. Here are the basic features of Transact-SQL in SQL Server 2022:
### Key Features of Transact-SQL
- Procedural Programming: - **Variables: Allows the declaration and use of variables to store temporary data. - **Control-of-Flow Language: Includes statements such as `BEGIN`, `END`, `IF...ELSE`, `WHILE`, and `CASE` to control the flow of execution. - **Loops: Supports iterative processing through loops like `WHILE`.
- Error Handling: - **TRY...CATCH: Blocks that allow for robust error handling by capturing and responding to errors within T-SQL scripts and stored procedures.
- Transaction Management: - **BEGIN TRANSACTION**, **COMMIT**, and **ROLLBACK: Commands to explicitly control transactions, ensuring data integrity and consistency. - **SAVEPOINT: Defines points within a transaction to which you can later roll back.
- Built-in Functions: - A wide array of built-in functions for string manipulation, date and time processing, mathematical calculations, and other common tasks (e.g., `LEN()`, `GETDATE()`, `CONVERT()`, `ROUND()`).
- Stored Procedures: - Precompiled collections of T-SQL statements stored in the database. These can accept parameters, return values, and are used to encapsulate complex logic, improve performance, and maintain code reuse.
- Triggers: - Special types of stored procedures that automatically execute in response to certain events on a table or view (e.g., `AFTER INSERT`, `BEFORE UPDATE`).
- Views: - Virtual tables created by a `SELECT` query that can simplify complex queries, provide data abstraction, and enhance security by limiting access to specific data.
- User-Defined Functions (UDFs): - Custom functions created by users to encapsulate reusable logic, which can return scalar values or result sets.
- Common Table Expressions (CTEs): - Temporary result sets that can be referenced within a `SELECT`, `INSERT`, `UPDATE`, or `DELETE` statement, improving readability and manageability of complex queries.
- XML and JSON Support: - Functions and queries to parse, modify, and query XML and JSON data, facilitating the integration and manipulation of semi-structured data within SQL Server.
- Dynamic SQL: - The ability to construct and execute SQL statements dynamically at runtime using the `EXEC` and `sp_executesql` commands.
- Enhancements in SQL Server 2022: - **Intelligent Query Processing: Enhancements like adaptive joins, memory grant feedback, and improved cardinality estimation for optimized query performance. - **Ledger: Provides tamper-evident capabilities to maintain the integrity of data by creating an immutable record of data changes. - **Data Virtualization: Access external data sources such as Oracle, Teradata, MongoDB, and other SQL Server instances without moving the data.
- Security Features: - **Dynamic Data Masking: Obfuscates sensitive data in query results to prevent unauthorized access. - **Always Encrypted: Ensures data is encrypted both at rest and in transit, with the ability to perform operations on encrypted data without decrypting it. - **Row-Level Security: Restricts access to specific rows in a table based on the user’s identity.
Glossary terms
This module introduced you to the following terms:- Boolean expression
- metadata
- Structured Query Language (SQL)