<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx</link><description>This is a pretty common situation that comes up when performing database operations. A stored procedure is called and the data needs to be updated if it already exists and inserted if it does not. If we refer to the Books Online documentation, it gives</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>MSDN Blog Postings  &amp;raquo; SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#7765201</link><pubDate>Mon, 18 Feb 2008 06:41:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7765201</guid><dc:creator>MSDN Blog Postings  » SQL: If Exists Update Else Insert</dc:creator><description>&lt;p&gt;PingBack from &lt;a rel="nofollow" target="_new" href="http://msdnrss.thecoderblogs.com/2008/02/17/sql-if-exists-update-else-insert/"&gt;http://msdnrss.thecoderblogs.com/2008/02/17/sql-if-exists-update-else-insert/&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#7766173</link><pubDate>Mon, 18 Feb 2008 07:31:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7766173</guid><dc:creator>jackbond</dc:creator><description>&lt;p&gt;Thanks for the tip, I have a lot of code with the inferior approach that I will be updating. Fortunately, SQL 2008's Merge statement will make this a non-issue.&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#7780016</link><pubDate>Mon, 18 Feb 2008 23:53:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7780016</guid><dc:creator>Jeremiah.Clark</dc:creator><description>&lt;p&gt;Sent to me by Chris:&lt;/p&gt;
&lt;p&gt;&amp;quot;Since I don't have a blog and you don't allow anonymous comments I thought I'd shoot a quick email with a question/concern. Regarding your post &amp;quot;SQL: If Exists Update Else Insert&amp;quot; with the alternative method of doing the Update and then checking the @@ROWCOUNT as to whether to perform an insert or not... I definitely would not have thought of it that way either. However, I was wondering, with the scope of @@ROWCOUNT being global is there the possibility that the @@ROWCOUNT value could be incorrect within the local scope if there is heavy use on the database? Or am I incorrect on the scope being global? &amp;nbsp;The reason I thought of this goes back to the use of SCOPE_IDENTITY() over @@IDENTITY &amp;nbsp;to get the last identity in the same scope.&lt;/p&gt;
&lt;p&gt;Thanks for your time.&amp;quot;&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#7780302</link><pubDate>Tue, 19 Feb 2008 00:11:20 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7780302</guid><dc:creator>Jeremiah.Clark</dc:creator><description>&lt;p&gt;I have enabled anonymous comments. &amp;nbsp;I thought that I had done that before, but I guess not.&lt;/p&gt;
&lt;p&gt;Chris,&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Great question! &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;The scope of @@ROWCOUNT (or ROWCOUNT_BIG() in the case of a VERY large result) is limited to the current scope that contains the statement that was previously executed. &amp;nbsp;So you are guaranteed the correct result when using @@ROWCOUNT. &amp;nbsp;&lt;/p&gt;
&lt;p&gt; &amp;nbsp;Like you mentioned, this is not the case with @@IDENTITY. &amp;nbsp;You can get some really unexpected results if it is used improperly. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Jeremiah&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#7840180</link><pubDate>Thu, 21 Feb 2008 20:37:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7840180</guid><dc:creator>Andrew Herron</dc:creator><description>&lt;p&gt;Will this code work for any SQL compliant server?&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#7841145</link><pubDate>Thu, 21 Feb 2008 22:19:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7841145</guid><dc:creator>Jeremiah.Clark</dc:creator><description>&lt;p&gt;Andrew,&lt;/p&gt;
&lt;p&gt; &amp;nbsp;The basic idea should work with other types of SQL servers (however, I have not verified this). &amp;nbsp;You should just be able to use the @@ROWCOUNT equivalent for the system that you are using.&lt;/p&gt;
&lt;p&gt;For example:&lt;/p&gt;
&lt;p&gt;MySql uses &amp;quot;row_count()&amp;quot;&lt;/p&gt;
&lt;p&gt;Oracle uses &amp;quot;sql%rowcount&amp;quot;&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#7841200</link><pubDate>Thu, 21 Feb 2008 22:26:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7841200</guid><dc:creator>Guy</dc:creator><description>&lt;p&gt;If you are thinking of MySQL there is a function called ROW_COUNT() that serves the same purpose (available as of version 5). Theoretically Jeremiah's suggestion should result in improved performance on MySQL also, but you should test it to make sure.&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#7841337</link><pubDate>Thu, 21 Feb 2008 22:42:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7841337</guid><dc:creator>Richard Ayotte</dc:creator><description>&lt;p&gt;MySQL does something much better. It's not part of the standard but still very useful.&lt;/p&gt;
&lt;p&gt;REPLACE [LOW_PRIORITY | DELAYED]&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;[INTO] tbl_name [(col_name,...)]&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;VALUES ({expr | DEFAULT},...),(...),...&lt;/p&gt;
&lt;p&gt;It does what you describe. If the record exists, it is updated, else inserted.&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://dev.mysql.com/doc/refman/5.0/en/replace.html"&gt;http://dev.mysql.com/doc/refman/5.0/en/replace.html&lt;/a&gt;&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#7841450</link><pubDate>Thu, 21 Feb 2008 22:53:10 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7841450</guid><dc:creator>Jeremiah.Clark</dc:creator><description>&lt;p&gt;Richard,&lt;/p&gt;
&lt;p&gt;Thanks for the comment. &amp;nbsp;I was not aware of the REPLACE statement in MySQL.&lt;/p&gt;
&lt;p&gt;Looking at the documentation, it does effectively do the same thing. &amp;nbsp;But it seems to do it in a different manner. &amp;nbsp;If a row with the same PK or UNIQUE index exists, it deletes the old row and then inserts the new row. &amp;nbsp;For anything that does not already exist in the table, it inserts.&lt;/p&gt;
&lt;p&gt;I don't know if the deleting is more or less efficient than doing an update in MySQL. &amp;nbsp;I would advise trying both ways and then compare the performance to see which solution better fits your needs.&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#7841484</link><pubDate>Thu, 21 Feb 2008 22:57:53 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7841484</guid><dc:creator>Jeremiah.Clark</dc:creator><description>&lt;p&gt;Looking into the MySql documentation a little more I found this statement:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html" target="_blank"&gt;INSERT ... ON DUPLICATE KEY UPDATE&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This seems to be closer to the upsert code that I initially wrote about.&lt;/p&gt;</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#7842273</link><pubDate>Fri, 22 Feb 2008 00:52:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7842273</guid><dc:creator>JohnEric</dc:creator><description>&lt;p&gt;RE: @@identity&lt;/p&gt;
&lt;p&gt;You can use Scope_Identity() instead. &amp;nbsp;That guarantees the correct value.&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#7851889</link><pubDate>Sat, 23 Feb 2008 02:27:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7851889</guid><dc:creator>Marc Brooks</dc:creator><description>&lt;p&gt;I vastly prefer this sort of game, for example a HitCounter for some item that automatically inserts new rows houry:&lt;/p&gt;
&lt;p&gt;INSERT INTO dbo.HitCounter(ItemID, TimeSlot)&lt;/p&gt;
&lt;p&gt;SELECT TOP 1 &lt;/p&gt;
&lt;p&gt; @ItemID AS ID&lt;/p&gt;
&lt;p&gt;,DateAdd(hh, DateDiff(hh, 0, GetUtcDate()), 0) AS TimeSlot&lt;/p&gt;
&lt;p&gt;FROM (SELECT 1 AS FakeColumn) AS FakeTable&lt;/p&gt;
&lt;p&gt;WHERE NOT EXISTS (SELECT * FROM dbo.HitCounter&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;WHERE ItemID = @ItemID&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AND TimeSlot = DateAdd(hh, DateDiff(hh, 0, GetUtcDate()), 0))&lt;/p&gt;
&lt;p&gt;UPDATE dbo.HitCounter&lt;/p&gt;
&lt;p&gt;SET Hits = Hits + 1&lt;/p&gt;
&lt;p&gt;WHERE ItemID = @ItemID&lt;/p&gt;
&lt;p&gt;AND TimeSlot = DateAdd(hh, DateDiff(hh, 0, GetUtcDate()), 0)&lt;/p&gt;
&lt;p&gt;SELECT ID &lt;/p&gt;
&lt;p&gt;FROM dbo.HitCounter&lt;/p&gt;
&lt;p&gt;WHERE ItemID = @ItemID&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#8007978</link><pubDate>Mon, 03 Mar 2008 22:09:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8007978</guid><dc:creator>Gabriel O. Perez</dc:creator><description>&lt;p&gt;This is a great approach that I never thought of, and the only concern I had was answered within the comments section. Thanks! &lt;/p&gt;
</description></item><item><title>Interesting Finds: 2008.02.23</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#8015312</link><pubDate>Tue, 04 Mar 2008 04:45:14 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8015312</guid><dc:creator>gOODiDEA</dc:creator><description>&lt;p&gt;Debug:DebuggingIIS7.0WebapplicationsremotelywithVisualStudio2008EmbeddingASP.NETServer...&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#8358189</link><pubDate>Fri, 04 Apr 2008 23:09:44 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8358189</guid><dc:creator>Joe Davies</dc:creator><description>&lt;p&gt;This is great! &amp;nbsp;I shaved lots of time off my bulk insert/updates. &amp;nbsp;&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#8573452</link><pubDate>Wed, 04 Jun 2008 18:06:47 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8573452</guid><dc:creator>Travis Nelson</dc:creator><description>&lt;p&gt;Thanks for the tip--just tried it and it worked perfectly.&lt;/p&gt;
&lt;p&gt;Is this more efficient than doing an &amp;quot;IF SELECT COUNT(*) FROM ... &amp;gt; 0 ELSE ... &amp;quot;&lt;/p&gt;
&lt;p&gt;That would select only one value, I'm just not fully sure of the overhead created when SQL Server executes the SELECT in this vs. an UPDATE that makes no changes. &amp;nbsp;I'm not really sure how to really test this either and for my small app, it's really negligible anyway.&lt;/p&gt;
&lt;p&gt;Either way, yours worked seamlessly, so thanks!&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#8738253</link><pubDate>Wed, 16 Jul 2008 15:22:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8738253</guid><dc:creator>Geoff Winkless</dc:creator><description>&lt;p&gt;Using REPLACE in mysql is problematic because it DELETEs the existing row before INSERTing.&lt;/p&gt;
&lt;p&gt;The INSERT ... ON DUPLICATE KEY UPDATE you described is the better solution for mysql, until SQL2003's MERGE is implemented widely. &lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#9058140</link><pubDate>Mon, 10 Nov 2008 19:07:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9058140</guid><dc:creator>John</dc:creator><description>&lt;p&gt;What about the performance difference in doing a delete and insert vs. an update?&lt;/p&gt;
&lt;p&gt;I have an application that will probably always update 4 rows in the table (after the first iteration). &amp;nbsp;Performance wise, should I do a delete for those four rows and then insert each row or simply do four updates?&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#9227584</link><pubDate>Tue, 16 Dec 2008 22:12:38 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9227584</guid><dc:creator>Brad</dc:creator><description>&lt;p&gt;This was a great help Jeremiah! &amp;nbsp;Our DBA recommended checking @@ERROR=0 in addition to @@ROWCOUNT=0 just in case the UPDATE was the right thing to do, but didn't work for whatever reason. &lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#9360384</link><pubDate>Thu, 22 Jan 2009 02:59:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9360384</guid><dc:creator>PAKO</dc:creator><description>&lt;p&gt;I CANTO USE DE IF CONDITION IN MYSQL SERVER 5.0&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#9434059</link><pubDate>Thu, 19 Feb 2009 16:44:01 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9434059</guid><dc:creator>Martin</dc:creator><description>&lt;p&gt;I'm trying to do this in a way that will work on SQL Server and DB2. Unfortunately DB2 is extremely primitive and as soon as you involve 'IF' you are into requiring a procedure, can't just run it in a script. The other side of the coin is that MERGER, which will do the job nicely on DB2, isn't supported in SQL Server before SQL 2008. &lt;/p&gt;
&lt;p&gt;Has anyone a solution that will run in a script on both DB2 and SQL Server 2005?&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#9506744</link><pubDate>Wed, 25 Mar 2009 10:52:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9506744</guid><dc:creator>Mithun</dc:creator><description>&lt;p&gt;How does this behave in a multi-threaded environment? How to solve in that case?&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#9618807</link><pubDate>Fri, 15 May 2009 19:12:48 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9618807</guid><dc:creator>Terry</dc:creator><description>&lt;p&gt;My scenario was that I needed to SELECT a journal number (if it existed), otherwise create the journal number, and INSERT it. &amp;nbsp;(I didn't need to do any updates in my scenario). &amp;nbsp;Here is my quick solution:&lt;/p&gt;
&lt;p&gt;--------------------------------------&lt;/p&gt;
&lt;p&gt;select @JournalNumber = JournalNumber &amp;nbsp;from JournalHeaderTable with (nolock) where BatchName = @BatchName&lt;/p&gt;
&lt;p&gt;IF @@ROWCOUNT=0&lt;/p&gt;
&lt;p&gt;BEGIN&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;exec &amp;nbsp;taGetNextJournalEntry &amp;nbsp;@O_vJournalEntryNumber = @JournalNumber output&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;BEGIN TRANSACTION&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;insert into JournalHeaderTable(BatchName, JournalNumber, Reference, TrxDate, TrxType)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;values (@BatchName, @JournalNumber, @Refrence, @TransactionDate, 0)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;COMMIT TRANSACTION&lt;/p&gt;
&lt;p&gt;END&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#9724047</link><pubDate>Wed, 10 Jun 2009 15:15:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9724047</guid><dc:creator>Karthik</dc:creator><description>&lt;p&gt;In case of SQL 2008, we can use MERGE for this..just implemented that within my SSIS package to handle new and modified data, and it works like a charm..&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#9806377</link><pubDate>Sat, 27 Jun 2009 09:19:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9806377</guid><dc:creator>Karim Sultan</dc:creator><description>&lt;p&gt;Hey thanks Jeremiah, for SQL 2005, your solution is the best I've seen yet (after trolling and review 3 other suggestions). &amp;nbsp;I'll gladly give up portability (it's a quick port to MySQL and Oracle anyways) for a performance gain. &amp;nbsp;In fact, it's not so much a performance gain as it is a reduction in queries (max 2 versus max 3) that makes it much more appealing over the long run. &amp;nbsp;Thanks for the insight, and keep sharing.&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#9895815</link><pubDate>Wed, 16 Sep 2009 15:17:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9895815</guid><dc:creator>Pete</dc:creator><description>&lt;p&gt;This is a great tip no doubt, however one observation is, it is being compared with bad code in the first instance.&lt;/p&gt;
&lt;p&gt;Here's the original example&lt;/p&gt;
&lt;p&gt;IF EXISTS (SELECT * FROM Table1 WHERE Column1='SomeValue')&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;UPDATE Table1 SET (...) WHERE Column1='SomeValue'&lt;/p&gt;
&lt;p&gt;ELSE&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;INSERT INTO Table1 VALUES (...)&lt;/p&gt;
&lt;p&gt;yes this will be slow. The major error is in the select * statement.&lt;/p&gt;
&lt;p&gt;Most Developers know not to use this, but for the wrong reasons.&lt;/p&gt;
&lt;p&gt;It's not because there are more columns returned and therefore bytes per row (which is true, but has minimal effect for most queries based on normailsed tables)&lt;/p&gt;
&lt;p&gt;No, the real problem is Select * forces sql to tablescan unless every column returned is in an index. In other words, every row in the table is read - that's why it's slow.&lt;/p&gt;
&lt;p&gt;If you use Select 'PrimaryKeyColumn' or Select 'IndexedColumn(s),....' performance is improved by a huge factor. &lt;/p&gt;
&lt;p&gt;Just try it on a large table with a bulk update. or use SLQ profiler and view the queryplan - you'll see select * using 99% more resources when returning the exact same query (except for the columns) limited by the same where clause&lt;/p&gt;
&lt;p&gt;Never the less, the solution offered by Jerimihah is a great idea and outside the box. I think adding an errorcheck as suggested by one DBA is also a good idea.&lt;/p&gt;
&lt;p&gt;just my 2 cents worth&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#9901715</link><pubDate>Thu, 01 Oct 2009 11:05:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9901715</guid><dc:creator>pooja</dc:creator><description>&lt;p&gt;this is not working on mysql..plz comment why&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#9922852</link><pubDate>Mon, 16 Nov 2009 08:38:51 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9922852</guid><dc:creator>chris</dc:creator><description>&lt;p&gt;Hello, &lt;/p&gt;
&lt;p&gt;Is this code thread safe?&lt;/p&gt;
&lt;p&gt;Would... &lt;/p&gt;
&lt;p&gt;UPDATE Table1 SET (...) WHERE Column1='SomeValue'&lt;/p&gt;
&lt;p&gt;IF @@ROWCOUNT=0&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;INSERT INTO Table1 VALUES (...)&lt;/p&gt;
&lt;p&gt;...need wrapping in a transaction? i.e if two processes read @@ rowcount as 0 (as the row didn't exist and then both try and INSERT you'd get an error? Or would SQL server run the whole statement as an atomic operation?&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#9930059</link><pubDate>Mon, 30 Nov 2009 07:14:23 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9930059</guid><dc:creator>Jeremiah.Clark</dc:creator><description>&lt;p&gt;chris, &lt;/p&gt;
&lt;p&gt; &amp;nbsp;Although I cannot find the official documentation that backs this up, I am slmost 100% sure that @@rowcount is scoped to the query/session/connection that you are using. &amp;nbsp;As far as I know, it is &amp;quot;thread safe&amp;quot; as pertaining to your question.&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#9938078</link><pubDate>Thu, 17 Dec 2009 09:18:21 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9938078</guid><dc:creator>emmy</dc:creator><description>&lt;p&gt;hi Jeremiah,&lt;/p&gt;
&lt;p&gt;i have a problem inserting data only once.&lt;/p&gt;
&lt;p&gt;i knew that the data is inserted twice as i'm grasping these data from finger print device.for example i grasp data from 28/10/2009 from the buffer of the machine.i just want to compare datetime field in database with today's date.suppose&lt;/p&gt;
&lt;p&gt;min(datetime1) is 28/10/2009 and max(datetime1) is 13/12/2009 i want to insert 14,15 &amp;amp; 16/12.if tommorrow is 17/12/2009 .&lt;/p&gt;
&lt;p&gt;i tried to make condition to &amp;nbsp;insert only today's date but it keeps inserting it.i mean i eant to make sure it's inserted only once.if it's exists i may make update to the existing data.&lt;/p&gt;
&lt;p&gt;i tried to make this procedure: it didnt work out.&lt;/p&gt;
&lt;p&gt;create procedure sp_attendance5 @EnrollNo int=null,@name1 varchar(20)=null,&lt;/p&gt;
&lt;p&gt;@datetime1 datetime=NULL,@check1 varchar(20)=null,@num int= null&lt;/p&gt;
&lt;p&gt;as&lt;/p&gt;
&lt;p&gt;begin&lt;/p&gt;
&lt;p&gt;IF EXISTS (select EnrollNo,name1,datetime1,check1 from attendance)&lt;/p&gt;
&lt;p&gt;update attendance set (EnrollNo=@EnrollNo,name1=@name1,datetime1=@datetime1,check1=@check1) where num=@num&lt;/p&gt;
&lt;p&gt;else&lt;/p&gt;
&lt;p&gt;if datetime1&amp;lt;=dateadd(dd,DATEDIFF(dd,0,getdate()),0) &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/p&gt;
&lt;p&gt;insert into attendance(EnrollNo,name1,datetime1,check1) values (@EnrollNo,@name1,@datetime1,@check1)&lt;/p&gt;
&lt;p&gt;end&lt;/p&gt;
&lt;p&gt;go&lt;/p&gt;
&lt;p&gt; &amp;nbsp;the primary key in attendance database is Num it's auto increment &lt;/p&gt;
&lt;p&gt; Num,EnrollNo,name1,datetime1,check1&lt;/p&gt;
&lt;p&gt;1 134 Adam Hurry 28/10/2009 14:29:49 check in &lt;/p&gt;
&lt;p&gt;2 102 Noel Lipson 28/10/2009 17:21:12 check in &lt;/p&gt;
&lt;p&gt;3 102 Noel Lipson 28/10/2009 17:21:19 Check Out &lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#9938352</link><pubDate>Thu, 17 Dec 2009 19:46:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9938352</guid><dc:creator>Brian</dc:creator><description>&lt;p&gt;I had a need for a similar use, but on MySQL. &amp;nbsp;Instead of @@ROWCOUNT you can use ROW_COUNT(), but in using it, I revealed another interesting problem...&lt;/p&gt;
&lt;p&gt;Affected rows (in mySQL anyways) counts an affected row only if it CHANGES. &amp;nbsp;This means that if a row exists and your update was successful, it may still return 0 affected rows. &amp;nbsp;If you don't know if the row exists, chances are you might not know if it's changing either.&lt;/p&gt;
&lt;p&gt;This means this method may add a duplicate row anyways!&lt;/p&gt;
&lt;p&gt;At least this is what I encountered in mySQL. &amp;nbsp;I would have liked to do it all in one Query, but until I know a way that works better, I'll have to settle!&lt;/p&gt;
&lt;p&gt;Brian&lt;/p&gt;
</description></item><item><title>re: SQL: If Exists Update Else Insert</title><link>http://blogs.msdn.com/miah/archive/2008/02/17/sql-if-exists-update-else-insert.aspx#9939067</link><pubDate>Sat, 19 Dec 2009 08:15:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9939067</guid><dc:creator>emmy</dc:creator><description>&lt;p&gt;but i'm not using my sql.it's sql server 2005!.&lt;/p&gt;
</description></item></channel></rss>