0% found this document useful (0 votes)
39 views

Transact-SQL: Transact-SQL (T-SQL) Is Microsoft's and Sybase's Proprietary Extension To SQL. SQL, The Acronym

Transact-SQL

Uploaded by

ddcv
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Transact-SQL: Transact-SQL (T-SQL) Is Microsoft's and Sybase's Proprietary Extension To SQL. SQL, The Acronym

Transact-SQL

Uploaded by

ddcv
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

11/5/2014

Transact-SQL - Wikipedia, the free encyclopedia

Transact-SQL
From Wikipedia, the free encyclopedia

Transact-SQL (T-SQL) is Microsoft's and Sybase's proprietary extension to SQL. SQL, the acronym
for Structured Query Language, is a standardized computer language that was originally developed by
IBM for querying, altering and defining relational databases, using declarative statements. T-SQL
expands on the SQL standard to include procedural programming, local variables, various support
functions for string processing, date processing, mathematics, etc. and changes to the DELETE and
UPDATE statements. These additional features make Transact-SQL Turing complete.
Transact-SQL is central to using Microsoft SQL Server. All applications that communicate with an
instance of SQL Server do so by sending Transact-SQL statements to the server, regardless of the user
interface of the application.

Contents
1 Variables
2 Changes to DELETE and UPDATE statements
3 BULK INSERT
4 TRY CATCH
5 See also
6 References
7 External links

Variables
Keywords for flow control in Transact-SQL include BEGINand END, BREAK, CONTINUE, GOTO, IFand
ELSE, RETURN, WAITFOR, and WHILE.
IFand ELSEallow

conditional execution. This batch statement will print "It is the weekend" if the
current date is a weekend day, or "It is a weekday" if the current date is a weekday. (Note: This code
assumes that Sunday is configured as the first day of the week in the @@DATEFIRSTsetting.)

IF DATEPART(dw, GETDATE()) = 7 OR DATEPART(dw, GETDATE()) = 1


PRINT 'It is the weekend.'
ELSE
PRINT 'It is a weekday.'

BEGINand ENDmark

a block of statements. If more than one statement is to be controlled by the


conditional in the example above, we can use BEGINand ENDlike this:

IF DATEPART(dw, GETDATE()) = 7 OR DATEPART(dw, GETDATE()) = 1


http://en.wikipedia.org/wiki/Transact-SQL

1/4

11/5/2014

Transact-SQL - Wikipedia, the free encyclopedia

BEGIN
PRINT 'It is the weekend.'
PRINT 'Get some rest on the weekend!'
END
ELSE
BEGIN
PRINT 'It is a weekday.'
PRINT 'Get to work on a weekday!'
END

WAITFORwill

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.
RETURNis

used to immediately return from a stored procedure or function.

BREAKends

the enclosing WHILEloop, while CONTINUEcauses the next iteration of the loop to execute.
An example of a WHILEloop is given below.

DECLARE @i INT
SET @i = 0
WHILE @i < 5
BEGIN
PRINT 'Hello world.'
SET @i = @i + 1
END

Changes to DELETE and UPDATE statements


In Transact-SQL, both the DELETEand UPDATEstatements allow a FROMclause to be added, which allows
joins to be included.
This example deletes all userswho have been flagged with the 'Idle' flag.

DELETE u
FROM users AS u
INNER JOIN user_flags AS f
ON u.id = f.id
WHERE f.name = 'idle'

BULK INSERT
BULK INSERTis

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 INSERTresults in
better performance than processes that issue individual INSERTstatements for each row to be added.
Additional details are available in MSDN (http://msdn2.microsoft.com/en-us/library/ms188365.aspx).

TRY CATCH

http://en.wikipedia.org/wiki/Transact-SQL

2/4

11/5/2014

Transact-SQL - Wikipedia, the free encyclopedia

Beginning with SQL Server 2005, Microsoft introduced additional TRY CATCHlogic to support exception
type behaviour. This behaviour enables developers to simplify their code and leave out @@ERROR
checking after each SQL execution statement.

-- begin transaction
BEGIN TRAN
BEGIN TRY
-- execute each statement
INSERT INTO MYTABLE(NAME) VALUES ('ABC')
INSERT INTO MYTABLE(NAME) VALUES ('123')
-- commit the transaction
COMMIT TRAN
END TRY
BEGIN CATCH
-- rollback the transaction because of error
ROLLBACK TRAN
END CATCH

See also
Adaptive Server Enterprise (Sybase)
PL/SQL (Oracle)
PL/pgSQL (PostgreSQL)
SQL/PSM (ISO standard)
Sys.sysobjects

References
External links
Sybase Transact-SQL User's Guide (http://infocenter.sybase.com/help/index.jsp?
topic=/com.sybase.help.ase_15.0.sqlug/html/sqlug/title.htm)
Transact-SQL Reference for SQL Server 2000 (MSDN) (http://msdn2.microsoft.com/enus/library/aa260642(SQL.80).aspx)
Transact-SQL Reference for SQL Server 2005 (MSDN) (http://msdn2.microsoft.com/enus/library/ms189826.aspx)
Transact-SQL Reference for SQL Server 2008 (MSDN) (http://msdn.microsoft.com/enus/library/bb510741(SQL.100).aspx)
Transact-SQL Reference for SQL Server 2012 (MSDN) (http://msdn.microsoft.com/enus/library/bb510741.aspx)
Transact-SQL Tutorial (http://www.tsql.info)
Retrieved from "http://en.wikipedia.org/w/index.php?title=Transact-SQL&oldid=627639939"
http://en.wikipedia.org/wiki/Transact-SQL

3/4

11/5/2014

Transact-SQL - Wikipedia, the free encyclopedia

Categories: SQL Data-centric programming languages


This page was last modified on 30 September 2014 at 05:10.
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms
may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia is a
registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.

http://en.wikipedia.org/wiki/Transact-SQL

4/4

You might also like