TSQL
   HOME

TheInfoList



OR:

Transact-SQL (T-SQL) is
Microsoft Microsoft Corporation is an American multinational technology corporation producing computer software, consumer electronics, personal computers, and related services headquartered at the Microsoft Redmond campus located in Redmond, Washing ...
's and
Sybase Sybase, Inc. was an enterprise software and services company. The company produced software to manage and analyze information in relational databases, with facilities located in California and Massachusetts. Sybase was acquired by SAP in 2010; ...
's proprietary extension to the SQL (Structured Query Language) used to interact with
relational database A relational database is a (most commonly digital) database based on the relational model of data, as proposed by E. F. Codd in 1970. A system used to maintain relational databases is a relational database management system (RDBMS). Many relatio ...
s. T-SQL expands on the SQL standard to include
procedural programming Procedural programming is a programming paradigm, derived from imperative programming, based on the concept of the ''procedure call''. Procedures (a type of routine or subroutine) simply contain a series of computational steps to be carried ...
,
local variable In computer science, a local variable is a Variable (programming), variable that is given ''local scope (programming), scope''. A local variable reference in the subroutine, function or block (programming), block in which it is declared overrides ...
s, various support functions for string processing, date processing, mathematics, etc. and changes to the DELETE and UPDATE statements. Transact-SQL is central to using
Microsoft SQL Server Microsoft SQL Server is a relational database management system developed by Microsoft. As a database server, it is a software product with the primary function of storing and retrieving data as requested by other software applications—which ma ...
. 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.
Stored procedure A stored procedure (also termed proc, storp, sproc, StoPro, StoredProc, StoreProc, sp, or SP) is a subroutine available to applications that access a relational database management system (RDBMS). Such procedures are stored in the database data dic ...
s in SQL Server are executable server-side routines. The advantage of stored procedures is the ability to pass parameters.


Variables

Transact-SQL provides the following statements to declare and set local variables: DECLARE, SET and SELECT. DECLARE @var1 NVARCHAR(30); SET @var1 = 'somya som'; SELECT @var1 = Name FROM Sales.Store WHERE CustomerID = 100;


Flow control

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 "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 @@DATEFIRST setting.) 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 In computer programming, a block or code block or block of code is a lexical structure of source code which is grouped together. Blocks consist of one or more declarations and statements. A programming language that permits the creation of bl ...
. 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 on the weekend!'; END; ELSE BEGIN PRINT 'It is a weekday.'; PRINT 'Get to work on a weekday!'; 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 A stored procedure (also termed proc, storp, sproc, StoPro, StoredProc, StoreProc, sp, or SP) is a subroutine available to applications that access a relational database management system (RDBMS). Such procedures are stored in the database data dic ...
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. 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 DELETE and UPDATE statements are enhanced to enable data from another table to be used in the operation, without needing a subquery: * DELETE accepts joined tables in the FROM clause, similarly to SELECT. When this is done, the name or alias of which table in the join is to be deleted from is placed between DELETE and FROM. * UPDATE allows a FROM clause to be added. The table to be updated can be either joined in the FROM clause and referenced by alias, or referenced only at the start of the statement as per standard SQL. This example deletes all users who 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 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 availabl
in MSDN


TRY CATCH

Beginning with SQL Server 2005,"T-SQL Improvements in SQL Server 2012"
Jonathan Allen on Mar 19, 2012, infoq.com Microsoft introduced additional TRY CATCH logic 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 -- roll back the transaction because of error ROLLBACK TRAN; END CATCH;


See also

* Adaptive Server Enterprise (Sybase) * PL/SQL (Oracle) * PL/pgSQL (PostgreSQL) *
SQL/PSM SQL/PSM ( SQL/Persistent Stored Modules) is an ISO standard mainly defining an extension of SQL with a procedural language for use in stored procedures. Initially published in 1996 as an extension of SQL-92 (ISO/IEC 9075-4:1996, a version sometime ...
(ISO standard) *
Tabular Data Stream Tabular Data Stream (TDS) is an application layer Protocol (computing), protocol used to transfer data between a database server and a client. It was initially designed and developed by Sybase Inc. for their Sybase SQL Server relational database eng ...


References

{{reflist


External links


Transact-SQL Reference

Transact-SQL Tutorial
SQL Data-centric programming languages