Introduction
Transact-SQL enhances the power of SQL to provide a more powerful and versatile language set than other versions of SQL. The Transact-SQL implementation aims to minimize or eliminate the need to use any other programming language within the Sybase environment. Transact-SQL functionality exceeds both the ANSI (American National Standards Institute) standard and the many commercial versions of SQL. It lets you define business rules, transaction logic, and referential integrity as stored procedures.
This chapter describes the rich toolset that Transact-SQL provides, including:
select title_id, type, price from titles where type like "%cook" order by type, price compute avg(price) by type
title_id type price -------- ------------ ------------------------ MC3021 mod_cook 2.99 MC2222 mod_cook 19.99 avg ------------------------ 11.49 title_id type price -------- ------------ ------------------------ TC4203 trad_cook 11.95 TC7777 trad_cook 14.99 TC3218 trad_cook 20.95 avg ------------------------ 15.96
Generated Primary Keys
You can use the IDENTITY property to generate unique, sequential key values for numeric columns with a scale of 0 (that is, with no decimal places). When you insert a row into a table with an IDENTITY column, SQL Server automatically generates the column value. The first time a user inserts a row into a table, SQL Server assigns the IDENTITY column a value of 1. The IDENTITY column value is incremented by one for each new row inserted. You can use columns with the IDENTITY property as primary keys for applications where no other key exists.
Stored Procedures
Stored procedures are a key factor in SQL Server's efficiency because the procedure can be precompiled and stored in the procedure cache. Typically, they can be processed in a fraction of the time it takes to process a single ad hoc SQL command. Figure 5-1 shows a client invoking a stored procedure. Stored procedures give a three-part performance gain. First, because the procedure is stored as a precompiled tree on disk, it takes less time to prepare for execution; second, they reduce network traffic because the precompiled procedure constitutes a smaller amount of data to transmit than the non-compiled version; and last, once the procedure is in the procedure cash, there is no disk I/O time associated with running the procedure because it is already in cache.
Stored procedures offer the following advantages over batches:
Rules
Rules are user-defined integrity constraints; they specify the domain of legal values for a particular column or datatype. SQL Server checks data entries against any rules that have been created for the data's column and datatype. Rules are discussed in more detail in Chapter 8, "Data Integrity and Consistency."
Defaults
A default is a user-defined value linked to a particular column or datatype, and inserted by SQL Server if no value is provided at data-entry time. Defaults are discussed in more detail in Chapter 8, "Data Integrity and Consistency."
Triggers
A trigger is a special event-driven stored procedure associated with a specific table; it executes when a data modification statement, such as insert, update or delete, attempts to affect that table; that is, the trigger "fires" even if no rows are affected. Depending on which data modification statement drives the trigger, it is referred to as an update trigger, insert trigger, or delete trigger. Triggers are also discussed in more detail in Chapter 8, "Data Integrity and Consistency."
Figure 5-1 shows a delete trigger called del_title created on the titles table. The trigger fires whenever there is an attempt to delete a row in the titles table.
Views
Views provide an alternate way of looking at the data in one or more tables. Views are used to focus, simplify, and customize each user's perception of the database. They can also provide a security mechanism for limiting access to the data in the tables.
Cursors
Transact-SQL provides cursors that meet ISO/ANSI SQL 89 specifications. A cursor is a symbolic name that you can associate with a select command. A cursor is functionally a pointer to a row in a table. When you execute a select command that doesn't have a cursor, SQL Server processes the results for every row selected and then returns the result as a unit. When you associate a cursor with the select command, you control the forward traversal through the selected columns row by row.
Error Handling
The Transact-SQL programmer can use a number of error handling tools and techniques. These include capabilities that allow you to:
Transact-SQL System Administration Capabilities
Transact-SQL provides several system administration capabilities not found in other implementations of SQL. Many of the system administration commands are used in conjunction with system procedures. In addition, there are:
Transact-SQL's Debugger
SQL Debug®, a separate Sybase product, provides a graphical user interface for debugging SQL code. It provides full support for all Transact-SQL extended features, even nested stored procedure calls and full cursor debugging. Options include step-level execution, breakpoint setting, conditional tracing, and complete control over variables. You can use separate windows to display SQL commands, results, and variable values. It debugs code generated by third-party tools, and can debug multiple client connections from a single screen. It can identify performance bottlenecks, and highlight logic that might result in performance problems.
Other Transact-SQL Extensions
Other unique or unusual features of Transact-SQL include: