<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Transact-SQL</id>
	<title>Transact-SQL - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Transact-SQL"/>
	<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Transact-SQL&amp;action=history"/>
	<updated>2026-04-18T05:52:53Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://wiki.sarg.dev/index.php?title=Transact-SQL&amp;diff=191491&amp;oldid=prev</id>
		<title>imported&gt;Smjg: /* Changes to DELETE and UPDATE statements */ don&#039;t really need all these line breaks, and indentation didn&#039;t match the code structure</title>
		<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Transact-SQL&amp;diff=191491&amp;oldid=prev"/>
		<updated>2023-06-08T10:07:24Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Changes to DELETE and UPDATE statements: &lt;/span&gt; don&amp;#039;t really need all these line breaks, and indentation didn&amp;#039;t match the code structure&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{short description|Microsoft&amp;#039;s and Sybase&amp;#039;s proprietary extension to Structured Query Language}}&lt;br /&gt;
{{redirect|TSQL|the proposed temporal extension of SQL|TSQL2}}&lt;br /&gt;
{{multiple issues|&lt;br /&gt;
{{Refimprove|date=August 2017}}&lt;br /&gt;
{{missing information|the history of T-SQL|date=November 2022}}&lt;br /&gt;
}}&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Transact-SQL&amp;#039;&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;T-SQL&amp;#039;&amp;#039;&amp;#039;) is [[Microsoft]]&amp;#039;s and [[Sybase]]&amp;#039;s proprietary extension to the [[SQL]] (Structured Query Language) used to interact with [[relational database]]s. T-SQL expands on the SQL standard to include [[procedural programming]], [[local variable]]s, various support functions for string processing, date processing, mathematics, etc. and changes to the [[Delete (SQL)|DELETE]] and [[update (SQL)|UPDATE]] statements.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
[[Stored procedure]]s in SQL Server are executable server-side routines. The advantage of stored procedures is the ability to pass parameters.&lt;br /&gt;
&lt;br /&gt;
==Variables==&lt;br /&gt;
Transact-SQL provides the following statements to declare and set local variables: &amp;lt;code&amp;gt;DECLARE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;SET&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;SELECT&amp;lt;/code&amp;gt;.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;tsql&amp;quot;&amp;gt;&lt;br /&gt;
DECLARE @var1 NVARCHAR(30);&lt;br /&gt;
SET @var1 = &amp;#039;Some Name&amp;#039;;&lt;br /&gt;
SELECT @var1 = Name&lt;br /&gt;
  FROM Sales.Store&lt;br /&gt;
  WHERE CustomerID = 100;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Flow control==&lt;br /&gt;
Keywords for flow control in Transact-SQL include &amp;lt;code&amp;gt;BEGIN&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;END&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;BREAK&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;CONTINUE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;GOTO&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;IF&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ELSE&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;RETURN&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;WAITFOR&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;WHILE&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;IF&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;ELSE&amp;lt;/code&amp;gt; allow conditional execution. This batch statement will print &amp;quot;It is the weekend&amp;quot; if the current date is a weekend day, or &amp;quot;It is a weekday&amp;quot; if the current date is a weekday.  (Note: This code assumes that Sunday is configured as the first day of the week in the &amp;lt;code&amp;gt;@@DATEFIRST&amp;lt;/code&amp;gt; setting.)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;tsql&amp;quot;&amp;gt;&lt;br /&gt;
IF DATEPART(dw, GETDATE()) = 7 OR DATEPART(dw, GETDATE()) = 1&lt;br /&gt;
   PRINT &amp;#039;It is the weekend.&amp;#039;;&lt;br /&gt;
ELSE&lt;br /&gt;
   PRINT &amp;#039;It is a weekday.&amp;#039;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;BEGIN&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;END&amp;lt;/code&amp;gt; mark a [[block of statements]]. If more than one statement is to be controlled by the conditional in the example above, we can use &amp;lt;code&amp;gt;BEGIN&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;END&amp;lt;/code&amp;gt; like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;tsql&amp;quot;&amp;gt;&lt;br /&gt;
IF DATEPART(dw, GETDATE()) = 7 OR DATEPART(dw, GETDATE()) = 1&lt;br /&gt;
BEGIN&lt;br /&gt;
   PRINT &amp;#039;It is the weekend.&amp;#039;;&lt;br /&gt;
   PRINT &amp;#039;Get some rest on the weekend!&amp;#039;;&lt;br /&gt;
END;&lt;br /&gt;
ELSE&lt;br /&gt;
BEGIN&lt;br /&gt;
   PRINT &amp;#039;It is a weekday.&amp;#039;;&lt;br /&gt;
   PRINT &amp;#039;Get to work on a weekday!&amp;#039;;&lt;br /&gt;
END;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;WAITFOR&amp;lt;/code&amp;gt; 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.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;RETURN&amp;lt;/code&amp;gt; is used to immediately return from a [[stored procedure]] or function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;BREAK&amp;lt;/code&amp;gt; ends the enclosing &amp;lt;code&amp;gt;WHILE&amp;lt;/code&amp;gt; loop, while &amp;lt;code&amp;gt;CONTINUE&amp;lt;/code&amp;gt; causes the next iteration of the loop to execute. An example of a &amp;lt;code&amp;gt;WHILE&amp;lt;/code&amp;gt; loop is given below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;tsql&amp;quot;&amp;gt;&lt;br /&gt;
DECLARE @i INT;&lt;br /&gt;
SET @i = 0;&lt;br /&gt;
&lt;br /&gt;
WHILE @i &amp;lt; 5&lt;br /&gt;
BEGIN&lt;br /&gt;
   PRINT &amp;#039;Hello world.&amp;#039;;&lt;br /&gt;
   SET @i = @i + 1;&lt;br /&gt;
