﻿<?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 / SQL Server 2008 - General  / Multiple updates for save value / 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>Sat, 18 May 2013 18:16:53 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Multiple updates for save value</title><link>http://www.sqlservercentral.com/Forums/Topic1371315-391-1.aspx</link><description>[font="Verdana"]Thanks Gail Shaw, Vedran for your valuable inputs.@Vedran, ofcourse not. I keep my eyes widely open while writting Insert / Update / Delete :w00t:[/font]</description><pubDate>Thu, 11 Oct 2012 06:54:03 GMT</pubDate><dc:creator>Mahesh Bote</dc:creator></item><item><title>RE: Multiple updates for save value</title><link>http://www.sqlservercentral.com/Forums/Topic1371315-391-1.aspx</link><description>Mahesh example is very simple update, without any join, just a subquery.So, "FROM" syntax is very similar as a syntax without "FROM":[code="sql"]--SELECT *UPDATE t SET t.Column1 = &amp;lt;New Value&amp;gt;FROM &amp;lt;Table Name&amp;gt; tWhere Column1 = &amp;lt;Old Value&amp;gt; And column2 In (Select IDs From &amp;lt;Table Variable&amp;gt;)[/code]It's just matter of syntax, execution plan is the same.I personally prefer that syntax because it is clearer to read for me and it is straightforward to convert it to SELECT and check what data will be updated prior to actually do it (have you ever run UPDATE and forgot a WHERE clause? This form saved my day a few times :) ).</description><pubDate>Thu, 11 Oct 2012 06:42:14 GMT</pubDate><dc:creator>Vedran Kesegic</dc:creator></item><item><title>RE: Multiple updates for save value</title><link>http://www.sqlservercentral.com/Forums/Topic1371315-391-1.aspx</link><description>It doesn't necessarily actually do a distinct.I personally would not recommend changing the update to use the update from syntax. Since we know that the join will produce duplicate rows, it's not guaranteed what value the update will pick. An update from should never be used when there are duplicate rows produced by the join, as there will be in this situation.</description><pubDate>Thu, 11 Oct 2012 05:19:21 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>RE: Multiple updates for save value</title><link>http://www.sqlservercentral.com/Forums/Topic1371315-391-1.aspx</link><description>To add to Gail's reply, the optimizer parses the literal values in the IN predicate and extracts unique values.Here's an example using your code:[code]DECLARE @T TABLE (	Column1 int,	Column2 int)INSERT INTO @T VALUES(1,2),(2,3),(3,4),(4,5),(5,6),(6,7),(7,8),(8,9)SELECT *FROM @TWHERE Column2 IN (1, 2, 2, 3, 4, 5, 5, 5, 6, 6, 7, 8, 8, 8, 8, 8)[/code]The scan predicate found in the execution plan is the following:[code]Predicate: [Column2]=(1) OR [Column2]=(2) OR [Column2]=(3) OR [Column2]=(4) OR [Column2]=(5) OR [Column2]=(6) OR [Column2]=(7) OR [Column2]=(8)[/code]However, this is just an implementation detail you shouldn't care about.EDIT: typo</description><pubDate>Thu, 11 Oct 2012 05:18:42 GMT</pubDate><dc:creator>spaghettidba</dc:creator></item><item><title>RE: Multiple updates for save value</title><link>http://www.sqlservercentral.com/Forums/Topic1371315-391-1.aspx</link><description>Yes, "IN" internally does "distinct" of the values.I would recommend to use other syntax for update, one which is similar to SELECT.First, write the SELECT that returns the rows.Then, replace part before "FROM" with "UPDATE tablealias SET xy=...".For example:[code="sql"]--SELECT t1.*, t2.*UPDATE t2 SET t2.Name=t1.name+'xy', t2.Address=...FROM table1 t1  JOIN table2 t2 ON ...WHERE ...[/code]</description><pubDate>Thu, 11 Oct 2012 05:17:05 GMT</pubDate><dc:creator>Vedran Kesegic</dc:creator></item><item><title>RE: Multiple updates for save value</title><link>http://www.sqlservercentral.com/Forums/Topic1371315-391-1.aspx</link><description>It's not the update, it's the IN. IN just looks for matching values, it doesn't matter at all how many times the value is in the subquery/value list, just whether it is there or not. It's not a full join, just a check for matching values.</description><pubDate>Thu, 11 Oct 2012 05:05:37 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>Multiple updates for save value</title><link>http://www.sqlservercentral.com/Forums/Topic1371315-391-1.aspx</link><description>[font="Verdana"]Hi All,While working on one of the stored procedure, I observed one situation. Inside the stored procedure there is one Update statement like:Update &amp;lt;Table Name&amp;gt;Set Column1 = &amp;lt;New Value&amp;gt;Where Column1 = &amp;lt;Old Value&amp;gt; And column2 In (Select IDs From &amp;lt;Table Variable&amp;gt;)I observed the values in &amp;lt;Table Variable&amp;gt; are duplicating. Even though Update statement is updating values with unique values for Column2.Means,&amp;lt;Table Variable&amp;gt; has values like (1, 2, 2, 3, 4, 5, 5, 5, 6, 6, 7, 8, 8, 8, 8, 8) however update statement is returning (8 row(s) affected) message though &amp;lt;Table Variable&amp;gt; has record count of 16. To verify on this situation I created one Update Trigger on the said table and gathered the newly updating information from the affected table to audit table and found surprising results. Its showing / contains only 6 updated records.So does it means IN clause reads the distinct values from the given bunch of values?Please let me know on this.Thanks in advance,-- Mahesh[/font]</description><pubDate>Thu, 11 Oct 2012 03:49:15 GMT</pubDate><dc:creator>Mahesh Bote</dc:creator></item></channel></rss>