<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Update Multiple Rows With Different Values and a Single SQL Query</title>
	<atom:link href="http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-values-and-a-single-sql-query/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-values-and-a-single-sql-query/</link>
	<description>Web development, programming and I.T.</description>
	<lastBuildDate>Fri, 03 Sep 2010 22:16:42 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: CAP Marketer - 10 astuces SQL pour accélérer vos bases de données</title>
		<link>http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-values-and-a-single-sql-query/comment-page-1/#comment-5438</link>
		<dc:creator>CAP Marketer - 10 astuces SQL pour accélérer vos bases de données</dc:creator>
		<pubDate>Fri, 03 Sep 2010 22:16:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.karlrixon.co.uk/?p=51#comment-5438</guid>
		<description>[...] Source: http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-values-and-a-single-sql-... [...]</description>
		<content:encoded><![CDATA[<p>[...] Source: <a href="http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-values-and-a-single-sql-.." rel="nofollow">http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-values-and-a-single-sql-..</a>. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Karl</title>
		<link>http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-values-and-a-single-sql-query/comment-page-1/#comment-5387</link>
		<dc:creator>Karl</dc:creator>
		<pubDate>Thu, 02 Sep 2010 17:31:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.karlrixon.co.uk/?p=51#comment-5387</guid>
		<description>@Jay

The limit in the number of case statements does not mean the number of rows which can be updated is limited to 255 - the two factors are not necessarily related. You might have 10 case statements which could match 100000 rows each for example. If you are trying to target a single row per case statement then you may have an issue however. If this is the case you would probably be better off moving the conditions into their own table and joining to achieve the same thing:

&lt;code&gt;
category_order_helper
category_id  &#124; display_order
-------------+----------------
1            &#124; 6
2            &#124; 4
&lt;/code&gt;

[cc lang=&quot;sql&quot;]
UPDATE categories
SET display_order = h.display_order
INNER JOIN category_order_helper AS h
ON categories.id = h.category_id
[/cc]

It requires a couple of extra queries (a TRUNCATE and an INSERT), but it avoids the case statement limit.</description>
		<content:encoded><![CDATA[<p>@Jay</p>
<p>The limit in the number of case statements does not mean the number of rows which can be updated is limited to 255 &#8211; the two factors are not necessarily related. You might have 10 case statements which could match 100000 rows each for example. If you are trying to target a single row per case statement then you may have an issue however. If this is the case you would probably be better off moving the conditions into their own table and joining to achieve the same thing:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">category_order_helper<br />
category_id &nbsp;| display_order<br />
-------------+----------------<br />
1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 6<br />
2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| 4</div></td></tr></tbody></table></div>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">UPDATE</span> categories<br />
<span style="color: #993333; font-weight: bold;">SET</span> display_order <span style="color: #66cc66;">=</span> h<span style="color: #66cc66;">.</span>display_order<br />
<span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> category_order_helper <span style="color: #993333; font-weight: bold;">AS</span> h<br />
<span style="color: #993333; font-weight: bold;">ON</span> categories<span style="color: #66cc66;">.</span>id <span style="color: #66cc66;">=</span> h<span style="color: #66cc66;">.</span>category_id</div></td></tr></tbody></table></div>
<p>It requires a couple of extra queries (a TRUNCATE and an INSERT), but it avoids the case statement limit.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jay</title>
		<link>http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-values-and-a-single-sql-query/comment-page-1/#comment-5385</link>
		<dc:creator>Jay</dc:creator>
		<pubDate>Thu, 02 Sep 2010 16:01:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.karlrixon.co.uk/?p=51#comment-5385</guid>
		<description>The use of the CASE statement to update multiple records only works, if you&#039;re updating less than 255 rows - there is a limit on the number of CASE statements you can use.
So, if you have more than 255 rows to update, then just write it as multiple queries, using the very first syntax described above.</description>
		<content:encoded><![CDATA[<p>The use of the CASE statement to update multiple records only works, if you&#8217;re updating less than 255 rows &#8211; there is a limit on the number of CASE statements you can use.<br />
So, if you have more than 255 rows to update, then just write it as multiple queries, using the very first syntax described above.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zflag</title>
		<link>http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-values-and-a-single-sql-query/comment-page-1/#comment-4790</link>
		<dc:creator>zflag</dc:creator>
		<pubDate>Fri, 13 Aug 2010 18:39:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.karlrixon.co.uk/?p=51#comment-4790</guid>
		<description>Kirk:  your query doesn&#039;t work because your CASE statement is evaluating the same field you are trying to update.</description>
		<content:encoded><![CDATA[<p>Kirk:  your query doesn&#8217;t work because your CASE statement is evaluating the same field you are trying to update.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kirk</title>
		<link>http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-values-and-a-single-sql-query/comment-page-1/#comment-4366</link>
		<dc:creator>Kirk</dc:creator>
		<pubDate>Thu, 29 Jul 2010 23:47:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.karlrixon.co.uk/?p=51#comment-4366</guid>
		<description>Not Working.
Using MS Access

UPDATE navItems
SET navItems.pos = CASE navItems.pos WHEN 10 THEN 2 WHEN 25 THEN 10 WHEN 24 THEN 13 ELSE 0 END
WHERE (pos IN (10, 25, 24))

Receiving a syntax error.

Any ideas?

Kirk</description>
		<content:encoded><![CDATA[<p>Not Working.<br />
Using MS Access</p>
<p>UPDATE navItems<br />
SET navItems.pos = CASE navItems.pos WHEN 10 THEN 2 WHEN 25 THEN 10 WHEN 24 THEN 13 ELSE 0 END<br />
WHERE (pos IN (10, 25, 24))</p>
<p>Receiving a syntax error.</p>
<p>Any ideas?</p>
<p>Kirk</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bruno negrao</title>
		<link>http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-values-and-a-single-sql-query/comment-page-1/#comment-3883</link>
		<dc:creator>bruno negrao</dc:creator>
		<pubDate>Tue, 29 Jun 2010 17:44:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.karlrixon.co.uk/?p=51#comment-3883</guid>
		<description>great post, thank you.</description>
		<content:encoded><![CDATA[<p>great post, thank you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Colin</title>
		<link>http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-values-and-a-single-sql-query/comment-page-1/#comment-3872</link>
		<dc:creator>Colin</dc:creator>
		<pubDate>Mon, 28 Jun 2010 15:25:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.karlrixon.co.uk/?p=51#comment-3872</guid>
		<description>Anyone know how to program this exact same thing using C# for ASP.NET and maybe ACCESS DB for now? :)</description>
		<content:encoded><![CDATA[<p>Anyone know how to program this exact same thing using C# for ASP.NET and maybe ACCESS DB for now? <img src='http://www.karlrixon.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tacaza</title>
		<link>http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-values-and-a-single-sql-query/comment-page-1/#comment-3856</link>
		<dc:creator>Tacaza</dc:creator>
		<pubDate>Sat, 26 Jun 2010 16:44:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.karlrixon.co.uk/?p=51#comment-3856</guid>
		<description>I&#039;m also doing the jQuery sortable.

I was going to implement the method described here:
http://le-gall.net/pierrick/blog/index.php/2007/11/29/108-mysql-bulk-update-with-talend-open-studio

but fortunately I came across this article. The solution described here is much simpler to implement and as I&#039;ve tested myself, the query is very efficient, at least for me. Thank you very much for the post.</description>
		<content:encoded><![CDATA[<p>I&#8217;m also doing the jQuery sortable.</p>
<p>I was going to implement the method described here:<br />
<a href="http://le-gall.net/pierrick/blog/index.php/2007/11/29/108-mysql-bulk-update-with-talend-open-studio" rel="nofollow">http://le-gall.net/pierrick/blog/index.php/2007/11/29/108-mysql-bulk-update-with-talend-open-studio</a></p>
<p>but fortunately I came across this article. The solution described here is much simpler to implement and as I&#8217;ve tested myself, the query is very efficient, at least for me. Thank you very much for the post.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Feroz</title>
		<link>http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-values-and-a-single-sql-query/comment-page-1/#comment-3807</link>
		<dc:creator>Feroz</dc:creator>
		<pubDate>Sun, 20 Jun 2010 18:51:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.karlrixon.co.uk/?p=51#comment-3807</guid>
		<description>&lt;strong&gt;Excellent post!!!&lt;/strong&gt;

Perfect Solution for SQL Query... 

Great post</description>
		<content:encoded><![CDATA[<p><strong>Excellent post!!!</strong></p>
<p>Perfect Solution for SQL Query&#8230; </p>
<p>Great post</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: dralezero</title>
		<link>http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-values-and-a-single-sql-query/comment-page-1/#comment-3484</link>
		<dc:creator>dralezero</dc:creator>
		<pubDate>Sun, 16 May 2010 04:12:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.karlrixon.co.uk/?p=51#comment-3484</guid>
		<description>Thanks. I am doing the jQuery sortable so my IDs of the records I want to update are the values in my array. The array index is the ordering (keys). So I swapped the values in WHEN THEN
&lt;code&gt;
$sql = &quot;UPDATE `listitems` SET `position` = CASE `id`&quot;;
foreach ($_GET[&#039;listItem&#039;] as $position =&gt; $id) {
    $sql .= &quot; WHEN &quot;.$id.&quot; THEN &quot;.$position;
	$ids[] = $id;
}
$ids = implode(&quot;,&quot;,$ids);
$sql .= &quot; END WHERE `id` IN ($ids)&quot;;
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Thanks. I am doing the jQuery sortable so my IDs of the records I want to update are the values in my array. The array index is the ordering (keys). So I swapped the values in WHEN THEN</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$sql = &quot;UPDATE `listitems` SET `position` = CASE `id`&quot;;<br />
foreach ($_GET['listItem'] as $position =&gt; $id) {<br />
&nbsp; &nbsp; $sql .= &quot; WHEN &quot;.$id.&quot; THEN &quot;.$position;<br />
&nbsp; &nbsp; $ids[] = $id;<br />
}<br />
$ids = implode(&quot;,&quot;,$ids);<br />
$sql .= &quot; END WHERE `id` IN ($ids)&quot;;</div></td></tr></tbody></table></div>
]]></content:encoded>
	</item>
</channel>
</rss>
