<?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>cte &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/cte/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>Tue, 03 Oct 2017 13:21:55 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.6</generator>
	<item>
		<title>SQL Server: Database Developer (T-SQL) Interview Questions and Answers (Day-5)</title>
		<link>https://www.dbrnd.com/2016/06/sql-server-database-developer-t-sql-interview-questions-and-answers-database-join-temporary-table-table-variable-cardinality-day-5/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Thu, 16 Jun 2016 18:12:49 +0000</pubDate>
				<category><![CDATA[SQL Server Interview]]></category>
		<category><![CDATA[answers]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[cardinality]]></category>
		<category><![CDATA[cte]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[Database Developer]]></category>
		<category><![CDATA[database joins]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[questions]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tunning]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Table Variable]]></category>
		<category><![CDATA[Temp Table]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3045</guid>

					<description><![CDATA[What is an Inner join? The join that displays only the rows that have a match in both the joined tables is known as inner join. What is an Equi join? Equi join is a special type of join in which we use only equality operator. Hence, when you make a query for join using [&#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%2F2016%2F06%2Fsql-server-database-developer-t-sql-interview-questions-and-answers-database-join-temporary-table-table-variable-cardinality-day-5%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-5%29" 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%2F2016%2F06%2Fsql-server-database-developer-t-sql-interview-questions-and-answers-database-join-temporary-table-table-variable-cardinality-day-5%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-5%29" 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%2F2016%2F06%2Fsql-server-database-developer-t-sql-interview-questions-and-answers-database-join-temporary-table-table-variable-cardinality-day-5%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-5%29" 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%2F2016%2F06%2Fsql-server-database-developer-t-sql-interview-questions-and-answers-database-join-temporary-table-table-variable-cardinality-day-5%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-5%29" 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%2F2016%2F06%2Fsql-server-database-developer-t-sql-interview-questions-and-answers-database-join-temporary-table-table-variable-cardinality-day-5%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-5%29" 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%2F2016%2F06%2Fsql-server-database-developer-t-sql-interview-questions-and-answers-database-join-temporary-table-table-variable-cardinality-day-5%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-5%29" 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%2F2016%2F06%2Fsql-server-database-developer-t-sql-interview-questions-and-answers-database-join-temporary-table-table-variable-cardinality-day-5%2F&#038;title=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-5%29" data-a2a-url="https://www.dbrnd.com/2016/06/sql-server-database-developer-t-sql-interview-questions-and-answers-database-join-temporary-table-table-variable-cardinality-day-5/" data-a2a-title="SQL Server: Database Developer (T-SQL) Interview Questions and Answers (Day-5)"></a></p><p><strong>What is an Inner join?</strong></p>
<blockquote><p>The join that displays only the rows that have a match in both the joined tables is known as inner join.</p></blockquote>
<p><strong>What is an Equi join?</strong></p>
<blockquote><p>Equi join is a special type of join in which we use only equality operator. Hence, when you make a query for join using equality operator, then that join query comes under Equi join.</p>
<p>Equi join has only (=) operator in join condition.</p>
<p>Equi join can be inner join, left outer join, right outer join.</p></blockquote>
<p><strong>What is a Left Outer join?</strong></p>
<blockquote><p>Left join displays all the rows from the first table and matched rows from second table like that.</p></blockquote>
<p><strong>What is a Right outer join?</strong></p>
<blockquote><p>Right outer join displays all the rows of second table and matched rows from first table.</p></blockquote>
<p><strong>What is a Full outer join?</strong></p>
<blockquote><p>Full outer join returns all the rows from both tables whether it has been matched or not.</p></blockquote>
<p><strong>What is a Cross Join?</strong></p>
<blockquote><p>A cross join that produces Cartesian product of the tables that are involved in the join. The size of a Cartesian product is the number of the rows in the first table multiplied by the number of rows in the second table.</p></blockquote>
<p><strong>What is a Self join?</strong></p>
<blockquote><p>Joining the table itself called self join. Self join is used to retrieve the records having some relation or similarity with other records in the same table. Here, we need to use aliases for the same table to set a self join between single table and retrieve records satisfying the condition in where clause</p></blockquote>
<p><strong>What is the difference between cross join and Full outer join?</strong></p>
<p><strong>Cross Join:</strong></p>
<ul>
<li>It produces the Cartesian product.</li>
<li>Results in Cartesian product of two tables.</li>
<li>Results in pairs of rows.</li>
<li>No join conditions are specified.</li>
</ul>
<p><strong>Full Outer Join:</strong></p>
<ul>
<li>It includes all the rows from both the tables.</li>
<li>Assigns NULL for unmatched fields.</li>
<li>Results in every row from both of the tables , at least once.</li>
<li>A combination of both left and right outer joins.</li>
</ul>
<p><strong>What is the difference between Temp table and Common table Expression (CTE)?</strong></p>
<blockquote class="wp-embedded-content" data-secret="7sE1TwDePR"><p><a href="https://www.dbrnd.com/2016/02/sql-server-difference-between-temp-table-and-common-table-expression-cte/">SQL Server: Difference between Temp Table and Common Table Expression (CTE)</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/02/sql-server-difference-between-temp-table-and-common-table-expression-cte/embed/#?secret=7sE1TwDePR" data-secret="7sE1TwDePR" width="600" height="338" title="&#8220;SQL Server: Difference between Temp Table and Common Table Expression (CTE)&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p><strong>What is the difference between Temp table and Table Variable?</strong></p>
<blockquote class="wp-embedded-content" data-secret="5yNwchT1WL"><p><a href="https://www.dbrnd.com/2016/02/sql-server-difference-between-temporary-table-and-table-variable/">SQL Server: Difference between Temporary Table and Table Variable</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/02/sql-server-difference-between-temporary-table-and-table-variable/embed/#?secret=5yNwchT1WL" data-secret="5yNwchT1WL" width="600" height="338" title="&#8220;SQL Server: Difference between Temporary Table and Table Variable&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p><strong>What is the difference between Local and Global Temp Table?</strong></p>
<p>There are two types of temporary tables: local and global. Local temporary tables are visible only to their creators during the same connection to an instance of SQL Server as when the tables were first created or referenced. Local temporary tables are deleted after the user disconnects from the instance of SQL Server. Global temporary tables are visible to any user and any connection after they are created, and are deleted when all users that are referencing the table disconnect from the instance of SQL Server.</p>
<p><strong>How to execute Large SQL Script File by using SQLCmd?</strong></p>
<blockquote><p>sqlcmd -S [servername] -d [databasename] -i [scriptfilename]</blockquote>
<p><strong>What is ASCII method?</strong></p>
<blockquote><p>ASCII will return the ascii value of the character expression.</p></blockquote>
<p><strong>Would do you please write syntax of table variable?</strong></p>
<blockquote><p>DECLARE @TableVariabl Table (ID INT, Name VARCHAR(150))</p></blockquote>
<p><strong>How do we unlock particular user in SQL Server?</strong></p>
<blockquote><p>First login with system admin user and execute below ALTER command.</p>
<p>ALTER LOGIN login_name WITH password=&#8217;password&#8217; unlock;</p></blockquote>
<p><strong>What is the performance difference between EXISTS and IN?</strong></p>
<blockquote><p>EXISTS is efficient as if any row returned and it stops the further search.</p>
<p>IN is looking for a specific match which takes longer.</p>
<p>However, they may not always replace each other and we should use as per our system requirements.</p></blockquote>
<p><strong>Is SQL Case sensitive?</strong></p>
<blockquote><p>No: SQL is not case sensitive.</p></blockquote>
<p><strong>What are the different types of cardinality in SQL?</strong></p>
<blockquote><p><strong>High-cardinality:</strong> refers to columns with values that are very uncommon or unique. High-cardinality column values are typically identification numbers, email addresses, or user names. An example of a data table column with high-cardinality would be a USERS table with a column named USER_ID. This column would contain unique values of 1-n. Each time a new user is created in the USERS table, a new number would be created in the USER_ID column to identify them uniquely. Since the values held in the USER_ID column are unique, this column&#8217;s cardinality type would be referred to as high-cardinality.</p>
<p><strong>Normal-cardinality:</strong> refers to columns with values that are somewhat uncommon. Normal-cardinality column values are typically names, street addresses, or vehicle types. An example of a data table column with normal-cardinality would be a CUSTOMER table with a column named LAST_NAME, containing the last names of customers. While some people have common last names, such as Smith, others have uncommon last names. Therefore, an examination of all of the values held in the LAST_NAME column would show &#8220;clumps&#8221; of names in some places (e.g. a lot of Smith&#8217;s) surrounded on both sides by a long series of unique values. Since there is a variety of possible values held in this column, its cardinality type would be referred to as normal-cardinality.</p>
<p><strong>Low-cardinality:</strong> refers to columns with few unique values. Low-cardinality column values are typically status flags, Boolean values, or major classifications such as gender. An example of a data table column with low-cardinality would be a CUSTOMER table with a column named NEW_CUSTOMER. This column would contain only two distinct values: Y or N, denoting whether the customer was new or not. Since there are only two possible values held in this column, its cardinality type would be referred to as low-cardinality.</p></blockquote>
<p><strong>How to find any Text, Column, Comment, Object Name in SQL Server Objects?</strong></p>
<blockquote class="wp-embedded-content" data-secret="LHHgugG5ak"><p><a href="https://www.dbrnd.com/2015/06/how-to-find-any-text-column-comment-object-name-in-sql-server-objects/">How to find any Text, Column, Comment, Object Name in SQL Server Objects</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/06/how-to-find-any-text-column-comment-object-name-in-sql-server-objects/embed/#?secret=LHHgugG5ak" data-secret="LHHgugG5ak" width="600" height="338" title="&#8220;How to find any Text, Column, Comment, Object Name in SQL Server Objects&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p><strong>How to Find Missing Number and ID in the SQL Server?</strong></p>
<blockquote class="wp-embedded-content" data-secret="nb1IILJnvE"><p><a href="https://www.dbrnd.com/2015/06/query-to-find-missing-number-id-sql-server/">Query to Find Missing Number and ID in SQL Server</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/06/query-to-find-missing-number-id-sql-server/embed/#?secret=nb1IILJnvE" data-secret="nb1IILJnvE" width="600" height="338" title="&#8220;Query to Find Missing Number and ID in SQL Server&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Server: Difference between Temp Table and Common Table Expression (CTE)</title>
		<link>https://www.dbrnd.com/2016/02/sql-server-difference-between-temp-table-and-common-table-expression-cte/</link>
					<comments>https://www.dbrnd.com/2016/02/sql-server-difference-between-temp-table-and-common-table-expression-cte/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Thu, 18 Feb 2016 19:21:44 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[Common Table Expression]]></category>
		<category><![CDATA[cte]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tunning]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[Temp Table]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=1621</guid>

					<description><![CDATA[In this post, I am sharing the important difference between Temp Table and Common Table Expression (CTE) of the SQL Server. I recently wrote an article on the difference between Temp Table and Table Variable of the SQL Server. SQL Server: Difference between Temporary Table and Table Variable The theory and practice of Temp Table, [&#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%2F2016%2F02%2Fsql-server-difference-between-temp-table-and-common-table-expression-cte%2F&amp;linkname=SQL%20Server%3A%20Difference%20between%20Temp%20Table%20and%20Common%20Table%20Expression%20%28CTE%29" 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%2F2016%2F02%2Fsql-server-difference-between-temp-table-and-common-table-expression-cte%2F&amp;linkname=SQL%20Server%3A%20Difference%20between%20Temp%20Table%20and%20Common%20Table%20Expression%20%28CTE%29" 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%2F2016%2F02%2Fsql-server-difference-between-temp-table-and-common-table-expression-cte%2F&amp;linkname=SQL%20Server%3A%20Difference%20between%20Temp%20Table%20and%20Common%20Table%20Expression%20%28CTE%29" 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%2F2016%2F02%2Fsql-server-difference-between-temp-table-and-common-table-expression-cte%2F&amp;linkname=SQL%20Server%3A%20Difference%20between%20Temp%20Table%20and%20Common%20Table%20Expression%20%28CTE%29" 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%2F2016%2F02%2Fsql-server-difference-between-temp-table-and-common-table-expression-cte%2F&amp;linkname=SQL%20Server%3A%20Difference%20between%20Temp%20Table%20and%20Common%20Table%20Expression%20%28CTE%29" 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%2F2016%2F02%2Fsql-server-difference-between-temp-table-and-common-table-expression-cte%2F&amp;linkname=SQL%20Server%3A%20Difference%20between%20Temp%20Table%20and%20Common%20Table%20Expression%20%28CTE%29" 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%2F2016%2F02%2Fsql-server-difference-between-temp-table-and-common-table-expression-cte%2F&#038;title=SQL%20Server%3A%20Difference%20between%20Temp%20Table%20and%20Common%20Table%20Expression%20%28CTE%29" data-a2a-url="https://www.dbrnd.com/2016/02/sql-server-difference-between-temp-table-and-common-table-expression-cte/" data-a2a-title="SQL Server: Difference between Temp Table and Common Table Expression (CTE)"></a></p><p>In this post, I am sharing the important difference between Temp Table and Common Table Expression (CTE) of the SQL Server.<br />
<code>I recently wrote an article on the difference between Temp Table and Table Variable of the SQL Server. </code></p>
<blockquote class="wp-embedded-content" data-secret="hLRo3QNO6Y"><p><a href="https://www.dbrnd.com/2016/02/sql-server-difference-between-temporary-table-and-table-variable/">SQL Server: Difference between Temporary Table and Table Variable</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/02/sql-server-difference-between-temporary-table-and-table-variable/embed/#?secret=hLRo3QNO6Y" data-secret="hLRo3QNO6Y" width="600" height="338" title="&#8220;SQL Server: Difference between Temporary Table and Table Variable&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p>The theory and practice of Temp Table, Table Variable and CTE is one of my favourite topics of the SQL Server. </p>
<blockquote><p>A CTE is used for a temporary result set that is defined within the execution scope of the query.<br />
A Temp Table is also used for a temporary result set, but it can be defined for limited execution scope or can be used to define for global execution scope as a Global Temp Table. </p></blockquote>
<blockquote><p>A CTE is substituted for a view when the general use of a view is not required, and you do not have to store the definition of metadata. </p></blockquote>
<blockquote><p>CTE always uses memory and Temp Table stored in the TempDB. </p></blockquote>
<blockquote><p>The Scope of CTE is only for the query and can be referenced multiple times in other CTEs.<br />
The child objects also access the Temp Table, and it persists until the end of the sessions.
</p></blockquote>
<blockquote><p>We can not use NOLOCK and other isolation levels with CTE and with the Temp Table, we can apply it explicitly. </p></blockquote>
<blockquote><p>If we require to CTE for another batch query, it requires recreation of it.<br />
The Temp Table can be used for all batches of a session. </p></blockquote>
<blockquote><p>A CTE doesn&#8217;t maintain any statistics and any metadata information, Temp table is maintaining a require statistics.</p></blockquote>
<blockquote><p>A CTE is a never part of the transaction and locking and its treated as system transaction.<br />
The temp table operations are a part of user transactions. </p></blockquote>
<blockquote><p>If we have a large dataset, we should use Temp Table and with the small dataset, we can use CTE very effectively. </p></blockquote>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2016/02/sql-server-difference-between-temp-table-and-common-table-expression-cte/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
		<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 loading="lazy" 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="auto, (max-width: 201px) 100vw, 201px" /></a></p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
