<?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"
	>
<channel>
	<title>Comments on: Combining insert and update SQL logic</title>
	<atom:link href="http://www.chapter31.com/2007/04/11/combining-update-and-insert-sql-logic/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.chapter31.com/2007/04/11/combining-update-and-insert-sql-logic/</link>
	<description>development in a land far far away...</description>
	<pubDate>Thu, 28 Aug 2008 16:35:44 +0000</pubDate>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>By: Phoenix Web Concepts &#187; Blog Archive &#187; Creating a dynamic stored procedure with SQL Server and ColdFusion</title>
		<link>http://www.chapter31.com/2007/04/11/combining-update-and-insert-sql-logic/#comment-17331</link>
		<dc:creator>Phoenix Web Concepts &#187; Blog Archive &#187; Creating a dynamic stored procedure with SQL Server and ColdFusion</dc:creator>
		<pubDate>Fri, 17 Aug 2007 02:58:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.chapter31.com/2007/04/11/combining-update-and-insert-sql-logic/#comment-17331</guid>
		<description>[...] stuck again. After some more searching I lucked out and found this article. If you look at the second code example you&#8217;ll see where he&#8217;s using another property of [...]</description>
		<content:encoded><![CDATA[<p>[...] stuck again. After some more searching I lucked out and found this article. If you look at the second code example you&#8217;ll see where he&#8217;s using another property of [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew McGregor</title>
		<link>http://www.chapter31.com/2007/04/11/combining-update-and-insert-sql-logic/#comment-15506</link>
		<dc:creator>Andrew McGregor</dc:creator>
		<pubDate>Wed, 25 Jul 2007 05:47:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.chapter31.com/2007/04/11/combining-update-and-insert-sql-logic/#comment-15506</guid>
		<description>For MySQL users there is the REPLACE INTO command (to replace INSERT INTO statements).  This looks for matching rows with the same unique keys and deletes them before adding the new row.</description>
		<content:encoded><![CDATA[<p>For MySQL users there is the REPLACE INTO command (to replace INSERT INTO statements).  This looks for matching rows with the same unique keys and deletes them before adding the new row.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael Sharman</title>
		<link>http://www.chapter31.com/2007/04/11/combining-update-and-insert-sql-logic/#comment-6105</link>
		<dc:creator>Michael Sharman</dc:creator>
		<pubDate>Wed, 11 Apr 2007 23:35:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.chapter31.com/2007/04/11/combining-update-and-insert-sql-logic/#comment-6105</guid>
		<description>Hi Chip,

I see what you're saying, what I was getting at in the example though was avoiding having that logic in your application code. 

The example I used could easily return SCOPE_IDENTITY from the stored proc as well, thanks for that!</description>
		<content:encoded><![CDATA[<p>Hi Chip,</p>
<p>I see what you&#8217;re saying, what I was getting at in the example though was avoiding having that logic in your application code. </p>
<p>The example I used could easily return SCOPE_IDENTITY from the stored proc as well, thanks for that!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael Sharman</title>
		<link>http://www.chapter31.com/2007/04/11/combining-update-and-insert-sql-logic/#comment-6104</link>
		<dc:creator>Michael Sharman</dc:creator>
		<pubDate>Wed, 11 Apr 2007 23:30:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.chapter31.com/2007/04/11/combining-update-and-insert-sql-logic/#comment-6104</guid>
		<description>Hi Tony,

I didn't know this was the case, but reading the link you provided doesn't hold up for me.

 - Structual changes are not being made
 - Not using WITH RECOMPILE
 - Not using large amounts of INSERT, UPDATE or DELETE

I haven't come across anything which states that using conditional logic inside a SP will make it recompile on every execution.

The following was from &lt;a href="http://msdn2.microsoft.com/en-us/library/aa174792(SQL.80).aspx" rel="nofollow"&gt;MSDN&lt;/a&gt; (for MSSQL2000):

"If these SQL statements and conditional logic are written into a stored procedure, they become part of a single execution plan on the server."

Mind you I haven't used the Profiler on this to try and capture when the SP is recompiling :)

Maybe that's my next step.</description>
		<content:encoded><![CDATA[<p>Hi Tony,</p>
<p>I didn&#8217;t know this was the case, but reading the link you provided doesn&#8217;t hold up for me.</p>
<p> - Structual changes are not being made<br />
 - Not using WITH RECOMPILE<br />
 - Not using large amounts of INSERT, UPDATE or DELETE</p>
<p>I haven&#8217;t come across anything which states that using conditional logic inside a SP will make it recompile on every execution.</p>
<p>The following was from <a href="http://msdn2.microsoft.com/en-us/library/aa174792(SQL.80).aspx" rel="nofollow">MSDN</a> (for MSSQL2000):</p>
<p>&#8220;If these SQL statements and conditional logic are written into a stored procedure, they become part of a single execution plan on the server.&#8221;</p>
<p>Mind you I haven&#8217;t used the Profiler on this to try and capture when the SP is recompiling <img src='http://www.chapter31.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Maybe that&#8217;s my next step.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Qasim Rasheed</title>
		<link>http://www.chapter31.com/2007/04/11/combining-update-and-insert-sql-logic/#comment-6089</link>
		<dc:creator>Qasim Rasheed</dc:creator>
		<pubDate>Wed, 11 Apr 2007 21:22:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.chapter31.com/2007/04/11/combining-update-and-insert-sql-logic/#comment-6089</guid>
		<description>Oracle has nice feature named Merge aka Upsert that make this a breeze. Here is a url that explain it usage.

http://www.psoug.org/reference/merge.html

Thanks</description>
		<content:encoded><![CDATA[<p>Oracle has nice feature named Merge aka Upsert that make this a breeze. Here is a url that explain it usage.</p>
<p><a href="http://www.psoug.org/reference/merge.html" rel="nofollow">http://www.psoug.org/reference/merge.html</a></p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chip Temm</title>
		<link>http://www.chapter31.com/2007/04/11/combining-update-and-insert-sql-logic/#comment-6088</link>
		<dc:creator>Chip Temm</dc:creator>
		<pubDate>Wed, 11 Apr 2007 21:12:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.chapter31.com/2007/04/11/combining-update-and-insert-sql-logic/#comment-6088</guid>
		<description>Good idea.  A couple of notes.
If you are wrapping this in a CFC function, you could handle what Tony talks about inside the function, calling different SPs inside the function depending on whether a myprimarykey is passed.

I would recommend that such a function always return the primarykey value because after successful execution, you will always have one.

For SQL server, you could use the following to return this from your insert:
 IF (@myPrimaryKey Is Null)  
 BEGIN  
     --no record exists so run an INSERT statement  
     INSERT INTO myTable(FirstName, LastName, Email, Age)  
     VALUES      (@firstName, @lastName, @email, @age)  

SELECT SCOPE_IDENTITY()
 END</description>
		<content:encoded><![CDATA[<p>Good idea.  A couple of notes.<br />
If you are wrapping this in a CFC function, you could handle what Tony talks about inside the function, calling different SPs inside the function depending on whether a myprimarykey is passed.</p>
<p>I would recommend that such a function always return the primarykey value because after successful execution, you will always have one.</p>
<p>For SQL server, you could use the following to return this from your insert:<br />
 IF (@myPrimaryKey Is Null)<br />
 BEGIN<br />
     &#8211;no record exists so run an INSERT statement<br />
     INSERT INTO myTable(FirstName, LastName, Email, Age)<br />
     VALUES      (@firstName, @lastName, @email, @age)  </p>
<p>SELECT SCOPE_IDENTITY()<br />
 END</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jim collins</title>
		<link>http://www.chapter31.com/2007/04/11/combining-update-and-insert-sql-logic/#comment-6066</link>
		<dc:creator>jim collins</dc:creator>
		<pubDate>Wed, 11 Apr 2007 17:28:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.chapter31.com/2007/04/11/combining-update-and-insert-sql-logic/#comment-6066</guid>
		<description>Another approach is to do an update, run select @@ as recordsUpdated , check the value of recordsupdated, and if it's 0 , do an insert. This is similar to the Oracle Upsert concept.</description>
		<content:encoded><![CDATA[<p>Another approach is to do an update, run select @@ as recordsUpdated , check the value of recordsupdated, and if it&#8217;s 0 , do an insert. This is similar to the Oracle Upsert concept.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tony petruzzi</title>
		<link>http://www.chapter31.com/2007/04/11/combining-update-and-insert-sql-logic/#comment-6042</link>
		<dc:creator>tony petruzzi</dc:creator>
		<pubDate>Wed, 11 Apr 2007 14:04:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.chapter31.com/2007/04/11/combining-update-and-insert-sql-logic/#comment-6042</guid>
		<description>you do realize that by having a conditional statement inside your stored procedure (SP) you negate the benefits of using a SP since it will be recompiled on every execution.

This page has more information:
http://www.databasejournal.com/features/mssql/article.php/1565961

The proper way is to break down your SP into smaller SPs and having the main SP call them. In your example, you would create 3 SPs. One for the insert, one for the update and the main that would call the insert or update SP. This would give you better performance since the insert and update SPs wouldn't be recompiled only the main one would.</description>
		<content:encoded><![CDATA[<p>you do realize that by having a conditional statement inside your stored procedure (SP) you negate the benefits of using a SP since it will be recompiled on every execution.</p>
<p>This page has more information:<br />
<a href="http://www.databasejournal.com/features/mssql/article.php/1565961" rel="nofollow">http://www.databasejournal.com/features/mssql/article.php/1565961</a></p>
<p>The proper way is to break down your SP into smaller SPs and having the main SP call them. In your example, you would create 3 SPs. One for the insert, one for the update and the main that would call the insert or update SP. This would give you better performance since the insert and update SPs wouldn&#8217;t be recompiled only the main one would.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
