<?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>select &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/select/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>Mon, 02 Apr 2018 19:42:25 +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>SQL Server Interview: How to prevent &#8216;SELECT *&#8217; using T-SQL Script?</title>
		<link>https://www.dbrnd.com/2017/02/sql-server-interview-how-to-prevent-select-using-t-sql-script-select-all-column/</link>
					<comments>https://www.dbrnd.com/2017/02/sql-server-interview-how-to-prevent-select-using-t-sql-script-select-all-column/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sun, 19 Feb 2017 16:16:56 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Interview]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Prevent SELECT *]]></category>
		<category><![CDATA[select]]></category>
		<category><![CDATA[SELECT ALL Column]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server Administrator]]></category>
		<category><![CDATA[SQL Server Error]]></category>
		<category><![CDATA[SQL Server Monitoring]]></category>
		<category><![CDATA[SQL Server Performance Tuning]]></category>
		<category><![CDATA[SQL Server Programming]]></category>
		<category><![CDATA[SQL Server Tips and Tricks]]></category>
		<category><![CDATA[t-sql interview]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=4581</guid>

					<description><![CDATA[<div><img width="368" height="336" src="https://www.dbrnd.com/wp-content/uploads/2017/02/Anvesh-2-min.jpg" class="attachment-post-image size-post-image wp-post-image" alt="Anvesh (2)-min" decoding="async" fetchpriority="high" /></div>Guys, Don&#8217;t surprise with this question. If you are in front of me, surely I will ask this question in an interview. I know, most of you are using &#8216;SELECT *&#8217; in T-SQL code. I already shared an article on &#8216;SELECT *&#8217; to be good or bad. SELECT all columns to be good or bad [&#8230;]]]></description>
										<content:encoded><![CDATA[<div><img width="368" height="336" src="https://www.dbrnd.com/wp-content/uploads/2017/02/Anvesh-2-min.jpg" class="attachment-post-image size-post-image wp-post-image" alt="Anvesh (2)-min" decoding="async" /></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2017%2F02%2Fsql-server-interview-how-to-prevent-select-using-t-sql-script-select-all-column%2F&amp;linkname=SQL%20Server%20Interview%3A%20How%20to%20prevent%20%E2%80%98SELECT%20%2A%E2%80%99%20using%20T-SQL%20Script%3F" 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%2F2017%2F02%2Fsql-server-interview-how-to-prevent-select-using-t-sql-script-select-all-column%2F&amp;linkname=SQL%20Server%20Interview%3A%20How%20to%20prevent%20%E2%80%98SELECT%20%2A%E2%80%99%20using%20T-SQL%20Script%3F" 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%2F2017%2F02%2Fsql-server-interview-how-to-prevent-select-using-t-sql-script-select-all-column%2F&amp;linkname=SQL%20Server%20Interview%3A%20How%20to%20prevent%20%E2%80%98SELECT%20%2A%E2%80%99%20using%20T-SQL%20Script%3F" 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%2F2017%2F02%2Fsql-server-interview-how-to-prevent-select-using-t-sql-script-select-all-column%2F&amp;linkname=SQL%20Server%20Interview%3A%20How%20to%20prevent%20%E2%80%98SELECT%20%2A%E2%80%99%20using%20T-SQL%20Script%3F" 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%2F2017%2F02%2Fsql-server-interview-how-to-prevent-select-using-t-sql-script-select-all-column%2F&amp;linkname=SQL%20Server%20Interview%3A%20How%20to%20prevent%20%E2%80%98SELECT%20%2A%E2%80%99%20using%20T-SQL%20Script%3F" 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%2F2017%2F02%2Fsql-server-interview-how-to-prevent-select-using-t-sql-script-select-all-column%2F&amp;linkname=SQL%20Server%20Interview%3A%20How%20to%20prevent%20%E2%80%98SELECT%20%2A%E2%80%99%20using%20T-SQL%20Script%3F" 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%2F2017%2F02%2Fsql-server-interview-how-to-prevent-select-using-t-sql-script-select-all-column%2F&#038;title=SQL%20Server%20Interview%3A%20How%20to%20prevent%20%E2%80%98SELECT%20%2A%E2%80%99%20using%20T-SQL%20Script%3F" data-a2a-url="https://www.dbrnd.com/2017/02/sql-server-interview-how-to-prevent-select-using-t-sql-script-select-all-column/" data-a2a-title="SQL Server Interview: How to prevent ‘SELECT *’ using T-SQL Script?"></a></p><p><em>Guys, Don&#8217;t surprise with this question. If you are in front of me, surely I will ask this question in an interview. I know, most of you are using &#8216;SELECT *&#8217; in T-SQL code. </em></p>
<p>I already shared an article on &#8216;SELECT *&#8217; to be good or bad.</p>
<blockquote class="wp-embedded-content" data-secret="PdfM9S9wQa"><p><a href="https://www.dbrnd.com/2015/09/select-all-columns-to-be-good-or-bad-in-database-system/">SELECT all columns to be good or bad in database system</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/09/select-all-columns-to-be-good-or-bad-in-database-system/embed/#?secret=PdfM9S9wQa" data-secret="PdfM9S9wQa" width="600" height="338" title="&#8220;SELECT all columns to be good or bad in database system&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p><strong>Regarding database coding standard, I am always strict and even I never allow single extra space in T-SQL Code.</strong></p>
<p>Let me share my experience,</p>
<blockquote><p>In my group, I already set guidelines like &#8216;do not use SELECT *&#8217;, but then also I found total 320 &#8216;SELECT *&#8217; statements out of 2125 statements in our project.</p></blockquote>
<blockquote><p>The Developers are always running with their practice, but as DB Professionals we must prevent SELECT *.</p></blockquote>
<blockquote><p>When I found these bad queries, and I didn&#8217;t react immediately. I had been waiting for tight deadlines. Before a month ago I scheduled an internal demonstration.</p></blockquote>
<blockquote><p>On this occasion, I planned the penalty for those developers who wrote &#8216;SELECT *&#8217; in their stored procedures.</p>
<p>In the morning, I added one extra overnight computed column on those tables. The computed column was (BadSelect AS (1 / 0)), that means if anyone is selecting data using &#8216;SELECT *&#8217;, they get error like &#8216;Divide by zero error encountered&#8217; and fails the execution of a query.</p></blockquote>
<blockquote><p>Now after this alteration, developers had removed all &#8216;SELECT *&#8217; and we started our demonstration late by 3 hours.</p></blockquote>
<blockquote><p>It is OK for me, but next time they will never put &#8216;SELECT *&#8217; in T-SQL Code.</p></blockquote>
<p>Use below demonstration, to test this exercise and prepare yourself for this kind of interview questions as well.</p>
<p><strong>Generate a script to add computed column in all the table:</strong><br />
You can copy script for requiring a table and can create computed column.<br />
<pre class="crayon-plain-tag">SELECT 'ALTER TABLE ' + name + ' ADD BadSelect AS (1 / 0) 
GO'
FROM sys.tables</pre>
<p><strong>If developer execute &#8216;SELECT *&#8217;, they will get a bellow error:</strong><br />
<pre class="crayon-plain-tag">Msg 8134, Level 16, State 1, Line 1
Divide by zero error encountered.</pre>
<p><strong>Generate a script to remove computed column from all the table:</strong><br />
<pre class="crayon-plain-tag">SELECT 'ALTER TABLE ' + name + ' DROP COLUMN BadSelect 
GO'
FROM sys.tables</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2017/02/sql-server-interview-how-to-prevent-select-using-t-sql-script-select-all-column/feed/</wfw:commentRss>
			<slash:comments>9</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Server: Database Developer (T-SQL) Interview Questions and Answers (Day-14)</title>
		<link>https://www.dbrnd.com/2016/06/sql-server-database-developer-t-sql-interview-questions-and-answers-null-notnull-merge-hash-nested-loop-join-day-14/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 25 Jun 2016 04:56:07 +0000</pubDate>
				<category><![CDATA[SQL Server Interview]]></category>
		<category><![CDATA[answers]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[Database Developer]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[hash join]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[merge join]]></category>
		<category><![CDATA[nested loop join]]></category>
		<category><![CDATA[NOT NULL]]></category>
		<category><![CDATA[NULL]]></category>
		<category><![CDATA[questions]]></category>
		<category><![CDATA[select]]></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[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3078</guid>

					<description><![CDATA[Should we allow NULL or We should apply NOT NULL? Database Design: Should we allow NULL or We should apply NOT NULL ? &#8220;SELECT *&#8221; is good practice or bad practice in database system? SELECT all columns to be good or bad in database system What is Left Anti Semi Join Operator? The Left Anti [&#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-null-notnull-merge-hash-nested-loop-join-day-14%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-14%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-null-notnull-merge-hash-nested-loop-join-day-14%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-14%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-null-notnull-merge-hash-nested-loop-join-day-14%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-14%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-null-notnull-merge-hash-nested-loop-join-day-14%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-14%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-null-notnull-merge-hash-nested-loop-join-day-14%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-14%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-null-notnull-merge-hash-nested-loop-join-day-14%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-14%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-null-notnull-merge-hash-nested-loop-join-day-14%2F&#038;title=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-14%29" data-a2a-url="https://www.dbrnd.com/2016/06/sql-server-database-developer-t-sql-interview-questions-and-answers-null-notnull-merge-hash-nested-loop-join-day-14/" data-a2a-title="SQL Server: Database Developer (T-SQL) Interview Questions and Answers (Day-14)"></a></p><p><strong>Should we allow NULL or We should apply NOT NULL?</strong></p>
<blockquote class="wp-embedded-content" data-secret="nn6TWdIgPw"><p><a href="https://www.dbrnd.com/2016/01/database-design-should-we-allow-null-or-we-should-apply-not-null/">Database Design: Should we allow NULL or We should apply NOT NULL ?</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/01/database-design-should-we-allow-null-or-we-should-apply-not-null/embed/#?secret=nn6TWdIgPw" data-secret="nn6TWdIgPw" width="600" height="338" title="&#8220;Database Design: Should we allow NULL or We should apply NOT NULL ?&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p><strong>&#8220;SELECT *&#8221; is good practice or bad practice in database system?</strong></p>
<blockquote class="wp-embedded-content" data-secret="MbeaBBAPFG"><p><a href="https://www.dbrnd.com/2015/09/select-all-columns-to-be-good-or-bad-in-database-system/">SELECT all columns to be good or bad in database system</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/09/select-all-columns-to-be-good-or-bad-in-database-system/embed/#?secret=MbeaBBAPFG" data-secret="MbeaBBAPFG" width="600" height="338" title="&#8220;SELECT all columns to be good or bad in database system&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p><strong>What is Left Anti Semi Join Operator?</strong></p>
<p>The Left Anti Semi Join operator returns the result set for each row from the first (top) input when there is no matching row in the second (bottom) input. If no join predicate exists in the Argument column, each row is a matching row. Left Anti Semi Join is a logical operator.</p>
<p><strong>Why you should not use a cursor? What are its alternatives?</strong></p>
<blockquote><p>Microsoft requests us to avoid using cursors due to Memory and RAM overhead including comparatively more DISK IO. Alternatives: WHILE LOOP / CASE / Temprary Tables / Derived Tables / Sub Queries can be used in place of Cursors.</p></blockquote>
<p><strong>What are Dynamic Queries in T-SQL?</strong></p>
<blockquote><p>Dynamic T-SQL Queries are those queries designed on the fly / at run time using Variables or using CTE or other sources. We use SP_EXECUTESQL Stored Procedure or EXECUTE function to execute such queries.</p></blockquote>
<p><strong>How you can load large data in SQL Server?</strong></p>
<blockquote><p>IMPORT and EXPORT Wizard, BULK INSERT and BCP are the possible options. We need to set the RECOVERY MODEL to BULKLOGGED before we perform such activities.</p></blockquote>
<p><strong>What is Mutex error in Triggers?</strong></p>
<blockquote><p>The only use of &#8220;mutex&#8221; that I know of is for a mechanism for re-entrant code that can&#8217;t be simultaneously executed by multiple processes to force that code to be single threaded.  It&#8217;s not a typical SQL Server term.  If it were, it would apply as much to stored procedures as triggers.  Generally, in a well designed database with well written code, this would be handled by using standard data locking and transactions.</p></blockquote>
<p><strong>What is the use sp_refreshview?</strong></p>
<blockquote><p>It updates the metadata for the specified non-schema-bound view. Persistent metadata for a view can become outdated because of changes to the underlying objects upon which the view depends.</p>
<p>Syntax:</p>
<p>EXECUTE sp_refreshview &#8216;view_name&#8217;;</p></blockquote>
<p><strong>Can you create a Primary key without clustered index?</strong></p>
<blockquote><p>Yes, you can create a Primary Key with a Non Clustered Index. But by default creation of PK automatically creates a clustered index upon the table.</p></blockquote>
<p><strong>What are the virtual tables in Triggers?</strong></p>
<blockquote><p>Inserted &amp; Deleted are virtual tables in Triggers.</p></blockquote>
<p><strong>How will you know what indexes a particular table is using?</strong></p>
<blockquote><p>By checking the graphical Execution Plan.</p>
<p>By using sys.dm_db_index_usage_stats DMV.</p></blockquote>
<p><strong>How many types of internal joins are there in SQL Server?</strong></p>
<ul>
<li>Nested loop join</li>
<li>Merge join</li>
<li>Hash join</li>
</ul>
<p><strong>What are Join Operators?</strong></p>
<blockquote><p>A join operator is a type of an algorithm which the SQL Server Optimizer chooses in order to implement logical joins between two sets of data.</p>
<p>The SQL Server Optimizer may choose a different algorithm for different scenarios based on the requested query, available indexes, statistics and number of estimated rows in each data set.</p>
<p>It’s possible to find the operator which was used by looking at the execution plan that SQL Server has prepared for your query.</p></blockquote>
<p><strong>What are the Nested Loops Joins?</strong></p>
<blockquote><p>The nested loops join, also called nested iteration, uses one join input as the outer input table (shown as the top input in the graphical execution plan) and one as the inner (bottom) input table. The outer loop consumes the outer input table row by row. The inner loop, executed for each outer row, searches for matching rows in the inner input table.</p>
<p>In the simplest case, the search scans an entire table or index; this is called a naive nested loops join. If the search exploits an index, it is called an index nested loops join. If the index is built as part of the query plan (and destroyed upon completion of the query), it is called a temporary index nested loops join. All these variants are considered by the query optimizer.</p></blockquote>
<p><strong>What are the Merge Loops Joins?</strong></p>
<blockquote><p>The merge join requires both inputs to be sorted on the merge columns, which are defined by the equality (ON) clauses of the join predicate. The query optimizer typically scans an index, if one exists on the proper set of columns, or it places a sort operator below the merge join. In rare cases, there may be multiple equality clauses, but the merge columns are taken from only some of the available equality clauses.</p>
<p>Because each input is sorted, the Merge Join operator gets a row from each input and compares them. For example, for inner join operations, the rows are returned if they are equal. If they are not equal, the lower-value row is discarded and another row is obtained from that input. This process repeats until all rows have been processed.</p></blockquote>
<p><strong>What are the Hash Joins?</strong></p>
<blockquote><p>Hash joins are used for many types of set-matching operations: inner join; left, right, and full outer join; left and right semi-join; intersection; union; and difference. Moreover, a variant of the hash join can do duplicate removal and grouping, such as SUM(salary) GROUP BY department. These modifications use only one input for both the build and probe roles.</p></blockquote>
<p><strong>What is Logical Join and Physical Join?</strong></p>
<blockquote><p><strong>Logical Joins:</strong> These joins are simple joins that we apply in our SQL queries, like INNER JOIN, RIGHT/LEFT OUTER JOIN, CROSS JOIN, OUTER APPLY, etc.</p>
<p><strong>Physical Joins:</strong> These are the joins that users don’t use/write in their SQL queries. Instead these are implemented inside SQL Server engine as operators or algorithms to implement the Logical Joins. Their types are Nested Loop, Merge and Hash.</p></blockquote>
<p><strong>What is an In-Memory Hash Join?</strong></p>
<blockquote><p>The hash join first scans or computes the entire build input and then builds a hash table in memory. Each row is inserted into a hash bucket depending on the hash value computed for the hash key. If the entire build input is smaller than the available memory, all rows can be inserted into the hash table.</p>
<p>This build phase is followed by the probe phase. The entire probe input is scanned or computed one row at a time, and for each probe row, the hash key&#8217;s value is computed, the corresponding hash bucket is scanned, and the matches are produced.</p></blockquote>
<p><strong>What is a Grace Hash Join?</strong></p>
<blockquote><p>If the build input does not fit in memory, a hash join proceeds in several steps. This is known as a grace hash join. Each step has a build phase and probe phase. Initially, the entire build and probe inputs are consumed and partitioned (using a hash function on the hash keys) into multiple files.</p>
<p>Using the hash function on the hash keys guarantees that any two joining records must be in the same pair of files. Therefore, the task of joining two large inputs has been reduced to multiple, but smaller, instances of the same tasks. The hash join is then applied to each pair of partitioned files.</p></blockquote>
<p><strong>What is a Recursive Hash Join?</strong></p>
<blockquote><p>If the build input is so large that inputs for a standard external merge would require multiple merge levels, multiple partitioning steps and multiple partitioning levels are required. If only some of the partitions are large, additional partitioning steps are used for only those specific partitions. In order to make all partitioning steps as fast as possible, large, asynchronous I/O operations are used so that a single thread can keep multiple disk drives busy.</p>
<p>If the optimizer anticipates wrongly which of the two inputs is smaller and, therefore, should have been the build input, the build and probe roles are reversed dynamically. The hash join makes sure that it uses the smaller overflow file as build input. This technique is called role reversal. Role reversal occurs inside the hash join after at least one spill to the disk.</p></blockquote>
<p><strong>What is COMPUTE BY statement?</strong></p>
<blockquote><p>COMPUTE BY statement has both detail and summary rows within the SELECT statement. It calculates the summary values for subgroups, or a summary value for the entire result set.</p></blockquote>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>SQL Server: Truth about assigning variables using SET versus SELECT</title>
		<link>https://www.dbrnd.com/2016/03/sql-server-truth-about-assigning-variables-using-set-versus-select/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Thu, 17 Mar 2016 21:04:18 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[select]]></category>
		<category><![CDATA[SET]]></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[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=2087</guid>

					<description><![CDATA[In this post, I am sharing the major difference between SET and SELECT, which we are using for assigning a value to the Variable of SQL Server. What I have found is, the people are always confused to use SET or SELECT for assigning a value to the Variable and which one is faster ? [&#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%2F03%2Fsql-server-truth-about-assigning-variables-using-set-versus-select%2F&amp;linkname=SQL%20Server%3A%20Truth%20about%20assigning%20variables%20using%20SET%20versus%20SELECT" 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%2F03%2Fsql-server-truth-about-assigning-variables-using-set-versus-select%2F&amp;linkname=SQL%20Server%3A%20Truth%20about%20assigning%20variables%20using%20SET%20versus%20SELECT" 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%2F03%2Fsql-server-truth-about-assigning-variables-using-set-versus-select%2F&amp;linkname=SQL%20Server%3A%20Truth%20about%20assigning%20variables%20using%20SET%20versus%20SELECT" 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%2F03%2Fsql-server-truth-about-assigning-variables-using-set-versus-select%2F&amp;linkname=SQL%20Server%3A%20Truth%20about%20assigning%20variables%20using%20SET%20versus%20SELECT" 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%2F03%2Fsql-server-truth-about-assigning-variables-using-set-versus-select%2F&amp;linkname=SQL%20Server%3A%20Truth%20about%20assigning%20variables%20using%20SET%20versus%20SELECT" 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%2F03%2Fsql-server-truth-about-assigning-variables-using-set-versus-select%2F&amp;linkname=SQL%20Server%3A%20Truth%20about%20assigning%20variables%20using%20SET%20versus%20SELECT" 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%2F03%2Fsql-server-truth-about-assigning-variables-using-set-versus-select%2F&#038;title=SQL%20Server%3A%20Truth%20about%20assigning%20variables%20using%20SET%20versus%20SELECT" data-a2a-url="https://www.dbrnd.com/2016/03/sql-server-truth-about-assigning-variables-using-set-versus-select/" data-a2a-title="SQL Server: Truth about assigning variables using SET versus SELECT"></a></p><p><em>In this post, I am sharing the major difference between SET and SELECT, which we are using for assigning a value to the Variable of SQL Server. </p>
<p>What I have found is, the people are always confused to use SET or SELECT for assigning a value to the Variable and which one is faster ?</em></p>
<blockquote><p>SET is the ANSI standard, and SELECT is not the ANSI standard. </p></blockquote>
<blockquote><p>Using SET, we can only assign a scalar value, and if the query is returning multiple values, SET will raise an error.<br />
In contrast, SELECT never raises an error if the query is returning multiple values. </p></blockquote>
<blockquote><p>Using SET, we can assign one variable at a time, using SELECT we can assign multiple variables at a time. </p></blockquote>
<blockquote><p>There are not major differences in speed and performance, but SELECT is slightly faster than SET because it can perform multiple assignments at a time. </p></blockquote>
<blockquote><p>As per the ANSI standard, If there is no value returned, SET will assign NULL and SELECT will not assign any value and variable will not be changed from its previous value. </p></blockquote>
<p><strong>For this Difference, I prepared a small demonstration here. </strong></p>
<p><strong>Create table with Sample Data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE [dbo].[tbl_Employee]
(
	EmpID INTEGER IDENTITY(1,1)
	,EmpName VARCHAR(255)
	,Gender CHAR(1)
	,DOB DATETIME
	,CONSTRAINT pk_tbl_Employee_EmpID PRIMARY KEY(EmpID)
)
GO

INSERT INTO [dbo].[tbl_Employee]
(EmpName,Gender,DOB)
VALUES
('Anvesh','M','19880126')
,('Roy','M','19930214')
,('Jenny','F','19920320')
,('Martin','M','19850616')
GO</pre> </p>
<p><strong>Assigning variable using SET:</strong></p>
<pre class="crayon-plain-tag">DECLARE @EmpName VARCHAR(255)
SET @EmpName = 'Anu'
SET @EmpName = (SELECT EmpName FROM [dbo].[tbl_Employee] WHERE DOB ='19880127')
SELECT @EmpName /* @EmpName is NULL */
GO</pre> </p>
<p><strong>Assigning variable using SELECT:</strong></p>
<pre class="crayon-plain-tag">DECLARE @EmpName VARCHAR(255)
SET @EmpName = 'Anu'
SELECT @EmpName = EmpName FROM [dbo].[tbl_Employee] WHERE DOB ='19880127'
SELECT @EmpName /* @EmpName still having a value 'Anu' */</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>MySQL: FEDERATED Table Engine &#8211; SELECT data from another Server (Part 1/4)</title>
		<link>https://www.dbrnd.com/2016/01/mysql-federated-table-engine-select-data-from-another-server-part-1/</link>
					<comments>https://www.dbrnd.com/2016/01/mysql-federated-table-engine-select-data-from-another-server-part-1/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Fri, 29 Jan 2016 18:54:15 +0000</pubDate>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[FEDERATED Engine]]></category>
		<category><![CDATA[MySQL Command]]></category>
		<category><![CDATA[MySQL Database Administrator]]></category>
		<category><![CDATA[MySQL Database Designing]]></category>
		<category><![CDATA[MySQL Database Programming]]></category>
		<category><![CDATA[MySQL Error]]></category>
		<category><![CDATA[MySQL Performance Tunning]]></category>
		<category><![CDATA[MySQL Query]]></category>
		<category><![CDATA[MySQL Tips and Tricks]]></category>
		<category><![CDATA[remote server]]></category>
		<category><![CDATA[select]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=1418</guid>

					<description><![CDATA[In this post, I am going to start a small series of articles which divided into four different parts. In this set of articles, I will provide basic theory and practical to perform cross-platform or cross-server queries in MySQL. We can also say remote queries. Some of the Database Professional already aware about Linked server [&#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%2F01%2Fmysql-federated-table-engine-select-data-from-another-server-part-1%2F&amp;linkname=MySQL%3A%20FEDERATED%20Table%20Engine%20%E2%80%93%20SELECT%20data%20from%20another%20Server%20%28Part%201%2F4%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%2F01%2Fmysql-federated-table-engine-select-data-from-another-server-part-1%2F&amp;linkname=MySQL%3A%20FEDERATED%20Table%20Engine%20%E2%80%93%20SELECT%20data%20from%20another%20Server%20%28Part%201%2F4%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%2F01%2Fmysql-federated-table-engine-select-data-from-another-server-part-1%2F&amp;linkname=MySQL%3A%20FEDERATED%20Table%20Engine%20%E2%80%93%20SELECT%20data%20from%20another%20Server%20%28Part%201%2F4%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%2F01%2Fmysql-federated-table-engine-select-data-from-another-server-part-1%2F&amp;linkname=MySQL%3A%20FEDERATED%20Table%20Engine%20%E2%80%93%20SELECT%20data%20from%20another%20Server%20%28Part%201%2F4%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%2F01%2Fmysql-federated-table-engine-select-data-from-another-server-part-1%2F&amp;linkname=MySQL%3A%20FEDERATED%20Table%20Engine%20%E2%80%93%20SELECT%20data%20from%20another%20Server%20%28Part%201%2F4%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%2F01%2Fmysql-federated-table-engine-select-data-from-another-server-part-1%2F&amp;linkname=MySQL%3A%20FEDERATED%20Table%20Engine%20%E2%80%93%20SELECT%20data%20from%20another%20Server%20%28Part%201%2F4%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%2F01%2Fmysql-federated-table-engine-select-data-from-another-server-part-1%2F&#038;title=MySQL%3A%20FEDERATED%20Table%20Engine%20%E2%80%93%20SELECT%20data%20from%20another%20Server%20%28Part%201%2F4%29" data-a2a-url="https://www.dbrnd.com/2016/01/mysql-federated-table-engine-select-data-from-another-server-part-1/" data-a2a-title="MySQL: FEDERATED Table Engine – SELECT data from another Server (Part 1/4)"></a></p><p>In this post, I am going to start a small series of articles which divided into four different parts.</p>
<p>In this set of articles, I will provide basic theory and practical to perform cross-platform or cross-server queries in MySQL. We can also say remote queries. </p>
<blockquote><p>Some of the Database Professional already aware about Linked server object of Microsoft SQL Server, which is used for cross-server queries.<br />
Many times, we need to SELECT data from the different database server and even, we require to fetch data from various RDBMS products as well. </p></blockquote>
<p><strong>MySQL also provides the FEDERATED Storage Engine to access data from a remote MySQL Database Server.</strong></p>
<p>The FEDERATED Storage Engine is available beginning with MySQL 5.0.3.</p>
<blockquote><p>We don&#8217;t require to write any database level trigger to update real-time data into the local FEDERATED table because it is fetching data remotely and even does not need to store that data physically on the hard disk. </p></blockquote>
<blockquote><p>We can query to the local FEDERATED table like any other normal table, but it is working as a virtual table only. </p></blockquote>
<p><strong>The FEDERATED storage engine is not enabled by default in the running server. </strong><br />
You can visit the next article to configure and enable FEDERATED Engine. </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2016/01/mysql-federated-table-engine-select-data-from-another-server-part-1/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>SELECT all columns to be good or bad in database system</title>
		<link>https://www.dbrnd.com/2015/09/select-all-columns-to-be-good-or-bad-in-database-system/</link>
					<comments>https://www.dbrnd.com/2015/09/select-all-columns-to-be-good-or-bad-in-database-system/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Thu, 03 Sep 2015 19:06:23 +0000</pubDate>
				<category><![CDATA[Database Designing]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database concept]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[database standards]]></category>
		<category><![CDATA[Database Theory]]></category>
		<category><![CDATA[database topic]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[RDBMS]]></category>
		<category><![CDATA[select]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=555</guid>

					<description><![CDATA[In this post, I am going to write about one of the most common mistakes of Database Developers. &#8220;SELECT * &#8221; (SELECT all columns) to be good or bad in a Database System. I will discuss the pros and cons of &#8220;SELECT * &#8221; and what should be our best practice with SELECT all columns. [&#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%2F09%2Fselect-all-columns-to-be-good-or-bad-in-database-system%2F&amp;linkname=SELECT%20all%20columns%20to%20be%20good%20or%20bad%20in%20database%20system" 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%2F09%2Fselect-all-columns-to-be-good-or-bad-in-database-system%2F&amp;linkname=SELECT%20all%20columns%20to%20be%20good%20or%20bad%20in%20database%20system" 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%2F09%2Fselect-all-columns-to-be-good-or-bad-in-database-system%2F&amp;linkname=SELECT%20all%20columns%20to%20be%20good%20or%20bad%20in%20database%20system" 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%2F09%2Fselect-all-columns-to-be-good-or-bad-in-database-system%2F&amp;linkname=SELECT%20all%20columns%20to%20be%20good%20or%20bad%20in%20database%20system" 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%2F09%2Fselect-all-columns-to-be-good-or-bad-in-database-system%2F&amp;linkname=SELECT%20all%20columns%20to%20be%20good%20or%20bad%20in%20database%20system" 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%2F09%2Fselect-all-columns-to-be-good-or-bad-in-database-system%2F&amp;linkname=SELECT%20all%20columns%20to%20be%20good%20or%20bad%20in%20database%20system" 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%2F09%2Fselect-all-columns-to-be-good-or-bad-in-database-system%2F&#038;title=SELECT%20all%20columns%20to%20be%20good%20or%20bad%20in%20database%20system" data-a2a-url="https://www.dbrnd.com/2015/09/select-all-columns-to-be-good-or-bad-in-database-system/" data-a2a-title="SELECT all columns to be good or bad in database system"></a></p><p>In this post, I am going to write about one of the most common mistakes of Database Developers.</p>
<p>&#8220;SELECT * &#8221; (SELECT all columns) to be good or bad in a Database System.</p>
<p>I will discuss the pros and cons of &#8220;SELECT * &#8221; and what should be our best practice with SELECT all columns.</p>
<h3>Don&#8217;t go with &#8221; SELECT * &#8220;:</h3>
<ul>
<li>When you are using &#8221; SELECT * &#8220;, you are selecting more columns from the database, and your application might not use some of this column. This will create extra cost and load on the database system, and unnecessary data travels across the network.<br />
<hr />
</li>
<li>When you are using JOIN between two or more tables, and in that query, you use &#8220;SELECT * &#8220;, guess about two columns with the same name. It will return more columns than you needed and will create a binding problem.<br />
<hr />
</li>
<li>If you are using  &#8220;SELECT * &#8220;, you may get all columns in arbitrary sequence, and if specified the column name, it will return in the specified order, and you can also refer to this column by numerical index.<br />
If such a code exists in an application which requires all columns in a predefined order, you have to specify all the columns name in the correct order.</p>
<hr />
</li>
<li>When you are using &#8220;SELECT *&#8221;, you will face performance issue because this is not advisable to apply an index on all the columns of the table.<br />
<hr />
</li>
<li>When you are using &#8220;SELECT *&#8221;, you require to create documentation for those columns which are returning and using by an application otherwise, this will create confusion.<br />
<hr />
</li>
<li>When you are using &#8220;SELECT * &#8221; and in the future, someone adds one new column with TEXT data type, you can imagine performance and other errors.<br />
<hr />
</li>
<li>If you need to select all columns, also I would suggest providing a full list of columns because putting &#8220;SELECT *&#8221;  needs fetching the name of the columns from stored metadata or system information which will impact the query performance.<br />
<hr />
</li>
<li>Now Imagine that you are using &#8220;SELECT *&#8221; in all your stored procedure and now you require to find one column name from the text of the stored procedure which is not possible with &#8220;SELECT *&#8221;.</li>
</ul>
<h3>Why you go with &#8220;SELECT *&#8221; ?</h3>
<ul>
<li>The Lazy developers can only go with &#8220;SELECT *&#8221;.<br />
<hr />
</li>
<li>If you have special requirements and create a dynamic environment when add or delete column automatically handle by application code. In this particular case, you don&#8217;t require to change application and database code, and this will automatically be done in a production environment. In this case, you can use &#8220;SELECT *&#8221;.<br />
<hr />
</li>
<li>I didn&#8217;t find any other reason to use &#8220;SELECT *&#8221;.</li>
</ul>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2015/09/select-all-columns-to-be-good-or-bad-in-database-system/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>How to change ownership for all objects in PostgreSQL</title>
		<link>https://www.dbrnd.com/2015/05/how-to-change-ownership-for-all-objects/</link>
					<comments>https://www.dbrnd.com/2015/05/how-to-change-ownership-for-all-objects/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sat, 23 May 2015 16:51:25 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[alter]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[object ownership]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[select]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=191</guid>

					<description><![CDATA[In this post, I am sharing a script which we can use for changing ownership of all objects in PostgreSQL. Why ownership of object needs to change? I have created one application user for our production PostgreSQL database. But system admin and deployment manager made one mistake and executed objects with some another database user. [&#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%2F05%2Fhow-to-change-ownership-for-all-objects%2F&amp;linkname=How%20to%20change%20ownership%20for%20all%20objects%20in%20PostgreSQL" 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%2F05%2Fhow-to-change-ownership-for-all-objects%2F&amp;linkname=How%20to%20change%20ownership%20for%20all%20objects%20in%20PostgreSQL" 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%2F05%2Fhow-to-change-ownership-for-all-objects%2F&amp;linkname=How%20to%20change%20ownership%20for%20all%20objects%20in%20PostgreSQL" 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%2F05%2Fhow-to-change-ownership-for-all-objects%2F&amp;linkname=How%20to%20change%20ownership%20for%20all%20objects%20in%20PostgreSQL" 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%2F05%2Fhow-to-change-ownership-for-all-objects%2F&amp;linkname=How%20to%20change%20ownership%20for%20all%20objects%20in%20PostgreSQL" 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%2F05%2Fhow-to-change-ownership-for-all-objects%2F&amp;linkname=How%20to%20change%20ownership%20for%20all%20objects%20in%20PostgreSQL" 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%2F05%2Fhow-to-change-ownership-for-all-objects%2F&#038;title=How%20to%20change%20ownership%20for%20all%20objects%20in%20PostgreSQL" data-a2a-url="https://www.dbrnd.com/2015/05/how-to-change-ownership-for-all-objects/" data-a2a-title="How to change ownership for all objects in PostgreSQL"></a></p><p>In this post, I am sharing a script which we can use for changing ownership of all objects in PostgreSQL.</p>
<p><b>Why ownership of object needs to change?</b></p>
<p>I have created one application user for our production PostgreSQL database.</p>
<p>But system admin and deployment manager made one mistake and executed objects with some another database user.</p>
<p>After deployment, we came to know that objects were not deployed under proper ownership.</p>
<p>Now we have 2500 tables, 450 views and 230 stored functions are required to change their ownership.</p>
<p>Now, I need to solve this by using only one script because of manual changing ownership of this many objects will take too much time.</p>
<p><b>Below is my demonstration:</b></p>
<p>Let&#8217;s First, create one function with $parameter value so that you can use this function as a template.</p><pre class="crayon-plain-tag">CREATE OR REPLACE FUNCTION ChangeObjectsOwnerShip(text) 
returns text language plpgsql volatile
  AS $f$
    BEGIN
      EXECUTE $1;
      RETURN $1;
    END;
$f$;</pre><p>
Now generate two select statements to get the alter script.</p>
<p><b>Below select statement is for Tables, Sequences and Views.</b></p><pre class="crayon-plain-tag">SELECT ChangeObjectsOwnerShip
(
 'ALTER TABLE ' || quote_ident(s.nspname) || '.' ||quote_ident(s.relname) || ' owner TO app_user'
)
FROM 
(
 SELECT 
  nspname
  ,relname
 FROM pg_class c JOIN pg_namespace n ON (c.relnamespace = n.oid) 
 WHERE nspname NOT LIKE E'pg\\_%' 
  AND nspname &lt;&gt; 'information_schema' 
  AND relkind IN ('r','S','v') ORDER BY relkind = 'S'
)s;</pre><p>
<b>Below select statement is for all functions. </b></p><pre class="crayon-plain-tag">SELECT ChangeObjectsOwnerShip
(
 'ALTER FUNCTION ' || quote_ident(s.nspname) || '.' ||quote_ident(s.function_name) || '('||s.parms||') owner TO app_user'
)
FROM 
(
 SELECT 
  nspname
  ,proname AS function_name
  , pg_catalog.oidvectortypes(proargtypes) AS parms
 FROM pg_catalog.pg_proc AS c
 JOIN pg_namespace n
  ON (c.pronamespace = n.oid)
 WHERE nspname != 'information_schema'
  AND nspname NOT LIKE E'pg\\_%'
 ORDER BY proname
)s;</pre><p>
In above code, preparing one alter statement to change ownership for all objects from assign user to mentioned &#8221; <b>app_user</b> &#8220;. You can change <b>&#8221; app_user &#8221;  </b>by your require user name.</p>
<p>After executing above select statements, you can find all alter scripts in a result.</p>
<p>Copy this result and execute above alter scripts using super user.</p>
<p>All ownership will be changed after executing this alter the script.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2015/05/how-to-change-ownership-for-all-objects/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
