<?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>T-SQL - Techyaz.com</title>
	<atom:link href="https://techyaz.com/category/sql-server/t-sql/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>Tips, Tutorials and How-to Topics</description>
	<lastBuildDate>Sat, 24 Jul 2021 18:47:42 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>

<image>
	<url>https://techyaz.com/wp-content/uploads/2017/11/cropped-Site-icon-150x150.png</url>
	<title>T-SQL - Techyaz.com</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Learn How to use SQL Server BETWEEN Operator</title>
		<link>https://techyaz.com/sql-server/t-sql/learn-how-to-use-sql-server-between-operator/</link>
					<comments>https://techyaz.com/sql-server/t-sql/learn-how-to-use-sql-server-between-operator/#respond</comments>
		
		<dc:creator><![CDATA[Editorial Staff]]></dc:creator>
		<pubDate>Sat, 17 Jul 2021 16:06:06 +0000</pubDate>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[T-SQL Operators]]></category>
		<guid isPermaLink="false">https://techyaz.com/?p=2563</guid>

					<description><![CDATA[<p>SQL BETWEEN operator is used to get values between a range of data inputs. We can use SQL BETWEEN operator with SELECT, UPDATE, INSERT and DELETE statements. Range can be defined based on text, date or numbers values. Here i&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/learn-how-to-use-sql-server-between-operator/">Learn How to use SQL Server BETWEEN Operator</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>SQL BETWEEN operator is used to get values between a range of data inputs. We can use SQL BETWEEN operator with SELECT, UPDATE, INSERT and DELETE statements. Range can be defined based on text, date or numbers values. Here i will show you multiple use cases of this operator like get values between two date ranges using SQL BETWEEN operator, get details between two numbers using SQL Server operator BETWEEN etc.</p>



<h2 class="wp-block-heading">Get Values between Two Dates using SQL BETWEEN</h2>



<p>Let&#8217;s say you have a requirement to get all values between two date ranges or specific date time ranges. You can use SQL BETWEEN operator to get this done. Here is the example to get all values between date 19th March 2021 and 20th March 2021. Below example is also an use case of how to use BETWEEN operator in WHERE clause. You can remove &#8216;-&#8216; from date ranges if you want and you can directly mention date ranges as <em>&#8216;20210319&#8217; AND &#8216;20210320&#8217;.</em></p>



<p class="has-text-color" style="color:#0a43eb"><strong>SELECT name, type_desc,create_date, modify_date<br>FROM sys.objects<br>WHERE modify_date BETWEEN &#8216;2021-03-19&#8217; AND &#8216;2021-03-20&#8217;</strong></p>



<p>Here, i have fetched all modified objects on my SQL Server between given date range. Here is the output. We can see there are more than 730 rows returned. You can compare the dates in modify_date column whether output has returned correct data or not. </p>



<figure class="wp-block-image size-large is-style-default"><img fetchpriority="high" decoding="async" width="1024" height="609" src="//i0.wp.com/techyaz.com/wp-content/uploads/2021/07/1-BETWEEN-Operator-1024x609.jpg" alt="SQL BETWEEN Operator with Date ranges" class="wp-image-2580" srcset="https://techyaz.com/wp-content/uploads/2021/07/1-BETWEEN-Operator-1024x609.jpg 1024w, https://techyaz.com/wp-content/uploads/2021/07/1-BETWEEN-Operator-300x178.jpg 300w, https://techyaz.com/wp-content/uploads/2021/07/1-BETWEEN-Operator-768x456.jpg 768w, https://techyaz.com/wp-content/uploads/2021/07/1-BETWEEN-Operator.jpg 1489w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>Now, Next requirement could be to retrieve some data between two date time ranges like you want details between specific time of dates. I will take same above query to show case this example as well. When we don&#8217;t specify the time along with date range then it defaults to 12:00 A.M. </p>



<p class="has-text-color" style="color:#0a43eb"><strong>SELECT name, type_desc,create_date, modify_date<br>FROM sys.objects<br>WHERE modify_date BETWEEN &#8216;2021-03-19 4:38:00&#8217; AND &#8216;2021-03-19 4:38:30&#8217;</strong></p>



<p>Here, i have retrieved all modified objects between 30 seconds of above given time (Time 4:38:00 to 4:38:30). Here is the output and it is showing only 35 rows.</p>



<figure class="wp-block-image size-large is-style-default"><img decoding="async" width="1024" height="608" src="//i0.wp.com/techyaz.com/wp-content/uploads/2021/07/2-BETWEEN-Operator-1024x608.jpg" alt="SQL BETWEEN Operator with two date time ranges" class="wp-image-2581" srcset="https://techyaz.com/wp-content/uploads/2021/07/2-BETWEEN-Operator-1024x608.jpg 1024w, https://techyaz.com/wp-content/uploads/2021/07/2-BETWEEN-Operator-300x178.jpg 300w, https://techyaz.com/wp-content/uploads/2021/07/2-BETWEEN-Operator-768x456.jpg 768w, https://techyaz.com/wp-content/uploads/2021/07/2-BETWEEN-Operator.jpg 1485w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>You can use this SQL between two date ranges or datetime ranges to fetch your data. You can use this operator in INSERT, UPDATE or DELETE statements as well.</p>



<h2 class="wp-block-heading">Get Values between Two Numbers</h2>



<p>This section will explain about getting details between two number ranges. Let me take same table which we have taken for above section sys.objects. Now, i want to get all objects having object id between 25 to 30 or you can mention any range. This is just an example to showcase. These number ranges will change as per your need and nature of data. </p>



<p class="has-text-color" style="color:#0a43eb"><strong>SELECT name, object_id, type_desc, create_date<br>FROM sys.objects<br>WHERE object_id BETWEEN &#8217;25&#8217; AND &#8217;30&#8217;</strong></p>



<p>Note, above object_id is column name and not the T-SQL function. Have a look at its output in below image. We have got only rows which are assigned with these object ids.</p>



<figure class="wp-block-image size-large is-style-default"><img decoding="async" width="500" height="305" src="//i0.wp.com/techyaz.com/wp-content/uploads/2021/07/3-BETWEEN-Operator.jpg" alt="SQL BETWEEN Operator with number ranges" class="wp-image-2582" srcset="https://techyaz.com/wp-content/uploads/2021/07/3-BETWEEN-Operator.jpg 500w, https://techyaz.com/wp-content/uploads/2021/07/3-BETWEEN-Operator-300x183.jpg 300w" sizes="(max-width: 500px) 100vw, 500px" /></figure>



<h2 class="wp-block-heading">Use BETWEEN with NOT Operator</h2>



<p>Above all examples shows how to get data between two ranges but we can use NOT operator with BETWEEN to return all values which is not between specified two ranges. Let&#8217;s take above example where BETWEEN operator has returned only 5 rows based on their specified two ranges. Now if i will use NOT operator in above T-SQL statement then output will return all rows of table sys.objects except above 4 rows which will be prevented by NOT operator. Let me show you this example.</p>



<p class="has-text-color" style="color:#0a43eb"><strong>SELECT name, object_id, type_desc, create_date<br>FROM sys.objects<br>WHERE object_id NOT BETWEEN &#8217;25&#8217; AND &#8217;30&#8217;</strong></p>



<p>Have  a look at below highlighted are where objects ids between 25 to 30 are missing.</p>



<figure class="wp-block-image size-large is-style-default"><img loading="lazy" decoding="async" width="547" height="597" src="//i0.wp.com/techyaz.com/wp-content/uploads/2021/07/4-BETWEEN-Operator.jpg" alt="SQL BETWEEN Operator with NOT operator" class="wp-image-2583" srcset="https://techyaz.com/wp-content/uploads/2021/07/4-BETWEEN-Operator.jpg 547w, https://techyaz.com/wp-content/uploads/2021/07/4-BETWEEN-Operator-275x300.jpg 275w" sizes="auto, (max-width: 547px) 100vw, 547px" /></figure>



<p>Here, i have shown you few use cases of SQL Server T-SQL operator BETWEEN statement. You can go head and try this operator for your business needs. For more information on this operator, visit <a href="https://docs.microsoft.com/en-us/sql/t-sql/language-elements/between-transact-sql?view=sql-server-ver15" target="_blank" rel="noreferrer noopener">MSDN</a> article.</p>



<p class="has-vivid-red-color has-text-color"><strong><em>Read More</em></strong></p>



<ul class="wp-block-list"><li><strong><a href="https://techyaz.com/sql-server/troubleshooting/fixview-server-state-permission-was-denied-on-object-server-database-master/" target="_blank" rel="noreferrer noopener">Fix:VIEW SERVER STATE permission was denied on object ‘server’, database ‘master’</a></strong></li><li><strong><a href="https://techyaz.com/sql-server/t-sql/difference-between-union-and-union-all-operators/" target="_blank" rel="noreferrer noopener">Difference between UNION and UNION ALL T-SQL Operators</a></strong></li><li><a href="https://techyaz.com/interview-questions/sql-server-interview-questions/sql-server-interview-questions-answers-indexes/" target="_blank" rel="noreferrer noopener"><strong>SQL Server Interview Questions &amp; Answers on Indexes</strong></a></li></ul>



<p></p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/learn-how-to-use-sql-server-between-operator/">Learn How to use SQL Server BETWEEN Operator</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/t-sql/learn-how-to-use-sql-server-between-operator/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Change SQL Server Instance Name?</title>
		<link>https://techyaz.com/sql-server/change-sql-server-instance-name/</link>
					<comments>https://techyaz.com/sql-server/change-sql-server-instance-name/#respond</comments>
		
		<dc:creator><![CDATA[Manvendra Deo Singh]]></dc:creator>
		<pubDate>Mon, 23 Apr 2018 10:18:35 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Administration]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Troubleshooting]]></category>
		<category><![CDATA[SQL Server Connectivity Issue]]></category>
		<category><![CDATA[Stored Procedures]]></category>
		<guid isPermaLink="false">http://techyaz.com/?p=2112</guid>

					<description><![CDATA[<p>Changing SQL Server Instance name is not a routine task. Although, sometimes we get requirements to change SQL Instance name. There are multiple scenarios in which we need to rename SQL Server Instance like: If you have changed database server&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/change-sql-server-instance-name/">How to Change SQL Server Instance Name?</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Changing SQL Server Instance name is not a routine task. Although, sometimes we get requirements to change SQL Instance name. There are multiple scenarios in which we need to rename SQL Server Instance like:</p>
<ul>
<li>If you have changed database server name then we should change SQL Server Instance name as well.</li>
<li>If you have cloned a database server from DBSERVER1 to DBSERVER2. Here, SQL Server installed on DBSERVER1 will be copied with same name to DBSERVER2. You need to change the SQL Server instance name on DBSERVER2.</li>
<li>Naming convention is not met.</li>
</ul>
<p>Please keep in mind that we cannot change complete name of SQL Server named instance. Suppose you have installed a named instance <em>SERVERNAME</em>\<em>DBInstance1 </em>on your server. If you want to rename this named instance then we can only change first part of this name i.e. <em>SERVERNAME</em>. You cannot change <em>DBInstance1</em> so your named instance will look like <em>DBSERVERNAME</em>\<em>DBInstance1</em> post name change.</p>
<h3><span style="color: #333399;">Impact of Changing SQL Server Instance Name</span></h3>
<p><img loading="lazy" decoding="async" class="size-full wp-image-2113 alignright" src="http://techyaz.com/wp-content/uploads/2018/04/Rename-SQL-Server-Instance.png" alt="Rename SQL Server Instance name" width="300" height="300" srcset="https://techyaz.com/wp-content/uploads/2018/04/Rename-SQL-Server-Instance.png 300w, https://techyaz.com/wp-content/uploads/2018/04/Rename-SQL-Server-Instance-150x150.png 150w, https://techyaz.com/wp-content/uploads/2018/04/Rename-SQL-Server-Instance-160x160.png 160w, https://techyaz.com/wp-content/uploads/2018/04/Rename-SQL-Server-Instance-320x320.png 320w" sizes="auto, (max-width: 300px) 100vw, 300px" /></p>
<p>We need to plan carefully before renaming any SQL Server Instance. Below is the list of possible impacts that you should go through while renaming SQL Server Instance name.</p>
<ul>
<li>Those domain users who are accessing or connecting to the SQL Server Instance as part of Windows group will not be able to connect to renamed instance of SQL Server. Make sure to update the Windows group to specify the new computer name to establish connectivity to the SQL Server instance.</li>
<li>You would not be able to access reporting services after renaming the instance name. You should follow steps mentioned in attached article to <strong><a href="http://techyaz.com/sql-server/troubleshooting/configure-reporting-services-after-renaming-sql-server-instance/" target="_blank" rel="noopener">rename SQL Server instance that is running with reporting services</a></strong>.</li>
<li>If you have database mirroring on the SQL Server instance then you need to drop the mirroring and then reconfigure it post renaming the SQL Server Instance.</li>
<li>Renaming SQL Server instance name does not support if your instance has replication, except when you use log shipping with replication.</li>
<li>You can change the name of SQL Server instance on secondary server in log shipping if the primary server is permanently lost.</li>
<li>If you have linked server configurations then it will be affected by the computer renaming operation. Use <strong>sp_addlinkedserver</strong>or <strong>sp_setnetname</strong> to update computer name references.</li>
</ul>
<h3><span style="color: #333399;">Rename SQL Server Instance</span></h3>
<p>SQL Server instance name along with other system metadata stores in <em>sys.servers</em> system object. We need to update details in system metadata to reflect new name. Also i would suggest to not change SQL Server Instance name directly on your production server.</p>
<p>Suppose you have installed default instance of SQL Server in your database server. If you have changed the name of your database server then you need to update the name of default SQL Server instance as well in sys.servers system table. First, we need to drop the existing SQL Server instance name then we will add new name as per our choice. Follow below steps to change SQL Server instance name.</p>
<pre><span style="color: #0000ff;"><strong><span style="color: #008000;">--Drop existing name</span>
sp_dropserver [old_Instance_name]; </strong></span>
<span style="color: #0000ff;"><strong>GO </strong></span>

<span style="color: #0000ff;"><strong><span style="color: #008000;">--Update New name</span>
sp_addserver [new_Instance_name], local; </strong></span>
<span style="color: #0000ff;"><strong>GO</strong></span></pre>
<p>If you have installed a named instance on your database server then you can run below command to change SQL Server Instance name.</p>
<pre><strong><span style="color: #0000ff;">sp_dropserver [old_name\instancename]; </span></strong>
<strong><span style="color: #0000ff;">GO </span></strong>

<strong><span style="color: #0000ff;">sp_addserver [new_name\instancename], local; </span></strong>
<strong><span style="color: #0000ff;">GO</span></strong></pre>
<p><strong>Note:</strong> Make sure to restart SQL Server services post running above commands to reflect these changes.</p>
<p>You can verify the changes by running below commands or by querying sys.servers system table. If you have not restarted SQL Server services then you might be getting older SQL Server instance name. So restart SQL Server service to apply new changes.</p>
<pre><span style="color: #0000ff;"><strong>SELECT @@SERVERNAME</strong></span></pre>
<p>If you have remote logins on your database server then you might get error 15190 while running sp_dropserver command. Read below article to fix this issue and then proceed with the instance name change.</p>
<ul>
<li><strong><a href="http://techyaz.com/sql-server/troubleshooting/fix-error-15190-there-are-still-remote-logins-for-the-server-dbserver/" target="_blank" rel="noopener">Fix Error 15190: There are still remote logins for the server</a></strong></li>
</ul>
<p>I hope you like this article. Please follow our <a href="https://www.facebook.com/Techyaz/" target="_blank" rel="noopener">Facebook</a> and on <a href="https://twitter.com/Tech_yaz">Twitter</a> handle to get latest updates.</p>
<p><span style="color: #800000;"><em><strong>Read More:</strong></em></span></p>
<ul>
<li><strong><a href="http://techyaz.com/sql-server/troubleshooting/configure-reporting-services-after-renaming-sql-server-instance/" target="_blank" rel="noopener">Configure Reporting Services After Renaming SQL Server Instance Name</a></strong></li>
<li><strong><a href="http://techyaz.com/sql-server/find-when-sql-server-was-installed/" target="_blank" rel="noopener">How to Find When SQL Server was Installed on your database server?</a></strong></li>
<li><strong><a href="http://techyaz.com/sql-server/what-should-be-the-best-value-for-fill-factor-in-sql-server/" target="_blank" rel="noopener">What should be best value for fill factor in SQL Server?</a></strong></li>
<li><strong><a href="http://techyaz.com/sql-server/troubleshooting/to-find-out-no-of-traces-running-on-your-db-instance/" target="_blank" rel="noopener">Find all trace profilers running on SQL Server Instance</a></strong></li>
<li><strong><a href="http://techyaz.com/sql-server/troubleshooting/how-to-move-a-table-to-a-different-filegroup/" target="_blank" rel="noopener">How to Move a Table to Another Filegroup?</a></strong></li>
</ul>
<p>The post <a href="https://techyaz.com/sql-server/change-sql-server-instance-name/">How to Change SQL Server Instance Name?</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/change-sql-server-instance-name/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Fix Error 15190: There are still remote logins for the server &#8216;DBSERVER&#8217;.</title>
		<link>https://techyaz.com/sql-server/troubleshooting/fix-error-15190-there-are-still-remote-logins-for-the-server-dbserver/</link>
					<comments>https://techyaz.com/sql-server/troubleshooting/fix-error-15190-there-are-still-remote-logins-for-the-server-dbserver/#respond</comments>
		
		<dc:creator><![CDATA[Manvendra Deo Singh]]></dc:creator>
		<pubDate>Wed, 18 Apr 2018 09:22:59 +0000</pubDate>
				<category><![CDATA[Linked_server]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Administration]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Troubleshooting]]></category>
		<category><![CDATA[logins]]></category>
		<guid isPermaLink="false">http://techyaz.com/?p=2109</guid>

					<description><![CDATA[<p>I got this error 15190 while renaming SQL Server Instance name. I was not able to drop the existing SQL Server instance name using sp_dropserver stored procedure and was getting error 15190. Full description of this error is given below.&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/troubleshooting/fix-error-15190-there-are-still-remote-logins-for-the-server-dbserver/">Fix Error 15190: There are still remote logins for the server &#8216;DBSERVER&#8217;.</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>I got this error 15190 while <strong><a href="http://techyaz.com/sql-server/troubleshooting/change-sql-server-instance-name/" target="_blank" rel="noopener">renaming SQL Server Instance name</a>.</strong> I was not able to drop the existing SQL Server instance name using <em>sp_dropserver</em> stored procedure and was getting error 15190. Full description of this error is given below.</p>
<p><em><span style="color: #ff0000;">Server: Msg 15190, Level 16, State 1, Procedure sp_dropserver</span></em><br />
<em><span style="color: #ff0000;"> There are still remote logins for the server &#8216;DBSERVER&#8217;.</span></em></p>
<h3><span style="color: #333399;">Error 15190 – Root Cause</span></h3>
<p><img loading="lazy" decoding="async" class="size-full wp-image-2110 alignright" src="http://techyaz.com/wp-content/uploads/2018/04/error-15190.png" alt="error 15190" width="300" height="300" srcset="https://techyaz.com/wp-content/uploads/2018/04/error-15190.png 300w, https://techyaz.com/wp-content/uploads/2018/04/error-15190-150x150.png 150w, https://techyaz.com/wp-content/uploads/2018/04/error-15190-160x160.png 160w, https://techyaz.com/wp-content/uploads/2018/04/error-15190-320x320.png 320w" sizes="auto, (max-width: 300px) 100vw, 300px" /></p>
<p>As we know we need to first drop the existing details of SQL Server instance name from metadata system table sys.servers and then add new instance name in order to change the instance name. I was doing the same and running below command to drop the old instance name.</p>
<pre><strong><span style="color: #0000ff;">sp_dropserver &lt;old_Name&gt;; </span></strong>
<strong><span style="color: #0000ff;">GO</span></strong></pre>
<p>I had received this error 15190 while executing above command and that is because of some remote logins present of the SQL Server instance.</p>
<p>If your database server has any remote login that you use for linked servers or some other purposes then you will get such issues. We need to drop all the remote logins to fix this issue and execute above command successfully.</p>
<h3><span style="color: #333399;">Solution</span></h3>
<p>To fix this issue we need to identify remote logins and drop them before changing the SQL Server instance name. We will be using a system stored procedure <em>sp_dropremotelogin</em> to drop all remote logins from our SQL Server instance. Run below command to drop all remote logins from a default instance of SQL Server.</p>
<pre><strong><span style="color: #0000ff;">sp_dropremotelogin old_name; </span></strong>
<strong><span style="color: #0000ff;">GO</span></strong></pre>
<p>If you have named instance running on your database server then you can run below command. Make sure to change the name of server in place of <em>Old_DBSERVER\Instancename</em>.</p>
<pre><span style="color: #0000ff;"><strong>sp_dropremotelogin Old_DBSERVER\Instancename; </strong></span>
<span style="color: #0000ff;"><strong>GO</strong></span></pre>
<p>Once all remote logins have been dropped you can go ahead and perform your SQL Server Instance name change operation. Make sure to create remote logins for your linked servers or alias post changing the instance name.</p>
<p>I hope you like this article. Please follow our <a href="https://www.facebook.com/Techyaz/" target="_blank" rel="noopener">Facebook</a> page and <a href="https://twitter.com/Tech_yaz">Twitter</a> handle to get latest updates.</p>
<p><span style="color: #800000;"><em><strong>Read More:</strong></em></span></p>
<ul>
<li><strong><a href="http://techyaz.com/sql-server/troubleshooting/change-sql-server-instance-name/" target="_blank" rel="noopener">How to Change SQL Server Instance Name?</a></strong></li>
<li><strong><a href="http://techyaz.com/sql-server/troubleshooting/configure-reporting-services-after-renaming-sql-server-instance/" target="_blank" rel="noopener">How to Rename SQL Server Instance that is running with SQL Server Reporting Services?</a></strong></li>
<li><strong><a href="http://techyaz.com/sql-server/troubleshooting/fix-error-15434-not-drop-login-user-currently-logged/" target="_blank" rel="noopener">Fix Error 15434: Could not drop login &#8220;domain\loginname&#8221; as the user is currently logged in</a></strong></li>
<li><strong><a href="http://techyaz.com/sql-server/troubleshooting/fix-the-error-15174/" target="_blank" rel="noopener">Fix Error 15174: Login owns one or more databases. Change the owner of database before dropping the login.</a></strong></li>
<li><strong><a href="http://techyaz.com/sql-server/sql-server-on-linux/change-hostname-red-hat-linux-server/" target="_blank" rel="noopener">How to Change Hostname of RHEL system?</a></strong></li>
</ul>
<p>The post <a href="https://techyaz.com/sql-server/troubleshooting/fix-error-15190-there-are-still-remote-logins-for-the-server-dbserver/">Fix Error 15190: There are still remote logins for the server &#8216;DBSERVER&#8217;.</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/troubleshooting/fix-error-15190-there-are-still-remote-logins-for-the-server-dbserver/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Enable, Disable or Check SQL Server Trace Flags?</title>
		<link>https://techyaz.com/sql-server/how-to-enable-disable-or-check-sql-server-trace-flags/</link>
					<comments>https://techyaz.com/sql-server/how-to-enable-disable-or-check-sql-server-trace-flags/#comments</comments>
		
		<dc:creator><![CDATA[Manvendra Deo Singh]]></dc:creator>
		<pubDate>Sun, 01 Apr 2018 12:17:44 +0000</pubDate>
				<category><![CDATA[DBCC & DMVs]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Administration]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Troubleshooting]]></category>
		<category><![CDATA[DBCC]]></category>
		<category><![CDATA[SQL Server Configuration Manager]]></category>
		<category><![CDATA[trace flags]]></category>
		<guid isPermaLink="false">http://techyaz.com/?p=2047</guid>

					<description><![CDATA[<p>Trace flags are used to gather in-depth information about certain logs during some transactions. We use DBCC commands to enable trace flags, disable trace flags or check the status of trace flags. Here i am going to discuss about SQL&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/how-to-enable-disable-or-check-sql-server-trace-flags/">How to Enable, Disable or Check SQL Server Trace Flags?</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Trace flags are used to gather in-depth information about certain logs during some transactions. We use DBCC commands to enable trace flags, disable trace flags or check the status of trace flags. Here i am going to discuss about SQL Server Trace flag and how we can enable, disable or check their status whether they are enabled or disabled on your SQL Server Instance.</p>
<p>Let&#8217;s start with the basics. MSDN says &#8220;Trace flags are used to temporarily set specific server characteristics or to switch off a particular behavior. Trace flags are frequently used to diagnose performance issues or to debug stored procedures or complex computer systems.&#8221;</p>
<p>There are two types of trace flags in SQL Server.</p>
<ul>
<li>Session</li>
<li>Global</li>
</ul>
<p>We can enable trace flag for the context of a specific query that we can run under a session. Session trace flags can be enabled for a connection and are visible only to that connection. This type of trace flag never affects another session running on your database server and the effect of the trace flag is lost when that connection logs out.</p>
<p>Global trace flags are set at the server level and must be enabled globally. If you will try to enable global trace flag for a particular session then there will not be any impact of this trace flag. Let&#8217;s go ahead and understand how we can check the status of trace flags running on your SQL Server instance.</p>
<h3><span style="color: #333399;">Check Trace Flag Status</span></h3>
<p>We use <strong><em>DBCC TRACESTATUS</em></strong> command to check all running or active trace flags on database system. We can also check the status of a specific trace flag using this DBCC command. The output of this DBCC command has four columns. First column is the trace flag number, second column shows the status of trace flag, third and fourth columns are showing whether the trace flag is enabled at the global level or session level.</p>
<p>The status column of this DBCC command has two values. The value shows whether the trace flag is ON (1) or OFF (0). You can see the below screenshot to understand the trace flags and their status.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACESTATUS</strong></span></pre>
<p>We can run above DBCC command in different ways to get distinct outputs about trace flags status. Here, I am giving you few examples to use DBCC TRACESTATUS command.</p>
<p>Run below command to display the status of all trace flags that are currently enabled globally.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACESTATUS(-1);  </strong></span>
<span style="color: #0000ff;"><strong>GO</strong></span></pre>
<p>We can even get status of multiple specific trace flags by executing below command. Run below command to get the status of trace flags 8690 and 1118.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACESTATUS (8690, 1118);  </strong></span>
<span style="color: #0000ff;"><strong>GO</strong></span></pre>
<p>If you want to list all the trace flags that are enabled for the current session then you can get it by running below command.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACESTATUS();  </strong></span>
<span style="color: #0000ff;"><strong>GO</strong></span></pre>
<p>If you want to check whether trace flag status for specific trace is enabled globally or not then you can run below command.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACESTATUS(8690, -1)</strong></span></pre>
<h3><span style="color: #333399;">Enable Trace Flag</span></h3>
<p>We enable trace flags to gather more information about certain logs during some transactions. We use DBCC TRACEON command to enable SQL Server trace flags. Here, I will show you different uses of DBCC TRACEON command to enable trace flags.</p>
<p>Remember, all trace flags enabled using DBCC TRACEON command will be disabled once you restart the SQL Server service. If you want to enable any trace flag permanently then we recommend that you should enable them at startup, by using the <strong>-T</strong> command line option. This ensures the trace flag remains active after a server restart. You can launch <strong>SQL Server Configuration Manager</strong> and open the <strong>Properties</strong> window of <strong>SQL Server service</strong>. Here you can click on <strong>Startup Parameters</strong> tab. Here you can add your trace flag number along with <strong>-T</strong> and then click on Ok button to close the window.</p>
<p>If we need to enable a trace flag globally then we need to run below command with <strong>-1</strong> option as shown in below screenshot.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACEON (8690, -1)</strong></span></pre>
<p>Now we can check the trace flag status whether it’s been enabled globally on our database system or not.</p>
<p><img loading="lazy" decoding="async" class="alignnone size-full wp-image-2048" src="http://techyaz.com/wp-content/uploads/2018/04/1-traceon-min.jpg" alt="Enable Trace flag globally DBCC TRACEON" width="691" height="127" srcset="https://techyaz.com/wp-content/uploads/2018/04/1-traceon-min.jpg 691w, https://techyaz.com/wp-content/uploads/2018/04/1-traceon-min-300x55.jpg 300w" sizes="auto, (max-width: 691px) 100vw, 691px" /></p>
<p>If you want this trace to be enabled for current connection only then you should run below command. You can remove -1 from above command.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACEON (8690);  </strong></span>
<span style="color: #0000ff;"><strong>GO</strong></span></pre>
<p>Even you can enable multiple trace flags by running single command. You can run below command to enable trace flag 8690 and 1117 globally. If you want to enable it for current connection only then you can remove -1 from below command.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACEON (8690, 1117, -1);  </strong></span>
<span style="color: #0000ff;"><strong>GO</strong></span></pre>
<p>Run below DBCC command to check the status of the trace flag that we have enabled by running above command.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACESTATUS</strong></span></pre>
<p>You can see trace flag 8690 is showing along with its value set as<strong> 1</strong> for session as well as global in below screenshot.</p>
<p><img loading="lazy" decoding="async" class="alignnone size-full wp-image-2049" src="http://techyaz.com/wp-content/uploads/2018/04/2-tracestatus-min.jpg" alt="Check trace flag status using DBCC TRACESTATUS" width="383" height="225" srcset="https://techyaz.com/wp-content/uploads/2018/04/2-tracestatus-min.jpg 383w, https://techyaz.com/wp-content/uploads/2018/04/2-tracestatus-min-300x176.jpg 300w" sizes="auto, (max-width: 383px) 100vw, 383px" /></p>
<p>If you want to check trace flag status for specific trace number then you can run below command. Below command will let you know whether trace flag 8690 is enabled globally or not.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACESTATUS(8690, -1)</strong></span></pre>
<h3><span style="color: #333399;">Disable Trace Flag</span></h3>
<p>Similarly, we can disable trace flag 8690 that we have enabled in above section by executing another DBCC command known as DBCC TRACEOFF. Run below command to disable trace flag. If we will use -1 value along with DBCC TRACEOFF command that means it will disable this trace flag globally.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACEOFF(8690, -1)</strong></span></pre>
<p><img loading="lazy" decoding="async" class="alignnone size-full wp-image-2050" src="http://techyaz.com/wp-content/uploads/2018/04/3-traceon-min.jpg" alt="Disable Trace flag using DBCC TRACEOFF" width="699" height="128" srcset="https://techyaz.com/wp-content/uploads/2018/04/3-traceon-min.jpg 699w, https://techyaz.com/wp-content/uploads/2018/04/3-traceon-min-300x55.jpg 300w" sizes="auto, (max-width: 699px) 100vw, 699px" /></p>
<p>If you want to disable it for current connection only then we will remove <strong>-1</strong> from above command and run below command.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACEOFF(8690);   </strong></span>
<span style="color: #0000ff;"><strong>GO</strong></span></pre>
<p>Now we can again check the status of trace flag 8690 whether this trace is still active or it has been deactivated by above command.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACESTATUS</strong></span></pre>
<p>We can see trace 8690 is disabled now and this trace flag cannot be active in below image.</p>
<p><img loading="lazy" decoding="async" class="alignnone size-full wp-image-2051" src="http://techyaz.com/wp-content/uploads/2018/04/4-tracestatus-min.jpg" alt="dbcc tracestatus post deactivating a trace" width="394" height="214" srcset="https://techyaz.com/wp-content/uploads/2018/04/4-tracestatus-min.jpg 394w, https://techyaz.com/wp-content/uploads/2018/04/4-tracestatus-min-300x163.jpg 300w" sizes="auto, (max-width: 394px) 100vw, 394px" /></p>
<p>If you want to check trace flag status for a individual trace then you can run below command.</p>
<pre><span style="color: #0000ff;"><strong>DBCC TRACESTATUS(8690, -1)</strong></span></pre>
<p><img loading="lazy" decoding="async" class="alignnone size-full wp-image-2052" src="http://techyaz.com/wp-content/uploads/2018/04/5-tracestatus-min.jpg" alt="Check trace flag globally enabled or not" width="396" height="137" srcset="https://techyaz.com/wp-content/uploads/2018/04/5-tracestatus-min.jpg 396w, https://techyaz.com/wp-content/uploads/2018/04/5-tracestatus-min-300x104.jpg 300w" sizes="auto, (max-width: 396px) 100vw, 396px" /></p>
<p>I hope you like this article. Please follow our <a href="https://www.facebook.com/Techyaz/" target="_blank" rel="noopener">Facebook</a> page and <a href="https://twitter.com/Tech_yaz" target="_blank" rel="noopener">Twitter</a> handle to get latest updates.</p>
<p><em><strong><span style="color: #800000;">Read More:</span></strong></em></p>
<ul>
<li><strong><a href="http://techyaz.com/sql-server/troubleshooting/to-find-out-no-of-traces-running-on-your-db-instance/" target="_blank" rel="noopener">Find Total no of Running Traces files on your Database Server</a></strong></li>
<li><strong><a href="http://techyaz.com/sql-server/avoid-database-shrink-operation/" target="_blank" rel="noopener">Understanding Database Shrink Operation</a></strong></li>
<li><strong><a href="http://techyaz.com/sql-server/how-to-avoid-page-split-in-sql-server/" target="_blank" rel="noopener">How to Avoid Page Split in SQL Server?</a></strong></li>
</ul>
<p>The post <a href="https://techyaz.com/sql-server/how-to-enable-disable-or-check-sql-server-trace-flags/">How to Enable, Disable or Check SQL Server Trace Flags?</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/how-to-enable-disable-or-check-sql-server-trace-flags/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>New T-SQL Functions Introduced in SQL Server 2017</title>
		<link>https://techyaz.com/sql-server/t-sql/new-t-sql-functions-introduced-sql-server-2017/</link>
					<comments>https://techyaz.com/sql-server/t-sql/new-t-sql-functions-introduced-sql-server-2017/#respond</comments>
		
		<dc:creator><![CDATA[Manvendra Deo Singh]]></dc:creator>
		<pubDate>Fri, 09 Feb 2018 16:19:25 +0000</pubDate>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[New features in SQL Server 2017]]></category>
		<category><![CDATA[T-SQL functions]]></category>
		<guid isPermaLink="false">http://techyaz.com/?p=1776</guid>

					<description><![CDATA[<p>SQL Server 2017 is feature rich product that has been launched with multiple new features. You can read attached article where I have explained Top 10 new features added in SQL Server 2017 database engine. I have also explained about&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/new-t-sql-functions-introduced-sql-server-2017/">New T-SQL Functions Introduced in SQL Server 2017</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>SQL Server 2017 is feature rich product that has been launched with multiple new features. You can read attached article where I have explained<strong> <a href="http://techyaz.com/sql-server/10-new-features-sql-server-2017-database-engine/" target="_blank" rel="noopener">Top 10 new features added in SQL Server 2017 database engine</a>.</strong> I have also explained about new T-SQL functions introduced in SQL Server 2017 (CONCAT_WS, TRANSLATE, TRIM, STRING_AGG) in attached article. Here, I will explain these new T-SQL functions with examples and their use cases.</p>
<p><img loading="lazy" decoding="async" class="size-full wp-image-1790 alignright" src="http://techyaz.com/wp-content/uploads/2018/02/New-in-SQL-2017.png" alt="New T-SQL Functions in SQL 2017" width="300" height="300" srcset="https://techyaz.com/wp-content/uploads/2018/02/New-in-SQL-2017.png 300w, https://techyaz.com/wp-content/uploads/2018/02/New-in-SQL-2017-150x150.png 150w, https://techyaz.com/wp-content/uploads/2018/02/New-in-SQL-2017-160x160.png 160w, https://techyaz.com/wp-content/uploads/2018/02/New-in-SQL-2017-320x320.png 320w" sizes="auto, (max-width: 300px) 100vw, 300px" /></p>
<p>There are four new T-SQL functions introduced in SQL Server 2017. These are very useful that make SQL Server developers life easier. Let’s have a look at each one of these SQL 2017 new  functions with their examples and use cases.</p>
<h3><span style="color: #333399;">CONCAT_WS</span></h3>
<p>If you look at the name of this function <strong><em>CONCAT_WS</em></strong> then it will come out as <em><strong>concatenate with separator.</strong></em> This function requires a separator specified as 1<sup>st</sup> argument and minimum of two or more arguments mentioned as remaining arguments. All arguments will be concatenated into a single string with a separator specified in the 1st argument. Null values are ignored during concatenation, and does not add the separator.</p>
<p>Below are the examples of this new function. Suppose, you want to gather login details that has sysadmin access on your SQL Server Instance in a separator format then you can use below command to get these details. You can change the columns of this command or you can also change the T-SQL command if you want to gather some other information. You can also change the separator comma mentioned in first argument to some other separator as per your wish.</p>
<pre><strong><span style="font-size: medium; color: #0000ff;">SELECT CONCAT_WS (‘,’, name, status, sysadmin) As LoginDetails from syslogins
</span></strong></pre>
<p>The output of above command will look like below details.</p>
<pre><span style="color: #000000;"><strong><span style="font-size: medium;"><u>LoginDetails</u>

Sa,9,1
techyaz\test1,10,1
techyaz\deo1,9,0
ty1,9,0
</span></strong></span></pre>
<p>As I said above, this function skips the null values. You can see this by running below command on your SQL Server 2017 instance.</p>
<pre><strong><span style="font-size: medium; color: #0000ff;">SELECT CONCAT_WS(',',132, NULL, NULL, 'Urban Estate', 'Gurgaon', 122001) AS Address; </span></strong></pre>
<p>Output of above command will be:</p>
<pre><span style="color: #000000;"><strong><span style="font-size: medium;"><u>Address</u>

132,Urban Estate, Gurgaon, 122001
</span></strong></span></pre>
<p>You can see null values have been skipped in above output.</p>
<h3><span style="color: #333399;">TRANSLATE</span></h3>
<p>Another useful T-SQL function introduced in SQL Server 2017 is <strong><em>TRANSLATE</em></strong>. This function returns a string given in first argument after some characters given in second argument translated with the characters given in third arguments.</p>
<p>Here, you need to give three inputs. Input string is first argument that we want to modify with some changes. Second Argument is characters that is an expression of any character type containing characters that should be replaced. Third argument is an expression that will replace second argument mentioned in Inputstring. Make sure that the length and type of characters and translations will be same otherwise you will end up with errors. The output will be same as given in input string if you pass NULL value in any of the second or third argument.</p>
<p>Below is the example of this function.</p>
<pre><span style="color: #0000ff;"><strong><span style="font-size: medium;">SELECT TRANSLATE ('[7.124, 9.6]' , '[,]', '( )') AS Values,
</span></strong></span></pre>
<p>Here we are replacing a square bracket having comma with regular bracket and blank space in place of comma.   Output of above T-SQL will be given below.</p>
<pre><span style="color: #000000;"><strong><span style="font-size: medium;"><u>Values</u>

(7.124 9.6)
</span></strong></span></pre>
<p>You can see square bracket has been replaced with the given one and comma separator is also removed from above expression. If you want to revert it of you want to enter comma or any separator with changing the give brackets you can also get this done using this function.</p>
<pre><span style="color: #0000ff;"><strong><span style="font-size: medium;">SELECT TRANSLATE('(7.124 9.6)' , '( )', '[,]') AS Values;
</span></strong></span></pre>
<p>Output of above command will look like below information.</p>
<pre><span style="color: #000000;"><strong><span style="font-size: medium;"><u>Values</u>

[7.124,9.6]

</span></strong></span></pre>
<p>Another example of this function is given below. Run below t-sql command if you want to replace few to the brackets with the given in transactions argument.</p>
<pre><span style="color: #0000ff;"><strong><span style="font-size: medium;">SELECT TRANSLATE('[4+2]/{6-2}', '[]{}', '()()') As Formula;
</span></strong></span></pre>
<p>The output of above command will be look like below info.</p>
<pre><span style="color: #000000;"><strong><span style="font-size: medium;"><u>Formula</u>

(4+2)/(6-2)
</span></strong></span></pre>
<h3><span style="color: #333399;">TRIM</span></h3>
<p>This function has enabled users to remove the space characters or other characters from the start or end of the string. Earlier we used to do the same thing using <em><strong>LTRIM(RTRIM(@string))</strong></em> T-SQL command.</p>
<p>Below example will remove the free spaces given before and after the word techyaz.</p>
<pre><span style="color: #0000ff;"><strong><span style="font-size: medium;">SELECT TRIM( '             techyaz              ') AS Result;
</span></strong></span></pre>
<p>We can the output of above t-sql function given as:</p>
<pre><strong><span style="font-size: medium; color: #000000;">Techyaz
</span></strong></pre>
<p>Earlier we used to do the same thing using below t-sql command.</p>
<pre><span style="color: #0000ff;"><strong><span style="font-size: medium;">LTRIM(RTRIM('    techyaz    '))</span></strong></span></pre>
<h3><span style="color: #333399;">STRING_AGG</span></h3>
<p>Many developers were expected this function to be part of SQL Server since long time. Now their wish have been completed in SQL Server 2017. <strong><em>STRING_AGG</em></strong> is going to become very popular among all new functions. This function concatenates the values of string expressions and places separator values between them. The separator is not added at the end of string.</p>
<p><em><strong>STRING_AGG</strong></em> is an aggregate function that takes all expressions from rows and concatenates them into a single string. Here is the example where we will see output from a table in a given format.</p>
<pre><strong><span style="font-size: medium; color: #0000ff;">SELECT STRING_AGG(CONCAT(FirstName, ' ', LastName, ' (', ModifiedDate, ')'), CHAR(13))
AS names
FROM Person.Person;
</span></strong></pre>
<p>Output of above command will look like:</p>
<pre><span style="color: #000000;"><strong><span style="font-size: medium;"><u>Names</u>

Manvendra Singh (Feb 8 2018 12:00AM)
Maruti Nandan (Dec 24 2017 12:00AM)
Angelo M (Feb 5 2018 12:00AM)
</span></strong></span></pre>
<p>You can see all column values are showing a single string in each row. Another aspect of this function is given in below example.</p>
<p>Suppose, you have a database having information about your blog or published articles. There are separate tables to save articles and their tags. Now, developers want to return one row per each article with all associated tags. We can use below query to get the output.</p>
<pre><span style="color: #0000ff;"><strong><span style="font-size: medium;">SELECT a.ArticleId, Title, STRING_AGG (tag, ',') as Tags
FROM dbo.Article AS a
LEFT JOIN dbo.ArticleTag AS t
ON a.ArticleId = t.ArticleId
GROUP BY a.articleId, title;
</span></strong></span></pre>
<p>Output of above command will look like below details.</p>
<table width="716">
<thead>
<tr>
<td><span style="color: #000000;"><strong>ArticleId</strong></span></td>
<td><span style="color: #000000;"><strong>Title</strong></span></td>
<td><span style="color: #000000;"><strong>Tags</strong></span></td>
</tr>
</thead>
<tbody>
<tr>
<td><span style="color: #000000;"><strong>69</strong></span></td>
<td><span style="color: #000000;"><strong>How to Check IP Address in Linux</strong></span></td>
<td><span style="color: #000000;"><strong>Linux,ip,rhel,howto</strong></span></td>
</tr>
<tr>
<td><span style="color: #000000;"><strong>70</strong></span></td>
<td><span style="color: #000000;"><strong>How to Learn SQL Server DBA</strong></span></td>
<td><span style="color: #000000;"><strong>Learning</strong></span></td>
</tr>
<tr>
<td><span style="color: #000000;"><strong>71</strong></span></td>
<td><span style="color: #000000;"><strong>What is Pages and Extents</strong></span></td>
<td><span style="color: #000000;"><strong>Page,extent,datafiles,db</strong></span></td>
</tr>
</tbody>
</table>
<p>Here, I have described new T-SQL functions introduced in SQL Server 2017 with their examples. I hope you like this article. Please follow our <a href="https://www.facebook.com/Techyaz/">Facebook</a> page and <a href="https://twitter.com/Tech_yaz">Twitter</a> handle to get latest updates.</p>
<p><em><strong><span style="color: #800000;">Read More:</span></strong></em></p>
<ul>
<li><strong><a href="http://techyaz.com/sql-server/difference-between-union-and-union-all-operators/" target="_blank" rel="noopener">What is the Difference between UNION and UNION ALL T-SQL Operators?</a></strong></li>
<li><strong><a href="http://techyaz.com/sql-server/understanding-different-types-sql-joins-examples/" target="_blank" rel="noopener">Understanding Different types of SQL JOINs with their Examples</a></strong></li>
<li><strong><a href="http://techyaz.com/sql-server/get-row-counts-tables-database/" target="_blank" rel="noopener">How to get Total Row Count of all tables of a database</a></strong></li>
<li><strong><a href="http://techyaz.com/sql-server/how-to-disable-auto-commit-in-sql-server/" target="_blank" rel="noopener">How to Disable AutoCommit in SQL Server?</a></strong></li>
</ul>
<p>&nbsp;</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/new-t-sql-functions-introduced-sql-server-2017/">New T-SQL Functions Introduced in SQL Server 2017</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/t-sql/new-t-sql-functions-introduced-sql-server-2017/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Disk and Performance Impact of Online Index Rebuild Operation</title>
		<link>https://techyaz.com/sql-server/indexes/performing-online-index-rebuild-operation/</link>
					<comments>https://techyaz.com/sql-server/indexes/performing-online-index-rebuild-operation/#respond</comments>
		
		<dc:creator><![CDATA[Manvendra Deo Singh]]></dc:creator>
		<pubDate>Fri, 29 Dec 2017 12:18:51 +0000</pubDate>
				<category><![CDATA[Indexes]]></category>
		<category><![CDATA[Performance Tuning]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Administration]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Clustered Index]]></category>
		<category><![CDATA[Index]]></category>
		<category><![CDATA[index rebuild]]></category>
		<category><![CDATA[Nonclustered Index]]></category>
		<category><![CDATA[SQL Server Indexes]]></category>
		<guid isPermaLink="false">http://techyaz.com/?p=1421</guid>

					<description><![CDATA[<p>Here, we are going to talk about disk and performance Impact of online Index rebuild operation. As we know index rebuild is offline operation due to which indexes become inaccessible. Here, I will describe about SQL Server rebuild index online&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/indexes/performing-online-index-rebuild-operation/">Disk and Performance Impact of Online Index Rebuild Operation</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Here, we are going to talk about disk and performance Impact of online Index rebuild operation. As we know index rebuild is offline operation due to which indexes become inaccessible. Here, I will describe about SQL Server rebuild index online that will keep SQL Server indexes accessible. I hope you are aware about the<strong> <a href="http://techyaz.com/sql-server/difference-index-rebuild-reorganize/" target="_blank" rel="noopener">difference between Index rebuild and Index reorganize operation</a>.</strong> If you are not aware please have a look at attached article. Index Reorganize is already an online operation whereas Index rebuild can be done using offline as well as online method. Performing online index operations include index creation, index rebuild and drop indexes. I will describe online rebuild index operation and its impact on disk and performance in this article and same concept will be applied to online index creation and online indexe drop as well.</p>
<p>There is new feature called <em>Resumable Online Index Rebuild</em> introduced in SQL Server 2017 that is an extension of online index rebuild operation. Learn about<strong> <a href="http://techyaz.com/sql-server/use-resumable-online-index-rebuild-operation-sql-server/" target="_blank" rel="noopener">Resumable Online Index Rebuild</a></strong> operation in SQL Server 2017 in attached article.</p>
<p><em><span style="color: #800000;"><strong>Read More:</strong></span></em></p>
<ul>
<li><strong><a href="http://techyaz.com/sql-server/what-should-be-the-best-value-for-fill-factor-in-sql-server/" target="_blank" rel="noopener">What should be best value for fill factor in SQL Server?</a></strong></li>
<li><strong><a href="http://techyaz.com/sql-server/how-to-avoid-page-split-in-sql-server/" target="_blank" rel="noopener">How to Avoid Page Split in SQL Server?</a></strong></li>
<li><strong><a href="http://techyaz.com/sql-server/use-resumable-online-index-rebuild-operation-sql-server/" target="_blank" rel="noopener">How to use Resumable Online Rebuild Operation?</a></strong></li>
</ul>
<h3><span style="color: #333399;">ONLINE Index Rebuild Operation</span></h3>
<p>Rebuilding an index drops and re-creates the index. Indexes becomes unavailable during this exercise that causes an outage for that object. Microsoft has given a solution to avoid inaccessibility of the object during index rebuild operation. We can specify ONLINE keyword value as ON in CREATE INDEX or ALTER INDEX to rebuild indexes online.</p>
<p><img loading="lazy" decoding="async" class="size-full wp-image-1423 alignleft" src="http://techyaz.com/wp-content/uploads/2017/12/Perform-Online-Index-Operations.png" alt="Online index operation" width="560" height="315" srcset="https://techyaz.com/wp-content/uploads/2017/12/Perform-Online-Index-Operations.png 560w, https://techyaz.com/wp-content/uploads/2017/12/Perform-Online-Index-Operations-300x169.png 300w" sizes="auto, (max-width: 560px) 100vw, 560px" /></p>
<p>When you are rebuilding an index and the ONLINE option is set to ON, the underlying objects, the tables and associated indexes, are available for queries and data modification. Row versioning is used to allow for transactional consistency. SQL Server reindex ONLINE option does not work if your table has BLOB data types or index is an XML index or Spatial index.</p>
<p>I have given the list of T-SQL statements that will rebuild indexes ONLINE on your table. You just need to change the index name, table name and column name from below statements.</p>
<p>Below SQL code will rebuild the clustered index on column <strong>EMPID</strong> on the table <strong>dbo.Salary</strong>. As we are rebuilding this cluster index using dropping and recreating the cluster indexes so existing index will be dropped in the process, but it will be available during the operation because the ONLINE option is specified.</p>
<pre class="brush:sql;"><strong><span style="color: #0000ff;">CREATE CLUSTERED INDEX CI_Salary_EMPID ON dbo.Salary(EMPID)
WITH(DROP_EXISTING = ON, ONLINE = ON)
</span></strong></pre>
<p>We can also use ALTER INDEX statement to rebuild a specific index or all indexes on a specified table. The ONLINE syntax remains the same as the CREATE INDEX statement if you want to run this operation online.</p>
<pre class="brush:sql;"><strong><span style="color: #0000ff;">ALTER INDEX ALL ON [dbo.Salary]
REBUILD WITH(ONLINE = ON)
</span></strong></pre>
<p>Above statement will rebuild all indexes on table Salary keeping everything accessible during this operation. Now, we can also rebuild clustered index only on the dbo.Salary table. The ONLINE option is specified, which means that the table will be accessible during the rebuild operation.</p>
<pre class="brush:sql;"><strong><span style="color: #0000ff;">ALTER INDEX CI_Salary_EMPID ON dbo.salary
REBUILD WITH(ONLINE = ON)
</span></strong></pre>
<p>But if we will remove ONLINE option from above statement, then table will NOT be accessible during rebuild operation. You can run below command to validate the accessibility of your table for which you are rebuilding the indexes.</p>
<pre class="brush:sql;"><strong><span style="color: #0000ff;">ALTER INDEX CI_Salary_EMPID ON dbo.salary
REBUILD
</span></strong></pre>
<h3><span style="color: #333399;">Disk Space Considerations </span></h3>
<p>Disk space is an important consideration when you create, rebuild, or drop indexes. Inadequate disk space can degrade performance or even cause the index operation to fail. There might be other major performance impact of Online Index Rebuild operation as well. Please consider below recommendations if you are using Online index operations or during SQL Server rebuild index online.</p>
<ol>
<li>During ONLINE index rebuild and index creation, additional space is required in the data file to keep the second copy of the index. This is because SQL Server creates a snapshot of the index and once index rebuild or creation completes, second copy of this index will be removed by SQL Server.</li>
<li>Additional disk space is required for the temporary mapping index. This temporary index is used in online index operations that create, rebuild, or drop a clustered index.</li>
<li>Make sure the transaction log has been backed up and truncated before running index operations online, and that the log has sufficient space to store the projected index and user transactions.</li>
<li>Specify SORT_IN_TEMPDB option to ON for the index operation. This separates the index transactions from the concurrent user transactions. The index transactions will be stored in the tempdb transaction log, and the concurrent user transactions will be stored in the transaction log of the user database. Make sure to keep tempdb log and user database log files on separate drives to avoid any disk space issues.</li>
<li>Verify that the tempdb database and transaction log have sufficient disk space to handle the index operation. The tempdb transaction log cannot be truncated until the index operation is completed.</li>
<li>Do not run the online index operation in an explicit transaction. The log will not be truncated until the explicit transaction ends.</li>
</ol>
<h3><span style="color: #333399;">Performance Considerations</span></h3>
<p>If we think about performance impact of Online Index Rebuild operation on SQL Server then it will put more burden on your database server than offline index operations. Because both the source and target structures are maintained during the online index operation.</p>
<p>ONLINE Index operations will be slower than equivalent offline index operations because insert, update, and delete transactions is increased, potentially up to double. This could cause a decrease in performance and greater resource usage, especially CPU time, during the index operation. Online index operations are enterprise features and are fully logged operations.</p>
<p>It is recommended to use legacy way of index rebuild or index operations if your system allows a maintenance window. Remember users will be restricted to access data during the offline index operation, but the operation finishes faster and uses fewer resources.</p>
<p>If you have a system that runs 24&#215;7 then ONLINE index rebuild or creation will be a better choice but make sure to consider all points described in this article before choosing online index operations. You need to think from disk space and performance both prospect and design your system that can handle the load and sustain during peak hours.</p>
<p><em><span style="color: #800000;"><strong>Related Articles</strong></span></em></p>
<ul>
<li><strong><a href="http://techyaz.com/sql-server/sql-server-interview-questions-answers-indexes/" target="_blank" rel="noopener">SQL Server Interview Questions &amp; Answers on Indexes</a></strong></li>
<li><strong><a href="http://techyaz.com/sql-server/understanding-sql-server-indexes/" target="_blank" rel="noopener">Understanding Different types of Indexes in SQL Server</a></strong></li>
</ul>
<p>Here, I have described Disk and Performance Impact of Online Index Rebuild operation on SQL Server. I hope you like this article. Please follow our <a href="https://www.facebook.com/Techyaz/">Facebook</a> page and <a href="https://twitter.com/Tech_yaz">Twitter </a>handle to get latest updates.</p>
<p>The post <a href="https://techyaz.com/sql-server/indexes/performing-online-index-rebuild-operation/">Disk and Performance Impact of Online Index Rebuild Operation</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/indexes/performing-online-index-rebuild-operation/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Understanding Different Types of SQL JOINs with Examples</title>
		<link>https://techyaz.com/sql-server/t-sql/understanding-different-types-sql-joins-examples/</link>
					<comments>https://techyaz.com/sql-server/t-sql/understanding-different-types-sql-joins-examples/#respond</comments>
		
		<dc:creator><![CDATA[Maruti Nandan]]></dc:creator>
		<pubDate>Thu, 14 Dec 2017 08:38:18 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Joins]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[tsql]]></category>
		<guid isPermaLink="false">http://techyaz.com/?p=1343</guid>

					<description><![CDATA[<p>SQL JOINs are used to retrieve set of information from two or more different tables based upon certain common values between them. We use joins to combine tables with Select SQL statements. Related Articles: Read UNION vs UNION ALL Logon&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/understanding-different-types-sql-joins-examples/">Understanding Different Types of SQL JOINs with Examples</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>SQL JOINs are used to retrieve set of information from two or more different tables based upon certain common values between them. We use joins to combine tables with Select SQL statements.</p>
<p><em><strong><span style="color: #800000;">Related Articles:</span></strong></em></p>
<ul>
<li><strong><a href="https://techyaz.com/sql-server/difference-between-union-and-union-all-operators/" target="_blank" rel="noopener">Read UNION vs UNION ALL</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/create-logon-trigger-restrict-sysadmin-logins-connect-sql-server-given-time-interval/" target="_blank" rel="noopener">Logon Trigger to Restrict sysadmin logins</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/troubleshooting/table-row-count-showing-incorrect-value-sys-partitions-dmv-sys-dm_db_partition_stats/" target="_blank" rel="noopener">Why rou count showing incorrect value in sys.partitions?</a></strong></li>
</ul>
<h3><span style="color: #333399;"><strong>Types of Joins</strong></span></h3>
<p><strong> </strong>There are four types of Joins and each type is having separate ways to retrieve data.</p>
<ul>
<li><strong>Inner Join</strong></li>
<li><strong>Right Join</strong></li>
<li><strong>Left Join</strong></li>
<li><strong>Full Join</strong></li>
</ul>
<p>We will use two tables Employees and Leavesdetails to explain you each type of Joins with example. The details of both tables are given below.</p>
<p><strong>Table Employees:</strong></p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-1345" src="http://techyaz.com/wp-content/uploads/2017/12/1-Employees-Table.png" alt="Employees Table" width="378" height="196" /></p>
<p><strong>Table LeavesDetails:</strong></p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-1346" src="http://techyaz.com/wp-content/uploads/2017/12/2-LeaveDetails-Table.png" alt="LeavesDetails Table" width="380" height="194" /></p>
<p><strong> </strong>We will apply all types of Joins between these two tables to show you how they work. Let&#8217;s start with Inner Join.<strong> </strong></p>
<h5><span style="color: #333399;"><strong>Inner Join</strong><strong> </strong></span></h5>
<p>Inner join will show data that is matched between both joined tables. This is default join where result is based upon where clause and matched value from all rows fetch data. The visual representation of inner join will look like below image. You can see the coloured area that is common between both tables and that will be output of INNER JOIN.</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-1347 " src="http://techyaz.com/wp-content/uploads/2017/12/3-Inner-Join.png" alt="Inner Join" width="244" height="150" /></p>
<p>As per above image, both tables Employees and Leavesdetails have been joined together to get output details for rows that are matched to each other.</p>
<pre><strong><span style="color: #0000ff;">SELECT *
FROM EMPLOYEES
<em>INNER JOIN</em> [dbo].[LeavesDetails]
ON [LeaveDetails].[Employeeid]=[Employees].[Employeeid]
</span></strong></pre>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1348" src="http://techyaz.com/wp-content/uploads/2017/12/4-Example-Inner-Join.png" alt="inner join example" width="806" height="214" srcset="https://techyaz.com/wp-content/uploads/2017/12/4-Example-Inner-Join.png 806w, https://techyaz.com/wp-content/uploads/2017/12/4-Example-Inner-Join-300x80.png 300w, https://techyaz.com/wp-content/uploads/2017/12/4-Example-Inner-Join-768x204.png 768w" sizes="auto, (max-width: 806px) 100vw, 806px" /></p>
<p><span style="color: #333399;"><strong>Right Join</strong></span></p>
<p>When we use Right Join keyword, it will return all rows from table 2 that is placed right side of <em>RIGHT JOIN</em> keyword and matched rows from table 1 that is mentioned just left to the <em>RIGHT JOIN</em> keyword in SQL statement. In right join, if rows do not match with table 1 it will return <strong>null</strong> value. Sometimes, we call it as Right Outer Join as well. The visual diagram of Right Join is given below.</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-1349" src="http://techyaz.com/wp-content/uploads/2017/12/6-Right-Join.png" alt="right join" width="217" height="145" /></p>
<p><strong>Example</strong></p>
<p>We can see Employees table is mentioned as table 2 and placed just right after keyword <em>RIGHT JOIN</em> and Leavesdetails table has been mentioned as table 1 that is just left to the keyword <em>RIGHT JOIN</em>.</p>
<pre><span style="color: #0000ff;"><strong>SELECT * FROM Leavesdetails
<em>RIGHT JOIN</em> Employees
ON [Employees].[Employeeid]=[Leavesdetails].[Employeeid]
</strong></span></pre>
<p>You can see all values have been displayed from table Employees  along with corresponding details from table 2 Leavesdetails whereas there is one null value row from table Leavesdetails because there is no matched entry for this row from Employees table.</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1350" src="http://techyaz.com/wp-content/uploads/2017/12/5-Example-Right-Join.png" alt="Right Join Example" width="725" height="226" srcset="https://techyaz.com/wp-content/uploads/2017/12/5-Example-Right-Join.png 725w, https://techyaz.com/wp-content/uploads/2017/12/5-Example-Right-Join-300x94.png 300w" sizes="auto, (max-width: 725px) 100vw, 725px" /></p>
<h5><span style="color: #333399;"><strong>Left Join</strong></span></h5>
<p>When we use <em>LEFT JOIN</em> keyword, it will return all rows from table 1 that is placed just before or left side to the <em>LEFT JOIN</em> keyword and matched rows from table 2 that is mentioned just right side to the <em>LEFT JOIN</em> keyword in SQL statement. In left join, if rows do not match with table 2 it will return <strong>null</strong> value. Left Join is also known as left outer join. You can see the visual representation of Left Join in below image.</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-1351" src="http://techyaz.com/wp-content/uploads/2017/12/8-Left-Join.png" alt="left join" width="242" height="146" /></p>
<p><strong>Example</strong></p>
<p>you can see, now Employee table is mentioned left side of <em>LEFT JOIN</em> keyword and Leavesdetails table is mentioned in right side of this keyword so Employee table will work as table 1 and Leavesdetails will work as table 2.</p>
<pre><span style="color: #0000ff;"><strong>SELECT *
FROM Employees
<em>LEFT JOIN</em> Leavesdetails
ON [Leavesdetails].[Employeeid]=[Employees].[Employeeid]
</strong></span></pre>
<p>We can see all records from Employees table along with its corresponding details from table 2 Leavesdetails whereas one null values from table2 that is Leavesdetails because it&#8217;s matching with the data presented in table 1 Employees table. As highlighted record does not exist in table 2 it is showing null value.</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1352" src="http://techyaz.com/wp-content/uploads/2017/12/7-Example-Left-Join.png" alt="Left Join Example" width="721" height="221" srcset="https://techyaz.com/wp-content/uploads/2017/12/7-Example-Left-Join.png 721w, https://techyaz.com/wp-content/uploads/2017/12/7-Example-Left-Join-300x92.png 300w" sizes="auto, (max-width: 721px) 100vw, 721px" /></p>
<h5><span style="color: #333399;"><strong>Full Join</strong></span></h5>
<p>When we use <em>FULL JOIN</em>, it fetches results from both tables when there is a match in either in left or right table. We can also say it is kind of combination of left and right join.</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-1353" src="http://techyaz.com/wp-content/uploads/2017/12/10-Full-Join.png" alt="full join" width="235" height="151" /></p>
<p><strong>Example</strong></p>
<p>As visual representation shows, it will show all records from Employees table and Leavesdetails table. You can see in below result set Employee Manvendra has not any leaves detail and same way Employee ID 108 has not any record in Employees table but both rows are showing their values.</p>
<pre><strong><span style="color: #0000ff;">SELECT *
FROM EMPLOYEES
<em>FULL JOIN</em> [dbo].[Leavesdetails] on [Leavesdetails].[Employeeid]=[Employees].[Employeeid]
</span></strong></pre>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1354" src="http://techyaz.com/wp-content/uploads/2017/12/9-Full-Join.png" alt="full join example" width="779" height="231" srcset="https://techyaz.com/wp-content/uploads/2017/12/9-Full-Join.png 779w, https://techyaz.com/wp-content/uploads/2017/12/9-Full-Join-300x89.png 300w, https://techyaz.com/wp-content/uploads/2017/12/9-Full-Join-768x228.png 768w" sizes="auto, (max-width: 779px) 100vw, 779px" /><br />
I hope you like this article. Please follow our <a href="https://www.facebook.com/Techyaz/">Facebook</a> page and <a href="https://twitter.com/Tech_yaz">Twitter </a>handle to get latest updates.</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/understanding-different-types-sql-joins-examples/">Understanding Different Types of SQL JOINs with Examples</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/t-sql/understanding-different-types-sql-joins-examples/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Difference between UNION and UNION ALL T-SQL Operators</title>
		<link>https://techyaz.com/sql-server/t-sql/difference-between-union-and-union-all-operators/</link>
					<comments>https://techyaz.com/sql-server/t-sql/difference-between-union-and-union-all-operators/#respond</comments>
		
		<dc:creator><![CDATA[Manvendra Deo Singh]]></dc:creator>
		<pubDate>Wed, 29 Nov 2017 09:55:36 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[T-SQL Operators]]></category>
		<category><![CDATA[union]]></category>
		<guid isPermaLink="false">http://techyaz.com/?p=1269</guid>

					<description><![CDATA[<p>If you want to combine output from multiple tables or queries into one result set, you can do this using an operator known as UNION. A UNION is used to extract rows based on some conditions specified whereas UNION ALL&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/difference-between-union-and-union-all-operators/">Difference between UNION and UNION ALL T-SQL Operators</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>If you want to combine output from multiple tables or queries into one result set, you can do this using an operator known as UNION. A UNION is used to extract rows based on some conditions specified whereas UNION ALL will extract all rows from both tables. Here, i will discuss difference between UNION and UNION ALL operators ( UNION vs UNION ALL ) in this article along with their use cases.</p>
<h3><span style="color: #333399;">UNION</span></h3>
<p>The UNION operator allows us to combine the results of two or more SELECT statements into a single result set and removes duplicate rows from that result set. Basically, it is performing a DISTINCT operation across all columns in the result set. The UNION operator is different from using joins that combine columns from two tables.</p>
<p>The following are basic rules for combining the result sets of two queries by using UNION:</p>
<ul>
<li>The number and the order of the columns must be the same in all queries.</li>
<li>The data types must be compatible.</li>
<li>Corresponding result set columns in the individual statements that are being combined with UNION must occur in the same order, because UNION compares the columns one-to-one in the order given in the individual queries.</li>
<li>The column names in the result from UNION are taken from the first individual query in the UNION statement.</li>
</ul>
<p>Below example shows how can you use UNION to combine multiple outputs into one result set.</p>
<pre><strong><span style="color: #0000ff;">SELECT * FROM Table1
<span style="color: #008000;">UNION</span>
SELECT * FROM Table2
<span style="color: #008000;">UNION</span>
SELECT * FROM Table3
</span></strong></pre>
<p>If tables would not have same structure or same data types, you will get below error while using UNION:</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-1272 size-full" src="http://techyaz.com/wp-content/uploads/2017/11/Union-Error.jpg" alt="error 205" width="799" height="157" srcset="https://techyaz.com/wp-content/uploads/2017/11/Union-Error.jpg 799w, https://techyaz.com/wp-content/uploads/2017/11/Union-Error-300x59.jpg 300w, https://techyaz.com/wp-content/uploads/2017/11/Union-Error-768x151.jpg 768w" sizes="auto, (max-width: 799px) 100vw, 799px" /></p>
<div>
<p><em><span style="color: #ff0000;">Msg 205, Level 16, State 1, Line 1</span></em><br />
<em><span style="color: #ff0000;"> All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.</span></em></p>
</div>
<h3><span style="color: #333399;">UNION ALL</span></h3>
<p>UNION ALL operator also works like UNION operator that is used to combine multiple result sets into one result set but duplicate rows will not be removed from the result set using UNION ALL unlike the result set of UNION operator that gives distinct rows.</p>
<pre><span style="color: #0000ff;"><strong>SELECT * FROM Table1
<span style="color: #008000;">UNION ALL</span>
SELECT * FROM Table2
<span style="color: #008000;">UNION ALL</span>
SELECT * FROM Table3
</strong></span></pre>
<h3><span style="color: #333399;">Difference Between UNION and UNION ALL ( UNION vs UNION ALL )</span></h3>
<p>Please find important points if we compare the difference between UNION vs UNION ALL result sets and their uses.</p>
<ul>
<li>The UNION operator removes duplicate rows from the result set and gives us distinct rows whereas UNION ALL operator does not remove duplicate rows and give all outputs in the result set.</li>
<li>UNION ALL operator works faster than UNION operator because it does not remove duplicate rows. UNION operator performs distinct sort to remove duplicate rows so its performance is little slower than UNION ALL.</li>
<li>Operator UNION ALL works with all data type columns whereas UNION operator does not work with a column that has a TEXT data type and it will throw error during execution.</li>
</ul>
<h3><span style="color: #333399;">Guidelines to use UNION  and UNION ALL</span></h3>
<p>I have discussed difference between UNION and UNION ALL in above section. Here i will explain below guidelines you should consider while using UNION and UNION ALL operators with other T-SQL statements. Below rules are also apply to EXCEPT and INTERSECT operator:</p>
<ul>
<li>The first query can contain an INTO clause that creates a table to hold the final result set. Only the first query can use an INTO clause. If the INTO clause appears anywhere else, SQL Server displays an error message.</li>
<li>ORDER BY is allowed only at the end of the statement. It cannot be used within the individual queries that make up the statement.</li>
<li>GROUP BY and HAVING clauses can be used only within individual queries; they cannot be used to affect the final result set.</li>
<li>UNION, EXCEPT and INTERSECT can be used within an INSERT statement.</li>
<li>The FOR BROWSE clause cannot be used in statements that involve the UNION, EXCEPT and INTERSECT operators.</li>
</ul>
<p><em><span style="color: #800000;"><strong>Related Articles:</strong></span></em></p>
<ul>
<li><strong><a href="https://techyaz.com/sql-server/how-to-protect-my-stored-procedure-code/" target="_blank" rel="noopener">How to Protect Stored Procedure Code?</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/create-logon-trigger-restrict-sysadmin-logins-connect-sql-server-given-time-interval/" target="_blank" rel="noopener">Create a Logon Trigger to Restrict sysadmin logins to Connect to SQL Server during a given time Interval</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/get-row-counts-tables-database/" target="_blank" rel="noopener">How to get Total Row Count of all tables of a database</a></strong></li>
</ul>
<p>I hope you like this article. Please follow our <a href="https://www.facebook.com/Techyaz/">Facebook</a> page and <a href="https://twitter.com/Tech_yaz">Twitter </a>handle to get latest updates.</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/difference-between-union-and-union-all-operators/">Difference between UNION and UNION ALL T-SQL Operators</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/t-sql/difference-between-union-and-union-all-operators/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Why is table row count showing incorrect value in sys.partitions or in DMV sys.dm_db_partition_stats</title>
		<link>https://techyaz.com/sql-server/t-sql/table-row-count-showing-incorrect-value-sys-partitions-dmv-sys-dm_db_partition_stats/</link>
					<comments>https://techyaz.com/sql-server/t-sql/table-row-count-showing-incorrect-value-sys-partitions-dmv-sys-dm_db_partition_stats/#comments</comments>
		
		<dc:creator><![CDATA[Manvendra Deo Singh]]></dc:creator>
		<pubDate>Tue, 05 Sep 2017 08:37:02 +0000</pubDate>
				<category><![CDATA[DBCC & DMVs]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Troubleshooting]]></category>
		<category><![CDATA[DBCC]]></category>
		<category><![CDATA[dmv]]></category>
		<category><![CDATA[row count]]></category>
		<guid isPermaLink="false">http://techyaz.com/?p=800</guid>

					<description><![CDATA[<p>Last week, I had a requirement to check the row counts of all the tables present in a database. I used attached article to get the row counts of all tables. We get row counts of all tables using system&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/table-row-count-showing-incorrect-value-sys-partitions-dmv-sys-dm_db_partition_stats/">Why is table row count showing incorrect value in sys.partitions or in DMV sys.dm_db_partition_stats</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Last week, I had a requirement to check the row counts of all the tables present in a database. I used attached article to <a href="https://techyaz.com/sql-server/get-row-counts-tables-database/" target="_blank" rel="noopener">get the row counts of all tables</a>. We get row counts of all tables using system catalog view sys.partitions or DMV sys.dm_db_partition_stats. But row counts were not showing correct for some of the tables. Here i will explain why row counts are not showing equal to the exact row count of the table.</p>
<p>Let&#8217;s check the row counts of tables using script that are using sys.partitions system catalog view and compare the output of a table with the output of SELECT COUNT (*) .</p>
<h3><span style="color: #000080;"><strong>Get Row Counts of All Tables using sys.partitions or DMV sys.dm_db_partition_stats</strong></span></h3>
<p>System catalog view sys.partitions contains a row for each partition of all the tables and most types of indexes in the database. All tables and indexes in SQL Server contain at least one partition, whether they are explicitly partitioned. Run below script to get the row count of all tables in a database. If you want to find row counts using DMV sys.dm_db_partition_stats, i would suggest you to visit our attached article <a href="https://techyaz.com/sql-server/get-row-counts-tables-database/" target="_blank" rel="noopener">how to find row counts of all tables in a database</a>.</p>
<pre class="brush: sql; title: ; notranslate" title=""><strong><span class="kwrd" style="color: blue;"><span style="color: #008000;">--Run below script to get the row counts of all tables in a database.</span>
SELECT      SCHEMA_NAME(O.schema_id) + '.' +
        O.Name As [Schema.TableName], SUM(P.rows) AS 'Total_RowCount'
FROM        sys.objects O
INNER JOIN sys.partitions P ON O.object_id = P.object_id
WHERE     O.type = 'U'
GROUP BY    O.schema_id, O.Name
</span></strong></pre>
<p>You can see row count of all tables in below screenshot.</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-801" src="http://techyaz.com/wp-content/uploads/2017/09/1-sys.patitions.jpg" alt="row count" width="649" height="390" srcset="https://techyaz.com/wp-content/uploads/2017/09/1-sys.patitions.jpg 649w, https://techyaz.com/wp-content/uploads/2017/09/1-sys.patitions-300x180.jpg 300w" sizes="auto, (max-width: 649px) 100vw, 649px" /></p>
<p>Now we will check row count of a table using SELECT COUNT(*) command. I ran below command and you can see that row count is not same here. Row count came using sys.partitions system catalog view has more value than the row count of SELECT COUNT (*).</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-802" src="http://techyaz.com/wp-content/uploads/2017/09/2-select-count.jpg" alt="" width="872" height="158" srcset="https://techyaz.com/wp-content/uploads/2017/09/2-select-count.jpg 872w, https://techyaz.com/wp-content/uploads/2017/09/2-select-count-300x54.jpg 300w, https://techyaz.com/wp-content/uploads/2017/09/2-select-count-768x139.jpg 768w" sizes="auto, (max-width: 872px) 100vw, 872px" /></p>
<p>As i said above &#8220;sys.partitions or DMV sys.dm_db_partition_stats contains a row for each partition of all the tables and most types of indexes in the database&#8221; so when you are fetching row counts of tables using sys.partitions system catalog view or DMV sys.dm_db_partition_stats, it will display the row counts after adding indexes row counts and table row counts. That is why value is showing more in sys.partitions or in sys.dm_db_partition_stats output.</p>
<p>Suppose you have a table with 5000 rows in it and you&#8217;ve defined 5 indexes on it, the above query would show 5*5000=25000 rows for that table. So, it isn&#8217;t just that some rows are inconsistence, it is that some rows are multiplied by the number of indexes they have.</p>
<p>If we need correct row count using the system cataog view sys.partitions then we need to put a condition in WHERE clause for index_id column. We can also get total row count of all the heap tables or for all the clustered index tables as well by changing the value of index_id mentioned in WHERE clause. Index_id value indicates the ID of the index within the object to which this table belongs.</p>
<ul>
<li>0 = heap</li>
<li>1 = clustered index</li>
<li>2 or greater = nonclustered index</li>
</ul>
<p>We can pass index_id value in above script to get the total row count of all tables whether it is heap or clustered index table. If you need total row count of all the tables present in a database, you should put index_id&lt;2 condition in WHERE clause. Run below scripts to get the row count of all heap &amp; clustered index tables.</p>
<pre class="brush: sql; title: ; notranslate" title=""><strong><span class="kwrd" style="color: blue;"><span style="color: #008000;">--Run below script to get the row counts of all tables in a database.
--Change the value of index_id to 0 or 1 to get total row count to heap or cluster index tables.
</span>SELECT      SCHEMA_NAME(O.schema_id) + '.' +
        O.Name As [Schema.TableName], SUM(P.rows) AS 'Total_RowCount'
FROM        sys.objects O
INNER JOIN sys.partitions P ON O.object_id = P.object_id
WHERE     O.type = 'U'
		AND P.index_id&lt;2  
GROUP BY    O.schema_id, O.Name
</span></strong></pre>
<p>Now you can run above script and compare the output with the SELECT COUNT(*) output. We can see row count for given table is showing same after adding idex_id condition in below screenshot.</p>
<p><img loading="lazy" decoding="async" class="aligncenter wp-image-803 size-full" src="http://techyaz.com/wp-content/uploads/2017/09/3.5.jpg" alt="row count of all table using sys.partitions" width="769" height="489" srcset="https://techyaz.com/wp-content/uploads/2017/09/3.5.jpg 769w, https://techyaz.com/wp-content/uploads/2017/09/3.5-300x191.jpg 300w, https://techyaz.com/wp-content/uploads/2017/09/3.5-768x488.jpg 768w" sizes="auto, (max-width: 769px) 100vw, 769px" /></p>
<p>I hope you like this article. You can comment us about your questions in below section. Please follow us on our <a href="https://www.facebook.com/Techyaz/">facebook page</a> and on <a href="https://twitter.com/Tech_yaz">Twitter </a>handle to get latest updates.</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/table-row-count-showing-incorrect-value-sys-partitions-dmv-sys-dm_db_partition_stats/">Why is table row count showing incorrect value in sys.partitions or in DMV sys.dm_db_partition_stats</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/t-sql/table-row-count-showing-incorrect-value-sys-partitions-dmv-sys-dm_db_partition_stats/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>How to Get Row Count of All Tables of a Database</title>
		<link>https://techyaz.com/sql-server/t-sql/get-row-counts-tables-database/</link>
					<comments>https://techyaz.com/sql-server/t-sql/get-row-counts-tables-database/#respond</comments>
		
		<dc:creator><![CDATA[Manvendra Deo Singh]]></dc:creator>
		<pubDate>Wed, 30 Aug 2017 14:08:41 +0000</pubDate>
				<category><![CDATA[DBCC & DMVs]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Administration]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[dmv]]></category>
		<category><![CDATA[row count]]></category>
		<guid isPermaLink="false">http://techyaz.com/?p=703</guid>

					<description><![CDATA[<p>Last week, I had a requirement to check the row count of all tables having a specific schema of databases. I did some search on this because my database has thousands of tables and it was not possible to run&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/get-row-counts-tables-database/">How to Get Row Count of All Tables of a Database</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Last week, I had a requirement to check the row count of all tables having a specific schema of databases. I did some search on this because my database has thousands of tables and it was not possible to run SELECT COUNT (*) for each table individually. Here i will explain how to get row counts of all:</p>
<ul>
<li>Tables</li>
<li>Heap tables only</li>
<li>Clustered index tables only</li>
<li>Tables having specific Schema</li>
</ul>
<p>We can get total row counts of all tables using system catalog view <strong>sys.partitions</strong> or DMV <strong>sys.dm_db_partition_stats</strong>. Read attached article if you are getting <a href="https://techyaz.com/sql-server/troubleshooting/table-row-count-showing-incorrect-value-sys-partitions-dmv-sys-dm_db_partition_stats/" target="_blank" rel="noopener">wrong row counts using this DMV or catalog view</a>. Let&#8217;s check the row counts using both ways in this tip.</p>
<h3><span style="color: #000080;"><strong>Get Row Counts of Tables using sys.partitions</strong></span></h3>
<p>System catalog view sys.partitions contains a row for each partition of all the tables and most types of indexes in the database. Special index types such as Full-Text, Spatial, and XML are not included in this view. All tables and indexes in SQL Server contain at least one partition, whether or not they are explicitly partitioned. Run below script to get the row count of all tables in a database.</p>
<pre class="brush: sql; title: ; notranslate" title=""><span class="kwrd" style="color: blue;"><strong><span style="color: #008000;">--Run below script to get the row counts of all tables in a database.</span></strong>
<strong>SELECT      SCHEMA_NAME(O.schema_id) + '.' +SELECT      SCHEMA_NAME(O.schema_id) + '.' + O.name As [Schema.TableName],  SUM(P.rows) AS [Total_RowCount] </strong>
<strong>FROM        sys.objects O</strong>
<strong>INNER JOIN sys.partitions P ON O.object_id = P.object_id</strong>
<strong>WHERE     O.type = 'U' AND P.index_id&lt;2  </strong>
<strong>GROUP BY    O.schema_id, O.name</strong>
<strong>ORDER BY [Total_RowCount] desc
</strong></span></pre>
<p>You can see row count of all tables in below screenshot.</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-704" src="http://techyaz.com/wp-content/uploads/2017/08/1-row-count-of-all-tables.jpg" alt="Row Counts of All Tables" width="824" height="439" srcset="https://techyaz.com/wp-content/uploads/2017/08/1-row-count-of-all-tables.jpg 824w, https://techyaz.com/wp-content/uploads/2017/08/1-row-count-of-all-tables-300x160.jpg 300w, https://techyaz.com/wp-content/uploads/2017/08/1-row-count-of-all-tables-768x409.jpg 768w" sizes="auto, (max-width: 824px) 100vw, 824px" /></p>
<p>We can get total row count of all the heap tables or for all the clustered index tables as well by changing the value of <strong>index_id</strong> mentioned in WHERE clause. <strong>Index_id</strong> value indicates the ID of the index within the object/table. If <strong>index_id</strong> value is 0 that means that table doesn&#8217;t have any cluster index and a Heap table.</p>
<ul>
<li>0 = heap</li>
<li>1 = clustered index</li>
<li>2 or greater = nonclustered index</li>
</ul>
<p>We can pass <strong>index_id</strong> value in above script to get the total row count of all tables whether it is heap or clustered index table. Run below scripts to get the row count of all heap tables.</p>
<pre class="brush: sql; title: ; notranslate" title=""><strong><span class="kwrd" style="color: blue;"><span style="color: #008000;">--Run below script to get the row counts of all Heap (tables without cluster index) in a database.
--Change the value of index_id from 0 to 1 to get total row count to cluster index tables.</span>
<span style="color: #0000ff;">SELECT      SCHEMA_NAME(O.schema_id) + '.' +
        O.</span></span><span style="color: #0000ff;">n<span class="kwrd">ame As [Schema.TableName], SUM(P.rows) AS [Total_RowCount</span>]<span class="kwrd">
FROM        sys.objects O
INNER JOIN sys.partitions P ON O.object_id = P.object_id
WHERE     O.type = 'U'
		AND P.index_id=0  <span style="color: #008000;">--Change this value according to your need</span>
GROUP BY    O.schema_id, O.</span>n<span class="kwrd">ame
ORDER BY [Total_RowCount] desc
</span></span></strong></pre>
<p><strong><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-705" src="http://techyaz.com/wp-content/uploads/2017/08/2-row-count-of-all-heap-tables.jpg" alt="row count of all heap tables" width="805" height="279" srcset="https://techyaz.com/wp-content/uploads/2017/08/2-row-count-of-all-heap-tables.jpg 805w, https://techyaz.com/wp-content/uploads/2017/08/2-row-count-of-all-heap-tables-300x104.jpg 300w, https://techyaz.com/wp-content/uploads/2017/08/2-row-count-of-all-heap-tables-768x266.jpg 768w" sizes="auto, (max-width: 805px) 100vw, 805px" /></strong></p>
<p>You can see there are only two tables that have no cluster index in above screenshot. We can also get the <strong>row count of all tables having a specific schema</strong> by adding a condition in WHERE clause.</p>
<pre class="brush: sql; title: ; notranslate" title=""><strong><span style="color: #0000ff;"><span class="kwrd"><span style="color: #008000;">--Run below script to get the row count of all tables having dbo Schema in a database.</span>
SELECT      SCHEMA_NAME(O.schema_id) + '.' +
        O.name As [Schema.TableName], SUM(P.rows) AS [Total_RowCount</span>]<span class="kwrd">
FROM        sys.objects O
INNER JOIN sys.partitions P ON O.object_id = P.object_id
WHERE     O.type = 'U'
		AND P.index_id&lt;2  
		AND SCHEMA_NAME(O.schema_id) ='dbo'<span style="color: #008000;">--Change dbo with your schema name for which you need row counts</span>
GROUP BY    O.schema_id, O.</span>n<span class="kwrd">ame
ORDER BY [Total_RowCount] desc
</span></span></strong></pre>
<p>You can see the output in below image. You can make changes as per your need.</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-708" src="http://techyaz.com/wp-content/uploads/2017/08/4-row-count-of-all-tables-using-dmv.jpg" alt="row count of heap" width="690" height="339" srcset="https://techyaz.com/wp-content/uploads/2017/08/4-row-count-of-all-tables-using-dmv.jpg 690w, https://techyaz.com/wp-content/uploads/2017/08/4-row-count-of-all-tables-using-dmv-300x147.jpg 300w" sizes="auto, (max-width: 690px) 100vw, 690px" /></p>
<h3><span style="color: #000080;"><strong>Get Row Count of All Tables using DMV sys.dm_db_partition_stats</strong></span></h3>
<p>DMV sys.dm_db_partition_stats returns page and row-count information for every partition in the current database. We will leverage this DMV to get row count details of all tables. Run below script to get the row count of all tables using this DMV.</p>
<pre class="brush: sql; title: ; notranslate" title=""><span class="kwrd" style="color: blue;"><strong><span style="color: #008000;">--Run below script to get the row count of all tables in a database.</span>
SELECT QUOTENAME(SCHEMA_NAME(O.schema_id)) + '.' + QUOTENAME(O.name) AS [Schema.TableName]
      , SUM(p_dmv.row_count) AS [Total_RowCount]
FROM sys.objects AS O
      INNER JOIN sys.dm_db_partition_stats AS p_dmv
            ON O.object_id = p_dmv.object_id
WHERE  O.type = 'U'
        AND p_dmv.index_id &lt; 2
GROUP BY
      O.schema_id
      , O.name
ORDER BY [Schema.TableName]
GO</strong> 
</span></pre>
<p>We can see the output of above script in below image.</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-707" src="http://techyaz.com/wp-content/uploads/2017/08/3-row-count-of-all-tables-for-a-specific-schema.jpg" alt="row count of all tables using dmv" width="751" height="464" srcset="https://techyaz.com/wp-content/uploads/2017/08/3-row-count-of-all-tables-for-a-specific-schema.jpg 751w, https://techyaz.com/wp-content/uploads/2017/08/3-row-count-of-all-tables-for-a-specific-schema-300x185.jpg 300w" sizes="auto, (max-width: 751px) 100vw, 751px" /></p>
<p>We can pass index_id value in above script to get the total row count of all tables whether it is heap or clustered index table. Run below scripts to get the row count of all heap &amp; clustered index tables.</p>
<pre class="brush: sql; title: ; notranslate" title=""><span class="kwrd" style="color: blue;"><strong><span style="color: #008000;">--Run below script to get the row counts of all Heap (tables without cluster index) in a database.
--Change the value of index_id from 0 to 1 to get total row count to cluster index tables.
--Change the schema name if you want the row count of tables that are having specific schema.</span>
SELECT QUOTENAME(SCHEMA_NAME(O.schema_id)) + '.' + QUOTENAME(O.name) AS [Schema.TableName]
      , SUM(p_dmv.row_count) AS [Total_RowCount]
FROM sys.objects AS O
      INNER JOIN sys.dm_db_partition_stats AS p_dmv
            ON O.object_id = p_dmv.object_id
WHERE  O.type = 'U'
        AND p_dmv.index_id &lt; 2
	AND SCHEMA_NAME(O.schema_id) = 'dbo'<span style="color: #008000;">--Change the name of your Schema to get row count of all tables having this schema.</span>
GROUP BY
      O.schema_id</strong>
<strong>      , O.name
ORDER BY [Schema.TableName]
GO 
</strong></span></pre>
<p>And here is the output:</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-708" src="http://techyaz.com/wp-content/uploads/2017/08/4-row-count-of-all-tables-using-dmv.jpg" alt="row count of heap" width="690" height="339" srcset="https://techyaz.com/wp-content/uploads/2017/08/4-row-count-of-all-tables-using-dmv.jpg 690w, https://techyaz.com/wp-content/uploads/2017/08/4-row-count-of-all-tables-using-dmv-300x147.jpg 300w" sizes="auto, (max-width: 690px) 100vw, 690px" /></p>
<p>I hope you like this article. You can comment about your questions in comment section. Please follow us on our <a href="https://www.facebook.com/Techyaz/">facebook page</a> and on <a href="https://twitter.com/Tech_yaz">Twitter </a>handle to get latest updates.</p>
<p><span style="color: #800000;"><em><strong>Related Articles:</strong></em></span></p>
<ul>
<li><strong><a href="https://techyaz.com/sql-server/troubleshooting/table-row-count-showing-incorrect-value-sys-partitions-dmv-sys-dm_db_partition_stats/" target="_blank" rel="noopener">Why rou count showing incorrect value in sys.partitions?</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/understanding-different-types-sql-joins-examples/" target="_blank" rel="noopener">Understanding Different Types of SQL JOINs with Examples</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/create-logon-trigger-restrict-sysadmin-logins-connect-sql-server-given-time-interval/" target="_blank" rel="noopener">Create a Logon Trigger to Restrict sysadmin logins to Connect to SQL Server during a given time Interval</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/how-to-protect-my-stored-procedure-code/" target="_blank" rel="noopener">How to Protect Stored Procedure Code/Script?</a></strong></li>
</ul>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/get-row-counts-tables-database/">How to Get Row Count of All Tables of a Database</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/t-sql/get-row-counts-tables-database/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Create a Logon Trigger to Restrict sysadmin logins to connect to SQL Server for given Time Interval</title>
		<link>https://techyaz.com/sql-server/t-sql/create-logon-trigger-restrict-sysadmin-logins-connect-sql-server-given-time-interval/</link>
					<comments>https://techyaz.com/sql-server/t-sql/create-logon-trigger-restrict-sysadmin-logins-connect-sql-server-given-time-interval/#comments</comments>
		
		<dc:creator><![CDATA[Maruti Nandan]]></dc:creator>
		<pubDate>Mon, 21 Aug 2017 07:58:44 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[HowTO]]></category>
		<category><![CDATA[logins]]></category>
		<category><![CDATA[Logon Trigger]]></category>
		<guid isPermaLink="false">http://techyaz.com/?p=566</guid>

					<description><![CDATA[<p>I was struggling with unwanted modifications of SQL Server configurations by random SQL Server sysadmin logins who were part of our SQL Server Instance. Every time we cannot monitor what has been changed and who are doing it on your&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/create-logon-trigger-restrict-sysadmin-logins-connect-sql-server-given-time-interval/">Create a Logon Trigger to Restrict sysadmin logins to connect to SQL Server for given Time Interval</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>I was struggling with unwanted modifications of SQL Server configurations by random SQL Server sysadmin logins who were part of our SQL Server Instance. Every time we cannot monitor what has been changed and who are doing it on your non-prod servers. We generally monitor our production boxes for any unwanted activities. here i will show you how to create a Logon trigger to restrict sysadmin logins to not perform such activities. I will also show you how we can restrict them between a given time interval. Access will be allowed during office hours and restricted during out of office hours.</p>
<h3><span style="color: #000080;"><strong>Logon trigger Overview</strong></span></h3>
<p><strong>Logon Triggers</strong> fire in response to a LOGON event. It fires after the authentication phase of logging in finishes, but before the user session is actually established. Logon Triggers are very useful in tracking and restricting login events.</p>
<p>Sometimes few applications require sysadmin privilege to run and if you downgrade this access, application stops working. So, we cannot restrict sysadmin logins of such applications. There is always a scope of security breach if another login has sysadmin access except DBAs on SQL Server Instance.</p>
<p>To fix this issue, I have created a logon trigger that will restrict access of such application logins who are <strong>sysadmin </strong>on your SQL Server Instance<strong>. </strong> We can restrict them to access SQL Server directly through SSMS or any other medium. You need to pass that medium into code just like I have passed Management studio.</p>
<h3><span style="color: #000080;"><strong>Logon Trigger T-SQL Code</strong></span></h3>
<p>Below is the Logon Trigger T-SQL code which will be used to restrict any login for given time frame.</p>
<pre class="brush: sql; title: ; notranslate" title=""><strong><span class="kwrd" style="color: blue;">
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE trigger [logon_rest]
on all server for logon
as begin
declare @Program varchar(128)
declare @systemname varchar(128)
select @Program =PROGRAM_NAME,@systemname=HOST_NAME from sys.dm_exec_sessions as A
where a.session_id=@@SPID
if ORIGINAL_LOGIN() in('sa') </span><span class="kwrd" style="color: green;">/**Login For Which Access Need to restricted **/</span><span class="kwrd" style="color: blue;">
and @Program like'%Management%studio' </span><span class="kwrd" style="color: green;">/** Program via access restricted **/</span><span class="kwrd" style="color: blue;">
and
(GETDATE()&gt;(dateadd(day, datediff(day, 0, getdate()), 0) + '18:00') </span><span class="kwrd" style="color: green;">/**Last time till access allowed **/</span><span class="kwrd" style="color: blue;">
or
GETDATE()&lt;(dateadd(day, datediff(day, 0, getdate()), 0) + '09:00')) </span><span class="kwrd" style="color: green;">/**Start time from access allowed **/</span><span class="kwrd" style="color: blue;">
begin
Raiserror ('This is out of office hour',1,1)
rollback;
end
end;
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
ENABLE TRIGGER [logon_rest] ON ALL SERVER
GO
</span></strong></pre>
<h5><span style="color: #000080;"><strong>Testing</strong></span></h5>
<p>For the testing purpose, I have taken sa account as a potential application login. You can use your identified login which are running with sysadmin rights. Even we can restrict such logins to connect to SQL Server during given time frame.</p>
<p>Here we have disabled sa server level login to connect through SSMS Before 9:00 AM and Post 6:00 PM. That means server login sa cannot connect to SQL Server Instance between given time slot. You can alter the time slot as per your convenience.</p>
<p>Just for example we have tried to login on SQL server through SSMS between given time frame and here you can see the output. I am not able to connect to SQL Server anymore. You can see this in below screenshot. But at the same time you can login to the same SQL Server Instance using other sysadmin or normal login accounts which is available in sys.syslogins.</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-567" src="http://techyaz.com/wp-content/uploads/2017/08/1-sa-login.jpg" alt="logon trigger" width="644" height="488" srcset="https://techyaz.com/wp-content/uploads/2017/08/1-sa-login.jpg 644w, https://techyaz.com/wp-content/uploads/2017/08/1-sa-login-300x227.jpg 300w" sizes="auto, (max-width: 644px) 100vw, 644px" /></p>
<p>Even we can track the <a href="https://techyaz.com/sql-server/troubleshooting/fix-error-15434-not-drop-login-user-currently-logged/" target="_blank" rel="noopener">failed login</a> attempt made on this SQL Server Instance once you can make a database connection using another login. You can check the SQL Server error logs or you can just run <strong>sp_readerrorlog</strong> stored procedure to display errors. You can see our test login attempt is showing in below screenshot on the server.</p>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-568" src="http://techyaz.com/wp-content/uploads/2017/08/2-errorlog.jpg" alt="" width="607" height="215" srcset="https://techyaz.com/wp-content/uploads/2017/08/2-errorlog.jpg 607w, https://techyaz.com/wp-content/uploads/2017/08/2-errorlog-300x106.jpg 300w" sizes="auto, (max-width: 607px) 100vw, 607px" /></p>
<p><span style="color: #800000;"><em><strong>Read more about SQL Server Login Issue related Articles:</strong></em></span></p>
<ul>
<li><strong><a href="https://techyaz.com/sql-server/troubleshooting/fix-error-15173/" target="_blank" rel="noopener">How to fix Error 15173: Revoke the permissions before dropping the login.</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/troubleshooting/fix-error-15138-error-3729/" target="_blank" rel="noopener">Fix error 15138 &amp; Error 3729: DROP login failed</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/troubleshooting/fix-error-15434-not-drop-login-user-currently-logged/" target="_blank" rel="noopener">How to fix error 15434: Could not drop login as the user is currently logged in</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/troubleshooting/fix-error-15170/" target="_blank" rel="noopener">Fixing error 15170: Login owns one or more SQL Agent Jobs</a></strong></li>
</ul>
<p>I hope you like this article. Please follow us on our <a href="https://www.facebook.com/Techyaz/">facebook page</a> and on <a href="https://twitter.com/Tech_yaz">Twitter </a>handle to get latest updates.</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/create-logon-trigger-restrict-sysadmin-logins-connect-sql-server-given-time-interval/">Create a Logon Trigger to Restrict sysadmin logins to connect to SQL Server for given Time Interval</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/t-sql/create-logon-trigger-restrict-sysadmin-logins-connect-sql-server-given-time-interval/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>How to Protect Stored Procedure Code in SQL Server?</title>
		<link>https://techyaz.com/sql-server/t-sql/how-to-protect-my-stored-procedure-code/</link>
					<comments>https://techyaz.com/sql-server/t-sql/how-to-protect-my-stored-procedure-code/#respond</comments>
		
		<dc:creator><![CDATA[Manvendra Deo Singh]]></dc:creator>
		<pubDate>Sun, 17 Mar 2013 15:27:00 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[HowTO]]></category>
		<guid isPermaLink="false"></guid>

					<description><![CDATA[<p>Have you ever thought how to protect stored procedure codes deployed on your SQL Server Instance? When deploying applications to a client&#8217;s server(s) or to a shared SQL Server, there is often a concern that other people might peek at&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/how-to-protect-my-stored-procedure-code/">How to Protect Stored Procedure Code in SQL Server?</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Have you ever thought how to protect stored procedure codes deployed on your SQL Server Instance? When deploying applications to a client&#8217;s server(s) or to a shared SQL Server, there is often a concern that other people might peek at your business logic. Since often the code in a stored procedure can be proprietary, it is understandable that we might want to protect our T-SQL work. Here, i will explain how to protect stored procedure from getting displayed its code in sp_helptext command.</p>
<p>We can use &#8220;<strong>WITH ENCRYPTION&#8221; </strong>option to protect our T-SQL code. Have a look at below SQL code that i have been used to protect my stored procedure.</p>
<pre><strong><span style="color: #0000ff;">CREATE PROCEDURE dbo.Manvendra
<span style="color: #008000;">WITH ENCRYPTION</span>
AS
BEGIN
SELECT 'SQL statements'
END
</span></strong></pre>
<p>Now when you will try to run sp_helptext to see the code, below error will appear.</p>
<p><strong><span style="color: #ff0000;">&#8220;The text for object &#8216;Manvendra&#8217; is encrypted&#8221;.</span></strong></p>
<p><em><span style="color: #800000;"><strong>Read Articles:</strong></span></em></p>
<ul>
<li><strong><a href="https://techyaz.com/sql-server/how-to-disable-auto-commit-in-sql-server/" target="_blank" rel="noopener">How to Disable Autocommit in SQL Server?</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/create-logon-trigger-restrict-sysadmin-logins-connect-sql-server-given-time-interval/" target="_blank" rel="noopener">Create a Logon Trigger to Restrict sysadmin logins to Connect to SQL Server during a given time Interval</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/troubleshooting/how-to-move-a-table-to-a-different-filegroup/" target="_blank" rel="noopener">How to Move a Table to another filegroup?</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/t-sql/new-t-sql-functions-introduced-sql-server-2017/" target="_blank" rel="noopener">New T-SQL Functions Introduced in SQL Server 2017</a></strong></li>
</ul>
<p>I hope you like his tip. You can drop your questions and feedbacks in comment section given below. You can also follow our <strong><a href="https://www.facebook.com/Techyaz/">Facebook</a></strong> page and <strong><a href="https://twitter.com/Tech_yaz">Twitter</a></strong> handle to get latest updates.</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/how-to-protect-my-stored-procedure-code/">How to Protect Stored Procedure Code in SQL Server?</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/t-sql/how-to-protect-my-stored-procedure-code/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Disable AutoCommit in SQL Server?</title>
		<link>https://techyaz.com/sql-server/t-sql/how-to-disable-auto-commit-in-sql-server/</link>
					<comments>https://techyaz.com/sql-server/t-sql/how-to-disable-auto-commit-in-sql-server/#respond</comments>
		
		<dc:creator><![CDATA[Manvendra Deo Singh]]></dc:creator>
		<pubDate>Wed, 09 Feb 2011 11:25:00 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[HowTO]]></category>
		<guid isPermaLink="false">http://techyaz.com/sql-server/t-sql-codes/how-to-disable-auto-commit-in-sql-server/</guid>

					<description><![CDATA[<p>Autocommit mode is the default transaction management mode of the SQL Server Database Engine. Every Transact-SQL statement is committed or rolled back when it completes. If a statement completes successfully, it is committed; if it encounters any error, it is&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/how-to-disable-auto-commit-in-sql-server/">How to Disable AutoCommit in SQL Server?</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Autocommit mode is the default transaction management mode of the SQL Server Database Engine. Every Transact-SQL statement is committed or rolled back when it completes. If a statement completes successfully, it is committed; if it encounters any error, it is rolled back.</p>
<h3><span style="color: #333399;">Enable or Disable Autocommit using T-SQL</span></h3>
<p>You can turn off auto commit by setting implicit_transactions ON. Run below T-SQL command to turn off autocommit in SQL Server.</p>
<pre><strong><span style="color: #0000ff;">SET IMPLICIT_TRANSACTIONS ON</span></strong></pre>
<p>When the setting is ON, it returns to implicit transaction mode. In implicit transaction mode, every change you make starts a transactions which you have to commit manually. When you need to enable it just run above command with OFF clause. Run below command to enable auto commit.</p>
<pre><span style="color: #0000ff;"><strong>SET IMPLICIT_TRANSACTIONS OFF</strong></span></pre>
<p>autocommit is the default for Sql Server 2000 onwards.</p>
<h3><span style="color: #333399;">Enable or Disable Autocommit using GUI</span></h3>
<p>You can also turn off autocommit using SQL Server Management Studio by following below steps.</p>
<ol>
<li>Connect to SQL Server Instance in SQL Server Management Studio.</li>
<li>From the Menu bar, click on <strong>Tools</strong> and then choose <strong>Options</strong></li>
<li>Select <strong>Query Execution</strong> then <strong>SQL Server</strong> followed by <strong>ANSI</strong></li>
<li>Make sure to click on check box <strong>SET IMPLICIT_TRANSACTIONS</strong></li>
<li>Click on<strong> OK</strong></li>
</ol>
<p>Now, open a new Query window and start executing the scripts. If you want to turn it on the follow same steps as given above and uncheck the <strong>SET IMPLICIT_TRANSACTIONS </strong>option that are mentioned in step no 4.</p>
<p><span style="color: #800000;"><strong>Related Articles:</strong></span></p>
<ul>
<li><strong><a href="https://techyaz.com/sql-server/get-row-counts-tables-database/" target="_blank" rel="noopener">How to get Total Row Count of all tables of a database?</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/how-to-avoid-page-split-in-sql-server/" target="_blank" rel="noopener">Understanding Page Split in SQL Server</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/what-should-be-the-best-value-for-fill-factor-in-sql-server/" target="_blank" rel="noopener">What should be the Best Value for Fill Factor?</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/sql-server-on-linux/21-missing-features-in-sql-server-on-linux/" target="_blank" rel="noopener">Top 21 New Features in SQL Server 2017 that is missing from SQL Server on Linux</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/always-turn-off-database-auto-shrink/" target="_blank" rel="noopener">Read Why We should always Turn Off Database AutoShrink Database Property</a></strong></li>
</ul>
<p>I hope you like this tip. You can drop your questions and feedbacks in comment sections. Also you can follow our <strong><a href="https://www.facebook.com/Techyaz/">facebook</a></strong> page and <strong><a href="https://twitter.com/Tech_yaz">Twitter</a></strong> handle to get latest updates.</p>
<p>The post <a href="https://techyaz.com/sql-server/t-sql/how-to-disable-auto-commit-in-sql-server/">How to Disable AutoCommit in SQL Server?</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/t-sql/how-to-disable-auto-commit-in-sql-server/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to find Total no of Traces Running on your Database Instance?</title>
		<link>https://techyaz.com/sql-server/traces/to-find-out-no-of-traces-running-on-your-db-instance/</link>
					<comments>https://techyaz.com/sql-server/traces/to-find-out-no-of-traces-running-on-your-db-instance/#respond</comments>
		
		<dc:creator><![CDATA[Manvendra Deo Singh]]></dc:creator>
		<pubDate>Fri, 21 Jan 2011 13:55:00 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Administration]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Traces]]></category>
		<category><![CDATA[Troubleshooting]]></category>
		<guid isPermaLink="false"></guid>

					<description><![CDATA[<p>Suppose you want to find out how many traces are running on your SQL Server database instance? If you don&#8217;t have any idea about it then you should read this article to get the answer. Here is a solution to&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/traces/to-find-out-no-of-traces-running-on-your-db-instance/">How to find Total no of Traces Running on your Database Instance?</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Suppose you want to find out how many traces are running on your SQL Server database instance?<br />
If you don&#8217;t have any idea about it then you should read this article to get the answer. Here is a solution to find out no of traces running on your SQL Server Instance. You can also stop the trace files using script given in this article. Run below query to find out total number of traces running on your database server.<b><br />
</b></p>
<pre><strong><span style="color: #0000ff;">SELECT * FROM fn_trace_getinfo(default);
GO
</span></strong></pre>
<p>You will get below output:</p>
<div style="clear: both; text-align: center;"><a style="margin-left: 1em; margin-right: 1em;" href="http://techyaz.com/wp-content/uploads/2011/01/trace.jpg"><img loading="lazy" decoding="async" src="http://techyaz.com/wp-content/uploads/2011/01/trace-300x46.jpg" width="320" height="50" border="0" /></a></div>
<p>You can find out the no of traces through trace ID column/count. Now suppose you have to stop them then run below script:</p>
<pre><span style="color: #0000ff;"><strong><span style="color: #008000;">--Replace TraceID with your TraceID number that you want to stop.</span>
DECLARE @TraceID int
SET @TraceID = 2
EXEC sp_trace_setstatus @TraceID, 0
EXEC sp_trace_setstatus @TraceID, 2
</strong></span></pre>
<p>Replace the trace id value with one which has to be stopped.</p>
<p><em><strong><span style="color: #800000;">Related Article:</span></strong></em></p>
<ul>
<li><strong><a href="https://techyaz.com/sql-server/what-should-be-the-best-value-for-fill-factor-in-sql-server/" target="_blank" rel="noopener">What should be Best value for Fill Factor in SQL Server?</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/always-turn-off-database-auto-shrink/" target="_blank" rel="noopener">Why Should You Always Turn Off Database Auto Shrink Property?</a></strong></li>
<li><strong><a href="https://techyaz.com/sql-server/understanding-database-autogrowth-sql-server/" target="_blank" rel="noopener">Understanding SQL Server Database Autogrowth Property</a></strong></li>
</ul>
<p>I hope you like this article. You can writes us about your questions and feedbacks in below comment section. You can also follow our <strong><a href="https://www.facebook.com/Techyaz/">Facebook</a></strong> page and <strong><a href="https://twitter.com/Tech_yaz">Twitter</a></strong> handle to get latest updates.</p>
<p>The post <a href="https://techyaz.com/sql-server/traces/to-find-out-no-of-traces-running-on-your-db-instance/">How to find Total no of Traces Running on your Database Instance?</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/traces/to-find-out-no-of-traces-running-on-your-db-instance/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Move Table to Another Filegroup?</title>
		<link>https://techyaz.com/sql-server/troubleshooting/how-to-move-a-table-to-a-different-filegroup/</link>
					<comments>https://techyaz.com/sql-server/troubleshooting/how-to-move-a-table-to-a-different-filegroup/#respond</comments>
		
		<dc:creator><![CDATA[Manvendra Deo Singh]]></dc:creator>
		<pubDate>Sun, 12 Sep 2010 06:21:00 +0000</pubDate>
				<category><![CDATA[Indexes]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server Administration]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Troubleshooting]]></category>
		<category><![CDATA[Clustered Index]]></category>
		<category><![CDATA[HowTO]]></category>
		<category><![CDATA[Index]]></category>
		<category><![CDATA[SQL Server Indexes]]></category>
		<guid isPermaLink="false">http://techyaz.com/big-data/how-to-move-a-table-to-a-different-filegroup/</guid>

					<description><![CDATA[<p>Suppose we had a database that has grown very large in size and located on the D drive that had a 100 GB Capacity. Now drive D is running out of space. We had other drives on the server where&#46;&#46;&#46;</p>
<p>The post <a href="https://techyaz.com/sql-server/troubleshooting/how-to-move-a-table-to-a-different-filegroup/">How to Move Table to Another Filegroup?</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Suppose we had a database that has grown very large in size and located on the D drive that had a 100 GB Capacity. Now drive D is running out of space. We had other drives on the server where there was available disk space, so our immediate solutions will be to move large tables from D drive to different drive where there is enough space to make some room for your database.  To move table to another filegroup, we need to perform below steps.</p>
<ol>
<li>Identify the Large tables that we will be moving to another filegroup placed on drive E.</li>
<li>Create new filegroup on disk E and if you have already a filegroup in this drive you can use that one.</li>
<li>Add a data file to this newly created filegroup.</li>
<li>Drop the clustered index from the table that has been identified for the movement and recreate it to the another filegroup.</li>
<li>Create cluster index on the same table.</li>
</ol>
<p><img loading="lazy" decoding="async" class="aligncenter size-full wp-image-1151" src="http://techyaz.com/wp-content/uploads/2010/09/Move-table-another-filegroup.jpg" alt="move table to another filegroup" width="719" height="719" srcset="https://techyaz.com/wp-content/uploads/2010/09/Move-table-another-filegroup.jpg 719w, https://techyaz.com/wp-content/uploads/2010/09/Move-table-another-filegroup-150x150.jpg 150w, https://techyaz.com/wp-content/uploads/2010/09/Move-table-another-filegroup-300x300.jpg 300w, https://techyaz.com/wp-content/uploads/2010/09/Move-table-another-filegroup-160x160.jpg 160w, https://techyaz.com/wp-content/uploads/2010/09/Move-table-another-filegroup-320x320.jpg 320w" sizes="auto, (max-width: 719px) 100vw, 719px" /></p>
<h5><span style="color: #333399;">Move a table to Another Filegroup</span></h5>
<p>We can move table from one filegroup to another filegroup through dropping and recreating the clustered indexes. Below T-SQL codes that will move your complete table from filegroup1 to filegroup2. Run below ALTER TABLE statement to move a table named TAB1 that has a cluster index named PK_TAB1 to another filegroup TEST_DATA_2.</p>
<pre><strong><span style="color: #0000ff;">ALTER TABLE TAB1 DROP CONSTRAINT PK_TAB1 WITH (MOVE TO TEST_DATA_2)
GO
ALTER TABLE TAB1 ADD CONSTRAINT PK_TAB1 PRIMARY KEY(TAB1_ID)
GO
</span></strong></pre>
<p>where TAB1 is table name PK_TAB1 is clustered index key and TEST_DATA_2 is target filegroup. It&#8217;s not necessary to create the another filegroup in to separate disk. This method will be useful if you want to move a table from one filegroup to another filegroup irrespective of the filegroup drives.</p>
<p>If your table doesn&#8217;t have a clustering index by any reason, you can still move table by creating a clustering index and then follow the same process to move it.</p>
<p><span style="color: #800000;"><strong>Related Articles:</strong></span></p>
<ul>
<li><a href="http://techyaz.com/sql-server/understanding-sql-server-indexes/" target="_blank" rel="noopener">Understanding SQL Server Indexes and difference between Clustered and Nonclustered Indexes</a></li>
<li><a href="http://techyaz.com/sql-server/difference-index-rebuild-reorganize/" target="_blank" rel="noopener">Rebuild Index vs Reorganize Index Operations</a></li>
<li><a href="http://techyaz.com/sql-server/what-should-be-the-best-value-for-fill-factor-in-sql-server/" target="_blank" rel="noopener">Understanding Fill Factor and its value</a></li>
<li><a href="http://techyaz.com/sql-server/how-to-avoid-page-split-in-sql-server/" target="_blank" rel="noopener">Relation between Page Split and Fill Factor</a></li>
</ul>
<p>I hope you like this article. Please follow our <a href="https://www.facebook.com/Techyaz/">Facebook</a> page and <a href="https://twitter.com/Tech_yaz">Twitter</a> handle to get latest updates.</p>
<p>The post <a href="https://techyaz.com/sql-server/troubleshooting/how-to-move-a-table-to-a-different-filegroup/">How to Move Table to Another Filegroup?</a> appeared first on <a href="https://techyaz.com">Techyaz.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://techyaz.com/sql-server/troubleshooting/how-to-move-a-table-to-a-different-filegroup/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
