<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Steve Kass : T-SQL language extensions, T-SQL, nonstandard</title><link>http://sqlblog.com/blogs/steve_kass/archive/tags/T-SQL+language+extensions/T-SQL/nonstandard/default.aspx</link><description>Tags: T-SQL language extensions, T-SQL, nonstandard</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>DELETE FROM Where?</title><link>http://sqlblog.com/blogs/steve_kass/archive/2009/04/30/delete-from-where.aspx</link><pubDate>Thu, 30 Apr 2009 21:48:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:13678</guid><dc:creator>skass</dc:creator><slash:comments>10</slash:comments><comments>http://sqlblog.com/blogs/steve_kass/comments/13678.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/steve_kass/commentrss.aspx?PostID=13678</wfw:commentRss><description>&lt;P&gt;&lt;SPAN style="WIDOWS:2;TEXT-TRANSFORM:none;TEXT-INDENT:0px;BORDER-COLLAPSE:separate;FONT:16px 'Times New Roman';WHITE-SPACE:normal;ORPHANS:2;LETTER-SPACING:normal;WORD-SPACING:0px;-webkit-text-size-adjust:auto;-webkit-border-horizontal-spacing:0px;-webkit-border-vertical-spacing:0px;-webkit-text-decorations-in-effect:none;-webkit-text-stroke-width:0px;" class=Apple-style-span&gt;&lt;SPAN style="TEXT-ALIGN:left;FONT-FAMILY:Verdana;FONT-SIZE:11px;" class=Apple-style-span&gt;For years, SQL Server has supported a (second) FROM clause in UPDATE and DELETE statements. Its behavior isn't always deterministic, a fact Microsoft points out in the documentation. &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="WIDOWS:2;TEXT-TRANSFORM:none;TEXT-INDENT:0px;BORDER-COLLAPSE:separate;FONT:16px 'Times New Roman';WHITE-SPACE:normal;ORPHANS:2;LETTER-SPACING:normal;WORD-SPACING:0px;-webkit-text-size-adjust:auto;-webkit-border-horizontal-spacing:0px;-webkit-border-vertical-spacing:0px;-webkit-text-decorations-in-effect:none;-webkit-text-stroke-width:0px;" class=Apple-style-span&gt;&lt;SPAN style="TEXT-ALIGN:left;FONT-FAMILY:Verdana;FONT-SIZE:11px;" class=Apple-style-span&gt;Today, someone was surprised by&amp;nbsp;the basic semantics of DELETE .. FROM.&amp;nbsp;This probably happens a lot, but fortunately today's surprisee&amp;nbsp;posted &lt;A title="Connect item #436328" href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=436328"&gt;this Connect item&lt;/A&gt; because the following&amp;nbsp;statement didn't do what the documentation seemed to say it would:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;SPAN style="WIDOWS:2;TEXT-TRANSFORM:none;TEXT-INDENT:0px;BORDER-COLLAPSE:separate;FONT:16px 'Times New Roman';WHITE-SPACE:normal;ORPHANS:2;LETTER-SPACING:normal;WORD-SPACING:0px;-webkit-text-size-adjust:auto;-webkit-border-horizontal-spacing:0px;-webkit-border-vertical-spacing:0px;-webkit-text-decorations-in-effect:none;-webkit-text-stroke-width:0px;" class=Apple-style-span&gt;&lt;SPAN style="TEXT-ALIGN:left;FONT-FAMILY:Verdana;FONT-SIZE:11px;" class=Apple-style-span&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;DELETE&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;FROM&lt;/FONT&gt; t2&lt;BR&gt;&lt;FONT color=#0000ff&gt;FROM&lt;/FONT&gt; t1 &lt;FONT color=#0000ff&gt;AS&lt;/FONT&gt; t2&lt;/P&gt;
&lt;P&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="WIDOWS:2;TEXT-TRANSFORM:none;TEXT-INDENT:0px;BORDER-COLLAPSE:separate;FONT:16px 'Times New Roman';WHITE-SPACE:normal;ORPHANS:2;LETTER-SPACING:normal;WORD-SPACING:0px;-webkit-text-size-adjust:auto;-webkit-border-horizontal-spacing:0px;-webkit-border-vertical-spacing:0px;-webkit-text-decorations-in-effect:none;-webkit-text-stroke-width:0px;" class=Apple-style-span&gt;&lt;SPAN style="TEXT-ALIGN:left;FONT-FAMILY:Verdana;FONT-SIZE:11px;" class=Apple-style-span&gt;Quick now, what table does this truncate?&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="WIDOWS:2;TEXT-TRANSFORM:none;TEXT-INDENT:0px;BORDER-COLLAPSE:separate;FONT:16px 'Times New Roman';WHITE-SPACE:normal;ORPHANS:2;LETTER-SPACING:normal;WORD-SPACING:0px;-webkit-text-size-adjust:auto;-webkit-border-horizontal-spacing:0px;-webkit-border-vertical-spacing:0px;-webkit-text-decorations-in-effect:none;-webkit-text-stroke-width:0px;" class=Apple-style-span&gt;&lt;SPAN style="TEXT-ALIGN:left;FONT-FAMILY:Verdana;FONT-SIZE:11px;" class=Apple-style-span&gt;You're in good company if you said t2, which is the wrong answer. &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="WIDOWS:2;TEXT-TRANSFORM:none;TEXT-INDENT:0px;BORDER-COLLAPSE:separate;FONT:16px 'Times New Roman';WHITE-SPACE:normal;ORPHANS:2;LETTER-SPACING:normal;WORD-SPACING:0px;-webkit-text-size-adjust:auto;-webkit-border-horizontal-spacing:0px;-webkit-border-vertical-spacing:0px;-webkit-text-decorations-in-effect:none;-webkit-text-stroke-width:0px;" class=Apple-style-span&gt;&lt;SPAN style="TEXT-ALIGN:left;FONT-FAMILY:Verdana;FONT-SIZE:11px;" class=Apple-style-span&gt;The statement truncates t1, however, not t2. In fact, this &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="WIDOWS:2;TEXT-TRANSFORM:none;TEXT-INDENT:0px;BORDER-COLLAPSE:separate;FONT:16px 'Times New Roman';WHITE-SPACE:normal;ORPHANS:2;LETTER-SPACING:normal;WORD-SPACING:0px;-webkit-text-size-adjust:auto;-webkit-border-horizontal-spacing:0px;-webkit-border-vertical-spacing:0px;-webkit-text-decorations-in-effect:none;-webkit-text-stroke-width:0px;" class=Apple-style-span&gt;&lt;SPAN style="TEXT-ALIGN:left;FONT-FAMILY:Verdana;FONT-SIZE:11px;" class=Apple-style-span&gt;DELETE statement contains no reference to the table t2, assuming there even is one. Here, t2 is only an alias, and the statement is semantically identical to&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;DELETE&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;FROM&lt;/FONT&gt; ThisIsAnAliasNotATable&lt;BR&gt;&lt;FONT color=#0000ff&gt;FROM&lt;/FONT&gt; t1 &lt;FONT color=#0000ff&gt;AS&lt;/FONT&gt; ThisIsAnAliasNotATable&lt;/P&gt;
&lt;P&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="WIDOWS:2;TEXT-TRANSFORM:none;TEXT-INDENT:0px;BORDER-COLLAPSE:separate;FONT:16px 'Times New Roman';WHITE-SPACE:normal;ORPHANS:2;LETTER-SPACING:normal;WORD-SPACING:0px;-webkit-text-size-adjust:auto;-webkit-border-horizontal-spacing:0px;-webkit-border-vertical-spacing:0px;-webkit-text-decorations-in-effect:none;-webkit-text-stroke-width:0px;" class=Apple-style-span&gt;&lt;SPAN style="TEXT-ALIGN:left;FONT-FAMILY:Verdana;FONT-SIZE:11px;" class=Apple-style-span&gt;When there are two FROM clauses in a DELETE statement, the first FROM clause is interpreted in the context of the table source defined by the second one, which is analogous to how SELECT works. The following query selects rows from t1, not t2, and that's no surprise:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;SPAN style="WIDOWS:2;TEXT-TRANSFORM:none;TEXT-INDENT:0px;BORDER-COLLAPSE:separate;FONT:16px 'Times New Roman';WHITE-SPACE:normal;ORPHANS:2;LETTER-SPACING:normal;WORD-SPACING:0px;-webkit-text-size-adjust:auto;-webkit-border-horizontal-spacing:0px;-webkit-border-vertical-spacing:0px;-webkit-text-decorations-in-effect:none;-webkit-text-stroke-width:0px;" class=Apple-style-span&gt;&lt;SPAN style="TEXT-ALIGN:left;FONT-FAMILY:Verdana;FONT-SIZE:11px;" class=Apple-style-span&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;SELECT&lt;/FONT&gt; t2&lt;FONT color=#808080&gt;.&lt;/FONT&gt;a&lt;BR&gt;&lt;FONT color=#0000ff&gt;FROM&lt;/FONT&gt; t1 &lt;FONT color=#0000ff&gt;AS&lt;/FONT&gt; t2&lt;/P&gt;
&lt;P&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="WIDOWS:2;TEXT-TRANSFORM:none;TEXT-INDENT:0px;BORDER-COLLAPSE:separate;FONT:16px 'Times New Roman';WHITE-SPACE:normal;ORPHANS:2;LETTER-SPACING:normal;WORD-SPACING:0px;-webkit-text-size-adjust:auto;-webkit-border-horizontal-spacing:0px;-webkit-border-vertical-spacing:0px;-webkit-text-decorations-in-effect:none;-webkit-text-stroke-width:0px;" class=Apple-style-span&gt;&lt;SPAN style="TEXT-ALIGN:left;FONT-FAMILY:Verdana;FONT-SIZE:11px;" class=Apple-style-span&gt;&lt;SPAN style="WIDOWS:2;TEXT-TRANSFORM:none;TEXT-INDENT:0px;BORDER-COLLAPSE:separate;FONT:16px 'Times New Roman';WHITE-SPACE:normal;ORPHANS:2;LETTER-SPACING:normal;WORD-SPACING:0px;-webkit-text-size-adjust:auto;-webkit-border-horizontal-spacing:0px;-webkit-border-vertical-spacing:0px;-webkit-text-decorations-in-effect:none;-webkit-text-stroke-width:0px;" class=Apple-style-span&gt;&lt;SPAN style="TEXT-ALIGN:left;FONT-FAMILY:Verdana;FONT-SIZE:11px;" class=Apple-style-span&gt;If you think about it, you should&amp;nbsp;agree that different behavior for DELETE would be very bad. If SQL Server produced what Vitaliy expected, someone could create tables with names used somewhere as aliases, and cause UPDATE and DELETE statements to stop updating the tables they were supposed to update. &lt;/SPAN&gt;&lt;/SPAN&gt;What makes the DELETE behavior more confusing than the SELECT behavior is the fact that in the case of DELETE, the keyword FROM can be used twice. It you write DELETE .. FROM statements with two FROMs,&amp;nbsp;remember that the bottom FROM clause is the "outer" one in the sense of scope. It should be considered first, as is clearer in this DELETE statement, which is equivalent to the one that suprised Vitaliy:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;SPAN style="WIDOWS:2;TEXT-TRANSFORM:none;TEXT-INDENT:0px;BORDER-COLLAPSE:separate;FONT:16px 'Times New Roman';WHITE-SPACE:normal;ORPHANS:2;LETTER-SPACING:normal;WORD-SPACING:0px;-webkit-text-size-adjust:auto;-webkit-border-horizontal-spacing:0px;-webkit-border-vertical-spacing:0px;-webkit-text-decorations-in-effect:none;-webkit-text-stroke-width:0px;" class=Apple-style-span&gt;&lt;SPAN style="TEXT-ALIGN:left;FONT-FAMILY:Verdana;FONT-SIZE:11px;" class=Apple-style-span&gt;&lt;FONT color=#0000ff&gt;
&lt;P&gt;WITH&lt;/FONT&gt; t2 &lt;FONT color=#0000ff&gt;AS &lt;/FONT&gt;&lt;FONT color=#808080&gt;(&lt;BR&gt;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;SELECT&lt;/FONT&gt;&amp;nbsp;* &lt;FONT color=#0000ff&gt;FROM&lt;/FONT&gt; t1&lt;BR&gt;&lt;FONT color=#808080&gt;)&lt;BR&gt;&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#0000ff&gt;DELETE&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;FROM&lt;/FONT&gt; t2&lt;/P&gt;
&lt;P&gt;For the record, this CTE-based DELETE is just as non-standard as the DELETE .. FROM, but it's less confusing. If you can't easily (or effeciently) avoid the use of DELETE .. FROM, consider rewriting DELETE .. FROM as a CTE to reduce the chance of confusion.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="WIDOWS:2;TEXT-TRANSFORM:none;TEXT-INDENT:0px;BORDER-COLLAPSE:separate;FONT:16px 'Times New Roman';WHITE-SPACE:normal;ORPHANS:2;LETTER-SPACING:normal;WORD-SPACING:0px;-webkit-text-size-adjust:auto;-webkit-border-horizontal-spacing:0px;-webkit-border-vertical-spacing:0px;-webkit-text-decorations-in-effect:none;-webkit-text-stroke-width:0px;" class=Apple-style-span&gt;&lt;SPAN style="TEXT-ALIGN:left;FONT-FAMILY:Verdana;FONT-SIZE:11px;" class=Apple-style-span&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=13678" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/steve_kass/archive/tags/Connect/default.aspx">Connect</category><category domain="http://sqlblog.com/blogs/steve_kass/archive/tags/DELETE/default.aspx">DELETE</category><category domain="http://sqlblog.com/blogs/steve_kass/archive/tags/nonstandard/default.aspx">nonstandard</category><category domain="http://sqlblog.com/blogs/steve_kass/archive/tags/T-SQL/default.aspx">T-SQL</category><category domain="http://sqlblog.com/blogs/steve_kass/archive/tags/T-SQL+language+extensions/default.aspx">T-SQL language extensions</category></item></channel></rss>