Transact-SQL
From Wikipedia, the free encyclopedia
Sometimes abbreviated T-SQL, Transact-SQL is Microsoft's and Sybase's proprietary extension to the SQL language. Microsoft's implementation ships in the Microsoft SQL Server product. Sybase uses the language in its Adaptive Server Enterprise, the successor to Sybase SQL Server.
In order to make it more powerful, SQL has been enhanced with additional features such as:
- Control-of-flow language
- Local variables
- User authentication integrated with Microsoft Windows
- Various support functions for string processing, date processing, mathematics, etc.
- Improvements to DELETE and UPDATE statements
Contents |
[edit] Control-of-flow Language
Keywords for flow control in Transact-SQL include BEGIN
and END
, BREAK
, CONTINUE
, GOTO
, IF
and ELSE
, RETURN
, WAITFOR
, and WHILE
.
IF
and ELSE
allow conditional execution. This batch statement will print "weekend" if the current date is a weekend day, or "weekday" if the current date is a weekday.
IF DATEPART(dw, GETDATE()) = 7 OR DATEPART(dw, GETDATE()) = 1 PRINT 'It is the weekend.' ELSE PRINT 'It is a weekday.'
BEGIN
and END
mark a block of statements. If more than one statement is to be controlled by the conditional in the example above, we can use BEGIN and END like this:
IF DATEPART(dw, GETDATE()) = 7 OR DATEPART(dw, GETDATE()) = 1 BEGIN PRINT 'It is the weekend.' PRINT 'Get some rest!' END ELSE BEGIN PRINT 'It is a weekday.' PRINT 'Get to work!' END
WAITFOR
will wait for a given amount of time, or until a particular time of day. The statement can be used for delays or to block execution until the set time.
RETURN
is used to immediately return from a stored procedure or function.
BREAK
ends the enclosing WHILE
loop, while CONTINUE
causes the next iteration of the loop to execute. An example of a WHILE
loop is given below.
[edit] Local variables
Local variables are so named because they're local to the script executing them. Transact SQL doesn't support user-defined global variables.
DECLARE
will declare a variable, giving it a name and a type. The SET statement can be used to provide a value, and the variable may be used in a statement by referencing its name.
This script declares a variable as an integer, initializes it, then uses WHILE
to execute a loop.
DECLARE @Counter AS INT SET @Counter = 10 WHILE @Counter > 0 BEGIN PRINT 'The count is ' + CONVERT(VARCHAR(10), @Counter) SET @Counter = @Counter - 1 END
The body of the loop will print a message including the value of the variable, and then decrement the counter.
A variable may be initialized as the result of a statement, like this:
DECLARE @ArticleCount INT SELECT @ArticleCount = COUNT(*) FROM Articles INSERT INTO SizeLog (SampleTime, ArticleCount) VALUES (GETDATE(), @ArticleCount)
which will get the count of rows in the Articles table, then insert a row including that count and the current clock time into the SizeLog table.
[edit] Improvements to DELETE and UPDATE statements
In Transact-SQL, both the DELETE and UPDATE statements allow a FROM clause to be added which allows joins to be included.
[edit] Criticism
Critics of Transact-SQL say that, not only do the additional features break compatibility with standard SQL but also break the discrete modular scope which SQL was intended to occupy. In other words, the additional functionality found in Transact-SQL would normally be implemented with a programming language and Embedded SQL. It muddies the waters, so to speak, when flow control, for example, may be implemented either in a programming language layer or in SQL itself.
[edit] External links
- Transact-SQL Reference for SQL Server 2000 (MSDN)
- Transact-SQL Reference for SQL Server 2005 (MSDN)
- ASE Reference Manual
- DevGuru T-SQL Quick Reference
[edit] See also
- Microsoft SQL Server
- Adaptive Server Enterprise
- SQL
- Sqsh - open source Transact-SQL client