END;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Changes to DELETE and UPDATE statements==&lt;br /&gt;
In Transact-SQL, both the &amp;lt;code&amp;gt;DELETE&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;UPDATE&amp;lt;/code&amp;gt; statements are enhanced to enable data from another table to be used in the operation, without needing a subquery:&lt;br /&gt;
* &amp;lt;code&amp;gt;DELETE&amp;lt;/code&amp;gt; accepts joined tables in the &amp;lt;code&amp;gt;FROM&amp;lt;/code&amp;gt; clause, similarly to &amp;lt;code&amp;gt;SELECT&amp;lt;/code&amp;gt;.  When this is done, the name or alias of which table in the join is to be deleted from is placed between &amp;lt;code&amp;gt;DELETE&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;FROM&amp;lt;/code&amp;gt;.&lt;br /&gt;
* &amp;lt;code&amp;gt;UPDATE&amp;lt;/code&amp;gt; allows a &amp;lt;code&amp;gt;FROM&amp;lt;/code&amp;gt; clause to be added.  The table to be updated can be either joined in the &amp;lt;code&amp;gt;FROM&amp;lt;/code&amp;gt; clause and referenced by alias, or referenced only at the start of the statement as per standard SQL.&lt;br /&gt;
&lt;br /&gt;
This example deletes all {{code|users}} who have been flagged in the {{code|user_flags}} table with the &amp;#039;idle&amp;#039; flag.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;tsql&amp;quot;&amp;gt;&lt;br /&gt;
DELETE u&lt;br /&gt;
FROM users AS u INNER JOIN user_flags AS f ON u.id = f.id&lt;br /&gt;
WHERE f.name = &amp;#039;idle&amp;#039;;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==BULK INSERT==&lt;br /&gt;
&amp;lt;code&amp;gt;BULK&amp;lt;/code&amp;gt; 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 &amp;lt;code&amp;gt;BULK INSERT&amp;lt;/code&amp;gt; results in better performance than processes that issue individual &amp;lt;code&amp;gt;INSERT&amp;lt;/code&amp;gt; statements for each row to be added. Additional details are available [http://msdn2.microsoft.com/en-us/library/ms188365.aspx in MSDN].&lt;br /&gt;
&lt;br /&gt;
==TRY CATCH==&lt;br /&gt;
Beginning with SQL Server 2005,&amp;lt;ref&amp;gt;[https://www.infoq.com/news/2012/03/T-SQL-2012 &amp;quot;T-SQL Improvements in SQL Server 2012&amp;quot;], Jonathan Allen on Mar 19, 2012, infoq.com&amp;lt;/ref&amp;gt; Microsoft introduced additional &amp;lt;code&amp;gt;TRY CATCH&amp;lt;/code&amp;gt; logic to support exception type behaviour.  This behaviour enables developers to simplify their code and leave out &amp;lt;code&amp;gt;@@ERROR&amp;lt;/code&amp;gt; checking after each SQL execution statement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;sql&amp;quot;&amp;gt;&lt;br /&gt;
-- begin transaction&lt;br /&gt;
BEGIN TRAN;&lt;br /&gt;
&lt;br /&gt;
BEGIN TRY&lt;br /&gt;
   -- execute each statement&lt;br /&gt;
   INSERT INTO MYTABLE(NAME) VALUES (&amp;#039;ABC&amp;#039;);&lt;br /&gt;
   INSERT INTO MYTABLE(NAME) VALUES (&amp;#039;123&amp;#039;);&lt;br /&gt;
&lt;br /&gt;
   -- commit the transaction&lt;br /&gt;
   COMMIT TRAN;&lt;br /&gt;
END TRY&lt;br /&gt;
BEGIN CATCH&lt;br /&gt;
   -- roll back the transaction because of error&lt;br /&gt;
   ROLLBACK TRAN;&lt;br /&gt;
END CATCH;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[[Adaptive Server Enterprise|Adaptive Server Enterprise (Sybase)]]&lt;br /&gt;
*[[PL/SQL|PL/SQL (Oracle)]]&lt;br /&gt;
*[[PL/pgSQL|PL/pgSQL (PostgreSQL)]]&lt;br /&gt;
*[[SQL/PSM]] (ISO standard)&lt;br /&gt;
*[[Tabular Data Stream]]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
{{reflist}}&lt;br /&gt;
&lt;br /&gt;
==External links==&lt;br /&gt;
* [https://docs.microsoft.com/en-us/sql/t-sql/language-reference Transact-SQL Reference]&lt;br /&gt;
* [http://www.tsql.info Transact-SQL Tutorial]&lt;br /&gt;
&lt;br /&gt;
[[Category:SQL]]&lt;br /&gt;
[[Category:Data-centric programming languages]]&lt;/div&gt;</summary>
		<author><name>imported&gt;Smjg</name></author>
	</entry>
</feed>