﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / When can I use an alias? / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 20 Jun 2013 01:17:30 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: When can I use an alias?</title><link>http://www.sqlservercentral.com/Forums/Topic1408100-392-1.aspx</link><description>Your attached query has a similar problem.  The optional FROM clause in a SELECT statement is the same as in a DELETE statement.  1)  The FROM keyword is required2)  The object is required3)  The alias is optionalYour EXISTS subquery is missing the required object in the FROM clause.  You can easily fix this by moving your derived table x to the EXISTS subquery.Drew</description><pubDate>Thu, 17 Jan 2013 10:05:19 GMT</pubDate><dc:creator>drew.allen</dc:creator></item><item><title>RE: When can I use an alias?</title><link>http://www.sqlservercentral.com/Forums/Topic1408100-392-1.aspx</link><description>[quote][b]grevesz (1/16/2013)[/b][hr]1. Why is the X necessary after the DELETE key word? Wouldn't [b]DELETE FROM demodelete x WHERE x.somenum = 1;[/b] work? If not, why?[/quote]The DELETE statement consists of six parts:1)  An optional CTE2)  A required DELETE clause3)  An optional OUTPUT clause4)  An optional FROM clause5)  An optional WHERE clause6)  An optional OPTION clauseThe DELETE clause of the DELETE statement has three parts1)  Required DELETE keyword2)  Optional FROM keyword3)  Required source    a)  Object    b)  AliasNote that the source is either an object or an alias, but not both.The simplest optional FROM clause contains three parts1)  Required FROM keyword2)  Required object name3)  Optional aliasSo there are two different sources for the FROM keyword in a DELETE statement:1)  The optional FROM in the required DELETE clause2)  The required FROM in the optional FROM clauseThe DELETE clause can use an alias that is defined in the FROM clause, but it cannot define it's own alias, which is what you are trying to do.  You're confusing the two different FROM keywords.  In other words, your attempted query can be parsed in one of two ways (both invalid).[code="sql"]DELETE FROM demodelete x WHERE x.somenum = 1;DELETE &amp;lt;missing required source&amp;gt;FROM demodelete xWHERE x.somenum = 1;DELETE FROM demodelete&amp;lt;missing required FROM&amp;gt; &amp;lt;missing required object&amp;gt; xWHERE x.somenum = 1;[/code]Perhaps including all of the optional FROM keywords will help it make sense.[code="sql"]DELETE FROM xFROM demodelete xWHERE x.somenum = 1;[/code]Drew</description><pubDate>Thu, 17 Jan 2013 09:51:29 GMT</pubDate><dc:creator>drew.allen</dc:creator></item><item><title>RE: When can I use an alias?</title><link>http://www.sqlservercentral.com/Forums/Topic1408100-392-1.aspx</link><description>The X isn't necessary.  You asked about delete statements not working with aliases.  That is merely a demo showing that aliases do in fact work with delete statements.As for your query, attached as a download document I won't download it.  Paste your query into a paste instead of as an attachment (that must be downloaded).</description><pubDate>Wed, 16 Jan 2013 20:33:16 GMT</pubDate><dc:creator>SQLRNNR</dc:creator></item><item><title>RE: When can I use an alias?</title><link>http://www.sqlservercentral.com/Forums/Topic1408100-392-1.aspx</link><description>Hello,Thanks for the response. However, I still have two questions:1. Why is the X necessary after the DELETE key word? Wouldn't [b]DELETE FROM demodelete x WHERE x.somenum = 1;[/b] work? If not, why?2. What is wrong with the usage of the alias x in the query attached to the original message?Thanks again!Gabor[quote][b]SQLRNNR (1/16/2013)[/b][hr]Here is a script demonstrating a delete using an alias as well as using the alias X[code="sql"]Use Tempdb;GOCREATE TABLE demodelete (somenum INT);INSERT INTO demodelete (somenum)VALUES (1),(2),(3),(4);SELECT * FROM demodelete;DELETE X	FROM demodelete x	WHERE x.somenum = 1;	SELECT *	FROM demodelete;[/code][/quote]</description><pubDate>Wed, 16 Jan 2013 18:25:36 GMT</pubDate><dc:creator>grevesz</dc:creator></item><item><title>RE: When can I use an alias?</title><link>http://www.sqlservercentral.com/Forums/Topic1408100-392-1.aspx</link><description>Here is a script demonstrating a delete using an alias as well as using the alias X[code="sql"]Use Tempdb;GOCREATE TABLE demodelete (somenum INT);INSERT INTO demodelete (somenum)VALUES (1),(2),(3),(4);SELECT * FROM demodelete;DELETE X	FROM demodelete x	WHERE x.somenum = 1;	SELECT *	FROM demodelete;[/code]</description><pubDate>Wed, 16 Jan 2013 16:00:38 GMT</pubDate><dc:creator>SQLRNNR</dc:creator></item><item><title>When can I use an alias?</title><link>http://www.sqlservercentral.com/Forums/Topic1408100-392-1.aspx</link><description>Hello,It seems that DELETE statements don't support aliases. Is that correct in 2k8R2?Also, the query attached does not permit the alias x. Why and how can I rewrite the query without using the alias x?Thanks a lot in advance,Gabor</description><pubDate>Wed, 16 Jan 2013 15:23:25 GMT</pubDate><dc:creator>grevesz</dc:creator></item></channel></rss>