- Transact-SQL
Transact-SQL (T-SQL) is
Microsoft 's andSybase 's proprietary extension to theSQL . Microsoft's implementation ships in theMicrosoft SQL Server product. Sybase uses the language in itsAdaptive Server Enterprise , the successor to Sybase SQL Server.Transact-SQL enhances SQL with these additional features:
* Control-of-flow language
*Local variable s
* Various support functions for string processing, date processing, mathematics, etc.
* Improvements to DELETE and UPDATE statementsFlow control
Keywords for flow control in Transact-SQL include
BEGIN
andEND
,BREAK
,CONTINUE
,GOTO
,IF
andELSE
,RETURN
,WAITFOR
, andWHILE
.IF
andELSE
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.BEGIN
andEND
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: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 astored procedure or function.BREAK
ends the enclosingWHILE
loop, whileCONTINUE
causes the next iteration of the loop to execute. An example of aWHILE
loop is given below.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.The body of the loop will print a message including the value of the variable, and then decrement the counter.
A variable can be initialized as the result of a statement, like this:
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.
Changes 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.
This example deletes all
users
who have been flagged with the 'Idle' flag.BULK INSERT
BULK INSERT is a Transact-SQL statement that implements a bulk data-loading process, inserting multiple rows into a table, reading data from an external sequential file. Use of BULK INSERT results in better performance than processes that issue individual INSERT statements for each row to be added. Additional details are available on [http://msdn2.microsoft.com/en-us/library/ms188365.aspx Microsoft's MSDN page] .
ee also
*Adaptive Server Enterprise (Sybase)
*PL/SQL (Oracle)
*SQL (ANSI)
*SQL Server (Microsoft)External links
* [http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.ase_15.0.sqlug/html/sqlug/title.htm Sybase Transact-SQL User's Guide]
* [http://msdn2.microsoft.com/en-us/library/aa260642(SQL.80).aspx Transact-SQL Reference for SQL Server 2000 (MSDN)]
* [http://msdn2.microsoft.com/en-us/library/ms189826.aspx Transact-SQL Reference for SQL Server 2005 (MSDN)]
* [http://msdn.microsoft.com/en-us/library/bb510741(SQL.100).aspx Transact-SQL Reference for SQL Server 2008 (MSDN)]
* [http://www.dotnet4all.com/snippets/2008/04/factsheet-for-sql-server-developers.html T-SQL factsheet for developers] (PDF)
Wikimedia Foundation. 2010.