<?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>merge &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/merge/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Fri, 22 Sep 2017 21:51:51 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>
	<item>
		<title>PostgreSQL: Insert &#8211; Update or Upsert &#8211; Merge using writable CTE</title>
		<link>https://www.dbrnd.com/2015/10/postgresql-insert-update-or-upsert-merge-using-writable-cte/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 10 Oct 2015 18:26:43 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[cte]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[merge]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[upsert]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=795</guid>

					<description><![CDATA[In this post, I am sharing a demonstration on how to write merge statement using CTE of PostgreSQL. PostgreSQL 9.1 has a new feature to write CTE and to use the CTE you can also merge INSERT/UPDATE in an expression. The merge statement is always a discussion point for Database Developers of MySQL/PostgreSQL. Below is [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F10%2Fpostgresql-insert-update-or-upsert-merge-using-writable-cte%2F&amp;linkname=PostgreSQL%3A%20Insert%20%E2%80%93%20Update%20or%20Upsert%20%E2%80%93%20Merge%20using%20writable%20CTE" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F10%2Fpostgresql-insert-update-or-upsert-merge-using-writable-cte%2F&amp;linkname=PostgreSQL%3A%20Insert%20%E2%80%93%20Update%20or%20Upsert%20%E2%80%93%20Merge%20using%20writable%20CTE" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F10%2Fpostgresql-insert-update-or-upsert-merge-using-writable-cte%2F&amp;linkname=PostgreSQL%3A%20Insert%20%E2%80%93%20Update%20or%20Upsert%20%E2%80%93%20Merge%20using%20writable%20CTE" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F10%2Fpostgresql-insert-update-or-upsert-merge-using-writable-cte%2F&amp;linkname=PostgreSQL%3A%20Insert%20%E2%80%93%20Update%20or%20Upsert%20%E2%80%93%20Merge%20using%20writable%20CTE" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F10%2Fpostgresql-insert-update-or-upsert-merge-using-writable-cte%2F&amp;linkname=PostgreSQL%3A%20Insert%20%E2%80%93%20Update%20or%20Upsert%20%E2%80%93%20Merge%20using%20writable%20CTE" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F10%2Fpostgresql-insert-update-or-upsert-merge-using-writable-cte%2F&amp;linkname=PostgreSQL%3A%20Insert%20%E2%80%93%20Update%20or%20Upsert%20%E2%80%93%20Merge%20using%20writable%20CTE" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F10%2Fpostgresql-insert-update-or-upsert-merge-using-writable-cte%2F&#038;title=PostgreSQL%3A%20Insert%20%E2%80%93%20Update%20or%20Upsert%20%E2%80%93%20Merge%20using%20writable%20CTE" data-a2a-url="https://www.dbrnd.com/2015/10/postgresql-insert-update-or-upsert-merge-using-writable-cte/" data-a2a-title="PostgreSQL: Insert – Update or Upsert – Merge using writable CTE"></a></p><p>In this post, I am sharing a demonstration on how to write merge statement using CTE of PostgreSQL.</p>
<p>PostgreSQL 9.1 has a new feature to write CTE and to use the CTE you can also merge INSERT/UPDATE in an expression.</p>
<p>The merge statement is always a discussion point for Database Developers of MySQL/PostgreSQL.<br />
Below is a full demonstration to insert a record if not exist and update a record if they exist.</p>
<p><strong>Let&#8217;s first create a sample table:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_TestUpsert 
(
	Rno INTEGER
	,Name VARCHAR(100)
);</pre>
<p><strong>Now create a Stored Function:</strong></p><pre class="crayon-plain-tag">CREATE OR REPLACE FUNCTION fn_InserUpdateMergeDemo
(
	InputRno INTEGER  
	,InputName VARCHAR(100)
)
RETURNS integer AS
$BODY$	
DECLARE 	
BEGIN	
	WITH  to_be_upserted 
	(Rno,Name) AS 
	(VALUES(InputRno,InputName))
	,updated AS 
	(
		UPDATE tbl_TestUpsert
		SET Rno=InputRno,Name=InputName
		FROM to_be_upserted
		WHERE tbl_TestUpsert.Rno = to_be_upserted.Rno
		RETURNING tbl_TestUpsert.Rno
	)
	INSERT INTO tbl_TestUpsert(Rno,Name)
	SELECT Rno,Name
	FROM to_be_upserted
	WHERE Rno NOT IN (SELECT Rno FROM updated);	
	
	RETURN 0;
	EXCEPTION WHEN OTHERS THEN	
	RAISE;	
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;</pre><p>
<p>As you can see in the above function that CTE is created with two input parameters and merge INSERT/UPDATE using an expression.</p>
<p><strong>Now call this function with sample data:</strong></p><pre class="crayon-plain-tag">SELECT *FROM fn_InserUpdateMergeDemo(1,'Anvesh');
SELECT *FROM fn_InserUpdateMergeDemo(2,'Neevan');
SELECT *FROM fn_InserUpdateMergeDemo(3,'Ronika');
SELECT *FROM fn_InserUpdateMergeDemo(2,'Roy');</pre><p>
<p>You can find the two records have same rno column and &#8220;Neevan&#8221; updated by &#8220;Roy&#8221;.<br />
<strong>Check this result:</strong></p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2015/10/PostgreSQL-Upsert-Merge.png"><img decoding="async" class="alignnone wp-image-801 size-full" title="PostgreSQL Upsert Merge" src="https://www.dbrnd.com/wp-content/uploads/2015/10/PostgreSQL-Upsert-Merge.png" alt="PostgreSQL Upsert Merge" width="201" height="103" srcset="https://www.dbrnd.com/wp-content/uploads/2015/10/PostgreSQL-Upsert-Merge.png 201w, https://www.dbrnd.com/wp-content/uploads/2015/10/PostgreSQL-Upsert-Merge-150x77.png 150w" sizes="(max-width: 201px) 100vw, 201px" /></a></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
