<?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>Moose56 blog &#187; SQL</title>
	<atom:link href="http://www.moose56.com/blog/category/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.moose56.com/blog</link>
	<description></description>
	<lastBuildDate>Fri, 13 Jan 2012 07:24:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>T-SQL Update on a join</title>
		<link>http://www.moose56.com/blog/2009/08/29/t-sql-update-on-a-join/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=t-sql-update-on-a-join</link>
		<comments>http://www.moose56.com/blog/2009/08/29/t-sql-update-on-a-join/#comments</comments>
		<pubDate>Sat, 29 Aug 2009 11:30:28 +0000</pubDate>
		<dc:creator>moose56</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://moose56.com/blog/?p=81</guid>
		<description><![CDATA[Imagine you have two tables and you want to update one table with values from another. So my example is a bit unrealistic, but just go with it. You have a Customers table and an Orders table. Currently you link your customers to orders by customer name. Now this is not good because it is [...]]]></description>
			<content:encoded><![CDATA[<p>Imagine you have two tables and you want to update one table with values from another.</p>
<p><span id="more-81"></span>
<p>So my example is a bit unrealistic, but just go with it. You have a Customers table and an Orders table. Currently you link your customers to orders by customer name. Now this is not good because it is not unique and if you want to update the name you have to do it in both tables not just the customer table, the usual stuff.</p>
<p><img src="http://moose56.com/blog/wp-content/uploads/2009/08/11.tiff" alt="1.tiff" border="0" width="406" height="126" /></p>
<p>So to improve the situation you want to add the customer IDs to their existing orders and then get rid of the customer name column from the orders table. This is done with the UPDATE statmet, but there are a few options as to how you use it. In this example I will use a join.</p>
<p>To test my theory lets write a SELECT statment that will mimic what the UPDATE will do:</p>
<p>
<pre class="viewsource"><code class="sql">SELECT
    Customers.ID,
    Customers.Name,
    Orders.ID,
    Orders.Item,
    Orders.CustomerName,
    Orders.CustomerID
FROM
    Customers
    RIGHT JOIN Orders
    ON Customers.Name = Order.CustomerName
</code></pre>
</p>
<p>This will give us all of the orders with the customer who made each order:</p>
<p><img src="http://moose56.com/blog/wp-content/uploads/2009/08/21.tiff" alt="2.tiff" border="0" width="383" height="126" /></p>
<p>As you can see Greg is missing because he has not made any orders and Dave is there twice because he has made two. By joining the table like this it also means that we have access to the ID from the customers table that matched the name in the orders table. Adding the ID to the orders table can be done like this:</p>
<p>
<pre class="viewsource"><code class="sql">UPDATE
    Orders
SET
    Orders.CustomerID = Customers.ID
FROM
    Customers
    RIGHT JOIN Orders
    ON Customers.Name = Order.CustomerName
</code></pre>
</p>
<p>Following this update the orders table looks like:</p>
<p><img src="http://moose56.com/blog/wp-content/uploads/2009/08/31.tiff" alt="3.tiff" border="0" width="273" height="135" /></p>
<p>All we have to do to finish this off is delete the CustomerName column and thats it:</p>
<p><img src="http://moose56.com/blog/wp-content/uploads/2009/08/41.tiff" alt="4.tiff" border="0" width="206" height="133" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.moose56.com/blog/2009/08/29/t-sql-update-on-a-join/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

