<?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>truncate &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/truncate/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>Wed, 27 Sep 2017 20:36:49 +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: Database Developer (T-SQL) Interview Questions and Answers (Day-3)</title>
		<link>https://www.dbrnd.com/2016/06/sql-server-database-developer-t-sql-interview-questions-and-answers-string-rank-functions-truncate-delete-dynamic-sql-top-clause-cte-day-3/</link>
					<comments>https://www.dbrnd.com/2016/06/sql-server-database-developer-t-sql-interview-questions-and-answers-string-rank-functions-truncate-delete-dynamic-sql-top-clause-cte-day-3/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Tue, 14 Jun 2016 17:46:57 +0000</pubDate>
				<category><![CDATA[SQL Server Interview]]></category>
		<category><![CDATA[answers]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[Common Table Expression]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[Database Developer]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[dense_rank()]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[OUTPUT clause]]></category>
		<category><![CDATA[questions]]></category>
		<category><![CDATA[RANK]]></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[truncate]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3039</guid>

					<description><![CDATA[Why is This SQL 3624 Error Caused? This error is basically caused due to bulk insertion in the MS SQL Server which consequently corrupts the transaction log file of that particular database. Ultimately the database is marked as SUSPECT. This SQL error 3624 issue can be a consequence of following situations; Case 1: If a [&#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-string-rank-functions-truncate-delete-dynamic-sql-top-clause-cte-day-3%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-3%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-string-rank-functions-truncate-delete-dynamic-sql-top-clause-cte-day-3%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-3%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-string-rank-functions-truncate-delete-dynamic-sql-top-clause-cte-day-3%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-3%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-string-rank-functions-truncate-delete-dynamic-sql-top-clause-cte-day-3%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-3%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-string-rank-functions-truncate-delete-dynamic-sql-top-clause-cte-day-3%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-3%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-string-rank-functions-truncate-delete-dynamic-sql-top-clause-cte-day-3%2F&amp;linkname=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-3%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-string-rank-functions-truncate-delete-dynamic-sql-top-clause-cte-day-3%2F&#038;title=SQL%20Server%3A%20Database%20Developer%20%28T-SQL%29%20Interview%20Questions%20and%20Answers%20%28Day-3%29" data-a2a-url="https://www.dbrnd.com/2016/06/sql-server-database-developer-t-sql-interview-questions-and-answers-string-rank-functions-truncate-delete-dynamic-sql-top-clause-cte-day-3/" data-a2a-title="SQL Server: Database Developer (T-SQL) Interview Questions and Answers (Day-3)"></a></p><p><strong>Why is This SQL 3624 Error Caused?</strong></p>
<blockquote><p>This error is basically caused due to bulk insertion in the MS SQL Server which consequently corrupts the transaction log file of that particular database. Ultimately the database is marked as SUSPECT.</p>
<p>This SQL error 3624 issue can be a consequence of following situations;</p>
<ul>
<li>Case 1: If a table has a non-clustered index.</li>
<li>Case 2: A TABLOCK query hint is used.</li>
<li>Case 3: Prior bulk insertion, table was not truncated.</li>
</ul>
</blockquote>
<p><strong>How you can rename the SQL Server database?</strong></p>
<blockquote><p>Using sp_renamedb system stored procedure, we can rename the SQL Server database.</p></blockquote>
<p><strong>Tell us, most useful FIVE string manipulation functions.</strong></p>
<blockquote><p><strong>CHARINDEX( findTextData, textData, [startingPosition] )</strong> &#8211; Returns the starting position of the specified expression in a character string. The starting position is optional.</p>
<p><strong>REPLACE( textData, findTextData, replaceWithTextData )</strong> &#8211; Replaces occurrences of text found in the string with a new value.</p>
<p><strong>SUBSTRING( textData, startPosition, length )</strong> &#8211; Returns portion of the string.</p>
<p><strong>STUFF( textData, start , length , insertTextData )</strong> &#8211; Deletes a specified length of characters and inserts another set of characters at a specified starting point.</p>
<p><strong>REVERSE( character_expression )</strong> &#8211; Returns the reverse of a character expression.</p>
<p><strong>LEN( textData )</strong> &#8211; Returns integer value of the length of the string, excluding trailing blanks.</p></blockquote>
<p><strong>What is the difference between TRUNCATE and DELETE?</strong></p>
<p><strong>DELETE:</strong></p>
<ul>
<li>DELETE is a DML Command.</li>
<li>DELETE statement is executed using a row lock, each row in the table is locked for deletion.</li>
<li>We can specify filters in where clause</li>
<li>It deletes specified data if where condition exists.</li>
<li>Delete activates a trigger because the operation are logged individually.</li>
<li>Slower than truncate because, it keeps logs.</li>
<li>Rollback is possible.</li>
</ul>
<p><strong>TRUNCATE:</strong></p>
<ul>
<li>TRUNCATE is a DDL command.</li>
<li>TRUNCATE TABLE always locks the table and page but not each row.</li>
<li>Cannot use Where Condition.</li>
<li>It Removes all the data.</li>
<li>TRUNCATE TABLE cannot activate a trigger because the operation does not log individual row deletions.</li>
<li>Faster in performance wise, because it doesn&#8217;t keep any logs.</li>
<li>Rollback is not possible.</li>
</ul>
<p><strong>How you are executing your dynamic SQL?</strong></p>
<ul>
<li>Writing a query with parameters.</li>
<li>Using EXEC.</li>
<li>Using sp_executesql.</li>
</ul>
<p><strong>Can you use TOP clause with UPDATE or DELETE statement?</strong></p>
<blockquote><p>Yes: we can use TOP clause with UPDATE or DELETE statement.</p></blockquote>
<p><strong>What is a TOP WITH TIES option?</strong></p>
<blockquote class="wp-embedded-content" data-secret="h1cdguIqDB"><p><a href="https://www.dbrnd.com/2016/02/sql-server-the-awesome-top-clause-with-ties-option/">SQL Server: The Awesome TOP clause WITH TIES option</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/02/sql-server-the-awesome-top-clause-with-ties-option/embed/#?secret=h1cdguIqDB" data-secret="h1cdguIqDB" width="600" height="338" title="&#8220;SQL Server: The Awesome TOP clause WITH TIES option&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p><strong>What is the use of the OUTPUT clause?</strong></p>
<ul>
<li>OUTPUT clause is extension to INSERT, DELETE and UPDATE commands.</li>
<li>It returns rows information those affected by INSERT,UPDATE or DELETE statement.</li>
<li>Its result you can store into variables or other tables for processing.</li>
<li>For INSERT and UPDATE operation use OUTPUT INSERTED.*</li>
<li>For DELETION operation use OUTPUT DELETED.*</li>
<li>is used to select all columns, if you want to select particular column then OUTPUT DELETED.Col1.</li>
</ul>
<p><strong>What do you know about Comman Table Expression (CTE)?</strong></p>
<ul>
<li>A CTE is essentially a disposable view, and can be used in place of derived tables.</li>
<li>CTE is defined by WITH-Keyword, and it is best practice to begin CTE with Semi-column, since CTE is not single-statement it is batch of statement.</li>
<li>CTE can be used in SELECT, INSERT, UPDATE or DELETE statement. But SELECT is desired.</li>
<li>CTE is single-time visible to sub-sequent query only.</li>
<li>CTE can be used to define VIEWS as part of View’s SELECT query.</li>
<li>CTE can be defined as Recursive-CTE or Non-Recursive-CTE;</li>
<li>Recursive-CTE calls itself whereas Non-Recursive does not. It is best practice to use MAXRECURSION to prevent infinite loops in Recursive-CTE.</li>
<li>CTE (Recursive) mainly used for navigating hierarchical data.</li>
<li>CTE (Non-Recursive) is substitutable to derived-table (or Inline-view or Sub-Query); Since CTE and Derived Table are non-persistent and have same visibility.</li>
<li>CTE is not substitutable to Temp-Table or Table-Variable; Because CTE and TempTable are differ based on: Visibility and Persistency; CTE visibility is limited to only a single SQL statement that would very first to it; whereas Temp-Table or Table-Variable is visible to at-least current scope where they defined and Temp-Table are persistent to Db.</li>
<li>CTE can be defined in SUB-ROUTIENS such as user-defined functions, stored procedures triggers or views.</li>
<li>You can define cursor on CTE-referenced Select Query.</li>
</ul>
<p><strong>What are the advantages of CTE?</strong></p>
<blockquote><p><strong>Light-weight:</strong> CTEs are light-weight than Temporary-tables (or Table-Variable). Since, they are non-persistent and do not write any schema information or business-data to disk. The second thing is that CTEs get cleaned up automatically by SQL Server. Therefore, CTE does not create any maintenance over-head and no performance impact.</p>
<p><strong>Recursion:</strong> A recursive-CTE is ultimate for navigating hierarchical data;</p>
<p><strong>Readability:</strong> CTE improves readability; this is awesome aspect that developers always look for. CTE separates data extraction from your main query. Consider a complex query containing multiple joins, data filter operations, group by etc. Using CTE, you can put all JOIN-operations in one CTE-variable; and all filters in another CTE variable then conveniently access them in your query. This will rapidly increase readability of your query. Hence, CTE is substitutable to Derived Table.</p></blockquote>
<p><strong>What are the limitations of CTE?</strong></p>
<ul>
<li>CTE-Select cannot include following clauses:</li>
<li>DISTINCT</li>
<li>INTO</li>
<li>ORDER BY (except when a TOP clause is specified)</li>
<li>GROUP BY</li>
<li>OPTION clause with query hints</li>
<li>FOR BROWSE</li>
<li>CTE-cannot include PIVOTed or Un-PIVOTed data.</li>
</ul>
<p><strong>What is a Recursive Common Table Expression?</strong></p>
<p>A common table expression (CTE) provides the significant advantage of being able to reference itself, thereby creating a recursive CTE. A recursive CTE is one in which an initial CTE is repeatedly executed to return subsets of data until the complete result set is obtained.</p>
<p>A query is referred to as a recursive query when it references a recursive CTE. Returning hierarchical data is a common use of recursive queries.</p>
<p><strong>What do you please tell me something about, RANK() and DENSE_RANK()?</strong></p>
<blockquote><p><strong>RANK():</strong></p>
<ul>
<li>Finds rank of for given column. It increments each row by 1 but skips sub-sequent increments for duplicates.</li>
<li>If repetitive values exist, the same rank would be assigned to all of them.</li>
<li>For each duplicate value, the RANK() function skips the subsequent value so that the next non-duplicate value remains in its rightful position.</li>
</ul>
<p><strong>DENSE_RANK():</strong></p>
<ul>
<li>Finds rank of for given column with subsequent increment even for duplicates.</li>
<li>The main difference between RANK() and DENSE_RANK() is that for duplicates(repetitive) values, RANK() skips sub-sequent rank value</li>
<li>whereas DENSE_RANK() does not skips and continues with next sub-sequent rank.</li>
</ul>
</blockquote>
<p><strong>We require to find a sequential number for each row within a result set. How is this possible?</strong></p>
<blockquote><p>Using ROW_NUMBER() OVER(). It returns the sequential number for each row within a result set, starting from 1.</p>
<p><strong>Syntax:</strong></p>
<p>ROW_NUMBER ( ) OVER ([&lt;partition_by_clause&gt;] &lt;order_by_clause&gt;)</p></blockquote>
<p><strong>Have you ever tried to concat string using GROUP BYclause?</strong></p>
<blockquote class="wp-embedded-content" data-secret="lIpEHgxnUo"><p><a href="https://www.dbrnd.com/2015/12/sql-server-concatenate-strings-using-the-group-by-clause/">SQL Server: Concatenate Strings using the GROUP BY clause</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/12/sql-server-concatenate-strings-using-the-group-by-clause/embed/#?secret=lIpEHgxnUo" data-secret="lIpEHgxnUo" width="600" height="338" title="&#8220;SQL Server: Concatenate Strings using the GROUP BY clause&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p><strong>How to find maximum value from multiple columns?</strong></p>
<blockquote class="wp-embedded-content" data-secret="TOdBft667Q"><p><a href="https://www.dbrnd.com/2015/12/sql-server-find-max-value-from-multiple-columns/">SQL Server: Find MAX value from Multiple Columns</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/12/sql-server-find-max-value-from-multiple-columns/embed/#?secret=TOdBft667Q" data-secret="TOdBft667Q" width="600" height="338" title="&#8220;SQL Server: Find MAX value from Multiple Columns&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p><strong>I want to remove all extra spaces from a string, how is it possible?</strong></p>
<blockquote class="wp-embedded-content" data-secret="od1nRDhGpB"><p><a href="https://www.dbrnd.com/2016/01/sql-server-script-to-remove-extra-spaces-from-a-string/">SQL Server: Script to Remove Extra Spaces from a String</a></p></blockquote>
<p><iframe loading="lazy" class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2016/01/sql-server-script-to-remove-extra-spaces-from-a-string/embed/#?secret=od1nRDhGpB" data-secret="od1nRDhGpB" width="600" height="338" title="&#8220;SQL Server: Script to Remove Extra Spaces from a String&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2016/06/sql-server-database-developer-t-sql-interview-questions-and-answers-string-rank-functions-truncate-delete-dynamic-sql-top-clause-cte-day-3/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Server 2016: Introduce New TRUNCATE  by Partitions Number</title>
		<link>https://www.dbrnd.com/2016/01/sql-server-2016-introduce-new-truncate-by-partitions-number/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Fri, 08 Jan 2016 18:02:13 +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[Partitions]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server 2016]]></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[truncate]]></category>
		<category><![CDATA[TSQL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=1181</guid>

					<description><![CDATA[In this post, I am sharing a new version of TRUNCATE command which used to truncate the partitions and newly introduced in SQL Server 2016. Always we keen like why we cannot apply WHERE condition or any other filter in TRUNCATE Command. TRUNCATE command is faster than delete because it consumes the fewer resources than [&#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%2Fsql-server-2016-introduce-new-truncate-by-partitions-number%2F&amp;linkname=SQL%20Server%202016%3A%20Introduce%20New%20TRUNCATE%20%20by%20Partitions%20Number" 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%2Fsql-server-2016-introduce-new-truncate-by-partitions-number%2F&amp;linkname=SQL%20Server%202016%3A%20Introduce%20New%20TRUNCATE%20%20by%20Partitions%20Number" 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%2Fsql-server-2016-introduce-new-truncate-by-partitions-number%2F&amp;linkname=SQL%20Server%202016%3A%20Introduce%20New%20TRUNCATE%20%20by%20Partitions%20Number" 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%2Fsql-server-2016-introduce-new-truncate-by-partitions-number%2F&amp;linkname=SQL%20Server%202016%3A%20Introduce%20New%20TRUNCATE%20%20by%20Partitions%20Number" 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%2Fsql-server-2016-introduce-new-truncate-by-partitions-number%2F&amp;linkname=SQL%20Server%202016%3A%20Introduce%20New%20TRUNCATE%20%20by%20Partitions%20Number" 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%2Fsql-server-2016-introduce-new-truncate-by-partitions-number%2F&amp;linkname=SQL%20Server%202016%3A%20Introduce%20New%20TRUNCATE%20%20by%20Partitions%20Number" 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%2Fsql-server-2016-introduce-new-truncate-by-partitions-number%2F&#038;title=SQL%20Server%202016%3A%20Introduce%20New%20TRUNCATE%20%20by%20Partitions%20Number" data-a2a-url="https://www.dbrnd.com/2016/01/sql-server-2016-introduce-new-truncate-by-partitions-number/" data-a2a-title="SQL Server 2016: Introduce New TRUNCATE  by Partitions Number"></a></p><p>In this post, I am sharing a new version of TRUNCATE command which used to truncate the partitions and newly introduced in SQL Server 2016.</p>
<p>Always we keen like why we cannot apply WHERE condition or any other filter in TRUNCATE Command. TRUNCATE command is faster than delete because it consumes the fewer resources than DELETE and it is not generating any transaction logs. </p>
<blockquote><p>Now with the SQL Server 2016 you can perform TRUNCATE basis on Partition Number.<br />
If you have a big size of the table and you already applied proper partitions on that table, you can use the new TRUNCATE command for truncating the data. </p></blockquote>
<p><strong>Below is a full demonstration on this:</strong></p>
<p><strong>CREATE SAMPLE DATABASE:</strong></p>
<pre class="crayon-plain-tag">CREATE DATABASE TruncatePartitions
GO</pre> </p>
<p><strong>ADD File Group to New Database:</strong></p>
<pre class="crayon-plain-tag">ALTER DATABASE TruncatePartitions ADD FILEGROUP [2015]
ALTER DATABASE TruncatePartitions ADD FILEGROUP [2016]
GO</pre> </p>
<p><strong>Create .ndf file to store partitions data and attached to perticular filegroup:</strong></p>
<pre class="crayon-plain-tag">ALTER DATABASE TruncatePartitions
  ADD FILE
  (NAME = N'Partitions_2015',
  FILENAME = N'C:\PartitionsData\Partitions_2015.ndf'
  )
  TO FILEGROUP [2015]  

ALTER DATABASE TruncatePartitions
  ADD FILE
  (NAME = N'Partitions_2016',
  FILENAME = N'C:\PartitionsData\Partitions_2016.ndf'
  )
  TO FILEGROUP [2016]
GO</pre> </p>
<p><strong>Create Partition functions:</strong></p>
<pre class="crayon-plain-tag">CREATE PARTITION FUNCTION DateRangeFN(DATETIME)  AS
  RANGE LEFT FOR VALUES
  ('2015-12-31',
  '2016-12-31'
  )
GO</pre> </p>
<p><strong>Create Partition scheme:</strong></p>
<pre class="crayon-plain-tag">CREATE PARTITION SCHEME DateRangeScheme AS
  PARTITION DateRangeFN  TO
  ([2015],
   [2016],
   [PRIMARY]  )
GO</pre> </p>
<p><strong>Create Sample table:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_SamplePartitionPartition
(ID INT NOT NULL,
[SampleDate] DATETIME)
ON DateRangeScheme ([SampleDate]);
GO</pre> </p>
<p><strong>Insert Sample data:</strong></p>
<pre class="crayon-plain-tag">INSERT INTO tbl_SamplePartition VALUES (1,'2015-08-06')
INSERT INTO tbl_SamplePartition VALUES (2,'2015-05-08')
INSERT INTO tbl_SamplePartition VALUES (3,'2015-04-02')
INSERT INTO tbl_SamplePartition VALUES (4,'2015-06-25')
INSERT INTO tbl_SamplePartition VALUES (5,'2015-09-26')
INSERT INTO tbl_SamplePartition VALUES (6,'2016-02-12')
INSERT INTO tbl_SamplePartition VALUES (7,'2016-08-16')
INSERT INTO tbl_SamplePartition VALUES (8,'2016-04-08')
INSERT INTO tbl_SamplePartition VALUES (9,'2016-09-14')
GO</pre> </p>
<p><strong>Script to check, if the data exist in Partitions:</strong></p><pre class="crayon-plain-tag">SELECT 
	OBJECT_NAME(OBJECT_ID) AS TableName
	, partition_number AS PartitionNumber
	, rows AS TotalRows
FROM sys.partitions
WHERE OBJECT_NAME(OBJECT_ID)='tbl_SamplePartition';
GO</pre><p> </p>
<p><strong>Final step to TRUNCATE Partitions by Partition Number:</strong></p>
<pre class="crayon-plain-tag">TRUNCATE TABLE tbl_Sample WITH (PARTITIONS (1));
GO</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>PostgreSQL: Function to truncate all Tables created by Particular User</title>
		<link>https://www.dbrnd.com/2015/11/postgresql-function-to-truncate-all-tables-created-by-particular-user/</link>
					<comments>https://www.dbrnd.com/2015/11/postgresql-function-to-truncate-all-tables-created-by-particular-user/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Tue, 10 Nov 2015 16:08:15 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[PostgreSQL DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></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[truncate]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=922</guid>

					<description><![CDATA[In this post, I am sharing a PostgreSQL user define a function to truncate all Tables which are created by particular X User. Sometimes, Database administrator or Database Developer creates a user only for test purpose, and this TEST user created an N number of test rows or tables which are required drop or truncate. [&#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%2F11%2Fpostgresql-function-to-truncate-all-tables-created-by-particular-user%2F&amp;linkname=PostgreSQL%3A%20Function%20to%20truncate%20all%20Tables%20created%20by%20Particular%20User" 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%2F11%2Fpostgresql-function-to-truncate-all-tables-created-by-particular-user%2F&amp;linkname=PostgreSQL%3A%20Function%20to%20truncate%20all%20Tables%20created%20by%20Particular%20User" 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%2F11%2Fpostgresql-function-to-truncate-all-tables-created-by-particular-user%2F&amp;linkname=PostgreSQL%3A%20Function%20to%20truncate%20all%20Tables%20created%20by%20Particular%20User" 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%2F11%2Fpostgresql-function-to-truncate-all-tables-created-by-particular-user%2F&amp;linkname=PostgreSQL%3A%20Function%20to%20truncate%20all%20Tables%20created%20by%20Particular%20User" 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%2F11%2Fpostgresql-function-to-truncate-all-tables-created-by-particular-user%2F&amp;linkname=PostgreSQL%3A%20Function%20to%20truncate%20all%20Tables%20created%20by%20Particular%20User" 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%2F11%2Fpostgresql-function-to-truncate-all-tables-created-by-particular-user%2F&amp;linkname=PostgreSQL%3A%20Function%20to%20truncate%20all%20Tables%20created%20by%20Particular%20User" 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%2F11%2Fpostgresql-function-to-truncate-all-tables-created-by-particular-user%2F&#038;title=PostgreSQL%3A%20Function%20to%20truncate%20all%20Tables%20created%20by%20Particular%20User" data-a2a-url="https://www.dbrnd.com/2015/11/postgresql-function-to-truncate-all-tables-created-by-particular-user/" data-a2a-title="PostgreSQL: Function to truncate all Tables created by Particular User"></a></p><p>In this post, I am sharing a PostgreSQL user define a function to truncate all Tables which are created by particular X User. </p>
<p>Sometimes, Database administrator or Database Developer creates a user only for test purpose, and this TEST user created an N number of test rows or tables which are required drop or truncate. </p>
<p>Using this function, you can truncate all tables of a database with the help of Owner and Schema input parameters.ter. </p>
<pre class="crayon-plain-tag">CREATE OR REPLACE FUNCTION fn_TruncateAllTable
(
	InputUserName CHARACTER VARYING 
	,InputSchemaName CHARACTER VARYING 
) RETURNS void AS $$
DECLARE
	statements CURSOR FOR
	SELECT tablename FROM pg_tables
	WHERE tableowner = InputUserName AND schemaname = InputSchemaName;
BEGIN
    FOR stmt IN statements LOOP
	EXECUTE 'TRUNCATE TABLE ' || quote_ident(stmt.tablename) || ' CASCADE;';	
    END LOOP;
END;
$$ LANGUAGE plpgsql;</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2015/11/postgresql-function-to-truncate-all-tables-created-by-particular-user/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
