<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>简单生活 -- Kevin Yang的博客 &#187; 表别名</title>
	<atom:link href="http://www.imkevinyang.com/tags/%e8%a1%a8%e5%88%ab%e5%90%8d/feed" rel="self" type="application/rss+xml" />
	<link>http://www.imkevinyang.com</link>
	<description>It&#039;s all about sharing</description>
	<lastBuildDate>Sun, 05 Feb 2012 15:37:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Sql Tips——Update语句也使用表别名(Table Alias)</title>
		<link>http://www.imkevinyang.com/2010/07/sql-tipsupdate%e8%af%ad%e5%8f%a5%e4%b9%9f%e4%bd%bf%e7%94%a8%e8%a1%a8%e5%88%ab%e5%90%8dtable-alias.html</link>
		<comments>http://www.imkevinyang.com/2010/07/sql-tipsupdate%e8%af%ad%e5%8f%a5%e4%b9%9f%e4%bd%bf%e7%94%a8%e8%a1%a8%e5%88%ab%e5%90%8dtable-alias.html#comments</comments>
		<pubDate>Thu, 01 Jul 2010 16:11:21 +0000</pubDate>
		<dc:creator>Kevin Yang</dc:creator>
				<category><![CDATA[BI/数据库]]></category>
		<category><![CDATA[其他随笔]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Update]]></category>
		<category><![CDATA[数据库]]></category>
		<category><![CDATA[表别名]]></category>

		<guid isPermaLink="false">http://www.imkevinyang.com/2010/07/sql-tipsupdate%e8%af%ad%e5%8f%a5%e4%b9%9f%e4%bd%bf%e7%94%a8%e8%a1%a8%e5%88%ab%e5%90%8dtable-alias.html</guid>
		<description><![CDATA[<p>在编写Sql脚本时通过表别名可以大大缩减Sql代码，同时表别名也是解决同表多次引用的手段之一。在select中使用表别名大家应该都很熟悉了：</p>
<pre class="brush: sql">select * from TableA as A inner join TableB as B on A.Key1 = B.Key1</pre>
<p>但是在Update中使用表别名可能就没那么多人知道了。&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>在编写Sql脚本时通过表别名可以大大缩减Sql代码，同时表别名也是解决同表多次引用的手段之一。在select中使用表别名大家应该都很熟悉了：</p>
<pre class="brush: sql">select * from TableA as A inner join TableB as B on A.Key1 = B.Key1</pre>
<p>但是在Update中使用表别名可能就没那么多人知道了。</p>
<pre class="brush: sql">update T
set T.Key1 = 'xxxx'
from TableA T</pre>
<p>这些天在写Sql Update脚本的时候需要引用两次同个表对象，如果直接像下面这样引用两次TableA则会抛出“The multi-part identifier ‘TableA.Index’ could not be bound”的错误。这是因为Sql引擎无法知道你在where子句中的TableA到底指的是要Update的表还是from后面的表。</p>
<pre class="brush: sql">update TableA
set TableA.NextKey = T.Key
from TableA T
where T.Index = TableA.Index + 1</pre>
<p>如果不对Update后面的TableA使用别名的话，我们只能通过以下方法来实现。</p>
<pre class="brush: sql">update TableA
set TableA.NextKey = T.Key
from
(
  select * from TableA
)T
where T.Index = TableA.Index + 1</pre>
<p>使用别名可以得到更简洁的写法:</p>
<pre class="brush: sql">update T1
set T1.NextKey = T2.Key
from TableA T1, TableA T2
whereT2.Index = T1.Index + 1</pre>
<p style="text-align: right;">——<a title="Sql Tips——Update语句也使用表别名(Table Alias)" href="http://www.imkevinyang.com/2010/07/sql-tipsupdate%E8%AF%AD%E5%8F%A5%E4%B9%9F%E4%BD%BF%E7%94%A8%E8%A1%A8%E5%88%AB%E5%90%8Dtable-alias.html" target="_self"><em>Kevin Yang</em></a></p>

	标签：<a href="http://www.imkevinyang.com/categories/techarticles/businessintelligence" title="BI/数据库" rel="tag">BI/数据库</a>, <a href="http://www.imkevinyang.com/tags/sql" title="SQL" rel="tag">SQL</a>, <a href="http://www.imkevinyang.com/tags/tips" title="Tips" rel="tag">Tips</a>, <a href="http://www.imkevinyang.com/tags/update" title="Update" rel="tag">Update</a>, <a href="http://www.imkevinyang.com/categories/techarticles/othertecharticles" title="其他随笔" rel="tag">其他随笔</a>, <a href="http://www.imkevinyang.com/tags/%e6%95%b0%e6%8d%ae%e5%ba%93" title="数据库" rel="tag">数据库</a>, <a href="http://www.imkevinyang.com/tags/%e8%a1%a8%e5%88%ab%e5%90%8d" title="表别名" rel="tag">表别名</a><br />

	<h4 style="background-color:#3B3B3B;border-bottom:2px groove gray;color:#F2F2F2;margin-top:20px;padding:6px 6px 6px 15px;margin:20px 0px 0px 0px">你可能对下面的文章感兴趣</h4>
	<ul class="st-related-posts">
	<li><a href="http://www.imkevinyang.com/2009/09/blend%e5%b0%8f%e8%b4%b4%e5%a3%ab%e6%94%b9%e5%8f%98%e9%bc%a0%e6%a0%87%e6%bb%9a%e8%bd%ae%e7%9a%84%e9%bb%98%e8%ae%a4%e8%a1%8c%e4%b8%ba-2.html" title="Blend小贴士&mdash;&mdash;改变鼠标滚轮的默认行为 (2009/09/18)">Blend小贴士&mdash;&mdash;改变鼠标滚轮的默认行为</a> </li>
	<li><a href="http://www.imkevinyang.com/2011/06/onenote-tips-%e6%b7%b7%e5%90%88%e4%bd%bf%e7%94%a8%e4%b8%ad%e8%8b%b1%e6%96%87%e5%ad%97%e4%bd%93.html" title="OneNote Tips — 混合使用中英文字体 (2011/06/16)">OneNote Tips — 混合使用中英文字体</a> </li>
	<li><a href="http://www.imkevinyang.com/2009/05/sql-server%e4%b8%ad%e4%bf%ae%e6%94%b9%e8%87%aa%e5%a2%9e%e9%95%bf%e5%88%97%e7%bb%8f%e5%b8%b8%e9%9c%80%e8%a6%81%e7%94%a8%e5%88%b0%e7%9a%84%e9%85%8d%e7%bd%ae.html" title="Sql Server中修改自增长列经常需要用到的配置 (2009/05/24)">Sql Server中修改自增长列经常需要用到的配置</a> </li>
	<li><a href="http://www.imkevinyang.com/2010/08/sql-server%e9%87%8d%e5%91%bd%e5%90%8d%e6%89%80%e6%9c%89%e5%a4%96%e9%94%ae%e7%ba%a6%e6%9d%9f.html" title="Sql Server重命名所有外键约束 (2010/08/02)">Sql Server重命名所有外键约束</a> </li>
	<li><a href="http://www.imkevinyang.com/2010/05/ssis%e8%b0%83%e7%94%a8%e5%ad%98%e5%82%a8%e8%bf%87%e7%a8%8b%e5%a4%b1%e8%b4%a5.html" title="SSIS调用存储过程失败 (2010/05/23)">SSIS调用存储过程失败</a> </li>
	<li><a href="http://www.imkevinyang.com/2009/09/visual-studio%e5%b0%8f%e8%b4%b4%e5%a3%ab%e5%8a%a0%e4%ba%86try-catch%e4%b9%9f%e8%83%bd%e8%87%aa%e5%8a%a8%e5%ae%9a%e4%bd%8d%e5%88%b0%e5%bc%82%e5%b8%b8%e4%bb%a3%e7%a0%81.html" title="Visual Studio小贴士&mdash;&mdash;加了Try-Catch也能自动定位到异常代码 (2009/09/18)">Visual Studio小贴士&mdash;&mdash;加了Try-Catch也能自动定位到异常代码</a> </li>
	<li><a href="http://www.imkevinyang.com/2009/09/%e9%87%8a%e6%94%besql-server%e5%8d%a0%e7%94%a8%e7%9a%84%e5%86%85%e5%ad%98.html" title="释放SQL Server占用的内存 (2009/09/01)">释放SQL Server占用的内存</a> </li>
</ul>

]]></content:encoded>
			<wfw:commentRss>http://www.imkevinyang.com/2010/07/sql-tipsupdate%e8%af%ad%e5%8f%a5%e4%b9%9f%e4%bd%bf%e7%94%a8%e8%a1%a8%e5%88%ab%e5%90%8dtable-alias.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

Served from: www.imkevinyang.com @ 2012-02-08 19:26:36 -->
