<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3544169305649860217</id><updated>2024-09-01T08:51:40.649-05:00</updated><category term="T-SQL comparison between 2000 and 2005."/><title type='text'>SQLSENSE: SQL Server Practical Approaches</title><subtitle type='html'>SQLSENSE - SQL situations and their Solutions - Suresh Kumar Maganti</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://sqlfinerpoints.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://sqlfinerpoints.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Suresh Kumar Maganti</name><uri>http://www.blogger.com/profile/16645888326754808866</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyuLq44DuP85JRUqfEDgaxcuih4-meamQYzD9Jk-DD_Z1Ul5oh9WXYLo1JF5VzNReeNSgttGQ5dWJgLibF42zcSqeHW9aWQUeDrNfd_ucgf5BQ7975u63-9Bd7VfUwkQ/s220/Suresh.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>18</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3544169305649860217.post-4475076869197922623</id><published>2017-10-16T17:49:00.003-05:00</published><updated>2017-10-17T13:27:58.670-05:00</updated><title type='text'>User Permissions in all databases in a SQL Server</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;form action=&quot;https://www.paypal.com/cgi-bin/webscr&quot; method=&quot;post&quot;&gt;
&lt;input name=&quot;cmd&quot; type=&quot;hidden&quot; value=&quot;_s-xclick&quot; /&gt;
&lt;input name=&quot;encrypted&quot; type=&quot;hidden&quot; value=&quot;-----BEGIN PKCS7-----MIIHHgYJKoZIhvcNAQcEoIIHDzCCBwsCAQExggEwMIIBLAIBADCBlDCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtQYXlQYWwgSW5jLjETMBEGA1UECxQKbGl2ZV9jZXJ0czERMA8GA1UEAxQIbGl2ZV9hcGkxHDAaBgkqhkiG9w0BCQEWDXJlQHBheXBhbC5jb20CAQAwDQYJKoZIhvcNAQEBBQAEgYCWuWl5inwCKXGnbZAsyyY5UYroefMfHDRxVp93OnGq/2X7Va56eoSrV2KsN9ymOUDkfLrMsIC282vJfDwpTxP8HFzXjw9AVTAz4vXE1CLGB2eEa2hwzOKknfG0lWlS3CwCS400CXLy7QupwXqe5pdH+31qQH+pMxg2vEVtVq52dzELMAkGBSsOAwIaBQAwgZsGCSqGSIb3DQEHATAUBggqhkiG9w0DBwQIXGrUtzrcPuKAeNlN1ehYOUQHHt6Yt/TkwtyTW9rntEKpAXs+cW7qLJXxXtEjKQGYHELvfIXtIAqmB9ebUPSOoDN0V/AsQzrtp1hkiNvgBB+0tqCm9dUh4/lChS6+H92swKGTXLX5byq8PCUHHyFMqPvPsy3ETllLdPd61DdtYv6TI6CCA4cwggODMIIC7KADAgECAgEAMA0GCSqGSIb3DQEBBQUAMIGOMQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExFjAUBgNVBAcTDU1vdW50YWluIFZpZXcxFDASBgNVBAoTC1BheVBhbCBJbmMuMRMwEQYDVQQLFApsaXZlX2NlcnRzMREwDwYDVQQDFAhsaXZlX2FwaTEcMBoGCSqGSIb3DQEJARYNcmVAcGF5cGFsLmNvbTAeFw0wNDAyMTMxMDEzMTVaFw0zNTAyMTMxMDEzMTVaMIGOMQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExFjAUBgNVBAcTDU1vdW50YWluIFZpZXcxFDASBgNVBAoTC1BheVBhbCBJbmMuMRMwEQYDVQQLFApsaXZlX2NlcnRzMREwDwYDVQQDFAhsaXZlX2FwaTEcMBoGCSqGSIb3DQEJARYNcmVAcGF5cGFsLmNvbTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAwUdO3fxEzEtcnI7ZKZL412XvZPugoni7i7D7prCe0AtaHTc97CYgm7NsAtJyxNLixmhLV8pyIEaiHXWAh8fPKW+R017+EmXrr9EaquPmsVvTywAAE1PMNOKqo2kl4Gxiz9zZqIajOm1fZGWcGS0f5JQ2kBqNbvbg2/Za+GJ/qwUCAwEAAaOB7jCB6zAdBgNVHQ4EFgQUlp98u8ZvF71ZP1LXChvsENZklGswgbsGA1UdIwSBszCBsIAUlp98u8ZvF71ZP1LXChvsENZklGuhgZSkgZEwgY4xCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJDQTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzEUMBIGA1UEChMLUGF5UGFsIEluYy4xEzARBgNVBAsUCmxpdmVfY2VydHMxETAPBgNVBAMUCGxpdmVfYXBpMRwwGgYJKoZIhvcNAQkBFg1yZUBwYXlwYWwuY29tggEAMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADgYEAgV86VpqAWuXvX6Oro4qJ1tYVIT5DgWpE692Ag422H7yRIr/9j/iKG4Thia/Oflx4TdL+IFJBAyPK9v6zZNZtBgPBynXb048hsP16l2vi0k5Q2JKiPDsEfBhGI+HnxLXEaUWAcVfCsQFvd2A1sxRr67ip5y2wwBelUecP3AjJ+YcxggGaMIIBlgIBATCBlDCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtQYXlQYWwgSW5jLjETMBEGA1UECxQKbGl2ZV9jZXJ0czERMA8GA1UEAxQIbGl2ZV9hcGkxHDAaBgkqhkiG9w0BCQEWDXJlQHBheXBhbC5jb20CAQAwCQYFKw4DAhoFAKBdMBgGCSqGSIb3DQEJAzELBgkqhkiG9w0BBwEwHAYJKoZIhvcNAQkFMQ8XDTEzMDMxNTE3MDY0MVowIwYJKoZIhvcNAQkEMRYEFBnL3wyWz1Dz8WjLqQl/MmDMb9uLMA0GCSqGSIb3DQEBAQUABIGAW8P+uJ0gjV3QIv4TfQrSRS/pkVoKaDlJXzbRESiRbksrIBvTO62AvMApCYj9UgmVfxz/NuBuw+dtNxRQd2xJkoO2wiuOa7LMb1QngU6w9x1qPmySt8lvQEZOXiEB4sDN0hDvKrON7cSSLocIqm6yyw/lN57dE8WpHKxuFqAKLUM=-----END PKCS7-----
&quot; /&gt;

&lt;img alt=&quot;&quot; border=&quot;0&quot; height=&quot;1&quot; src=&quot;https://www.paypalobjects.com/en_US/i/scr/pixel.gif&quot; width=&quot;1&quot; /&gt;
Audit is a regular activity for databases. Sometime internal, sometimes external, sometimes bi-yearly, sometimes yearly, sometimes for preparation for external audit, sometimes corrections to existing audits and so on. Doing it manually is quite time-consuming and ofcourse, boring due to repetitive nature of activities. I prefer to automate generation of portions of audit information as much as possible. For database audits, mostly it is about logins, what access they have, permissions by groups, etc. So I thought of creating a table with columns that would contain the data/information that auditors generally seek and a simple SP that would populate this table. The SP would traverse user databases to get the information. Usually DBA&#39;s have a database specific to high end DBA activities like Service broker, job progression, etc. Let us call it my DBADB for this article&#39;s sake. This kind of table can reside in such a database where interference is minimal. So let us start with the table, &lt;span style=&quot;font-family: &amp;quot;consolas&amp;quot;; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;consolas&amp;quot;; font-size: x-small;&quot;&gt;dbo&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-family: &amp;quot;consolas&amp;quot;; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: grey; font-family: &amp;quot;consolas&amp;quot;; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: grey; font-family: &amp;quot;consolas&amp;quot;; font-size: x-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;consolas&amp;quot;; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;consolas&amp;quot;; font-size: x-small;&quot;&gt;DB_Permissions_By_Group&amp;nbsp;&lt;/span&gt;&lt;/span&gt;some column names of which are self explanatory:&lt;br /&gt;
&lt;br /&gt;
USE [DBADB]&lt;br /&gt;
GO&lt;br /&gt;
SET ANSI_NULLS ON&lt;br /&gt;
GO&lt;br /&gt;
SET QUOTED_IDENTIFIER ON&lt;br /&gt;
GO&lt;br /&gt;
SET ANSI_PADDING&amp;nbsp;OFF;&lt;br /&gt;
go&lt;br /&gt;
CREATE TABLE [dbo].[DB_Permissions_By_Group](&lt;br /&gt;
&amp;nbsp;[DatabaseName] [varchar](32) NOT NULL,&lt;br /&gt;
&amp;nbsp;[Class_Desc] [varchar](128) NULL,&lt;br /&gt;
&amp;nbsp;[ObjectName] [varchar](64) NULL,&lt;br /&gt;
&amp;nbsp;[Minor_Id] [smallint] NULL,&lt;br /&gt;
&amp;nbsp;[Type] [varchar](24) NULL,&lt;br /&gt;
&amp;nbsp;[Permission_Name] [varchar](24) NULL,&lt;br /&gt;
&amp;nbsp;[State_Desc] [varchar](16) NULL,&lt;br /&gt;
&amp;nbsp;[GroupName] [varchar](64) NULL&lt;br /&gt;
) ON [PRIMARY]&lt;br /&gt;
with (data_compression = page)&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
The other columns&amp;nbsp;that need some description are following:&lt;br /&gt;
[Class_Desc] is type of objects which can be a database, schema, table, SP, etc. &lt;br /&gt;
[Type] is type of permission. For e.g. Execute, Select, Update, View, Showplan, etc.&lt;br /&gt;
[GroupName] is a Windows AD group that has some permission in some application database on this server. There can be one or more individual logins or groups within a given Windows group. And these members will have the same permission as the parent Windows group that they belong to.&lt;br /&gt;
&lt;br /&gt;
Now to the SP that would populate this table, [dbo].[DB_Permissions_By_Group]. The SP is called&amp;nbsp; &lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;consolas&amp;quot;; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;consolas&amp;quot;; font-size: x-small;&quot;&gt;[dbo]&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-family: &amp;quot;consolas&amp;quot;; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: grey; font-family: &amp;quot;consolas&amp;quot;; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: grey; font-family: &amp;quot;consolas&amp;quot;; font-size: x-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;consolas&amp;quot;; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;consolas&amp;quot;; font-size: x-small;&quot;&gt;[dbp_Show_Permissions]&lt;/span&gt;&lt;/span&gt;. This SP scans all the non-system databases and fetches the permission on a given object for each user that exists in that database. Here is the code of this SP:&lt;br /&gt;
&lt;br /&gt;
USE [DBADB]&lt;br /&gt;
GO&lt;br /&gt;
SET ANSI_NULLS ON&lt;br /&gt;
GO&lt;br /&gt;
SET QUOTED_IDENTIFIER ON&lt;br /&gt;
GO&lt;br /&gt;
create or alter procedure [dbo].[dbp_Show_Permissions]&lt;br /&gt;
as&lt;br /&gt;
set nocount on&lt;br /&gt;
declare @l_exec_String varchar(8000)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,@l_db_name&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; varchar(32)&lt;br /&gt;
truncate table SBMDB.dbo.DB_Permissions_By_Group&lt;br /&gt;
declare DB_Cursor scroll cursor &lt;br /&gt;
for&lt;br /&gt;
select name from sys.databases&lt;br /&gt;
where name not in (&#39;master&#39;, &#39;model&#39;, &#39;msdb&#39;, &#39;tempdb&#39;, &#39;SSISDB&#39;)&lt;br /&gt;
open DB_Cursor&lt;br /&gt;
fetch next from DB_Cursor into &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@l_db_name&lt;br /&gt;
while (@@FETCH_STATUS = 0)&lt;br /&gt;
begin&lt;br /&gt;
&amp;nbsp;select @l_exec_String = &#39;Use &#39; + @l_db_name + &#39;; select db_name() DatabaseName, a.Class_Desc, case when a.class_desc = &#39; + &#39;&#39;&#39;&#39; + &#39;Schema&#39; + &#39;&#39;&#39;&#39; &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + &#39; then&amp;nbsp; schema_name(a.major_id) else object_name(a.major_id) end ObjectName &#39;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + &#39;,a.minor_id, a.[Type], a.[permission_name], a.state_desc ,b.name from sys.database_permissions a &#39;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + &#39;inner join sys.database_Principals b &#39;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + &#39;on a.grantee_principal_id = b.principal_id &#39;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; + &#39;where b.name like &#39; + &#39;&#39;&#39;%MyDomain\%&#39;&#39;&#39;&lt;br /&gt;
&amp;nbsp;insert into [DBADB].dbo.DB_Permissions_By_Group (DatabaseName, Class_Desc, ObjectName, Minor_Id, [Type], [Permission_Name], State_Desc, GroupName)&lt;br /&gt;
&amp;nbsp;exec (@l_exec_String)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; fetch next from DB_Cursor into &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @l_db_name&lt;br /&gt;
end&lt;br /&gt;
close DB_Cursor&lt;br /&gt;
deallocate DB_Cursor&lt;br /&gt;
set nocount off;&lt;br /&gt;
GO&lt;br /&gt;
&lt;strong&gt;&lt;u&gt;Explanation:&lt;/u&gt;&lt;/strong&gt; Basically, it joins the DMV&#39;s sys.database_permissions (for permissions) and sys.database_principals (for users) and matches them based on the principal_id which is the ID of the database user for a given login. It does it for each user database and puts the result in our table,&amp;nbsp;&amp;nbsp;[DBADB].dbo.DB_Permissions_By_Group. &lt;br /&gt;
&lt;br /&gt;
Since it sifts through only the DMV&#39;s in each user database, performance is not a concern. It takes less than a few seconds to go through a few dozen databases. Once this SP gets executed, the user needs to simply extract the contents of&amp;nbsp; the table, [DBADB].dbo.DB_Permissions_By_Group into an Excel file and send it to auditors. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;u&gt;Execution:&lt;/u&gt;&lt;/strong&gt; execute [DBADB].[dbo].[dbp_Show_Permissions]&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;u&gt;Results:&lt;/u&gt;&lt;/strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Select * from [DBADB].dbo.DB_Permissions_By_Group with (nolock)&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&lt;/form&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://sqlfinerpoints.blogspot.com/feeds/4475076869197922623/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3544169305649860217/4475076869197922623' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/4475076869197922623'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/4475076869197922623'/><link rel='alternate' type='text/html' href='http://sqlfinerpoints.blogspot.com/2017/10/user-permissions-in-all-databases-in.html' title='User Permissions in all databases in a SQL Server'/><author><name>Suresh Kumar Maganti</name><uri>http://www.blogger.com/profile/16645888326754808866</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyuLq44DuP85JRUqfEDgaxcuih4-meamQYzD9Jk-DD_Z1Ul5oh9WXYLo1JF5VzNReeNSgttGQ5dWJgLibF42zcSqeHW9aWQUeDrNfd_ucgf5BQ7975u63-9Bd7VfUwkQ/s220/Suresh.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3544169305649860217.post-3278970141961652807</id><published>2017-06-20T17:08:00.002-05:00</published><updated>2017-06-20T17:26:04.435-05:00</updated><title type='text'>Finding Permission for user tables by Login</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;form action=&quot;https://www.paypal.com/cgi-bin/webscr&quot; method=&quot;post&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Sometimes, we need to find permissions of a SQL Server user or a Windows login on database tables. Doing so using the Object Explorer &amp;gt; Security &amp;gt; Logins or Object Explorer &amp;gt; [Database] &amp;gt; Security &amp;gt; Users in SSMS is quite tedious and hard to document. One simple way could be the use of the T-SQL system function, HAS_PERMS_BY_NAME. Let us say, I want to find out the permissions that a login called ReportLogin used in the data sources of all SSRS/Sharepoint reports has on the application tables &amp;nbsp;in a database called MyDB. This is what I do:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Use MyDB&lt;br /&gt;go&lt;br /&gt;execute as login = &#39;ReportLogin&#39;&lt;br /&gt;go&lt;br /&gt;select SUSER_sNAME()&lt;br /&gt;go&lt;br /&gt;SELECT HAS_PERMS_BY_NAME(db_name(), &#39;DATABASE&#39;, &#39;ANY&#39;);&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;SELECT DB_NAme() DatabaseName&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,HAS_PERMS_BY_NAME (QUOTENAME(SCHEMA_NAME(schema_id)) + &#39;.&#39; + QUOTENAME(name), &#39;OBJECT&#39;, &#39;SELECT&#39;) AS have_SELECT &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,HAS_PERMS_BY_NAME (QUOTENAME(SCHEMA_NAME(schema_id)) + &#39;.&#39; + QUOTENAME(name), &#39;OBJECT&#39;, &#39;INSERT&#39;) AS have_INSERT&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,HAS_PERMS_BY_NAME (QUOTENAME(SCHEMA_NAME(schema_id)) + &#39;.&#39; + QUOTENAME(name), &#39;OBJECT&#39;, &#39;UPDATE&#39;) AS have_UPDATE &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,HAS_PERMS_BY_NAME (QUOTENAME(SCHEMA_NAME(schema_id)) + &#39;.&#39; + QUOTENAME(name), &#39;OBJECT&#39;, &#39;DELETE&#39;) AS have_DELETE &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,QUOTENAME(SCHEMA_NAME(schema_id)) + &#39;.&#39; + QUOTENAME(name)&amp;nbsp; TABLE_NAME&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,* &lt;br /&gt;&amp;nbsp;&amp;nbsp; FROM sys.objects where [type] in (&#39;U&#39;)&lt;br /&gt;go&lt;br /&gt;revert&lt;br /&gt;go&lt;br /&gt;select SUSER_sNAME()&lt;br /&gt;go&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&amp;nbsp;&lt;/div&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;That&amp;nbsp;does the following: &lt;/span&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Executes the code as the SQL login called ReportLogin, &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Checks if that login has any access to the database MyDB,&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Gives the result as to the type&amp;nbsp;of access (select, insert, update, delete) in the first four columns&amp;nbsp;for each user table and &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;consolas&amp;quot;; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: small;&quot;&gt;Then reverts back to my login.&lt;/span&gt; &lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;span style=&quot;font-family: &amp;quot;consolas&amp;quot;; font-size: x-small;&quot;&gt;&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;If it is a windows login whose access needs to be verified, simply substitute it as &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Execute as Login = &#39;Domain\LoginName&#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;The rest of the code remains the same. Please note that this code doesn&#39;t work for Windows groups. That is because &#39;Execute As Login&#39; doesn&#39;t work for Windows Groups.&amp;nbsp;The next article&amp;nbsp;would address this goal.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Possibilities:&lt;/span&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;The code above can be converted to an SP.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;The SP can loop through differ logins in the syslogins catalog view. &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;For each login, it can insert data into a table.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;Adding exec sp_MSforEachDB to execute that SP would execute it on all databases if DB_NAME() is added in the select list.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;I would leave that portion to the reader.&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&amp;nbsp;&lt;/form&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://sqlfinerpoints.blogspot.com/feeds/3278970141961652807/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3544169305649860217/3278970141961652807' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/3278970141961652807'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/3278970141961652807'/><link rel='alternate' type='text/html' href='http://sqlfinerpoints.blogspot.com/2017/06/finding-permission-for-user-tables-by.html' title='Finding Permission for user tables by Login'/><author><name>Suresh Kumar Maganti</name><uri>http://www.blogger.com/profile/16645888326754808866</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyuLq44DuP85JRUqfEDgaxcuih4-meamQYzD9Jk-DD_Z1Ul5oh9WXYLo1JF5VzNReeNSgttGQ5dWJgLibF42zcSqeHW9aWQUeDrNfd_ucgf5BQ7975u63-9Bd7VfUwkQ/s220/Suresh.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3544169305649860217.post-1760277714086417922</id><published>2017-05-30T17:01:00.000-05:00</published><updated>2017-05-30T17:37:08.898-05:00</updated><title type='text'>Find occurrence of a specific day in a month</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;form action=&quot;https://www.paypal.com/cgi-bin/webscr&quot; method=&quot;post&quot; style=&quot;text-align: left;&quot;&gt;
&lt;input name=&quot;cmd&quot; type=&quot;hidden&quot; value=&quot;_s-xclick&quot; /&gt;
&lt;input name=&quot;encrypted&quot; type=&quot;hidden&quot; value=&quot;-----BEGIN PKCS7-----MIIHHgYJKoZIhvcNAQcEoIIHDzCCBwsCAQExggEwMIIBLAIBADCBlDCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtQYXlQYWwgSW5jLjETMBEGA1UECxQKbGl2ZV9jZXJ0czERMA8GA1UEAxQIbGl2ZV9hcGkxHDAaBgkqhkiG9w0BCQEWDXJlQHBheXBhbC5jb20CAQAwDQYJKoZIhvcNAQEBBQAEgYCWuWl5inwCKXGnbZAsyyY5UYroefMfHDRxVp93OnGq/2X7Va56eoSrV2KsN9ymOUDkfLrMsIC282vJfDwpTxP8HFzXjw9AVTAz4vXE1CLGB2eEa2hwzOKknfG0lWlS3CwCS400CXLy7QupwXqe5pdH+31qQH+pMxg2vEVtVq52dzELMAkGBSsOAwIaBQAwgZsGCSqGSIb3DQEHATAUBggqhkiG9w0DBwQIXGrUtzrcPuKAeNlN1ehYOUQHHt6Yt/TkwtyTW9rntEKpAXs+cW7qLJXxXtEjKQGYHELvfIXtIAqmB9ebUPSOoDN0V/AsQzrtp1hkiNvgBB+0tqCm9dUh4/lChS6+H92swKGTXLX5byq8PCUHHyFMqPvPsy3ETllLdPd61DdtYv6TI6CCA4cwggODMIIC7KADAgECAgEAMA0GCSqGSIb3DQEBBQUAMIGOMQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExFjAUBgNVBAcTDU1vdW50YWluIFZpZXcxFDASBgNVBAoTC1BheVBhbCBJbmMuMRMwEQYDVQQLFApsaXZlX2NlcnRzMREwDwYDVQQDFAhsaXZlX2FwaTEcMBoGCSqGSIb3DQEJARYNcmVAcGF5cGFsLmNvbTAeFw0wNDAyMTMxMDEzMTVaFw0zNTAyMTMxMDEzMTVaMIGOMQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExFjAUBgNVBAcTDU1vdW50YWluIFZpZXcxFDASBgNVBAoTC1BheVBhbCBJbmMuMRMwEQYDVQQLFApsaXZlX2NlcnRzMREwDwYDVQQDFAhsaXZlX2FwaTEcMBoGCSqGSIb3DQEJARYNcmVAcGF5cGFsLmNvbTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAwUdO3fxEzEtcnI7ZKZL412XvZPugoni7i7D7prCe0AtaHTc97CYgm7NsAtJyxNLixmhLV8pyIEaiHXWAh8fPKW+R017+EmXrr9EaquPmsVvTywAAE1PMNOKqo2kl4Gxiz9zZqIajOm1fZGWcGS0f5JQ2kBqNbvbg2/Za+GJ/qwUCAwEAAaOB7jCB6zAdBgNVHQ4EFgQUlp98u8ZvF71ZP1LXChvsENZklGswgbsGA1UdIwSBszCBsIAUlp98u8ZvF71ZP1LXChvsENZklGuhgZSkgZEwgY4xCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJDQTEWMBQGA1UEBxMNTW91bnRhaW4gVmlldzEUMBIGA1UEChMLUGF5UGFsIEluYy4xEzARBgNVBAsUCmxpdmVfY2VydHMxETAPBgNVBAMUCGxpdmVfYXBpMRwwGgYJKoZIhvcNAQkBFg1yZUBwYXlwYWwuY29tggEAMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADgYEAgV86VpqAWuXvX6Oro4qJ1tYVIT5DgWpE692Ag422H7yRIr/9j/iKG4Thia/Oflx4TdL+IFJBAyPK9v6zZNZtBgPBynXb048hsP16l2vi0k5Q2JKiPDsEfBhGI+HnxLXEaUWAcVfCsQFvd2A1sxRr67ip5y2wwBelUecP3AjJ+YcxggGaMIIBlgIBATCBlDCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtQYXlQYWwgSW5jLjETMBEGA1UECxQKbGl2ZV9jZXJ0czERMA8GA1UEAxQIbGl2ZV9hcGkxHDAaBgkqhkiG9w0BCQEWDXJlQHBheXBhbC5jb20CAQAwCQYFKw4DAhoFAKBdMBgGCSqGSIb3DQEJAzELBgkqhkiG9w0BBwEwHAYJKoZIhvcNAQkFMQ8XDTEzMDMxNTE3MDY0MVowIwYJKoZIhvcNAQkEMRYEFBnL3wyWz1Dz8WjLqQl/MmDMb9uLMA0GCSqGSIb3DQEBAQUABIGAW8P+uJ0gjV3QIv4TfQrSRS/pkVoKaDlJXzbRESiRbksrIBvTO62AvMApCYj9UgmVfxz/NuBuw+dtNxRQd2xJkoO2wiuOa7LMb1QngU6w9x1qPmySt8lvQEZOXiEB4sDN0hDvKrON7cSSLocIqm6yyw/lN57dE8WpHKxuFqAKLUM=-----END PKCS7-----
&quot; /&gt;

&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif;&quot;&gt;I am resuming blogging. Sorry for being away for so long. Many a time, we encounter situations where something is to be executed on a 2nd Saturday, a 4th Tuesday of a month or likewise. It would be so handy to have something that easily verifies the occurrence of a specific weekday in a given month and then executes other TSQL code&amp;nbsp;after such an occurrence has been verified. A reusable code that returns a true or false is much easier than actually going through the dates and counting the occurrence of a specific day. So, I embark on creating a scalar function for this purpose (In this function,&amp;nbsp;I have used CTE, Recursive CTE, IIF, EOMonth):&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
USE [DB_NAME]&lt;br /&gt;go&lt;br /&gt;SET ANSI_NULLS ON&lt;br /&gt;go&lt;br /&gt;SET QUOTED_IDENTIFIER ON&lt;br /&gt;go&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
create function [dbo].[dbf_Is_day_Nth_occurrence]&lt;br /&gt;
(@i_Day varchar(10)&lt;br /&gt;,@i_Nth_Occurrence tinyint)&lt;br /&gt;
&lt;br /&gt;
Returns bit&lt;br /&gt;as&lt;br /&gt;/************************************************************************************************************************************************************&lt;br /&gt;
&amp;nbsp;Date&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Altered by&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Description&lt;br /&gt;
__________ ___________________ ___________________________________________________________________________________________________________________________&lt;br /&gt;
04/15/2017 Suresh K. Maganti&amp;nbsp;&amp;nbsp; Created. True or False. is it the 2nd Tuesday of this Month?&lt;br /&gt;
************************************************************************************************************************************************************/&lt;br /&gt;begin&lt;br /&gt;&amp;nbsp;&amp;nbsp; declare @o_Return bit&lt;br /&gt;
&lt;br /&gt;
;with CTE_Base (First_of_Month, Last_of_Month)&lt;br /&gt;as &lt;br /&gt;&amp;nbsp; (select convert(datetime, convert(varchar(4), year(getdate())) + right(&#39;0&#39; + convert(varchar(2), month (getdate())), 2) + &#39;01&#39;), convert(datetime, EOMonth(getdate())) )&lt;br /&gt;--select * from CTE_Base&lt;br /&gt;
&lt;br /&gt;,CTE_Recursive (The_Date, The_Day, Occurrence_in_Month, Last_of_Month)&lt;br /&gt;as &lt;br /&gt;&amp;nbsp; (select First_of_Month, datename(WEEKDAY, First_of_Month), 1, Last_of_Month from CTE_Base&lt;br /&gt;&amp;nbsp;&amp;nbsp; union all&lt;br /&gt;&amp;nbsp;&amp;nbsp; select The_Date + 1, datename(WEEKDAY, The_Date + 1), Occurrence_in_Month + 1, Last_of_Month from CTE_Recursive&lt;br /&gt;&amp;nbsp;&amp;nbsp; where The_Date &amp;lt; Last_of_Month) &lt;br /&gt;
&lt;br /&gt;,CTE_Final (The_Date, The_Day, Nth_Occurrence_of_Day)&lt;br /&gt;as &lt;br /&gt;&amp;nbsp; (select The_Date, The_Day, Row_Number() over(partition by The_Day order by Occurrence_In_Month) &lt;br /&gt;&amp;nbsp;&amp;nbsp; from CTE_Recursive)&lt;br /&gt;
&lt;br /&gt;&amp;nbsp; select @o_Return = iif ( (The_Day = @i_Day) and (Nth_Occurrence_of_Day = @i_Nth_Occurrence), 1, 0) &lt;br /&gt;&amp;nbsp; from CTE_Final&lt;br /&gt;&amp;nbsp; where convert(varchar, The_Date, 112) = convert(varchar, getdate(), 112)&lt;br /&gt;
&lt;br /&gt;&amp;nbsp; return @o_Return&lt;br /&gt;
end&lt;br /&gt;go&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;Today, 05/30/2017 is 5th Tuesday of May 2017. Now, let me check using the function if today is the 1st Sunday of this month: &lt;br /&gt;
&lt;br /&gt;select [dbo].[dbf_Is_day_Nth_occurrence] (&#39;Sunday&#39;, 1)&lt;br /&gt;I get a 0. The answer is No. &lt;br /&gt;
&lt;br /&gt;
Let me try to see if it is the 5th Tuesday of this month: &lt;br /&gt;
&lt;br /&gt;select [dbo].[dbf_Is_day_Nth_occurrence] (&#39;Tuesday&#39;, 5)&lt;br /&gt;I get a 1. The answer is Yes. &lt;br /&gt;
&lt;br /&gt;
So, I can do the next set of things that I can do for today as follows: &lt;br /&gt;
&lt;br /&gt;if [dbo].[dbf_Is_day_Nth_occurrence] (&#39;Tuesday&#39;, 5) = 1&lt;br /&gt;begin&lt;br /&gt;&amp;nbsp;&amp;nbsp; Print &#39;The next article would be a bigger one.&#39;&lt;br /&gt;end&lt;br /&gt;go&lt;br /&gt;
&amp;nbsp;&lt;/form&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://sqlfinerpoints.blogspot.com/feeds/1760277714086417922/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3544169305649860217/1760277714086417922' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/1760277714086417922'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/1760277714086417922'/><link rel='alternate' type='text/html' href='http://sqlfinerpoints.blogspot.com/2017/05/find-occurrence-of-specific-day-in-month.html' title='Find occurrence of a specific day in a month'/><author><name>Suresh Kumar Maganti</name><uri>http://www.blogger.com/profile/16645888326754808866</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyuLq44DuP85JRUqfEDgaxcuih4-meamQYzD9Jk-DD_Z1Ul5oh9WXYLo1JF5VzNReeNSgttGQ5dWJgLibF42zcSqeHW9aWQUeDrNfd_ucgf5BQ7975u63-9Bd7VfUwkQ/s220/Suresh.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3544169305649860217.post-5870063741470650273</id><published>2013-03-14T18:09:00.003-05:00</published><updated>2013-03-14T18:20:09.190-05:00</updated><title type='text'>Index Keys &amp; Properties as a step in Database Tuning</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;background-color: #cccccc; color: black;&quot;&gt;&lt;u&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;u&gt;&lt;span style=&quot;background-color: #cccccc; color: black;&quot;&gt;&lt;/span&gt;&lt;/u&gt;&lt;br /&gt;
&lt;u&gt;&lt;span style=&quot;background-color: #cccccc; color: black;&quot;&gt;&lt;/span&gt;&lt;/u&gt;&lt;br /&gt;
As DBA&#39;s, we encounter long running queries that end-users compalin about. Indexing strategy is one area of database where developers usually tend to create indexes on the fly without much consideration for or analysis on how an index that helps one query marginally can slow down a bunch of other queries or stored procedures drastically. Some (not &amp;nbsp;all) of the situations that we&amp;nbsp;usually come across&amp;nbsp;are following:&lt;br /&gt;

&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Dbo.Orders table is being joined to dbo.Order_Details table but both of them exist on the same filegroup and on the same set of underlying discs. This would cause disc queuing and&amp;nbsp;the consequent slowness.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Dbo.Orders table is being joined to dbo.Order_Details table on the common column Product_ID but Product_ID is the leftmost colmn of a non-clustered index on dbo.Orders but is the third column in another non-clustered index on dbo.Order_Details. So these indexes don&#39;t help this join much.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Updates are taking place on a column that has a non-clustered index that is compressed. It would be good not to have an index at all on a column being updated. In case such an index is really required, not compressing it would be beneficial. Otherwise the update&amp;nbsp;is much slower than when the index is uncompressed.&lt;/li&gt;
&lt;li&gt;A majority of values in a particular&amp;nbsp;column used in the WHERE clause of a query have a value but the query is designed to fetch only such rows where the value is null. May be a filtered-index would have helped speed it up.&amp;nbsp;&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
In such situations&amp;nbsp;and some others,&amp;nbsp;we would like to have a quick gance at atleast the following to begin with:&lt;br /&gt;

&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;Whether the two tables being joined are physically resident on different filegroups.&lt;/li&gt;
&lt;li&gt;The columns being joined are the leftmost columns&amp;nbsp;in their respective indexes.&lt;/li&gt;
&lt;li&gt;The column being updated is not part of an index (first of all) that is compressed in any way (definitely not).&lt;/li&gt;
&lt;li&gt;In case, a query is looking for a small subset of values from a column, is the column a part of an index that has the appropriate filter defined on the index.&lt;/li&gt;
&lt;li&gt;Etc.&lt;/li&gt;
&lt;/ul&gt;
&amp;nbsp;Instead of trying to procure such relevant information painstakingly one at a time, an easier way would be to have a stored procedure return the same. This article is aimed at creating such a stored procedure that could come in handy in performance situations described above.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: #cccccc; color: black;&quot;&gt;&lt;strong&gt;&lt;u&gt;Details of the Stored procedure&lt;/u&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;The text of the store procedure is given below:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;If&lt;/span&gt; exists&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;select&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 1 &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;from&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;sys&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;procedures&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;where&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;USER_NAME&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;schema_id&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;dbo&#39;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;   name &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;dbp_Find_Index_Properties_by_Column&#39;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;   &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;type&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;P&#39;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;begin&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; drop&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;procedure&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; dbo&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;dbp_Find_Index_Properties_by_Column&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;go&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;             &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;create procedure&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; dbo&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;dbp_Find_Index_Properties_by_Column &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&amp;nbsp; @i_Table_Name&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;varchar&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;64&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;null&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;
,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;@i_Column_Name  &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;varchar&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;24&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;null&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;
,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;@l_Column_Position_in_Index &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;tinyint&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;null&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;as&lt;br /&gt;

&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;*******************************************************************************************************************************&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;
Created by: Suresh K Maganti&lt;br /&gt;

Purpose  &amp;nbsp;&amp;nbsp; : Find if a column is used in an index. View additional properties of the index.&lt;br /&gt;

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; For now, I have included only clustered and non-clustered indexes.&lt;br /&gt;

Input           &lt;br /&gt;

Parameters: If provided, the result set is appropriately filtered. Else, the complete result set is returned.&lt;br /&gt;

********************************************************************************************************************************/&lt;br /&gt;

&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;set&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;nocount&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;on&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;

&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;with&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;CTE_Base&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;Table_Name&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Index_Name&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Leftmost_Column_in_Index&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Position_in_Index&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Columns_in_the_Index&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Index_Type&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Is_Primary_Key&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;, &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;Is_The_Index_Unique&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Fill_Factor_When_Created&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; File_Group_Name&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Is_It_A_Filtered_Index&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Filter_Definition&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Is_An_Included_Column&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;, &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;Is_Compressed&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Type_Of_Compression_Used&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;as &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;select&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; C&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;Name Table_Name&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;name Index_Name &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;col_name&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;[object_id]&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;select&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; e&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;column_id &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;from&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;sys&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;index_columns&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; e &lt;/span&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;where&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; e&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;index_column_id &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;and&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;[object_id] &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;[object_id]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;index_id  &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;index_id&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Leftmost_Column&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;case&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;when&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; @i_Column_Name &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;is&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;null&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;then&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;null&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;key_ordinal &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;                                       &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Position_in_Index&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;replace&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;((&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;select&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;col_name&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;[object_id]&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; f&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;column_id&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;+&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39; ###&#39;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;data()&#39;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;from&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;sys&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;index_columns&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; f&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;where&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; f&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;[object_id] &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;[object_id]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;and&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp; f&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;index_id    &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;index_id&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;order&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;by&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; f&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;key_ordinal &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;asc&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;for&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;XML&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;Path &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;)),&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;###&#39;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;,&#39;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;             &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Columns_in_the_Index&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;case&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;Type&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;when&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 0 &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;then&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;Heap&#39;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; when&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 1 &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;then&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;Clustered&#39;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; when&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 2 &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;then&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;Nonclustered&#39;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;when&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 3 &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;then&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;XML&#39;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; when&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 4 &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;then&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;Spatial&#39;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;                                &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Index_Type&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;case&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;is_primary_key&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;when&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 1 &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;then&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;Yes&#39;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;No&#39;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;                                 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Is_Primary_Key&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;case&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; b&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;is_unique &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;when&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 1 &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;then&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;Yes&#39; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;No&#39;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;                                 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Is_The_Index_Unique  &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;      &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;fill_factor                       &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Fill_Factor_When_Created&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;FILEGROUP_NAME&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;data_space_id&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;    &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; File_Group_Name&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;case&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;has_filter&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; when&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 1 &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;then&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;Yes&#39;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;No&#39;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;                                 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Is_It_A_Filtered_Index   &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;filter_definition &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;case&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;is_included_column&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;when&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 1 &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;then&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;Yes&#39;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;No&#39;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;                                 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Is_An_Included_Column&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;      &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;case&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; when&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; d&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;data_compression&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 0 &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;then&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;Yes&#39;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;No&#39;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;end&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;                                 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Is_Compressed  &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;d&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;data_compression_desc              Type_Of_Compression_Used&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;

&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;from&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;sys&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;tables&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; c &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;left join&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;sys&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;index_columns&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; a&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;on c&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;[object_id] &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;[object_id]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;inner join&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;sys&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;indexes&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; b&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;on&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;a&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;[object_id] &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;[object_id]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;and&amp;nbsp; a&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;index_id &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;index_id&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;inner &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;join&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;sys&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;partitions&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; d&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;on&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; b&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;[object_id] &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; d&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;[object_id]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;and&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;index_id  &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; d&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;index_id&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;where b&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;[type] &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;in&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;and a&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;key_ordinal &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;isnull&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;@l_Column_Position_in_Index&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;key_ordinal&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;and &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;OBJECTPROPERTY&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;[object_id]&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;IsSystemTable&#39;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 0&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;and &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;OBJECTPROPERTY&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;[object_id]&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;IsTable&#39;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;and &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;col_name&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;[object_id]&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;column_id&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;ISNULL&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;@i_Column_Name&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;col_name&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;b&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;[object_id]&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;column_id&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;and &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;c&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;name &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;ISNULL&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;@i_Table_Name&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; c&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;

&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;select distinct&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Table_Name&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Index_Name&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Leftmost_Column_in_Index&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Position_in_Index&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;substring&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;Columns_in_the_Index&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: magenta; font-size: x-small;&quot;&gt;LEN&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;Columns_in_the_Index&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;-&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Columns_in_the_Index&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;Index_Type&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Is_The_Index_Unique&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Fill_Factor_When_Created&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; File_Group_Name&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Is_It_A_Filtered_Index&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Filter_Definition&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;, &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;Is_An_Included_Column&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Is_Compressed&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Type_Of_Compression_Used&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;from CTE_Base      &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;order by&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Table_Name&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;

&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;set &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;nocount&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;off&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;go&lt;br /&gt;
&lt;br /&gt;
For now, I have included only clustered and non-clustered index including filtered indexes.&lt;br /&gt;
This stored procedure basically uses a few catalogue views:&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;color: green;&quot;&gt;sys&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;partitions&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: green;&quot;&gt;sys&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;tables&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: green;&quot;&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;sys&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;index_columns&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: green;&quot;&gt;sys&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: green; font-size: x-small;&quot;&gt;indexes&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;span style=&quot;color: green;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;and a few system functions like:&lt;/span&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;color: magenta;&quot;&gt;OBJECTPROPERTY&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: magenta;&quot;&gt;FILEGROUP_NAME&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;color: magenta;&quot;&gt;COL_NAME&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;span style=&quot;color: blue;&quot;&gt;and a new construct FOR XML PATH that was introduced in Microsoft SQL Server 2008. XML PATH is being used in this stored procedure to generate a comma separated list of columns&amp;nbsp;that are part of&amp;nbsp;a given index ordered by their order of occurrence within the index. You may notice an interesting fact here. I am using ORDER BY inside the subquery. Normally ORDER BY is the last part of any SELECT statement. But when used with XML PATH, it comes before the FOR XML PATH construct. Another thing you may notice is that when ORDER BY is used inside a subquery, usage of TOP 100 PERCENT after the keyword SELECT, is mandatory in most cases. Not so in this case.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;The rest are all basic joins.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Now we will get to the&amp;nbsp;usage of this stored procedure. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;&lt;span style=&quot;background-color: #999999; color: black;&quot;&gt;&lt;strong&gt;Usage&lt;/strong&gt;&lt;/span&gt;&lt;/u&gt;
&lt;br /&gt;
For simplicity, I have used Microsoft&#39;s Adventureworks database in the examples below.&lt;br /&gt;
&lt;br /&gt;
&lt;em&gt;&lt;u&gt;&lt;strong&gt;Execution 1:&lt;/strong&gt;&lt;/u&gt;&lt;/em&gt;&lt;br /&gt;
&lt;strong&gt;&lt;em&gt;&lt;u&gt;&lt;/u&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;em&gt;&lt;u&gt;&lt;/u&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;
Please execute the following TSQL code without any input parameters:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt; execute dbo&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;dbp_Find_Index_Properties_by_Column&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyOQDfPDiSdlGcM4RGUefsoGqptHE0iubGCyGuunjxbl5cougAWJCOdoi1f1k9MYWRXEizU2Yusi964SGXoZMh4BGTsb3eBwCMn_Ar4g9XWj8R_ix3wbQIjTewuHdpNAwTccQ771ROUKPW/s1600/All.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyOQDfPDiSdlGcM4RGUefsoGqptHE0iubGCyGuunjxbl5cougAWJCOdoi1f1k9MYWRXEizU2Yusi964SGXoZMh4BGTsb3eBwCMn_Ar4g9XWj8R_ix3wbQIjTewuHdpNAwTccQ771ROUKPW/s1600/All.PNG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;em&gt;Figure 1&lt;/em&gt;&lt;br /&gt;
&lt;br /&gt;
Results for all clustered and non-clustered indexes in the Adventureworks database are displayed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;em&gt;&lt;u&gt;&lt;strong&gt;Execution 2&lt;/strong&gt;:&lt;/u&gt;&lt;/em&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Please execute the following TSQL code. It will display the results for all tables and their indexes in the Adventureworks database the column AddressID is&amp;nbsp;a part of:&lt;br /&gt;
&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;execute &lt;span style=&quot;color: blue;&quot;&gt;dbo.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;dbp_Find_Index_Properties_by_Column  &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;null,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;AddressID&#39;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;null&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJXJ-e5D9jlX1m3mJYJaTp0GfTBKmCzZbeucffMGLa44a_xjH5ZAg4HQB_f32PrHXOJp0SqVV281ZKqR_qrRtt_zqOs4Y-8TZIBy4HDyG-d7FlQ0JgWaIb8a4AOU_OqSsZPVDbsmPqFAx3/s1600/AddressID.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJXJ-e5D9jlX1m3mJYJaTp0GfTBKmCzZbeucffMGLa44a_xjH5ZAg4HQB_f32PrHXOJp0SqVV281ZKqR_qrRtt_zqOs4Y-8TZIBy4HDyG-d7FlQ0JgWaIb8a4AOU_OqSsZPVDbsmPqFAx3/s1600/AddressID.PNG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;em&gt;Figure 2&lt;/em&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;em&gt;&lt;u&gt;&lt;strong&gt;Execution 3&lt;/strong&gt;:&lt;/u&gt;&lt;/em&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: green;&quot;&gt;&lt;/span&gt;Now&amp;nbsp;execute the following TSQL statements to change the fillfactor on the index Purchasing.VendorAddress.IX_VendorAddress_AddressID from 80 to 50:&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;use Adventureworks&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;go&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;alter &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;index&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; IX_VendorAddress_AddressID &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;on&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Purchasing&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;VendorAddress &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;rebuild&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;with &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;statistics_norecompute&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;on&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;fillfactor&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 50&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;Online&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;ON&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;maxdop&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;sort_in_tempdb&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;on&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;allow_page_locks&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;on&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;go&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;br /&gt;
Now execute the statement below to see if the change in fill factor for this index has been captured:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;execute &lt;span style=&quot;color: blue;&quot;&gt;dbo.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;dbp_Find_Index_Properties_by_Column  &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;null,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;AddressID&#39;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;null&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiP49is0oUjUt16De7HjvRjLAcu_qRqQFHc-L70SRiXWmjQ5CxRbY2ejIjGUEEUrdVN2go0uTU-2fDhShH_HO8eVHEcpr9NDB0WPgCiIYTkfym7r1QX9TyWdYb81JSYEaCHNOxWpGYzP7ru/s1600/FillFactor.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiP49is0oUjUt16De7HjvRjLAcu_qRqQFHc-L70SRiXWmjQ5CxRbY2ejIjGUEEUrdVN2go0uTU-2fDhShH_HO8eVHEcpr9NDB0WPgCiIYTkfym7r1QX9TyWdYb81JSYEaCHNOxWpGYzP7ru/s1600/FillFactor.PNG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;em&gt;Figure 3&lt;/em&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
As you have noticed in the screenshot above,&amp;nbsp;the stored procedure displays 50 (&lt;em&gt;Figure 3&lt;/em&gt;)&amp;nbsp;intead of 80 (&lt;em&gt;Figure 2&lt;/em&gt;) for the fill factor for this index.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;em&gt;&lt;u&gt;&lt;strong&gt;Execution 4&lt;/strong&gt;:&lt;/u&gt;&lt;/em&gt;&lt;em&gt;&lt;u&gt;&lt;/u&gt;&lt;/em&gt;&amp;nbsp;&lt;em&gt;&lt;u&gt;&lt;/u&gt;&lt;/em&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&amp;nbsp;Now let us add a secondary filegroup named&amp;nbsp;&lt;span style=&quot;font-size: x-small;&quot;&gt;SecondaryFileGroup to the database Adventureworks, add a data file to this new filegroup, then drop the index Purchasing.VendorAddress.IX_VendorAddress_AddressID and finally recreate it on the new filegroup, SecondaryFileGroup&amp;nbsp;using data compression and a filter on the AddressID column. The same is accomplished by executing the following code. Please note that you may have to change the path of the new data file in the code below as per you custom configured disc layout:&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;alter database&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Adventureworks &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;add&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;filegroup&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; SecondaryFileGroup&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;go&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;alter &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;database&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Adventureworks &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;add&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;file &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;Name &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Adventureworks_2&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;filename&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;E:\Data3\MSSQL\Data\Adventureworks_2.ndf&#39;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; size &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 10MB&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Maxsize &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 100MB&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; filegrowth &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 10&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;%)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;to&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;filegroup&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; SecondaryFileGroup&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;go&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;drop &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;index&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Purchasing&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;VendorAddress&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;IX_VendorAddress_AddressID&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;go&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; create &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;index&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; IX_VendorAddress_AddressID &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;on&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Purchasing&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;VendorAddress&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;AddressID&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;where &lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;AddressID &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;is&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;not&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;null&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;with &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;statistics_norecompute&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;on&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;fillfactor&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 50&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;data_compression&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; Page&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;Online&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;ON&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;maxdop&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;sort_in_tempdb&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;on&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;allow_page_locks&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;=&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;on&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;on&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; SecondaryFileGroup&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;
&lt;/span&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;go&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp;Now execute the statement below to see if the changes for this index have been captured:&lt;br /&gt;
 &lt;br /&gt;
&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;execute &lt;span style=&quot;color: blue;&quot;&gt;dbo.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;dbp_Find_Index_Properties_by_Column  &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;null,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;AddressID&#39;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;null&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjt1AHDmXN7ljvbAoTkdsEtzjeqbG-8KZ3xG2y3MNDbprhFS3s3aq_PqvZrrIRBnMuXPp-p8eRowr0LZp16ucSzBl2H8yaAH9FZdgvtR427o-xz-nSrZFniPo5Y5lvGcPe1m5gH8bU0-pO/s1600/Changes_to_Index.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjt1AHDmXN7ljvbAoTkdsEtzjeqbG-8KZ3xG2y3MNDbprhFS3s3aq_PqvZrrIRBnMuXPp-p8eRowr0LZp16ucSzBl2H8yaAH9FZdgvtR427o-xz-nSrZFniPo5Y5lvGcPe1m5gH8bU0-pO/s1600/Changes_to_Index.PNG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;em&gt;&lt;span style=&quot;color: blue;&quot;&gt;Figure 4&lt;/span&gt;&lt;/em&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;The changes have been captured.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;img height=&quot;23&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyOQDfPDiSdlGcM4RGUefsoGqptHE0iubGCyGuunjxbl5cougAWJCOdoi1f1k9MYWRXEizU2Yusi964SGXoZMh4BGTsb3eBwCMn_Ar4g9XWj8R_ix3wbQIjTewuHdpNAwTccQ771ROUKPW/s320/All.PNG&quot; style=&quot;filter: alpha(opacity=30); left: 375px; opacity: 0.3; position: absolute; top: 3197px;&quot; width=&quot;96&quot; /&gt;&lt;em&gt;&lt;u&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;strong&gt;Execution 5&lt;/strong&gt;:&lt;/span&gt;&lt;/u&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;u&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;/span&gt;&lt;/u&gt;&lt;/em&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;Lets check to see on which tables the column, AddressID is being used in an index while occurring at the second position.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;Please execute the statement below to find this out:&lt;br /&gt;
 &lt;br /&gt;
&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;execute &lt;span style=&quot;color: blue;&quot;&gt;dbo.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;dbp_Find_Index_Properties_by_Column  &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;null,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: red; font-size: x-small;&quot;&gt;&#39;AddressID&#39;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;2&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: grey;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5zqcXvSSST6vXrHNHultRjiSZ8a7xkgj3G-6wJKe9W3AeI5Tg93Qw5czm-Q_qEgTdhn45lSTXoBuxPSIVOzDMWBNdYl3rbehgRt5RdFCcjIybOCL_F3LAv7S4Vgthh2h4YoWCf6XPEcII/s1600/AddressID_in_2nd_positin_in_Index.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5zqcXvSSST6vXrHNHultRjiSZ8a7xkgj3G-6wJKe9W3AeI5Tg93Qw5czm-Q_qEgTdhn45lSTXoBuxPSIVOzDMWBNdYl3rbehgRt5RdFCcjIybOCL_F3LAv7S4Vgthh2h4YoWCf6XPEcII/s1600/AddressID_in_2nd_positin_in_Index.PNG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;em&gt;Figure 5&lt;/em&gt;&lt;br /&gt;
&lt;span style=&quot;color: grey;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;AddressID&amp;nbsp;occurs as the second column in the three indexes on the three tables shown in Figure 5 above.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: #999999; color: black;&quot;&gt;&lt;strong&gt;Summary:&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
The stored procedure &lt;span style=&quot;font-size: x-small;&quot;&gt;dbo&lt;/span&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;&lt;span style=&quot;color: grey; font-size: xx-small;&quot;&gt;.&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;dbp_Find_Index_Properties_by_Column can be used to find various aspects of how a column is involved within an index. This SP also provides details on various indexes which can then be used to refine existing indexing strategy based on various application stored procedures. I haven&#39;t delved deeper into&amp;nbsp;performance optimization with respect to indexing strategy in this article. I have only attempted to provide an easier starting piont for a preliminary analysis.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
As usual, please let me know of your thoughts and comments.&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://sqlfinerpoints.blogspot.com/feeds/5870063741470650273/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3544169305649860217/5870063741470650273' title='11 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/5870063741470650273'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/5870063741470650273'/><link rel='alternate' type='text/html' href='http://sqlfinerpoints.blogspot.com/2013/03/index-keys-properties-as-step-for.html' title='Index Keys &amp; Properties as a step in Database Tuning'/><author><name>Suresh Kumar Maganti</name><uri>http://www.blogger.com/profile/16645888326754808866</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyuLq44DuP85JRUqfEDgaxcuih4-meamQYzD9Jk-DD_Z1Ul5oh9WXYLo1JF5VzNReeNSgttGQ5dWJgLibF42zcSqeHW9aWQUeDrNfd_ucgf5BQ7975u63-9Bd7VfUwkQ/s220/Suresh.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyOQDfPDiSdlGcM4RGUefsoGqptHE0iubGCyGuunjxbl5cougAWJCOdoi1f1k9MYWRXEizU2Yusi964SGXoZMh4BGTsb3eBwCMn_Ar4g9XWj8R_ix3wbQIjTewuHdpNAwTccQ771ROUKPW/s72-c/All.PNG" height="72" width="72"/><thr:total>11</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3544169305649860217.post-454355315845597478</id><published>2011-11-18T11:50:00.000-06:00</published><updated>2011-11-18T11:50:15.711-06:00</updated><title type='text'>Usage of CTE - Trick with Dates</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Situations arise where logical data generation becomes a requirement. By &#39;Logical Data Generation&#39;, I mean generating additional data based on a logic being applied to existing data. Of course, there are multiple ways of doing the same. In this article, I would like to demonstrate how it&amp;nbsp;conventionally being done&amp;nbsp;using cursors and how it can be done much more efficiently using CTE&#39;s. The concept is being brought out using a simple example.&lt;br /&gt;
&lt;br /&gt;
The problem:&lt;br /&gt;
In order to explain the problem, let me first create two tables, dbo.abc and dbo.def. Once we get familiarity with the data, it would be easier to explain the problem. The table dbo.abc has four columns namely, SeqNo, Date_Field, Month_Count, and&amp;nbsp;Payment. Lets create and populate it by executing the code below:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;/span&gt;if exists (select * from sys.tables&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where name = &#39;abc&#39;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and&amp;nbsp;&amp;nbsp; USER_NAME(schema_id) = &#39;dbo&#39;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and&amp;nbsp;&amp;nbsp; [type] = &#39;U&#39;)&lt;br /&gt;
drop table dbo.abc&amp;nbsp; &lt;br /&gt;
go&lt;br /&gt;
&lt;br /&gt;
create table dbo.abc&lt;br /&gt;
(SeqNo smallint&lt;br /&gt;
,Date_Field datetime&lt;br /&gt;
,Month_Count tinyint&lt;br /&gt;
,Payment decimal(10,2))&lt;br /&gt;
go&lt;br /&gt;
insert into dbo.abc (SeqNo, Date_Field, Month_Count, Payment)&lt;br /&gt;
values (1, &#39;20090101&#39;, 10, 100)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,(2, &#39;20100101&#39;, 7, 200)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; ,(3, &#39;20110101&#39;, 5, 300)&lt;br /&gt;
go&lt;br /&gt;
Lets create another table dbo.DEF with the columns SeqNo, Date_Field, and Payment only. We will keep it empty for now. &lt;br /&gt;
&lt;br /&gt;
if exists (select * from sys.tables&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where name = &#39;def&#39;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and&amp;nbsp;&amp;nbsp; USER_NAME(schema_id) = &#39;dbo&#39;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and&amp;nbsp;&amp;nbsp; [type] = &#39;U&#39;)&lt;br /&gt;
drop table dbo.def&lt;br /&gt;
go&lt;br /&gt;
&lt;br /&gt;
create table dbo.def&lt;br /&gt;
(SeqNo smallint&lt;br /&gt;
,Date_Field datetime&lt;br /&gt;
,Payment decimal(10,2))&lt;br /&gt;
go&lt;br /&gt;
Please execute the code below to view the data in the table, dbo.abc:&lt;br /&gt;
&lt;br /&gt;
select * from abc&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjXlyTzfRSvFPwOGJTo3h_ce3LyWQZuDtptghkyzdtWiRowQNGwbxMeT23_r3_Q3KfbzDyc-QrQbc0ETEEiVX8AVKk9acIpy3GmwZA2MQhI4q2H2c7CftF2KWuXa-5JbyNRp99cS4gqtb8h/s1600/abc.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;103&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjXlyTzfRSvFPwOGJTo3h_ce3LyWQZuDtptghkyzdtWiRowQNGwbxMeT23_r3_Q3KfbzDyc-QrQbc0ETEEiVX8AVKk9acIpy3GmwZA2MQhI4q2H2c7CftF2KWuXa-5JbyNRp99cS4gqtb8h/s320/abc.PNG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
go&lt;br /&gt;
The lets define the problem. Each row in the table, dbo.abc has&amp;nbsp;to be duplicated as many times as the value of Month_Count in that row. Currently the Date_Field has a value that represents the first day of an year. While duplicating the data, we need to ensure that each new row has its Date_Field value incremented by one month. We need to stop the duplication as soon as we reach the month value in the column Month_Count for that row.&amp;nbsp; The values for the columns SeqNo and Payment should remain as they are in the original row. This process needs to be done for all rows in the table, dbo.abc. Finally we need to display all the original and duplicated rows.&lt;br /&gt;
&lt;br /&gt;
Now let us write a conventional SQL Server 2000 type program. Since I have to loop through the data in each row, I am ending up using a&amp;nbsp;CURSOR and a WHILE loop. The T-SQL program is given below:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
declare @l_SeqNo int&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,@l_date_field datetime&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,@l_Month_Count smallint&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,@l_Payment decimal(10, 2)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,@l_counter smallint&lt;br /&gt;
&lt;br /&gt;
select @l_counter = 0&lt;br /&gt;
set nocount on;&lt;br /&gt;
declare i_Cursor insensitive cursor&lt;br /&gt;
for&lt;br /&gt;
select SeqNo, Date_Field, Month_Count, Payment from dbo.abc&lt;br /&gt;
&lt;br /&gt;
open i_Cursor&lt;br /&gt;
&lt;br /&gt;
fetch next from i_Cursor into &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@l_SeqNo&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,@l_date_field&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,@l_Month_Count&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,@l_Payment&lt;br /&gt;
&lt;br /&gt;
while @@fetch_status = 0&lt;br /&gt;
begin&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select @l_counter = 0&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while (@l_counter&amp;nbsp;&amp;lt; @l_Month_Count)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; begin&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; insert into dbo.def (SeqNo, Date_Field, Payment)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select @l_SeqNo, dateadd(mm, @l_counter, @l_date_field), @l_Payment&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select @l_counter = @l_counter + 1&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fetch next from i_Cursor into &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @l_SeqNo&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,@l_date_field&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,@l_Month_Count&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,@l_Payment&lt;br /&gt;
end&lt;br /&gt;
close i_Cursor&lt;br /&gt;
deallocate i_Cursor&lt;br /&gt;
set nocount off;&lt;br /&gt;
go&lt;br /&gt;
&lt;br /&gt;
After executing the above program, let us check the table dbo.DEF:&lt;br /&gt;
select * from def&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCgvD0hWXI4jXNkNKi-fXy1xxbvOg0MZadUs1i8meOgMcv1I6d6aVvpCfdSh_aRhXKg6Epyc-cpJksX1LMe1RqD5I5kYEyvmSIXbnyj1LcdFYdQnaCBZpAr8xJBuP5oxArg2z8Oe77F00s/s1600/def.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCgvD0hWXI4jXNkNKi-fXy1xxbvOg0MZadUs1i8meOgMcv1I6d6aVvpCfdSh_aRhXKg6Epyc-cpJksX1LMe1RqD5I5kYEyvmSIXbnyj1LcdFYdQnaCBZpAr8xJBuP5oxArg2z8Oe77F00s/s320/def.PNG&quot; width=&quot;256&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;It may be noted in the result set above that the row with SeqNo 1 has been duplicated 10 times, 10 being the value of the Month_Count field in table dbo.abc. Similarly, the row with SeqNo&amp;nbsp;2 has been duplicated&amp;nbsp;7 times,&amp;nbsp;7 being the value of the Month_Count field in table dbo.abc.&amp;nbsp;&amp;nbsp;Finally the row with SeqNo&amp;nbsp;3 in the table dbo.abc has been duplicated&amp;nbsp;5 times,&amp;nbsp;5 being the value of the Month_Count field in table dbo.abc. We have kept the values of SeqNo and Payment columns intact through this duplication exercise.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The problem has been solved. Now let us solve the same problem in a different way using Recursive CTE in SQL Server 2005/2008 style of coding. The code is given below. please execute the same:&lt;br /&gt;
&lt;br /&gt;
with CTE_Base (SeqNo, Date_Field, Month_Count, Payment, Begin_Date, End_Date, Frequency)&lt;br /&gt;
as&lt;br /&gt;
(select SeqNo, Date_Field, Month_Count, Payment, Date_Field, dateadd(mm, Month_Count-1, Date_Field), 1 from abc)&lt;br /&gt;
,CTE_Level1 (SeqNo, Date_Field, Month_Count, Payment, Begin_Date, End_Date, Frequency)&lt;br /&gt;
as&lt;br /&gt;
(select SeqNo, Date_Field, Month_Count, Payment, Begin_Date, End_Date, Frequency from CTE_Base&lt;br /&gt;
union all&lt;br /&gt;
select SeqNo, dateadd(mm, Frequency, Date_Field), Month_Count, Payment, Begin_Date, End_Date, Frequency&lt;br /&gt;
from CTE_Level1&lt;br /&gt;
where dateadd(mm, Frequency, Date_Field) between Begin_Date and End_Date)&lt;br /&gt;
&lt;br /&gt;
select SeqNo, Date_Field, Payment&lt;br /&gt;
from CTE_Level1&lt;br /&gt;
where Date_Field between Begin_Date and End_Date&lt;br /&gt;
order by SeqNo, Date_Field&lt;br /&gt;
go&lt;br /&gt;
&lt;br /&gt;
Here is the result set:&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJp8e5B7je9UXH7A9me3NGcMJkuoU4xXGaVuW7srOGeNJBw8PfzChHpdfVfRMqygh8C0aU7YT4yKBd1WiiOQrpvR_IeMZxbBJrv7wUBZ8qJ3Kgtzih9C3wjkFBWOjvAKlHko0C3LuHQ1Uz/s1600/def.PNG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhJp8e5B7je9UXH7A9me3NGcMJkuoU4xXGaVuW7srOGeNJBw8PfzChHpdfVfRMqygh8C0aU7YT4yKBd1WiiOQrpvR_IeMZxbBJrv7wUBZ8qJ3Kgtzih9C3wjkFBWOjvAKlHko0C3LuHQ1Uz/s320/def.PNG&quot; width=&quot;256&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;If we set &quot;Show Actual Execution Plan&quot; and then run both the queries (the cursor based one and the recursive CTE based one), you will note that the query cost of the conventional cursor based approach to this problem&#39;s solution is 94% where as it is 6% with the recursive CTE based solution.&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;It may also be noted that in the recursive CTE not a single variable has been used unlike the cursor based methodology. &lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;The problem I have used in this article is just to solve a situation where new data is to be generated based on applying certain logic on existing data. In practice, there are and would be&amp;nbsp;many more such situations where recursive CTE&#39;s can be put to good use for efficieny in execution and brevity of code.&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sqlfinerpoints.blogspot.com/feeds/454355315845597478/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3544169305649860217/454355315845597478' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/454355315845597478'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/454355315845597478'/><link rel='alternate' type='text/html' href='http://sqlfinerpoints.blogspot.com/2011/11/usage-of-cte-trick-with-dates.html' title='Usage of CTE - Trick with Dates'/><author><name>Suresh Kumar Maganti</name><uri>http://www.blogger.com/profile/16645888326754808866</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyuLq44DuP85JRUqfEDgaxcuih4-meamQYzD9Jk-DD_Z1Ul5oh9WXYLo1JF5VzNReeNSgttGQ5dWJgLibF42zcSqeHW9aWQUeDrNfd_ucgf5BQ7975u63-9Bd7VfUwkQ/s220/Suresh.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjXlyTzfRSvFPwOGJTo3h_ce3LyWQZuDtptghkyzdtWiRowQNGwbxMeT23_r3_Q3KfbzDyc-QrQbc0ETEEiVX8AVKk9acIpy3GmwZA2MQhI4q2H2c7CftF2KWuXa-5JbyNRp99cS4gqtb8h/s72-c/abc.PNG" height="72" width="72"/><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3544169305649860217.post-379619023522537556</id><published>2011-08-25T17:26:00.000-05:00</published><updated>2012-08-09T17:51:09.532-05:00</updated><title type='text'>Using Recursive CTE to Minimize Table Scans</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Company XYZ WORKS&amp;nbsp;has four vendors.&amp;nbsp; Each vendor is paid based on goods supplied by them to XYZ WORKS. Based on the payment history, the company wants to know the least non-zero amount that was paid to each vendor. If a vendor wasn&#39;t ever paid any amount, then that vendor should also fall in ths list. It is this extra requirement that adds a little complication to the task. Lets solve the problem now.&lt;br /&gt;
&lt;br /&gt;
First, we would create a table called dbo.Payment_Details as following:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;if exists (select 1 from sys.tables&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where Schema_NAME(schema_ID) = &#39;dbo&#39;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and&amp;nbsp;&amp;nbsp; name = &#39;Payment_Details&#39;)&lt;br /&gt;
begin&lt;br /&gt;
&amp;nbsp;&amp;nbsp; drop table dbo.Payment_Details&lt;br /&gt;
end;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;create table dbo.Payment_Details&lt;br /&gt;
(Name&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; varchar(10)&lt;br /&gt;
,Amount&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int&lt;br /&gt;
,Payment_Date&amp;nbsp; smalldatetime)&lt;br /&gt;
&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;Next, lets populate this table with relevant data:&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;br /&gt;
insert into dbo.Payment_Details&lt;br /&gt;
values (&#39;Veronica&#39;, &#39;0&#39;, &#39;20110201&#39;)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,(&#39;Veronica&#39;, &#39;10&#39;, &#39;20110301&#39;)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,(&#39;Veronica&#39;, &#39;20&#39;, &#39;20110401&#39;)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,(&#39;Veronica&#39;, &#39;30&#39;, &#39;20110501&#39;)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,(&#39;Tiffany&#39;, &#39;40&#39;, &#39;20110201&#39;)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,(&#39;Tiffany&#39;, &#39;50&#39;, &#39;20110301&#39;)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,(&#39;Tiffany&#39;, &#39;60&#39;, &#39;20110401&#39;)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,(&#39;John&#39;, &#39;0&#39;, &#39;20110201&#39;)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,(&#39;John&#39;, &#39;70&#39;, &#39;20110301&#39;)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,(&#39;John&#39;, &#39;80&#39;, &#39;20110401&#39;)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,(&#39;Anthony&#39;, &#39;0&#39;, &#39;20110201&#39;)&lt;br /&gt;
&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;Lets see the contents of the table, dbo.Payment_Details:&lt;br /&gt;
&lt;br /&gt;
select * from Payment_Details;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
What we expect in the final solution is following:&lt;br /&gt;
&lt;br /&gt;
Name&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Amount&lt;br /&gt;
------------&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ------------&lt;br /&gt;
Anthony&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&lt;br /&gt;
John&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 70&lt;br /&gt;
Tiffany&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 40&lt;br /&gt;
Veronica&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 10&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Lets code&amp;nbsp;some solutions:&lt;br /&gt;
&lt;br /&gt;
Variation 1:&lt;br /&gt;
&lt;br /&gt;
In this solution, we are using CTE but not recursively. In other words,&amp;nbsp;a CTE does not reference itself. In this solution, the table, dbo.Payment_Details is being referenced twice, once per CTE.&lt;br /&gt;
&lt;br /&gt;
with CTE_1 (Name, Amount)&lt;br /&gt;
as (select Name, min(Amount)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from dbo.Payment_Details with (nolock)&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; where Amount&amp;nbsp;&amp;gt; 0&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; group by Name)&lt;br /&gt;
,CTE_2 (Name, Amount)&lt;br /&gt;
as (select b.Name, b.Amount from dbo.Payment_Details b with (nolock)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;where b.Name not in (select Name from CTE_1)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and b.Amount = 0&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; union all&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select Name, Amount from CTE_1)&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;select * from CTE_2;&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;Variation 2:&lt;br /&gt;
&lt;br /&gt;
In this solution, we are again using CTE but not recursively. In this solution, the table, dbo.Payment_Details is being referenced twice,&amp;nbsp;both times in the first&amp;nbsp;CTE.&lt;br /&gt;
&lt;br /&gt;
with CTE_1 (Name, Amount, RowNumber)&lt;br /&gt;
as (select Name, Amount, row_number() over(Partition by Name order by Name, Amount) RowNumber &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; from dbo.Payment_Details with (nolock)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; where Amount = 0&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; union&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; select Name, Amount, row_number() over(Partition by Name order by Name, Amount desc) RowNumber &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; from dbo.Payment_Details with (nolock)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; where Amount&amp;nbsp;&amp;gt; 0)&lt;br /&gt;
,CTE_2 (Name, Amount, RowNumber)&lt;br /&gt;
as (select Name, Amount, row_number() over(partition by Name order by Name, RowNumber desc) RowNumber&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from CTE_1)&lt;br /&gt;
&lt;br /&gt;
select Name, Amount from CTE_2&lt;br /&gt;
where RowNumber = 1;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;Variation 3:&lt;br /&gt;
&lt;br /&gt;
In this solution, we are again using CTE but recursively. In other words, a CTE is referencing itself within itself. In this solution, the table, dbo.Payment_Details is being referenced only once.&lt;br /&gt;
&lt;br /&gt;
with CTE_1 (Name, Amount, RowNumber)&lt;br /&gt;
as (select distinct Name, Amount, row_number() over(Partition by Name order by Name, Amount) RowNumber &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from (select Name, Amount from dbo.Payment_Details with (nolock)) a&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp; union ALL&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; select Name, Amount, row_number() over(Partition by Name order by Name, RowNumber desc) RowNumber &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;from CTE_1&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where RowNumber = 2)&lt;br /&gt;
&lt;br /&gt;
,CTE_2 (Name, Min_Amt, Max_Amt, RowNumber)&lt;br /&gt;
as (select distinct Name, min(Amount) over(partition by Name, RowNumber) Min_Amt, MAX(Amount) over(partition&amp;nbsp;&amp;nbsp; by&amp;nbsp;Name, RowNumber) Max_Amt, RowNumber&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from CTE_1&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where RowNumber = 1)&lt;br /&gt;
&lt;br /&gt;
SELECT Name, case&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; when Min_Amt = 0 then Max_Amt&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; else Min_Amt&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end Amount &lt;br /&gt;
FROM CTE_2;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;In this article, we have shown that Recursive CTE could be used effectively to reduce the number of Scans of the referenced table.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sqlfinerpoints.blogspot.com/feeds/379619023522537556/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3544169305649860217/379619023522537556' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/379619023522537556'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/379619023522537556'/><link rel='alternate' type='text/html' href='http://sqlfinerpoints.blogspot.com/2011/08/using-recursive-cte-to-minimize-table.html' title='Using Recursive CTE to Minimize Table Scans'/><author><name>Suresh Kumar Maganti</name><uri>http://www.blogger.com/profile/16645888326754808866</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyuLq44DuP85JRUqfEDgaxcuih4-meamQYzD9Jk-DD_Z1Ul5oh9WXYLo1JF5VzNReeNSgttGQ5dWJgLibF42zcSqeHW9aWQUeDrNfd_ucgf5BQ7975u63-9Bd7VfUwkQ/s220/Suresh.jpg'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3544169305649860217.post-7989428812768449968</id><published>2011-08-25T15:41:00.000-05:00</published><updated>2011-08-25T16:17:02.057-05:00</updated><title type='text'>Calculating Cumulative percentages with CTE</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;A company sells different products on the internet. For the sake of this example, lets say it sells bicycles, scooters and lawn mowers. Everyday, customers order different quantities of these three products. The company&#39;s capacity is to ship upto 25% of pending orders by sale amount of each product everyday. And the company wants to meet its capacity every weekday while making sure that customers are being served as per their order date starting with the earliest&lt;br /&gt;
pending order for a given product. For example, if there are pending orders worth $1000 for bicycles today, bicycles worth upto $250 should be shipped today in the order in which orders were placed by the customers. At the same time,&amp;nbsp;orders should be shipped in full and not partially.&lt;br /&gt;
&lt;br /&gt;
I am using just one table for this demo. It is not in the third normal form just to make the explanation simple. So lets create a base table with the relevant sample data. Please note that the code below is compatible with microsoft SQL Server 2008. With slight modifications, it can work in Microsoft SQL Server 2005 as well&amp;nbsp;:&lt;br /&gt;
&lt;br /&gt;
if exists (select * from sys.tables&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where Schema_NAME(schema_ID) = &#39;dbo&#39;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and&amp;nbsp;&amp;nbsp; name = &#39;Order_details&#39;)&lt;br /&gt;
begin&lt;br /&gt;
&amp;nbsp;&amp;nbsp; drop table dbo.Order_details&lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
create table dbo.Order_details&lt;br /&gt;
(Order_ID&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int identity(1, 1) primary key clustered with (fillfactor = 50, data_compression = page)&lt;br /&gt;
,Order_Date&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; smalldatetime not null&lt;br /&gt;
,Product_Name&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;varchar(50) not null&lt;br /&gt;
,Unit_Price&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; decimal(28,2) not null&lt;br /&gt;
,Quantity_Ordered&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; smallint not null&lt;br /&gt;
,Order_Value&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; as (Quantity_Ordered * Unit_Price)&lt;br /&gt;
,Order_Value_As_Percent_of_Pending_Orders_By_Product&amp;nbsp; decimal(28, 10) null&lt;br /&gt;
,CutOff_Value_By_Product&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;decimal(28, 10) null&lt;br /&gt;
,&lt;span style=&quot;font-size: x-small;&quot;&gt;Ship_It_Today&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;bit not null default (0)&lt;br /&gt;
,Shipping_Date&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;smalldatetime null)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Next, let us populate the table, dbo.Order_Details. &lt;br /&gt;
&lt;br /&gt;
insert into dbo.Order_details (Order_Date, Product_Name, Unit_Price, Quantity_Ordered)&lt;br /&gt;
--10 orders for bicycles&lt;br /&gt;
&amp;nbsp;values (GETDATE()-10 , &#39;Bicycle&#39;, 100.00, 1)&lt;br /&gt;
,(GETDATE()-9 , &#39;Bicycle&#39;, 100.00, 2)&lt;br /&gt;
,(GETDATE()-8 , &#39;Bicycle&#39;, 100.00, 3)&lt;br /&gt;
,(GETDATE()-7 , &#39;Bicycle&#39;, 100.00, 4)&lt;br /&gt;
,(GETDATE()-6 , &#39;Bicycle&#39;, 100.00, 5)&lt;br /&gt;
,(GETDATE()-5 , &#39;Bicycle&#39;, 100.00, 6)&lt;br /&gt;
,(GETDATE()-4 , &#39;Bicycle&#39;, 100.00, 7)&lt;br /&gt;
,(GETDATE()-3 , &#39;Bicycle&#39;, 100.00, 8)&lt;br /&gt;
,(GETDATE(), &#39;Bicycle&#39;, 100.00, 2)&lt;br /&gt;
,(GETDATE(), &#39;Bicycle&#39;, 100.00, 10)&lt;br /&gt;
--10 orders for scooters&lt;br /&gt;
,(GETDATE()-10 , &#39;Scooter&#39;, 300.00, 5)&lt;br /&gt;
,(GETDATE()-9 , &#39;Scooter&#39;, 300.00, 2)&lt;br /&gt;
,(GETDATE()-8 , &#39;Scooter&#39;, 300.00, 4)&lt;br /&gt;
,(GETDATE()-7 , &#39;Scooter&#39;, 300.00, 1)&lt;br /&gt;
,(GETDATE()-6 , &#39;Scooter&#39;, 300.00, 6)&lt;br /&gt;
,(GETDATE()-5 , &#39;Scooter&#39;, 300.00, 9)&lt;br /&gt;
,(GETDATE()-4 , &#39;Scooter&#39;, 300.00, 10)&lt;br /&gt;
,(GETDATE()-3 , &#39;Scooter&#39;, 300.00, 2)&lt;br /&gt;
,(GETDATE(), &#39;Scooter&#39;, 300.00, 15)&lt;br /&gt;
,(GETDATE(), &#39;Scooter&#39;, 300.00, 13)&lt;br /&gt;
--10 orders for lawn mowers&lt;br /&gt;
,(GETDATE()-10 , &#39;Lawn Mower&#39;, 500.00, 1)&lt;br /&gt;
,(GETDATE()-9 , &#39;Lawn Mower&#39;, 500.00, 2)&lt;br /&gt;
,(GETDATE()-8 , &#39;Lawn Mower&#39;, 500.00, 1)&lt;br /&gt;
,(GETDATE()-7 , &#39;Lawn Mower&#39;, 500.00, 2)&lt;br /&gt;
,(GETDATE()-6 , &#39;Lawn Mower&#39;, 500.00, 1)&lt;br /&gt;
,(GETDATE()-5 , &#39;Lawn Mower&#39;, 500.00, 2)&lt;br /&gt;
,(GETDATE()-4 , &#39;Lawn Mower&#39;, 500.00, 1)&lt;br /&gt;
,(GETDATE()-3 , &#39;Lawn Mower&#39;, 500.00, 1)&lt;br /&gt;
,(GETDATE(), &#39;Lawn Mower&#39;, 500.00, 5)&lt;br /&gt;
,(GETDATE(), &#39;Lawn Mower&#39;, 500.00, 3)&lt;br /&gt;
&lt;br /&gt;
Lets read the contents of the table dbo.Order_Details:&lt;br /&gt;
&lt;br /&gt;
select * from dbo.Order_details;&lt;br /&gt;
&lt;br /&gt;
Our purpose is to mark the orders to be shipped today based on upto 25% of the pending order value for each product. This is accomplished by the code below:&lt;br /&gt;
&lt;br /&gt;
With CTE_Base (Order_ID, Order_Date, Product_Name, Order_Value, RowNumber, Total_Value_of_Pending_Orders_By_Product, Order_Value_As_Percent_of_Pending_Orders_By_Product)&lt;br /&gt;
as &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; (select Order_ID&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,Order_Date&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,Product_Name&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,Order_Value&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,ROW_NUMBER () over(partition by Product_Name order by Product_Name, Order_Date) RowNumber&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,SUM(Order_Value) over(partition by Product_Name) Total_Value_of_Pending_Orders_By_Product&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,convert(decimal(28, 15), convert(decimal(28, 15), Order_Value) / convert(decimal(28, 15), SUM(Order_Value) over(partition by Product_Name))) Order_Value_As_Percent_of_Pending_Orders_By_Product&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from dbo.Order_details with (nolock)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where Ship_It_Today = 0)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
--select * from CTE_Base&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;,CTE_Level1 (Product_Name, RowNumber, CutOff_Value_By_Product)&lt;br /&gt;
as &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; (select a.Product_Name&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,a.RowNumber&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ,(select SUM(b.Order_Value) from CTE_Base b&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where b.Product_Name = a.Product_Name&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; and&amp;nbsp;&amp;nbsp; b.RowNumber &amp;lt;= a.RowNumber) &amp;nbsp;/ &amp;nbsp;a.Total_Value_of_Pending_Orders_By_Product&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from CTE_Base a)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;--select * from CTE_Level1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;color: blue; font-size: x-small;&quot;&gt;&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;br /&gt;
update a&lt;br /&gt;
set a.Order_Value_As_Percent_of_Pending_Orders_By_Product = b.Order_Value_As_Percent_of_Pending_Orders_By_Product&lt;br /&gt;
&amp;nbsp;&amp;nbsp; ,a.CutOff_Value_By_Product&amp;nbsp;&amp;nbsp;= c.CutOff_Value_By_Product&lt;br /&gt;
&amp;nbsp;&amp;nbsp; ,a.Ship_It_Today&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;= case &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; when c.CutOff_Value_By_Product &amp;lt;= 0.26 then 1&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else a.Ship_It_Today&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end&lt;br /&gt;
&amp;nbsp;&amp;nbsp; ,a.Shipping_Date&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;= case &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; when c.CutOff_Value_By_Product &amp;lt;= 0.26 then GETDATE()&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else null&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
from dbo.Order_details a &lt;br /&gt;
inner join CTE_Base b&lt;br /&gt;
on a.Order_ID = b.Order_ID&lt;br /&gt;
inner join CTE_Level1 c&lt;br /&gt;
on&amp;nbsp; b.Product_Name = c.Product_Name&lt;br /&gt;
and b.RowNumber&amp;nbsp;&amp;nbsp;&amp;nbsp; = c.RowNumber&lt;br /&gt;
where a.Ship_It_Today = 0&lt;br /&gt;
and a.Shipping_Date is null;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now lets see the orders that need to be shipped today. It may be noted that we have shipped upto 25% of the orders by value for each product today:&lt;br /&gt;
&lt;br /&gt;
select * from dbo.Order_details;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;
</content><link rel='replies' type='application/atom+xml' href='http://sqlfinerpoints.blogspot.com/feeds/7989428812768449968/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3544169305649860217/7989428812768449968' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/7989428812768449968'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/7989428812768449968'/><link rel='alternate' type='text/html' href='http://sqlfinerpoints.blogspot.com/2011/08/calculating-cumulative-percentages-with.html' title='Calculating Cumulative percentages with CTE'/><author><name>Suresh Kumar Maganti</name><uri>http://www.blogger.com/profile/16645888326754808866</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyuLq44DuP85JRUqfEDgaxcuih4-meamQYzD9Jk-DD_Z1Ul5oh9WXYLo1JF5VzNReeNSgttGQ5dWJgLibF42zcSqeHW9aWQUeDrNfd_ucgf5BQ7975u63-9Bd7VfUwkQ/s220/Suresh.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3544169305649860217.post-4141659872559502781</id><published>2011-08-25T11:34:00.000-05:00</published><updated>2011-08-25T11:34:08.216-05:00</updated><title type='text'>Table_Sizes</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Introduction: &lt;br /&gt;
&lt;br /&gt;
Designing databases is a common activity for most of us. Assigning proper datatypes to attributes within entities is a part of that. Once promoted to production when thousands of users pound these tables, the DBA usually keeps track of the growth trends of databases and tables within them. For a DBA, maintaining these statistics (apart from numerous others) is important to see how the application is doing, how the  &lt;br /&gt;
user-response is varying and how the hardware is being utilized. &lt;br /&gt;
&lt;br /&gt;
When an application slows down and the database response is poorer, there could be several contributing factors like bad database layout, sub-optimal database design, ill-designed application (no SP&#39;s/adhoc TSQL), ill-written stored procedures, improper allocation of CPU/memory, and a few more and several  &lt;br /&gt;
combinations of them. A common problem that I as a DBA came across is inapropriate dataypes for fields during design phase but detected a few months after implementation. This is so rampant especially when a DBA is not part of the design or is hired after the fact. I would like to share one such case with you today. &lt;br /&gt;
&lt;br /&gt;
Building up a scenario: &lt;br /&gt;
&lt;br /&gt;
The first step for me is to get a list of a few of the largest tables (in terms of row counts) in a database. Then I need to get the size of these individual tables (on disc) as well. Having done that, I can figure out the largest tables in terms of rowcount and size on disc. &lt;br /&gt;
&lt;br /&gt;
I would also like to see how many rows (on an average) reside on each data page allocated to the table. Let us call it &quot;row density&quot; for our discussion. &lt;br /&gt;
&lt;br /&gt;
To get this data, I used to use different methods in Microsoft SQL Server 2000 and 2005. However in SQL Server 2008, I use the undocumented system function, sys.fn_PhysLocFormatter and the system stored procedure sp_MSForEachTable. I use them for a quick view of the database tables for my own benefit rather than to send a report to the CTO as these objects are unsupported. &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
select sys.fn_PhysLocFormatter (%%physloc%%) Rows_per_Page from Orders &lt;br /&gt;
&lt;br /&gt;
The results look like the following: &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
Rows_per_Page &lt;br /&gt;
------------------------- &lt;br /&gt;
(1:90536:0) &lt;br /&gt;
(1:90536:1) &lt;br /&gt;
(1:90536:2) &lt;br /&gt;
&lt;br /&gt;
Each row in the result set represents one row in Orders table. So there would be as many rows in this result set as there are rows in the table, Orders. &lt;br /&gt;
&lt;br /&gt;
Let&#39;s describe the three portions in the result above: 1:90536:0. First portion, 1 is the id of the data file on which the page (90536) containing this row (first row = Row 0) exists. You may observe that rows numbered 1 and 2 in the result set above also reside on the same page, 90536. Please note the first row  &lt;br /&gt;
contained in a page is the 0th row in the result set above as the counting starts from 0 onwards instead of from 1. In effect, there are 3 rows in the page 90536 for this table. So our row_density is 3.0. &lt;br /&gt;
&lt;br /&gt;
Most of us are very familiar with the system stored procedure, sp_msforeachtable. It performs the same set of prescribed activities on all tables in a given database. As it has been discussed so often on the forums, I would skip its description here. &lt;br /&gt;
&lt;br /&gt;
Now let&#39;s customize the usage of sys.fn_PhysLocFormatter and sp_msforeachtable for each table so that we can obtain more user-friendly results for analysis and action: &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
/******************************* &lt;br /&gt;
NAME : Table Size script. &lt;br /&gt;
Author : Suresh K Maganti &lt;br /&gt;
Purpose: Get Pages, pages per row, Table size for each table. Find large tables with low row-density. &lt;br /&gt;
********************************/ &lt;br /&gt;
if OBJECT_ID(&#39;tempdb..#TEMP&#39;) is not null &lt;br /&gt;
drop table #TEMP  &lt;br /&gt;
&lt;br /&gt;
create table #TEMP (Table_Name varchar(100) null &lt;br /&gt;
,Pages int null &lt;br /&gt;
,Rows_Per_Page decimal(28,4) null &lt;br /&gt;
,Rows_in_Table as convert(decimal(28, 0), Pages * Rows_Per_Page) &lt;br /&gt;
,Table_Size_MB as convert(decimal(28, 0), (convert(decimal(28,2), Pages) * 8192.00) / (1024.00 * 1024.00))) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
insert into #TEMP (Table_Name, Pages, Rows_Per_Page) &lt;br /&gt;
exec sp_msforeachtable &#39;select &#39;&#39;?&#39;&#39;, count (Page_Number) Numer_of_Pages_in_Table, isnull(Avg (isnull(convert(decimal(28, 4), Rows_Per_Page), 0.0000)), 0.0000) Avg_Rows_Per_Page &lt;br /&gt;
from &lt;br /&gt;
(select substring(SUBSTRING(File_Page_Row, CHARINDEX(&#39;&#39;:&#39;&#39;, File_Page_Row, 1) + 1, 100), 1, charindex(&#39;&#39;:&#39;&#39;, SUBSTRING(File_Page_Row, CHARINDEX(&#39;&#39;:&#39;&#39;, File_Page_Row, 1) + 1, 100), 1)-1) Page_Number, COUNT(1) Rows_Per_Page &lt;br /&gt;
from &lt;br /&gt;
(select sys.fn_PhysLocFormatter (%%physloc%%) File_Page_Row from ?) a &lt;br /&gt;
group by substring(SUBSTRING(File_Page_Row, CHARINDEX(&#39;&#39;:&#39;&#39;, File_Page_Row, 1) + 1, 100), 1, charindex(&#39;&#39;:&#39;&#39;, SUBSTRING(File_Page_Row, CHARINDEX(&#39;&#39;:&#39;&#39;, File_Page_Row, 1) + 1, 100), 1)-1)) b&#39; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
select * from #TEMP &lt;br /&gt;
order by 2 desc, 3 asc &lt;br /&gt;
&lt;br /&gt;
if OBJECT_ID(&#39;tempdb..#TEMP&#39;) is not null &lt;br /&gt;
drop table #TEMP  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is the result set: &lt;br /&gt;
&lt;br /&gt;
Table_Name Pages Rows_Per_Page Rows_in_Table Table_Size_MB &lt;br /&gt;
----------------------------------- --------------- ------------------------ ----------------------- ---------------------- &lt;br /&gt;
[dbo].[Order_Details] 100021 35.0380 3511193 783 &lt;br /&gt;
[dbo].[Order_Master] 3345 83.1629 278180 26 &lt;br /&gt;
[dbo].[Customer_Details] 2376 526.0118 1249804 19 &lt;br /&gt;
[dbo].[Customer_Master] 358 404.1006 144668 3 &lt;br /&gt;
... &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
I have shown the partial result set above for our explanation. &lt;br /&gt;
&lt;br /&gt;
From past designs and analyses, I generally mark tables with under 100 rows/page as problematic. I always try to find ways to bring this figure to 100 rows/page or more in consultaion with the application development teams. Sometimes it requires further normalization as well. In our example, Order_Details  &lt;br /&gt;
table is our largest (~3.5 million rows) occupying ~783 MB of disc space. However, the row density is just around ~35. If we could increase the row density somehow, we could auotmatically bring down the table size on disc. &lt;br /&gt;
&lt;br /&gt;
Let&#39;s see the table structure now: &lt;br /&gt;
&lt;br /&gt;
Sp_help [Order_Details] &lt;br /&gt;
&lt;br /&gt;
Column_name Type Computed Length Prec Scale Nullable &lt;br /&gt;
&lt;br /&gt;
--------------------- ------------ ---------------- -------------- ---------- ----------- ----------------- &lt;br /&gt;
Order_ID int no 4 10 0 no &lt;br /&gt;
Product_ID int no 4 10 0 no &lt;br /&gt;
Specifications nvarchar no 400 yes &lt;br /&gt;
Status varchar no 255 yes &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
Observations and verifications: &lt;br /&gt;
&lt;br /&gt;
The column, [Specifications] has the datatype nvarchar(400). In our case, we do not store unicode values in this column as per the application team. &lt;br /&gt;
There is a composite clustered index on the columns (Order_ID, Product_ID) with fillfactor = 100. &lt;br /&gt;
There is no compression on this table or its index. &lt;br /&gt;
This is a pretty normalized table with just 4 columns. &lt;br /&gt;
This table is not partitioned. &lt;br /&gt;
Recommendations: &lt;br /&gt;
&lt;br /&gt;
If we could change the dataype for the column, [Specifications] to varchar(400), we could reduce the storage space. &lt;br /&gt;
If we could compress the lone index and the table data, we could reduce the table size further. &lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
Evaluation of recommendations: &lt;br /&gt;
Let us estimate our gains through page compression: &lt;br /&gt;
&lt;br /&gt;
sp_estimate_data_compression_savings @schema_name = &#39;dbo&#39;, @object_name = &#39;Order_Details&#39;, @index_id = 1, @partition_number = 1, @data_compression = &#39;Page&#39; &lt;br /&gt;
&lt;br /&gt;
Here is the result: &lt;br /&gt;
object_name schema_name index_id partition_number size_with_current_compression_setting(KB) size_with_requested_compression_setting(KB) &lt;br /&gt;
Order_Details dbo 1 1 983688 758784 &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
That&#39;s almost a gain of 23% space. Let us estimate our gains through row compression: &lt;br /&gt;
&lt;br /&gt;
sp_estimate_data_compression_savings @schema_name = &#39;dbo&#39;, @object_name = &#39;Order_Details&#39;, @index_id = 1, @partition_number = 1, @data_compression = &#39;Page&#39; &lt;br /&gt;
&lt;br /&gt;
Here is the result: &lt;br /&gt;
object_name schema_name index_id partition_number size_with_current_compression_setting(KB) size_with_requested_compression_setting(KB) &lt;br /&gt;
Order_Details dbo 1 1 983688 969192 &lt;br /&gt;
&lt;br /&gt;
With row compression, our gain is around 1.5% only. So we could definitely benefit from Page compression in this case. &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
Implementation: &lt;br /&gt;
So here we go with the implementation: &lt;br /&gt;
&lt;br /&gt;
--Change the datatype from nvarchar(400) to varchar(400) &lt;br /&gt;
alter table [dbo].[Order_Details] alter column [Specifications] varchar(400) null &lt;br /&gt;
&lt;br /&gt;
--Compress the clustered index using PAGE compression: &lt;br /&gt;
alter index all on [dbo].[Order_Details] rebuild with (fillfactor = 95, Data_Compression = PAGE, maxdop = 1) &lt;br /&gt;
&lt;br /&gt;
--Compress the table using PAGE compression (Not necessary if the above step is done): &lt;br /&gt;
alter table [dbo].[Order_Details] rebuild Partition = All with (Data_Compression = PAGE, maxdop = 1) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Testing: &lt;br /&gt;
&lt;br /&gt;
Now let&#39;s run our script again to see the row density and table sizes: &lt;br /&gt;
&lt;br /&gt;
/******************************* &lt;br /&gt;
NAME : Table Size script. &lt;br /&gt;
Author : Suresh K Maganti &lt;br /&gt;
Purpose: Get Pages, pages per row, Table size for each table. Find large tables with low row-density. &lt;br /&gt;
********************************/ &lt;br /&gt;
if OBJECT_ID(&#39;tempdb..#TEMP&#39;) is not null &lt;br /&gt;
drop table #TEMP  &lt;br /&gt;
&lt;br /&gt;
create table #TEMP (Table_Name varchar(100) null &lt;br /&gt;
,Pages int null &lt;br /&gt;
,Rows_Per_Page decimal(28,4) null &lt;br /&gt;
,Rows_in_Table as convert(decimal(28, 0), Pages * Rows_Per_Page) &lt;br /&gt;
,Table_Size_MB as convert(decimal(28, 0), (convert(decimal(28,2), Pages) * 8192.00) / (1024.00 * 1024.00))) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
insert into #TEMP (Table_Name, Pages, Rows_Per_Page) &lt;br /&gt;
exec sp_msforeachtable &#39;select &#39;&#39;?&#39;&#39;, count (Page_Number) Numer_of_Pages_in_Table, isnull(Avg (isnull(convert(decimal(28, 4), Rows_Per_Page), 0.0000)), 0.0000) Avg_Rows_Per_Page &lt;br /&gt;
from &lt;br /&gt;
(select substring(SUBSTRING(File_Page_Row, CHARINDEX(&#39;&#39;:&#39;&#39;, File_Page_Row, 1) + 1, 100), 1, charindex(&#39;&#39;:&#39;&#39;, SUBSTRING(File_Page_Row, CHARINDEX(&#39;&#39;:&#39;&#39;, File_Page_Row, 1) + 1, 100), 1)-1) Page_Number, COUNT(1) Rows_Per_Page &lt;br /&gt;
from &lt;br /&gt;
(select sys.fn_PhysLocFormatter (%%physloc%%) File_Page_Row from ?) a &lt;br /&gt;
group by substring(SUBSTRING(File_Page_Row, CHARINDEX(&#39;&#39;:&#39;&#39;, File_Page_Row, 1) + 1, 100), 1, charindex(&#39;&#39;:&#39;&#39;, SUBSTRING(File_Page_Row, CHARINDEX(&#39;&#39;:&#39;&#39;,  &lt;br /&gt;
&lt;br /&gt;
File_Page_Row, 1) + 1, 100), 1)-1)) b&#39; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
select * from #TEMP &lt;br /&gt;
order by 2 desc, 3 asc &lt;br /&gt;
&lt;br /&gt;
if OBJECT_ID(&#39;tempdb..#TEMP&#39;) is not null &lt;br /&gt;
drop table #TEMP  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Here is the result set: &lt;br /&gt;
&lt;br /&gt;
Table_Name Pages Rows_Per_Page Rows_in_Table Table_Size_MB &lt;br /&gt;
----------------------------------- ------------- ------------------------ ----------------------- --------------- &lt;br /&gt;
[dbo].[Order_Details] 51103 68.7081 3511190 399 &lt;br /&gt;
[dbo].[Order_Master] 3345 83.1629 278180 26 &lt;br /&gt;
[dbo].[Customer_Details] 2376 526.0118 1249804 19 &lt;br /&gt;
[dbo].[Customer_Master] 358 404.1006 144668 3 &lt;br /&gt;
... &lt;br /&gt;
&lt;br /&gt;
Please note that for the table, [dbo].[Order_Details] the row density(rows per page) has gone up from 35 to 68 now. At the same time, the table size itself has come down from 783MB to 399MB, a saving of almost 50% disc space. But that is not all. Let&#39;s try to compare the statistics before and after our changes to the Order_Details table by simply getting its rowcount using the following code: &lt;br /&gt;
&lt;br /&gt;
set statistics io on &lt;br /&gt;
set statistics time on &lt;br /&gt;
set statistics profile on &lt;br /&gt;
&lt;br /&gt;
select COUNT(1) from dbo.Order_Details with (nolock) &lt;br /&gt;
&lt;br /&gt;
set statistics io off &lt;br /&gt;
set statistics time off &lt;br /&gt;
set statistics profile off &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
Before changes: &lt;br /&gt;
Table &#39;Order_Details&#39;. Scan count 3, logical reads 79825, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. &lt;br /&gt;
&lt;br /&gt;
SQL Server Execution Times: CPU time = 500 ms, elapsed time = 247 ms. &lt;br /&gt;
&lt;br /&gt;
After changes: &lt;br /&gt;
&lt;br /&gt;
Table &#39;Order_Details&#39;. Scan count 3, logical reads 51193, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0. &lt;br /&gt;
&lt;br /&gt;
SQL Server Execution Times: CPU time = 483 ms, elapsed time = 241 ms. &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
You may note that the logical reads have come down by almost 36%. CPU time has reduced only marginally because there is a storage overhead due to compression. However reduction in logical reads enhances performance when this same table is joined with other tables. &lt;br /&gt;
&lt;br /&gt;
Conclusion: &lt;br /&gt;
&lt;br /&gt;
Required Design changes can be ascertained by our script. &lt;br /&gt;
Implemenation of optimal datatypes and appropriate compression level could reduce the disc space usage by around 50% and logical reads by around 36%. (Please note that these results could vary by tables depending on the datatypes and characteristics of the columns within the tables.) &lt;br /&gt;
Different studies show an increase in CPU utilization by 1% to 2% due to compression (not in our specific case though). So please do test and verify your improvements before implementation. &lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sqlfinerpoints.blogspot.com/feeds/4141659872559502781/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3544169305649860217/4141659872559502781' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/4141659872559502781'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/4141659872559502781'/><link rel='alternate' type='text/html' href='http://sqlfinerpoints.blogspot.com/2011/08/tablesizes.html' title='Table_Sizes'/><author><name>Suresh Kumar Maganti</name><uri>http://www.blogger.com/profile/16645888326754808866</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyuLq44DuP85JRUqfEDgaxcuih4-meamQYzD9Jk-DD_Z1Ul5oh9WXYLo1JF5VzNReeNSgttGQ5dWJgLibF42zcSqeHW9aWQUeDrNfd_ucgf5BQ7975u63-9Bd7VfUwkQ/s220/Suresh.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3544169305649860217.post-7284458568989363637</id><published>2010-06-25T20:02:00.001-05:00</published><updated>2010-09-09T06:49:19.978-05:00</updated><title type='text'>Database File Usage &amp; Growth</title><content type='html'>In this article, I would focus on finding empty claimable spaces within databases. Once found, the space can be claimed back using various methods which we would discuss in a later article.  &lt;br /&gt;&lt;br /&gt;The following script provides the details on a database, its data and log files, the respective filegroups which they logically belong to, the current fize size, the actual space used in the file and the growth factor for the file. I have used this code with the system stored procedure, sp_msforeachdb so as to gather this data for all databases on the server. It may be noted that the temporary table that is being used here, #temp_Data_Sizes is compressed. The next SELECT statement then displays those database files that have 50% or more free space in them. The TSQL code here is for Microsoft SQL Server 2008 and later. &lt;br /&gt;  &lt;br /&gt;if OBJECT_ID (&#39;tempdb..#temp_Data_Sizes&#39;) is not null&lt;br /&gt;drop table #temp_Data_Sizes&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;create table #temp_Data_Sizes&lt;br /&gt;(DatabaseName              varchar(255) not null&lt;br /&gt;,Fully_Qualified_File_Name varchar(512) not null &lt;br /&gt;,File_Type                 varchar(45) not null&lt;br /&gt;,FileGroup                 varchar(25) not null&lt;br /&gt;,Current_File_Size_GB      varchar(25) not null&lt;br /&gt;,Used_File_Space_GB        varchar(25) not null&lt;br /&gt;,Growth_Factor             varchar(25) not null&lt;br /&gt;,Constraint PK Primary Key clustered (DatabaseName, Fully_Qualified_File_Name) with (fillfactor = 50)&lt;br /&gt;)&lt;br /&gt;with (data_compression = Page)&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;set nocount on;&lt;br /&gt;insert into #temp_Data_Sizes (DatabaseName, Fully_Qualified_File_Name, File_Type, FileGroup, Current_File_Size_GB, Used_File_Space_GB, Growth_Factor)&lt;br /&gt;exec sp_msforeachdb &#39; use [?]&lt;br /&gt;select DB_NAME(database_id) DatabaseName&lt;br /&gt;      ,physical_name Fully_Qualified_File_Name&lt;br /&gt;      ,case [type]&lt;br /&gt;          when 0 then &#39;&#39;Data&#39;&#39;&lt;br /&gt;          when 1 then &#39;&#39;Log&#39;&#39;&lt;br /&gt;       end + &lt;br /&gt;       case fileproperty (name, &#39;&#39;IsLogFile&#39;&#39;)&lt;br /&gt;          when 0 then case fileproperty (name, &#39;&#39;IsPrimaryFile&#39;&#39;)&lt;br /&gt;                         when 0 then &#39;&#39; - Secondary Data File&#39;&#39;&lt;br /&gt;                         when 1 then &#39;&#39; - Primary Data File&#39;&#39;&lt;br /&gt;                      end&lt;br /&gt;          when 1 then &#39;&#39;&#39;&#39;&lt;br /&gt;       end                                                                File_Type&lt;br /&gt;      ,isnull(Filegroup_name (data_space_id), &#39;&#39;&#39;&#39;)                       FileGroup&lt;br /&gt;      ,convert(decimal(28,2), (size * 8192.0) / (1024.0 * 1024.0 * 1024.0)) Current_File_Size_GB&lt;br /&gt;      ,convert(decimal(28,2), (fileproperty (name, &#39;&#39;spaceused&#39;&#39;) * 8192.0) / (1024.0 * 1024.0 * 1024.0)) Used_File_Space_GB&lt;br /&gt;      ,case &lt;br /&gt;          when is_percent_growth = 0 then convert(varchar(20), convert(decimal(28, 4), (growth * 8192.0) / (1024.0 * 1024.0 * 1024.0))) + &#39;&#39; GB&#39;&#39;&lt;br /&gt;          when is_percent_growth = 1 then CONVERT(varchar(3), growth) + &#39;&#39;%&#39;&#39;&lt;br /&gt;       end  Growth_Factor&lt;br /&gt;from sys.master_files&lt;br /&gt;where database_id = DB_ID ()&#39;&lt;br /&gt;&lt;br /&gt;select * from #temp_Data_Sizes&lt;br /&gt;where 100.00 * ((CONVERT(Decimal(28, 4), Current_File_Size_GB) - CONVERT(Decimal(28, 4), Used_File_Space_GB)) / CONVERT(Decimal(28, 4), Current_File_Size_GB)) &gt;= 50.00&lt;br /&gt;and CONVERT(Decimal(28, 4), Current_File_Size_GB) &gt; 0.0000&lt;br /&gt;order by CONVERT(Decimal(28, 4), Current_File_Size_GB) desc&lt;br /&gt;&lt;br /&gt;set nocount off;&lt;br /&gt;&lt;br /&gt;if OBJECT_ID (&#39;tempdb..#temp_Data_Sizes&#39;) is not null&lt;br /&gt;drop table #temp_Data_Sizes&lt;br /&gt;go</content><link rel='replies' type='application/atom+xml' href='http://sqlfinerpoints.blogspot.com/feeds/7284458568989363637/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3544169305649860217/7284458568989363637' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/7284458568989363637'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/7284458568989363637'/><link rel='alternate' type='text/html' href='http://sqlfinerpoints.blogspot.com/2010/06/data-file-usage-growth-compression.html' title='Database File Usage &amp; Growth'/><author><name>Suresh Kumar Maganti</name><uri>http://www.blogger.com/profile/16645888326754808866</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyuLq44DuP85JRUqfEDgaxcuih4-meamQYzD9Jk-DD_Z1Ul5oh9WXYLo1JF5VzNReeNSgttGQ5dWJgLibF42zcSqeHW9aWQUeDrNfd_ucgf5BQ7975u63-9Bd7VfUwkQ/s220/Suresh.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3544169305649860217.post-3183181411825857260</id><published>2010-02-14T03:29:00.000-06:00</published><updated>2010-02-14T01:57:54.301-06:00</updated><title type='text'>Eliminating Duplicate Rows - The PARTITION BY clause</title><content type='html'>&lt;div&gt;Often we come across situations where duplicate rows exist in a table. And a need arises to eliminate such duplicates/triplcates, etc. Microsoft SQL Server 2005 &amp;amp; of course 2008 offer a simple option of eliminating such duplicates. In SQL server 2000, a program to eliminate duplicates used to be a bit long. It is a combination of derived tables and Row_Number() Over() funtion in 2005 &amp;amp; 2008 that makes this elimination process so simple. Let&#39;s look into it from an example:&lt;br /&gt;&lt;br /&gt;--create a table like then one below:&lt;br /&gt;&lt;br /&gt;create table Emp_Details (Emp_Name varchar(10), Company varchar(15), Join_Date datetime, Resigned_Date datetime)&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;--Insert some values into this table. Ensure that you insert atleast one duplicate and one triplicate for an easier understanding of the help that the Row_Number() Over() feature offers us:&lt;br /&gt;&lt;br /&gt;insert into Emp_Details (Emp_Name, Company, Join_Date, Resigned_Date)&lt;br /&gt;values (&#39;John&#39;, &#39;Software Inc&#39;, &#39;20060101&#39;, &#39;20061231&#39;)&lt;br /&gt;,(&#39;John&#39;, &#39;Software Inc&#39;, &#39;20060101&#39;, &#39;20061231&#39;)&lt;br /&gt;,(&#39;John&#39;, &#39;Software Inc&#39;, &#39;20060101&#39;, &#39;20061231&#39;)&lt;br /&gt;,(&#39;John&#39;, &#39;SuperSoft Inc&#39;, &#39;20070101&#39;, &#39;20071231&#39;)&lt;br /&gt;,(&#39;John&#39;, &#39;UltraSoft Inc&#39;, &#39;20070201&#39;, &#39;20080131&#39;)&lt;br /&gt;,(&#39;John&#39;, &#39;ImproSoft Inc&#39;, &#39;20080201&#39;, &#39;20081231&#39;)&lt;br /&gt;,(&#39;John&#39;, &#39;ImproSoft Inc&#39;, &#39;20080201&#39;, &#39;20081231&#39;)&lt;br /&gt;,(&#39;Mary&#39;, &#39;Software Inc&#39;, &#39;20060101&#39;, &#39;20081231&#39;)&lt;br /&gt;,(&#39;Mary&#39;, &#39;SuperSoft Inc&#39;, &#39;20090101&#39;, &#39;20090531&#39;)&lt;br /&gt;,(&#39;Mary&#39;, &#39;SuperSoft Inc&#39;, &#39;20090101&#39;, &#39;20090531&#39;)&lt;br /&gt;,(&#39;Mary&#39;, &#39;UltraSoft Inc&#39;, &#39;20090601&#39;, &#39;20100531&#39;)&lt;br /&gt;,(&#39;Mary&#39;, &#39;UltraSoft Inc&#39;, &#39;20090601&#39;, &#39;20100531&#39;)&lt;br /&gt;&lt;br /&gt;--Let&#39;s see the duplicates/triplicates, etc now using the old method:&lt;br /&gt;&lt;br /&gt;select *, COUNT(*) from Emp_Details&lt;br /&gt;group by Emp_Name, Company, Join_Date, Resigned_Date&lt;br /&gt;&lt;br /&gt;Emp_Name Company Join_Date Resigned_Date Occurrences&lt;br /&gt;--------- --------------- ----------------------- ----------------------- -----------&lt;br /&gt;John ImproSoft Inc 2008-02-01 00:00:00.000 2008-12-31 00:00:00.000 2&lt;br /&gt;John Software Inc 2006-01-01 00:00:00.000 2006-12-31 00:00:00.000 3&lt;br /&gt;John SuperSoft Inc 2007-01-01 00:00:00.000 2007-12-31 00:00:00.000 1&lt;br /&gt;John UltraSoft Inc 2007-02-01 00:00:00.000 2008-01-31 00:00:00.000 1&lt;br /&gt;Mary Software Inc 2006-01-01 00:00:00.000 2008-12-31 00:00:00.000 1&lt;br /&gt;Mary SuperSoft Inc 2009-01-01 00:00:00.000 2009-05-31 00:00:00.000 2&lt;br /&gt;Mary UltraSoft Inc 2009-06-01 00:00:00.000 2010-05-31 00:00:00.000 2&lt;br /&gt;&lt;br /&gt;--Although the table has twelve row, only seven are good ones. The remaining are duplicates or triplicates as we see from the Occurrences column above.&lt;br /&gt;&lt;br /&gt;--A better was of seeing the duplicates &amp;amp; more is the query below where Row_Number() Over() has been used with teh Partition By clause:&lt;br /&gt;&lt;br /&gt;select Emp_Name, Company, Join_Date, Resigned_Date, ROW_NUMBER() over (partition by Emp_Name, Company, Join_Date, Resigned_Date order by Emp_Name, Company, Join_Date, Resigned_Date) RowNumber from Emp_Details&lt;br /&gt;&lt;br /&gt;Emp_Name Company Join_Date Resigned_Date RowNumber&lt;br /&gt;---------- --------------- ----------------------- ----------------------- ----------&lt;br /&gt;John ImproSoft Inc 2008-02-01 00:00:00.000 2008-12-31 00:00:00.000 1&lt;br /&gt;John ImproSoft Inc 2008-02-01 00:00:00.000 2008-12-31 00:00:00.000 2&lt;br /&gt;John Software Inc 2006-01-01 00:00:00.000 2006-12-31 00:00:00.000 1&lt;br /&gt;John Software Inc 2006-01-01 00:00:00.000 2006-12-31 00:00:00.000 2&lt;br /&gt;John Software Inc 2006-01-01 00:00:00.000 2006-12-31 00:00:00.000 3&lt;br /&gt;John SuperSoft Inc 2007-01-01 00:00:00.000 2007-12-31 00:00:00.000 1&lt;br /&gt;John UltraSoft Inc 2007-02-01 00:00:00.000 2008-01-31 00:00:00.000 1&lt;br /&gt;Mary Software Inc 2006-01-01 00:00:00.000 2008-12-31 00:00:00.000 1&lt;br /&gt;Mary SuperSoft Inc 2009-01-01 00:00:00.000 2009-05-31 00:00:00.000 1&lt;br /&gt;Mary SuperSoft Inc 2009-01-01 00:00:00.000 2009-05-31 00:00:00.000 2&lt;br /&gt;Mary UltraSoft Inc 2009-06-01 00:00:00.000 2010-05-31 00:00:00.000 1&lt;br /&gt;Mary UltraSoft Inc 2009-06-01 00:00:00.000 2010-05-31 00:00:00.000 2&lt;br /&gt;&lt;br /&gt;--Wherever RowNumber is greater than 1, its is a duplicate row.&lt;br /&gt;&lt;br /&gt;--Now lets remove the duplicates/triplicates in one query in an efficient way using Row_Number() Over() using Partition By clause:&lt;br /&gt;&lt;br /&gt;delete from a&lt;br /&gt;from&lt;br /&gt;(select Emp_Name, Company, Join_Date, Resigned_Date, ROW_NUMBER() over (partition by Emp_Name, Company, Join_Date, Resigned_Date order by Emp_Name, Company, Join_Date, Resigned_Date) RowNumber from Emp_Details) a&lt;br /&gt;where a.RowNumber &gt; 1&lt;br /&gt;&lt;br /&gt;--Let&#39;s verify:&lt;br /&gt;&lt;br /&gt;select *, COUNT(*) Occurrences from Emp_Details&lt;br /&gt;group by Emp_Name, Company, Join_Date, Resigned_Date&lt;br /&gt;&lt;br /&gt;Emp_Name Company Join_Date Resigned_Date Occurrences&lt;br /&gt;--------- --------------- ----------------------- ----------------------- ----------&lt;br /&gt;John ImproSoft Inc 2008-02-01 00:00:00.000 2008-12-31 00:00:00.000 1&lt;br /&gt;John Software Inc 2006-01-01 00:00:00.000 2006-12-31 00:00:00.000 1&lt;br /&gt;John SuperSoft Inc 2007-01-01 00:00:00.000 2007-12-31 00:00:00.000 1&lt;br /&gt;John UltraSoft Inc 2007-02-01 00:00:00.000 2008-01-31 00:00:00.000 1&lt;br /&gt;Mary Software Inc 2006-01-01 00:00:00.000 2008-12-31 00:00:00.000 1&lt;br /&gt;Mary SuperSoft Inc 2009-01-01 00:00:00.000 2009-05-31 00:00:00.000 1&lt;br /&gt;Mary UltraSoft Inc 2009-06-01 00:00:00.000 2010-05-31 00:00:00.000 1&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The duplicates/triplcates are gone.&lt;br /&gt;&lt;br /&gt;A slightly more involved way of eliminating them would have been using Row_Number() Over() without the Partition By clause. Please remember to reload the table, Emp_Details before proceeding further. The code below shows the extra rows that need not exist because they are either duplicates or triplicates. Note that in this query, Partition By has not been used:&lt;br /&gt;&lt;br /&gt;select a.* from&lt;br /&gt;(select Emp_Name, Company, Join_Date, Resigned_Date, ROW_NUMBER() over (order by Emp_Name, Company, Join_Date, Resigned_Date) RowNumber from Emp_Details) a&lt;br /&gt;inner join&lt;br /&gt;(select Emp_Name, Company, Join_Date, Resigned_Date, ROW_NUMBER() over (order by Emp_Name, Company, Join_Date, Resigned_Date) RowNumber from Emp_Details) b&lt;br /&gt;on a.Emp_Name = b.Emp_Name&lt;br /&gt;and a.Company = b.Company&lt;br /&gt;and a.Join_Date = b.Join_Date&lt;br /&gt;and a.Resigned_Date = b.Resigned_Date&lt;br /&gt;where a.RowNumber = b.RowNumber + 1&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Emp_Name Company Join_Date Resigned_Date RowNumber&lt;br /&gt;---------- --------------- ----------------------- ----------------------- ----------&lt;br /&gt;John ImproSoft Inc 2008-02-01 00:00:00.000 2008-12-31 00:00:00.000 2&lt;br /&gt;John Software Inc 2006-01-01 00:00:00.000 2006-12-31 00:00:00.000 4&lt;br /&gt;John Software Inc 2006-01-01 00:00:00.000 2006-12-31 00:00:00.000 5&lt;br /&gt;Mary SuperSoft Inc 2009-01-01 00:00:00.000 2009-05-31 00:00:00.000 10&lt;br /&gt;Mary UltraSoft Inc 2009-06-01 00:00:00.000 2010-05-31 00:00:00.000 12&lt;br /&gt;&lt;br /&gt;--We can delete them as follows as well:&lt;br /&gt;&lt;br /&gt;delete from a&lt;br /&gt;from&lt;br /&gt;(select Emp_Name, Company, Join_Date, Resigned_Date, ROW_NUMBER() over (order by Emp_Name, Company, Join_Date, Resigned_Date) RowNumber from Emp_Details) a&lt;br /&gt;inner join&lt;br /&gt;(select Emp_Name, Company, Join_Date, Resigned_Date, ROW_NUMBER() over (order by Emp_Name, Company, Join_Date, Resigned_Date) RowNumber from Emp_Details) b&lt;br /&gt;on a.Emp_Name = b.Emp_Name&lt;br /&gt;and a.Company = b.Company&lt;br /&gt;and a.Join_Date = b.Join_Date&lt;br /&gt;and a.Resigned_Date = b.Resigned_Date&lt;br /&gt;where a.RowNumber = b.RowNumber + 1&lt;br /&gt;&lt;br /&gt;--Verify:&lt;br /&gt;&lt;br /&gt;select *, COUNT(*) Occurrences from Emp_Details&lt;br /&gt;group by Emp_Name, Company, Join_Date, Resigned_Date&lt;br /&gt;&lt;br /&gt;Emp_Name Company Join_Date Resigned_Date Occurrences&lt;br /&gt;--------- --------------- ----------------------- ----------------------- ----------&lt;br /&gt;John ImproSoft Inc 2008-02-01 00:00:00.000 2008-12-31 00:00:00.000 1&lt;br /&gt;John Software Inc 2006-01-01 00:00:00.000 2006-12-31 00:00:00.000 1&lt;br /&gt;John SuperSoft Inc 2007-01-01 00:00:00.000 2007-12-31 00:00:00.000 1&lt;br /&gt;John UltraSoft Inc 2007-02-01 00:00:00.000 2008-01-31 00:00:00.000 1&lt;br /&gt;Mary Software Inc 2006-01-01 00:00:00.000 2008-12-31 00:00:00.000 1&lt;br /&gt;Mary SuperSoft Inc 2009-01-01 00:00:00.000 2009-05-31 00:00:00.000 1&lt;br /&gt;Mary UltraSoft Inc 2009-06-01 00:00:00.000 2010-05-31 00:00:00.000 1&lt;br /&gt;&lt;br /&gt;--The duplicates/triplicates are gone.&lt;br /&gt;&lt;br /&gt;But note that difference in the two methods. Although Row_Number() Over() has been used in both the methods, Partition By has been used only in the first method.&lt;br /&gt;&lt;br /&gt;The partition By clause was more efficient in the following way:&lt;br /&gt;&lt;br /&gt;a.] Need to scan the Emp_Details table only once.&lt;br /&gt;b.] Need to have only one derived table instead of two.&lt;br /&gt;c.] No need to join two derived tables.&lt;br /&gt;d.] Less CPU &amp;amp; Memory utilization.&lt;br /&gt;&lt;br /&gt;Doing the same in Microsoft SQL Server 2000 and earlier would have been quite a lenghy code which I don&#39;t want to write now. However, I want to stress the contribution of the Partition By clause in keeping the code shorter , faster and less demanding on resources.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sqlfinerpoints.blogspot.com/feeds/3183181411825857260/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3544169305649860217/3183181411825857260' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/3183181411825857260'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/3183181411825857260'/><link rel='alternate' type='text/html' href='http://sqlfinerpoints.blogspot.com/2010/02/eliminating-duplicate-rows-partition-by.html' title='Eliminating Duplicate Rows - The PARTITION BY clause'/><author><name>Suresh Kumar Maganti</name><uri>http://www.blogger.com/profile/16645888326754808866</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyuLq44DuP85JRUqfEDgaxcuih4-meamQYzD9Jk-DD_Z1Ul5oh9WXYLo1JF5VzNReeNSgttGQ5dWJgLibF42zcSqeHW9aWQUeDrNfd_ucgf5BQ7975u63-9Bd7VfUwkQ/s220/Suresh.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3544169305649860217.post-3853384382140662515</id><published>2009-05-29T15:26:00.000-05:00</published><updated>2009-05-29T15:30:44.183-05:00</updated><title type='text'>The RECOMPILE Options</title><content type='html'>Recompile Options:&lt;br /&gt;              WITH RECOMPILE [at a Stored Procedure level only]&lt;br /&gt;              Option (Recompile) [at a TSQL statement level only]&lt;br /&gt;&lt;br /&gt;Their functionality:&lt;br /&gt;              When used in the code of a particular Stored procedure, Option 1compiles that SP everytime it is executed by any user. It can not be used at a individual statement level. Any existing plan is never reused even if the new plan is exactly the same as any pre-existing plan for that SP.  Once the SP is executed, the plan is discarded immediately. There is no caching of the execution plan for future reuse.&lt;br /&gt;&lt;br /&gt;              When used with a TSQL statement whether inside an SP or adhoc, Option 2 above creates a new execution plan for that particular statement.  Any pre- existing plan even if it is exactly the same as the new plan, is not used. The new plan is discarded imeediately after execution of the statement. &lt;br /&gt;&lt;br /&gt;General Plan Reuse in brief:&lt;br /&gt;           SP:&lt;br /&gt;              Assuming both of these options are not being used, an execution of an SP prompts a search for pre-existing plans (one serial plan and one parallel plan) in memory (plan cache). If found, it is reused. New input parameters in the current execution replace the previous input parameters from a previous execution plan in the execution context handle which is part of the overall execution plan. So usage of different input parameters doesn&#39;t cause a recompilation. Recompilation happens only when about 20% of the data in the tables being called from within the SP is found to have changed since the last time statistics was updated for those tables and its indexes. If say, you are accessing four tables in that SP and roughly 20% of the data for one table has been found to have changed since last statistics update, then that statement is recompiled. If all four tables have changed by about 20% since last statistics update, then the entire SP is recompiled. This plan reuse system works as long as objects are qualified with it owner and DatabaseName (for e.g. DBName.dbo.TableName instead of just TableName). Since we have auto-update statistics off, we have less recompilation of SP&#39;s to begin with. Use of a temporary table within an SP also causes recompilation of that statement. There are other factors also that affect plan reuse but that theory is too detailed. So I have just mentioned the most relevant points. Please note that while searching for an existing execution plan, SQL server searches by the ID of the SP being currently submitted for execution. So the search for existing execution plans is faster in case of SP&#39;s.&lt;br /&gt;&lt;br /&gt;         Adhoc TSQL:&lt;br /&gt;&lt;br /&gt;            Adhoc TSQL statements follow the same rules (statistics, etc) as an SP regarding plan reuse and some more. An adhoc SQL statement doesn&#39;t have an ID to begin with since it is not a database object. So existing plans are searched by the statement structure and the objects contained in that statement. So it takes a bit longer for SQL Engine to search for existing reusable plan in case of adhoc TSQL statements. Any existing execution plan in order to be reused is matched with the previous similar statement in the plan cache referencing the same object(s) (like tables). For e.g. the second statement below would reuse the plan created by the first statement. This is due to a database setting called &quot;Simple Parameterization&quot; which is ON by default (it treats the constant in the WHERE clause as a parameter and just switches the parameter value from 1 to 2 in the execution context of the existing plan created by the first statement).  The third statement would also reuse the existing plan. The fourth statement below (it has the exact same functionality as the first statement) will not reuse the existing plan because the structure of the physical query is not exactly the same as the first query (extra spaces before the keyword WHERE). So the fourth statement would be compiled afresh and will have a new execution plan.&lt;br /&gt;&lt;br /&gt;select a, b from db.user.abc where c = 1 &lt;br /&gt;&lt;br /&gt;select a, b from db.user.abc where c = 2&lt;br /&gt;&lt;br /&gt;select a, b from db.user.abc where c = @input_parameter&lt;br /&gt;&lt;br /&gt;select a, b from db.user.abc          where c = 1&lt;br /&gt;&lt;br /&gt;        Again, if the statement is a bit complex unlike the statements above, even Simple Parameterization won&#39;t help and the statement would always generate a fresh execution plan unless the database option called &quot;Forced Parameterization&quot; is set. That again is a different ballgame altogether. The rules for exact match of the complex adhoc TSQL statement still hold good though.  &lt;br /&gt;&lt;br /&gt;Explanation:&lt;br /&gt;                  SP:&lt;br /&gt;         It is for the reasons given in the theory above that RECOMPILE clause at an SP level should not be used unless we have just dropped and recreated/changed indexes, updated statistics, etc or if it is a new SP in which case it would be automatically generate a fresh plan anyway. Another drawback is that this SP-level RECOMPILE clause would erase any previous execution plan and create a new plan on each execution which is unnecessary and expensive.&lt;br /&gt;&lt;br /&gt;                  AdHoc TSQL:&lt;br /&gt;         Using the RECOMPILE option would discard any reusable plans. But most of the existing plans do not get reused in case of adHoc TSQL due to reasons mentioned above. So plan cache ends up holding those un-reusable plans unnecessarily till the time comes when they need to be really discarded to make space for new plans in the procedure cache. Usage of the RECOMPILE option would help reduce the utilization of plan cache in such cases. But again the choice is between RECOMPILE option in adhoc TSQL or, the basic rules to be followed (same structure, same columns, same tables, fully qualified names, etc) for plan reuse. Changing an SP is simple. Changing an adhoc TSQL statement means making changes to the application as well that generates the adhoc TSQL code and recompiling the affected application dll&#39;s in most cases. So SP is always the best choice not only for this reason but for scores of other reasons as well whcih we can discuss in a separate thread.&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;                 Note: I would like to stress the point. that the Data Access Layer (DAL) would never pose a limitation on the ability to use SP&#39;s. If you can call even just one SP through DAL, you can call any number and any type of SP&#39;s from the same DAL whether the SP&#39;s use static or dynamic TSQL code inside them. DAL can not be blamed under any circumstance for excess usage of adhoc TSQL code in the application. &lt;br /&gt;  &lt;br /&gt;A slight clarification:&lt;br /&gt;                      Once these two options are used (SP level and adhoc statement level), there would be no scope for reusing any &quot;good&quot; plans. This is because plans won&#39;t exist after an execution is over. They would be discarded immediately after the execution. &lt;br /&gt;&lt;br /&gt;Caution:&lt;br /&gt;                      Developers often tend to use OPTION (RECOMPILE) an an excuse for introducing more and more adhoc TSQL and then tend to eliminate SP&#39;s altogether. And then they reach a stage where plan cache gets filled up to the brim again. This defeats the very purpose of Option (recompile) for adhoc TSQL code. Please note that every recompile issues locks on the underlying tables. And every new plan generation process takes an additional time and overhead (CPU time and statistics analysis). Adhoc TSQL reflects/implies inappropriate application design. It also increases network roundtrips and length/duration of a business logic implementation and execution.  OPTION (RECOMPILE) is to be only used as a temporary measure while a process for converting adhoc TSQL code to Stored Procedures is underway.&lt;br /&gt;&lt;br /&gt;Suggestion/Conclusion:&lt;br /&gt;                       Based on the explanation above, using WITH RECOMPILE clause for SP&#39;s is not suggested. Using OPTION (RECOMPILE) at a statement level can be tried for adhoc TSQL. The effort in changing this for each adhoc TSQL statement in the application can be of course  more productive instead if conversion from adhoc TSQL to Stored Procedures is taken up. This is because SP&#39;s are generally much superior to adhoc TSQL statements in all respects.</content><link rel='replies' type='application/atom+xml' href='http://sqlfinerpoints.blogspot.com/feeds/3853384382140662515/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3544169305649860217/3853384382140662515' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/3853384382140662515'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/3853384382140662515'/><link rel='alternate' type='text/html' href='http://sqlfinerpoints.blogspot.com/2009/05/recompile-options.html' title='The RECOMPILE Options'/><author><name>Suresh Kumar Maganti</name><uri>http://www.blogger.com/profile/16645888326754808866</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyuLq44DuP85JRUqfEDgaxcuih4-meamQYzD9Jk-DD_Z1Ul5oh9WXYLo1JF5VzNReeNSgttGQ5dWJgLibF42zcSqeHW9aWQUeDrNfd_ucgf5BQ7975u63-9Bd7VfUwkQ/s220/Suresh.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3544169305649860217.post-5576856323141548762</id><published>2008-11-13T10:13:00.001-06:00</published><updated>2009-05-15T16:46:52.618-05:00</updated><title type='text'>Database Mirroring in Microsoft SQL Server 2005: The finer points</title><content type='html'>I have often wondered why people still use features like log-shipping, transactional replication, etc for their disastery recovery solutions rather than use simpler options. A software DR solution should be easy to implement, be effective and safe as well while ensuring near zero or zero data loss by itself.&lt;br /&gt;&lt;br /&gt;Some drawbacks of log-shipping are following:&lt;br /&gt;a.] You need frequent log backups of the production database. &lt;br /&gt;b.] The recoverability of your DR database depends largely on the frequency of transaction log backups and the time taken to copy such backups from production to DR and the time taken to restore these backups.&lt;br /&gt;c.] The log-shipped database remains dormant except having production transaction log backups getting restored on to it.&lt;br /&gt;d.] From an application end-user perspective, the DR database is useful only after a disaster, not anytime else.&lt;br /&gt;e.]The entire transaction log backup is restored at DR and then Uncommitted transactions are rolled back (which is fine). It is not that only committed transactions go from production to DR. So there is unnecessary avoidable network traffic. &lt;br /&gt;f.] The physical transaction log backup file has to be copied to the DR causing avoidable network traffic and causing pressure on the DPC [NIC] CPU&#39;s.&lt;br /&gt;h.] With log-shipping, there is always a scope for data loss in case of disasters. And companies do compromise on this front which is not needed.&lt;br /&gt;i.] Too many jobs are in place to run log-shipping (to backup logs at source, copy them to DR and restore them at DR).&lt;br /&gt;j.] Too many time-consuming processes involved when failure occurs especially if the database involved in huge (VLDB).&lt;br /&gt;k.] Failover is always manual.&lt;br /&gt;l.] Even if the data loss is of 10 minutes, the actual downtime would be atleast an hour at a minimum. This directly means loss of revenue for that period.&lt;br /&gt;m.] Manual user redirection in case of failure of the main server/database is needed.&lt;br /&gt;n.] Restored transaction log backup files on DR have to be deleted. Continuous copying and deleting of files make the DR look like a file server and of couse, causes the discs over there to get fragmented much faster.&lt;br /&gt;&lt;br /&gt;Drawbacks of Replication:&lt;br /&gt;a.] Complex process to set up involving snapshot replication, etc to begin with.&lt;br /&gt;b.] In certain types of replication, an extra column (GUID) is created at the end of each replicated table causing an increase in disc space usage.&lt;br /&gt;c.] Complex conflict resolution rules have to be put in place during setup.&lt;br /&gt;d.] A failure in replication could actually mean reinitialization of subscriptions most of the time. That&#39;s a time-consuming process and means that DR would not be technically existing during that period if replication is being used for DR purposes.&lt;br /&gt;e.] Two things to manage instead of one. You need to manage Production database as well as the replication for failures, conflicts, maintenance, etc.&lt;br /&gt;f.] Needs some skill to manage replication and its own set of problems.&lt;br /&gt;g.] Of course there are certain merits in replication like updatable subscriptions, merge replication, bi-directional transactional replication, availability of the subscriber database for reads/writes, etc. But the drawbacks above make replication a more tediuos and time-consuming operation/process than necessary.&lt;br /&gt;h.] Manual failover and user redirection in case of failure of the main server/database.&lt;br /&gt;&lt;br /&gt;What is the easiest alternative? Database Mirroring is. Why? Because of the following reasons:&lt;br /&gt;&lt;br /&gt;a.] Easy to set up, easier still if log-shipping is already in place and needs to be replaced with database mirroring.&lt;br /&gt;b.] Easy to monitor. Inbuilt Microsoft Database Mirroring Monitor in SQL Server Management Studio.&lt;br /&gt;c.] Manual or automatic failover.&lt;br /&gt;d.] Separate counters in Windows Performance Monitor for database mirroring.&lt;br /&gt;e.] Dynamic Management Views for up to the point-in-time information on the principal and mirror databases, lag time between mirror and principal and so on.&lt;br /&gt;f.] Minimal or no data loss even in asynchronous mode.&lt;br /&gt;g.] Guaranteed zero-data-loss in synchronous mode.&lt;br /&gt;h.] Possibility of automatic user redirection in case of failures.&lt;br /&gt;i.] Hardly requires maintenance. Set it up and forget it.&lt;br /&gt;j.] Easy to failover. It&#39;s a one line code to failover and failback.&lt;br /&gt;k.] Makes other maintenance jobs like Windows patching and SQL Server patching easy with no downtime.&lt;br /&gt;l.] Possibility to use mirror for reporting application using the snapshot feature.&lt;br /&gt;m.] Requires least amount of maintenance of all the DR options. &lt;br /&gt;n.] Easy to use.&lt;br /&gt;o.] Easy to change between different modes of mirroring (synchronous and asynchronous).&lt;br /&gt;p.] Only committed transactions flow to mirror. So less network banmndwidth consumed.&lt;br /&gt;q.] No file transfers involved.&lt;br /&gt;r.] No conflict resolutions needed.&lt;br /&gt;s.] Miniam disc fragmentation as no copying and deleting fo files is involved.&lt;br /&gt;t.] Maintenance actions on principal database automatically move over to mirror. I am talking of operations like shrinking log/data files, etc.&lt;br /&gt;&lt;br /&gt;I am not going into the set up of mirroring as it is simple, easy to use, and easy to code. In roughly 10-15 lines of TSQL code, mirroring can be established in the desired mode.&lt;br /&gt;&lt;br /&gt;Some points which I would consider important with respect to mirroring are as follows:&lt;br /&gt;&lt;br /&gt;Do not install mirror database in a different node of the same cluster where principal resides. This would be a disastrous approach in an active-active cluster when one node has to failover to the other. Always keep you mirror database on a separate cluster or machine if you don&#39;t have a cluster.&lt;br /&gt;&lt;br /&gt;Do not keep the transaction log file size unnecessarily large. If say your peak transaction log file consumption is 5GB for example, it would be good to have the actual file size within 10GB to 20GB. Don&#39;t keep it at say 50GB. That slows down writes to the transaction log file on principal and hence also on mirror. The point is don&#39;t keep too much empty space in the transaction log file on principal. Expand it as needed.&lt;br /&gt;&lt;br /&gt;Don&#39;t create full or differential database backups on principal when you are in the process of restoring logs on to the mirror database to bring it upto speed with the principal database. If by mistake that does happen, stop any scheduled transaction log backups on principal and let the mirror sync up wth principal on its own although it would take a few minutes for the mirror to catch up and get synchronized with the principal this way. While the catching-up process is going on, it would be  better to stop transaction log backups on the principal as well. That is okay. Patience helps in this avoidable scenario. After the synchronization is established, turn on the jobs for transaction log backups, full backups and differentail backups back on the principal. Please do consider this point during the setup process for database mirroring just in this avoidable case where somebody performs a full or differential database backup of the principal database by mistake while you are setting up database mirroring.&lt;br /&gt;&lt;br /&gt;Start with asynchronous mode to begin with. Observe for a few days how the transactions are doing, whether there is any piling-up of log on mirror and how often the entities are not in sync per day. If you find synchonization 90% of the time, you can consider moving on to synchronous mode if need be.&lt;br /&gt;&lt;br /&gt;I would suggest using TSQL code instead of the GUI for the setup. There is a lot more flexibility in TSQL coding than in the GUI. And it is a lot easier to debug/rollback/comeback/startover with TSQL during the setup process if something goes really bad due to circumstances beyond your control. Mirroring is so straightforward that it would not go wrong if you are following the 10-11 simple steps to setting up mirroring. The GUI is good for monitoring though and for setting up alerts.&lt;br /&gt;&lt;br /&gt;Do not use synchronous mode if the CPU utilization is more than 50% on your principal server averaged for a given hour. This can potentially cause unwanted automatic failovers. In such a case, either set right you application / database code/architecture/design/indexes, etc first or use asynchronous mode of mirroring only.&lt;br /&gt;&lt;br /&gt;suresh_m_kumar@hotmail.com</content><link rel='replies' type='application/atom+xml' href='http://sqlfinerpoints.blogspot.com/feeds/5576856323141548762/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3544169305649860217/5576856323141548762' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/5576856323141548762'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/5576856323141548762'/><link rel='alternate' type='text/html' href='http://sqlfinerpoints.blogspot.com/2008/11/database-mirroring-in-microsoft-sql.html' title='Database Mirroring in Microsoft SQL Server 2005: The finer points'/><author><name>Suresh Kumar Maganti</name><uri>http://www.blogger.com/profile/16645888326754808866</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyuLq44DuP85JRUqfEDgaxcuih4-meamQYzD9Jk-DD_Z1Ul5oh9WXYLo1JF5VzNReeNSgttGQ5dWJgLibF42zcSqeHW9aWQUeDrNfd_ucgf5BQ7975u63-9Bd7VfUwkQ/s220/Suresh.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3544169305649860217.post-8182116612076608192</id><published>2008-09-04T10:12:00.000-05:00</published><updated>2008-09-08T17:37:33.803-05:00</updated><title type='text'>A Tricky Scenario with T-SQL</title><content type='html'>The good part of T-SQL is that unlike most other areas of databases in general, one needs to think in order to write a good piece of valid code. Thinking not just to establish functionality but also to ensure performance. Now there are hundreds, even thousands of scenarios where the code can be better than what it has been already written as just to establish functionality. Let us take up one such scenario.&lt;br /&gt;&lt;br /&gt;There is a supplier of Dell, HP, and IBM servers. Servers are shipped from the warehouse to three locations. Each location is represented by a location code. Each machine to be shipped to the warehouse has a ten digit Machine_X_Code that has leading zeroes for data that has less than ten digits in it. All this is represented roughly by the Main_Table below:&lt;br /&gt;&lt;br /&gt;create table Main_Table&lt;br /&gt;(Location_Code smallint not null&lt;br /&gt;,Machine_Brand varchar(25) not null&lt;br /&gt;,Machine_X_Code varchar(10) not null&lt;br /&gt;,Number_Of_CPUs tinyint not null&lt;br /&gt;,Procurement_Date Datetime not null default GETDATE()&lt;br /&gt;constraint PK_LC_MC Primary Key Clustered (Location_Code, Machine_X_Code, Procurement_Date) with fillfactor = 70)&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;There is another table called Code_Generation_Table. This table determines what the Machine_X_Code for the next procured machine would be. For a given combination of store location code and a machine brand in this Code_Generation_Table, the Machine_X_Code for the Main_Table needs to be generated. The new machine_X_Code generated should be within the Start_code and End_Code range for that Location_Code and Machine_Brand. The table is as follows:&lt;br /&gt;&lt;br /&gt;create table Code_Generation_Table&lt;br /&gt;(Location_Code smallint not null&lt;br /&gt;,Machine_Brand varchar(25) not null&lt;br /&gt;,Start_Code int not null&lt;br /&gt;,Current_Code int not null&lt;br /&gt;,End_Code int not null&lt;br /&gt;,Increment_Value tinyint not null&lt;br /&gt;constraint PK_LC_MB Primary Key clustered (Location_Code, Machine_Brand) with fillfactor = 90)&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;Lets populate these two tables now with relevant data:&lt;br /&gt;&lt;br /&gt;insert into Code_Generation_Table&lt;br /&gt;select 1, &#39;DELL&#39;, 1001, 1001, 1005, 1&lt;br /&gt;union all&lt;br /&gt;select 2, &#39;DELL&#39;, 4001, 4001, 4005, 1&lt;br /&gt;union all&lt;br /&gt;select 3, &#39;DELL&#39;, 7001, 7001, 7005, 1&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;HP&#39;, 2001, 2003, 2005, 1&lt;br /&gt;union all&lt;br /&gt;select 2, &#39;HP&#39;, 5001, 5003, 5005, 1&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;IBM&#39;, 3001, 3001, 3005, 1&lt;br /&gt;union all&lt;br /&gt;select 2, &#39;IBM&#39;, 8001, 8003, 8005, 1&lt;br /&gt;union all&lt;br /&gt;select 3, &#39;IBM&#39;, 6001, 6004, 6005, 1&lt;br /&gt;union all&lt;br /&gt;select 3, &#39;SUN&#39;, 6011, 6011, 6015, 1&lt;br /&gt;&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;insert into Main_Table (Location_Code, Machine_Brand, Machine_X_Code, Number_Of_CPUs)&lt;br /&gt;select 1, &#39;DELL&#39;, &#39;0000001001&#39;, 2&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;DELL&#39;, &#39;0000001002&#39;, 4&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;DELL&#39;, &#39;0000001003&#39;, 8&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;DELL&#39;, &#39;0000001004&#39;, 16&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;HP&#39;, &#39;0000002002&#39;, 2&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;HP&#39;, &#39;0000002003&#39;, 4&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;HP&#39;, &#39;0000002004&#39;, 8&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;HP&#39;, &#39;0000002005&#39;, 32&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;IBM&#39;, &#39;0000003003&#39;, 2&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;IBM&#39;, &#39;0000003004&#39;, 8&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;IBM&#39;, &#39;0000003005&#39;, 32&lt;br /&gt;union all&lt;br /&gt;select 2, &#39;DELL&#39;, &#39;0000004001&#39;, 2&lt;br /&gt;union all&lt;br /&gt;select 2, &#39;DELL&#39;, &#39;0000004002&#39;, 4&lt;br /&gt;union all&lt;br /&gt;select 2, &#39;DELL&#39;, &#39;0000004003&#39;, 8&lt;br /&gt;union all&lt;br /&gt;select 3, &#39;DELL&#39;, &#39;0000007004&#39;, 16&lt;br /&gt;union all&lt;br /&gt;select 2, &#39;HP&#39;, &#39;0000005002&#39;, 2&lt;br /&gt;union all&lt;br /&gt;select 2, &#39;HP&#39;, &#39;0000005003&#39;, 4&lt;br /&gt;union all&lt;br /&gt;select 2, &#39;HP&#39;, &#39;0000005004&#39;, 8&lt;br /&gt;union all&lt;br /&gt;select 2, &#39;HP&#39;, &#39;0000005005&#39;, 32&lt;br /&gt;union all&lt;br /&gt;select 3, &#39;IBM&#39;, &#39;0000006001&#39;, 2&lt;br /&gt;union all&lt;br /&gt;select 3, &#39;IBM&#39;, &#39;0000006003&#39;, 2&lt;br /&gt;union all&lt;br /&gt;select 3, &#39;IBM&#39;, &#39;0000006004&#39;, 8&lt;br /&gt;union all&lt;br /&gt;select 3, &#39;IBM&#39;, &#39;0000006005&#39;, 32&lt;br /&gt;Go&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here are the conditions that must be met for generating the Machine_X_Code:&lt;br /&gt;1. Start from Current_Code value in the table, Code_Generation_Table.&lt;br /&gt;2. Look for any values between Current_Code and End_Code that don&#39;t already exist in &lt;br /&gt;   the Machine_X_Code column of the Main_Table for the given Location_Code and &lt;br /&gt;   Machine_Brand. That becomes the new Machine_X_Code.&lt;br /&gt;3. Given a scenario like the one below in the table Code_Generation_Table, the following conditions must also be met:&lt;br /&gt;Location_Code, Machine_Brand, Start_Code, Current_Code, End_Code, Increment_Value&lt;br /&gt;1              DELL           1001         1003         1005       1&lt;br /&gt;&lt;br /&gt;Scenario 1: &lt;br /&gt; &lt;br /&gt;If there is no record in Main_Table for that Machine Brand, then new Machine_X_Code needs to be 0000001003.&lt;br /&gt; &lt;br /&gt;Scenario 2:&lt;br /&gt; &lt;br /&gt;If Main_Table has records with Machine_X_Codes 0000001003, 0000001004, 0000001005 then the new Machine_X_Code will be 0000001001.&lt;br /&gt;&lt;br /&gt;Scenario 3:&lt;br /&gt; &lt;br /&gt;If Main_Table has all records with Machine_X_Code between and including 0000001001 to 0000001005 except 0000001002, then the next newly generated Machine_X_Code should be 0000001002.&lt;br /&gt;&lt;br /&gt;The requirements having been made clear, let us see how a developer approached this problem. He chose to create a stored procedure (SP), usp_Generate_Machine_X_Code and a user-defined function (UDF), F_Validate_Machine_Code whihc is invoked from within the SP. The SP gives out the new Machine_X_Code. The unnctionality of the code below is explained in more granular detail just before the object definitions below. The code for both is given below.&lt;br /&gt;&lt;br /&gt;Code for the user-defined function (UDF), F_Validate_Machine_Code:&lt;br /&gt;&lt;br /&gt;--UDF&lt;br /&gt;--This function checks that thet newly generated Machine_X_Code lies between &lt;br /&gt;--the Start_Code and the End_Code. If it does, then the newly generated Machine_X_Code&lt;br /&gt;--represented by the variable @l_Temp_Machine_X_Value is valid. If by chance, the newly &lt;br /&gt;--generated Machine_X_Code lies outside the range of Start_Code and End_Code, then &lt;br /&gt;--the new machine_x_code is initialized to the Start_Code generated Machine_X_Code&lt;br /&gt;--reprsented by the variable, @l_Start_Machine_X_Value. The newely generated &lt;br /&gt;--machine_X_code, the Start_Code and the ENd_Code are inputs to this function.&lt;br /&gt;--A supposedly valid Machine_X_Code is returned by this function. The output however &lt;br /&gt;--is not guarenteed to be valid unless it is re-checked for validity in the SP, &lt;br /&gt;--usp_Generate_Machine_X_Code. If it is found to be invalid, then this &lt;br /&gt;--user-defined function is invoked again and again as need be.&lt;br /&gt;&lt;br /&gt;CREATE Function DBO.F_Validate_Machine_Code&lt;br /&gt;(@l_Temp_Machine_X_Value Int,  &lt;br /&gt; @l_Start_Machine_X_Value int,  &lt;br /&gt; @l_End_Machine_X_Value int  &lt;br /&gt;) Returns Integer  &lt;br /&gt;AS  &lt;br /&gt;Begin  &lt;br /&gt;  --  This function validates the whether the newly generated Machine_X_Code lies within the Range of Start_Code and End_Code.&lt;br /&gt;  If (@l_Temp_Machine_X_Value &gt; @l_Start_Machine_X_Value) &lt;br /&gt;  or (@l_Temp_Machine_X_Value &lt; @l_End_Machine_X_Value)  &lt;br /&gt;    Set @l_Temp_Machine_X_Value = @l_Temp_Machine_X_Value&lt;br /&gt;  &lt;br /&gt;  If (@l_Temp_Machine_X_Value &lt; @l_Start_Machine_X_Value) &lt;br /&gt;  or (@l_Temp_Machine_X_Value &gt; @l_End_Machine_X_Value)  &lt;br /&gt;    Set @l_Temp_Machine_X_Value = @l_Start_Machine_X_Value&lt;br /&gt;&lt;br /&gt;   Return @l_Temp_Machine_X_Value&lt;br /&gt;End  &lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Code for the stored procedure (SP), usp_Generate_Machine_X_Code:&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;       &lt;br /&gt;--This SP&#39;s purpose is to generate a new Machine_X_Code. It generates a new &lt;br /&gt;--Machine_X_Code from the Current_Code column of the table, Code_Generation_Table &lt;br /&gt;--while checking that the newly generated Machine_X_Code does not already exist&lt;br /&gt;--in the Machine_X_Code column of the table Main_Table. In addition it call the&lt;br /&gt;--user-defined function  F_Validate_Machine_Code to check that a newly generated &lt;br /&gt;--Machine_X_Code lies between Start_Code and End_Code in the table, Code_Generation_Table.&lt;br /&gt;--If the newly generated machin_x_code falls outside that range, then the machine_x_code&lt;br /&gt;--is reset to Start_Code. In this whole process it keeps updating the Current_Code column&lt;br /&gt;--of the table, Code_Generation_Table with the newly generated Machine_X_Code before its &lt;br /&gt;--validation through the function, F_Validate_Machine_Code. It also updates the the  &lt;br /&gt;--Current_Code column of the table, Code_Generation_Table with the next possible&lt;br /&gt;--Machine_X_Code value. Looping occurs till a valid Machine_X_Code is found. So,&lt;br /&gt;--effectively there would be as many iterations of the loop as the number of attempts to &lt;br /&gt;--find one good  Machine_X_Number.&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;Create Procedure usp_Generate_Machine_X_Code&lt;br /&gt;@i_Location_Code Smallint,&lt;br /&gt;@i_Machine_Brand VarChar(10),&lt;br /&gt;@o_Machine_X_Code Integer OutPut,&lt;br /&gt;@o_Return_Value Smallint OutPut&lt;br /&gt;As&lt;br /&gt;Begin&lt;br /&gt;  Set nocount on&lt;br /&gt;  Set @o_Return_Value = 0&lt;br /&gt;&lt;br /&gt;  Declare @l_Increment_Value Smallint,&lt;br /&gt;          @l_Temp_Machine_X_Value Integer,&lt;br /&gt;          @l_Next_Machine_X_Value Integer,&lt;br /&gt;          @l_Start_Machine_X_Value Int,&lt;br /&gt;          @l_End_Machine_X_Value Integer,&lt;br /&gt;          @l_First_Machine_X_Value_Taken Integer,&lt;br /&gt;          @l_Affected_Rows Smallint,&lt;br /&gt;          @l_Execution_String VarChar(2000)&lt;br /&gt;&lt;br /&gt;  Select @l_Temp_Machine_X_Value  = Current_Code&lt;br /&gt;        ,@l_Increment_Value       = Increment_Value&lt;br /&gt;        ,@l_Start_Machine_X_Value = Start_Code&lt;br /&gt;        ,@l_End_Machine_X_Value   = End_Code&lt;br /&gt;  From DBO.Code_Generation_Table With (NoLock) &lt;br /&gt;  Where Location_Code = @i_Location_Code &lt;br /&gt;  And Machine_Brand   = @i_Machine_Brand&lt;br /&gt;  &lt;br /&gt;  Set @l_Temp_Machine_X_Value = DBO.F_Validate_Machine_Code (@l_Temp_Machine_X_Value, @l_Start_Machine_X_Value, @l_End_Machine_X_Value)&lt;br /&gt;&lt;br /&gt;  Set @l_First_Machine_X_Value_Taken = @l_Temp_Machine_X_Value&lt;br /&gt;&lt;br /&gt;  Set @l_Affected_Rows = 0&lt;br /&gt;&lt;br /&gt;  While @l_Affected_Rows = 0&lt;br /&gt;  Begin&lt;br /&gt;&lt;br /&gt;    Set @l_Next_Machine_X_Value = @l_Temp_Machine_X_Value + @l_Increment_Value &lt;br /&gt;&lt;br /&gt;    Set @l_Next_Machine_X_Value = DBO.F_Validate_Machine_Code (@l_Next_Machine_X_Value, @l_Start_Machine_X_Value, @l_End_Machine_X_Value)&lt;br /&gt;&lt;br /&gt;    Set @l_Execution_String = &#39;Update A  Set A.Current_Code = &#39; + Cast(@l_Next_Machine_X_Value as VarChar) +&lt;br /&gt;    &#39; From Dbo.Code_Generation_Table A &#39;+   &lt;br /&gt;    + &#39; Where Location_Code = &#39; + Cast(@i_Location_Code AS VarChar)  + &#39; And Machine_Brand = &#39; + &#39;&#39;&#39;&#39;+ @i_Machine_Brand +  &#39;&#39;&#39;&#39; +&lt;br /&gt;    &#39; And Not Exists (Select &#39; + &#39;&#39;&#39;&#39; &lt;br /&gt;    + &#39;x&#39; + &#39;&#39;&#39;&#39; + &#39; From  Main_Table &#39; +&lt;br /&gt;    &#39;Where Location_Code = &#39; + Cast(@i_Location_Code AS VarChar) +&lt;br /&gt;    &#39; AND Machine_Brand = &#39; + &#39;&#39;&#39;&#39; + @i_Machine_Brand +  &#39;&#39;&#39;&#39; +  &lt;br /&gt;    &#39; And Machine_X_Code = &#39; + &#39;&#39;&#39;&#39; + Replicate(&#39;0&#39;,10 - Len(@l_Temp_Machine_X_Value)) +  Cast(@l_Temp_Machine_X_Value as Varchar(10)) + &#39;&#39;&#39;&#39; + &#39;)&#39;&lt;br /&gt;&lt;br /&gt;    Exec (@l_Execution_String) &lt;br /&gt;&lt;br /&gt;    Set @l_Affected_Rows = @@Rowcount&lt;br /&gt;&lt;br /&gt;    If @l_Affected_Rows = 1&lt;br /&gt;    Begin&lt;br /&gt;       Select @o_Machine_X_Code = @l_Temp_Machine_X_Value, @o_Return_Value = 1&lt;br /&gt;       Break&lt;br /&gt;    End&lt;br /&gt;&lt;br /&gt;    Set @l_Temp_Machine_X_Value = @l_Temp_Machine_X_Value + @l_Increment_Value&lt;br /&gt;&lt;br /&gt;    Set @l_Temp_Machine_X_Value = DBO.F_Validate_Machine_Code (@l_Temp_Machine_X_Value, @l_Start_Machine_X_Value, @l_End_Machine_X_Value)&lt;br /&gt;&lt;br /&gt;    If @l_First_Machine_X_Value_Taken = @l_Temp_Machine_X_Value &lt;br /&gt;    Begin&lt;br /&gt;      -- For the current day all the Machine X Codes have been used up.&lt;br /&gt;      Select @l_Affected_Rows = 2, @o_Return_Value = -1 &lt;br /&gt;      Break&lt;br /&gt;    End&lt;br /&gt;&lt;br /&gt;  End&lt;br /&gt;  Set nocount off&lt;br /&gt;End&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Before we proceed further, please familiarize yourself with the logic of the SP and UDF, and the data in the two tables so that we can try to execute his logic. Let us try to generate a new Machine_X_Code for the Location_Code = 1 and Brand_Name = &#39;Dell&#39;.&lt;br /&gt;&lt;br /&gt;TEST 1:&lt;br /&gt;Execute the following:&lt;br /&gt;&lt;br /&gt;select Location_Code, Machine_Brand, Machine_X_Code, Number_Of_CPUs from Main_Table &lt;br /&gt;where Location_Code = 1&lt;br /&gt;and  Machine_Brand = &#39;Dell&#39;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The result set is as follows:&lt;br /&gt;&lt;br /&gt;Location_Code Machine_Brand             Machine_X_Code Number_Of_CPUs&lt;br /&gt;------------- ------------------------- -------------- --------------&lt;br /&gt;1             DELL                      0000001001     2&lt;br /&gt;1             DELL                      0000001002     4&lt;br /&gt;1             DELL                      0000001003     8&lt;br /&gt;1             DELL                      0000001004     16&lt;br /&gt;&lt;br /&gt;So the next generated machine_x_code to has to be 1005. Remember zeroes are suffixed later and that is not so important right now. What is important is generating the new Machine_X_Code that meets all the requirements above. &lt;br /&gt;&lt;br /&gt;Now run the following code:&lt;br /&gt;&lt;br /&gt;select * from Code_Generation_Table&lt;br /&gt; &lt;br /&gt;The result set is as follows:&lt;br /&gt;Location_Code Machine_Brand Start_Code  Current_Code End_Code    Increment_Value&lt;br /&gt;------------- ------------  ----------- ------------ ----------- ---------------&lt;br /&gt;1             DELL          1001        1001         1005        1&lt;br /&gt;1             HP            2001        2003         2005        1&lt;br /&gt;1             IBM           3001        3001         3005        1&lt;br /&gt;2             DELL          4001        4001         4005        1&lt;br /&gt;2             HP            5001        5003         5005        1&lt;br /&gt;2             IBM           8001        8003         8005        1&lt;br /&gt;3             DELL          7001        7001         7005        1&lt;br /&gt;3             IBM           6001        6004         6005        1&lt;br /&gt;3             SUN           6011        6011         6015        1&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Since the codes (1001, 1002, 1003 and 1004) already exists in the Main_Table for Location_Code =1 and Machine_Brand = &#39;Dell&#39;, the next one has to be 1005 as it exists in the Code_Generation_Table between Start_Code and End_Code. Let&#39;s see if the SP generates this machine code. Execute the following code to execute the SP:&lt;br /&gt;&lt;br /&gt;declare @i_Location_Code Smallint&lt;br /&gt;       ,@i_Machine_Brand VarChar(10)&lt;br /&gt;       ,@o_Machine_X_Code Integer &lt;br /&gt;       ,@o_Return_Value Smallint &lt;br /&gt;&lt;br /&gt;select @i_Location_Code = 1&lt;br /&gt;      ,@i_Machine_Brand = &#39;Dell&#39;&lt;br /&gt;      ,@o_Return_Value = 0&lt;br /&gt;&lt;br /&gt;execute usp_Generate_Machine_X_Code&lt;br /&gt;        @i_Location_Code &lt;br /&gt;       ,@i_Machine_Brand &lt;br /&gt;       ,@o_Machine_X_Code OutPut&lt;br /&gt;       ,@o_Return_Value OutPut&lt;br /&gt;&lt;br /&gt;select  @o_Machine_X_Code Machine_Code&lt;br /&gt;       ,@o_Return_Value   Return_Value&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is the result set:&lt;br /&gt;&lt;br /&gt;Machine_Code Return_Value&lt;br /&gt;------------ ------------&lt;br /&gt;1005         1&lt;br /&gt;&lt;br /&gt;We got the exact machine code. Now let&#39;s check the table Code_Generation_Table for the column Current_Code by executing the following code:&lt;br /&gt;&lt;br /&gt;select * from Code_Generation_Table&lt;br /&gt;where Location_Code = 1&lt;br /&gt;and Machine_Brand = &#39;Dell&#39;&lt;br /&gt; &lt;br /&gt;The result set is as follows:&lt;br /&gt;&lt;br /&gt;Location_Code Machine_Brand Start_Code  Current_Code End_Code    Increment_Value&lt;br /&gt;------------- ------------  ----------- ------------ ----------- ---------------&lt;br /&gt;1             DELL          1001        1001         1005        1&lt;br /&gt;&lt;br /&gt;You may note that the Current_Code has been updated. It has actually been initialized with the next possible Current_Code because 1005 has been taken by us via execution of the SP. So the next available value can be 1006. But since 1006 is outside the range of 1001 and 1005, the next possible value has to be re-initialized to the start_code which is 1001. So this condition has also been satisfied. Very well.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;TEST 2:&lt;br /&gt;Now let&#39;s test another condition:&lt;br /&gt;&lt;br /&gt;Scenario 1:&lt;br /&gt;&lt;br /&gt;For Location_Code = 2 and Machine_Brand = &#39;IBM&#39;, there is no record in the Main_Table. Let&#39;s check by executing the following code:&lt;br /&gt;&lt;br /&gt;select Location_Code, Machine_Brand, Machine_X_Code, Number_Of_CPUs from Main_Table &lt;br /&gt;where Location_Code = 2&lt;br /&gt;and  Machine_Brand = &#39;IBM&#39;&lt;br /&gt;&lt;br /&gt;(0 rows affected)&lt;br /&gt;&lt;br /&gt;Let&#39;s check in the Code_Generation_Table by executing the following code:&lt;br /&gt;&lt;br /&gt;select * from Code_Generation_Table&lt;br /&gt;where Location_Code = 2&lt;br /&gt;and  Machine_Brand = &#39;IBM&#39;&lt;br /&gt; &lt;br /&gt;The result set is as follows:&lt;br /&gt;Location_Code Machine_Brand Start_Code  Current_Code End_Code    Increment_Value&lt;br /&gt;------------- ------------  ----------- ------------ ----------- ---------------&lt;br /&gt;2             IBM           8001        8003         8005        1&lt;br /&gt;&lt;br /&gt;So as per the given condition, the new machine_x_code shoudl be 8003 and the Current_Code column of the table, Code_Generation_Table should get updated with 8004. So let&#39;s execute the SP from the developer by executng the following code:&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;declare @i_Location_Code Smallint&lt;br /&gt;       ,@i_Machine_Brand VarChar(10)&lt;br /&gt;       ,@o_Machine_X_Code Integer &lt;br /&gt;       ,@o_Return_Value Smallint &lt;br /&gt;&lt;br /&gt;select @i_Location_Code = 2&lt;br /&gt;      ,@i_Machine_Brand = &#39;IBM&#39;&lt;br /&gt;      ,@o_Return_Value = 0&lt;br /&gt;&lt;br /&gt;execute usp_Generate_Machine_X_Code&lt;br /&gt;        @i_Location_Code &lt;br /&gt;       ,@i_Machine_Brand &lt;br /&gt;       ,@o_Machine_X_Code OutPut&lt;br /&gt;       ,@o_Return_Value OutPut&lt;br /&gt;&lt;br /&gt;select  @o_Machine_X_Code Machine_Code&lt;br /&gt;       ,@o_Return_Value   Return_Value&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is the result set:&lt;br /&gt;&lt;br /&gt;Machine_Code Return_Value&lt;br /&gt;------------ ------------&lt;br /&gt;8003         1&lt;br /&gt;&lt;br /&gt;It is perfect. Let&#39;s check the Current_Code column of the table, Code_Generation_Table by executing the following code:&lt;br /&gt;&lt;br /&gt;select * from Code_Generation_Table&lt;br /&gt;where Location_Code = 2&lt;br /&gt;and  Machine_Brand = &#39;IBM&#39;&lt;br /&gt; &lt;br /&gt;The result set is as follows:&lt;br /&gt;Location_Code Machine_Brand Start_Code  Current_Code End_Code    Increment_Value&lt;br /&gt;------------- ------------  ----------- ------------ ----------- ---------------&lt;br /&gt;2             IBM           8001        8004         8005        1&lt;br /&gt;&lt;br /&gt;So the new Machine_X_Number generated in 8003 which is perfect. And the Crrent_Code column has also been updated to the next possible Machine_X_Number which is 8004. So the Scenario 1 has been satisfied.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;TEST 3:&lt;br /&gt;Let&#39;s check out if Scenario 2 is satisfied:&lt;br /&gt;&lt;br /&gt;For Location_Code = 2 and Machine_Brand = &#39;HP&#39;, there are four records in the Main_Table with machine codes (0000005002, 0000005003, 0000005004 and 0000005005). Let&#39;s check by executing the following code:&lt;br /&gt;&lt;br /&gt;select Location_Code, Machine_Brand, Machine_X_Code, Number_Of_CPUs from Main_Table &lt;br /&gt;where Location_Code = 2&lt;br /&gt;and  Machine_Brand = &#39;HP&#39;&lt;br /&gt;&lt;br /&gt;Location_Code Machine_Brand             Machine_X_Code Number_Of_CPUs&lt;br /&gt;------------- ------------------------- -------------- --------------&lt;br /&gt;2             HP                        0000005002     2&lt;br /&gt;2             HP                        0000005003     4&lt;br /&gt;2             HP                        0000005004     8&lt;br /&gt;2             HP                        0000005005     32&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Let&#39;s check in the Code_Generation_Table by executing the following code:&lt;br /&gt;&lt;br /&gt;select * from Code_Generation_Table&lt;br /&gt;where Location_Code = 2&lt;br /&gt;and  Machine_Brand = &#39;HP&#39;&lt;br /&gt; &lt;br /&gt;The result set is as follows:&lt;br /&gt;Location_Code Machine_Brand Start_Code  Current_Code End_Code    Increment_Value&lt;br /&gt;------------- ------------  ----------- ------------ ----------- ---------------&lt;br /&gt;2             HP            5001        5003         5005        1&lt;br /&gt;&lt;br /&gt;So as per the given condition for Scenario 2, the new machine_x_code generated should be 5001 and the Current_Code column of the table, Code_Generation_Table should get updated with 8002. So let&#39;s execute the SP from the developer by executng the following code:&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;declare @i_Location_Code Smallint&lt;br /&gt;       ,@i_Machine_Brand VarChar(10)&lt;br /&gt;       ,@o_Machine_X_Code Integer &lt;br /&gt;       ,@o_Return_Value Smallint &lt;br /&gt;&lt;br /&gt;select @i_Location_Code = 2&lt;br /&gt;      ,@i_Machine_Brand = &#39;HP&#39;&lt;br /&gt;      ,@o_Return_Value = 0&lt;br /&gt;&lt;br /&gt;execute usp_Generate_Machine_X_Code&lt;br /&gt;        @i_Location_Code &lt;br /&gt;       ,@i_Machine_Brand &lt;br /&gt;       ,@o_Machine_X_Code OutPut&lt;br /&gt;       ,@o_Return_Value OutPut&lt;br /&gt;&lt;br /&gt;select  @o_Machine_X_Code Machine_Code&lt;br /&gt;       ,@o_Return_Value   Return_Value&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is the result set:&lt;br /&gt;&lt;br /&gt;Machine_Code Return_Value&lt;br /&gt;------------ ------------&lt;br /&gt;5001         1&lt;br /&gt;&lt;br /&gt;It is perfect. Let&#39;s check the Current_Code column of the table, Code_Generation_Table by executing the following code:&lt;br /&gt;&lt;br /&gt;select * from Code_Generation_Table&lt;br /&gt;where Location_Code = 2&lt;br /&gt;and  Machine_Brand = &#39;HP&#39;&lt;br /&gt; &lt;br /&gt;The result set is as follows:&lt;br /&gt;Location_Code Machine_Brand Start_Code  Current_Code End_Code    Increment_Value&lt;br /&gt;------------- ------------  ----------- ------------ ----------- ---------------&lt;br /&gt;2             HP            5001        5002         5005        1&lt;br /&gt;&lt;br /&gt;So the new Machine_X_Number generated in 5001 which is perfect. And the Current_Code column has also been updated to the next possible Machine_X_Number which is 5002. The next possible Machine_X_Number should be equal to the output of the SP + Increment_Value and should exists within the range of Start_Code and End_Code. So the Scenario 2 has been satisfied.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;TEST 4:&lt;br /&gt;Let&#39;s check the Scenario 3, the final scenario:&lt;br /&gt;&lt;br /&gt;For Location_Code = 3 and Machine_Brand = &#39;IBM&#39;, there are four records in the Main_Table with machine codes (0000006001, 0000006003, 0000006004 and 0000006005). Let&#39;s check by executing the following code:&lt;br /&gt;&lt;br /&gt;select Location_Code, Machine_Brand, Machine_X_Code, Number_Of_CPUs from Main_Table &lt;br /&gt;where Location_Code = 3&lt;br /&gt;and  Machine_Brand = &#39;IBM&#39;&lt;br /&gt;&lt;br /&gt;Location_Code Machine_Brand             Machine_X_Code Number_Of_CPUs&lt;br /&gt;------------- ------------------------- -------------- --------------&lt;br /&gt;3             IBM                       0000006001     2&lt;br /&gt;3             IBM                       0000006003     2&lt;br /&gt;3             IBM                       0000006004     8&lt;br /&gt;3             IBM                       0000006005     32&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Let&#39;s check in the Code_Generation_Table by executing the following code:&lt;br /&gt;&lt;br /&gt;select * from Code_Generation_Table&lt;br /&gt;where Location_Code = 3&lt;br /&gt;and  Machine_Brand = &#39;IBM&#39;&lt;br /&gt; &lt;br /&gt;The result set is as follows:&lt;br /&gt;Location_Code Machine_Brand Start_Code  Current_Code End_Code    Increment_Value&lt;br /&gt;------------- ------------  ----------- ------------ ----------- ---------------&lt;br /&gt;3             IBM           6001        6004         6005        1&lt;br /&gt;&lt;br /&gt;So as per the given condition for Scenario 3, the new machine_x_code generated should be 6002 and the Current_Code column of the table, Code_Generation_Table should get updated with 6003. So let&#39;s execute the SP from the developer by executng the following code:&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;declare @i_Location_Code Smallint&lt;br /&gt;       ,@i_Machine_Brand VarChar(10)&lt;br /&gt;       ,@o_Machine_X_Code Integer &lt;br /&gt;       ,@o_Return_Value Smallint &lt;br /&gt;&lt;br /&gt;select @i_Location_Code = 3&lt;br /&gt;      ,@i_Machine_Brand = &#39;IBM&#39;&lt;br /&gt;      ,@o_Return_Value = 0&lt;br /&gt;&lt;br /&gt;execute usp_Generate_Machine_X_Code&lt;br /&gt;        @i_Location_Code &lt;br /&gt;       ,@i_Machine_Brand &lt;br /&gt;       ,@o_Machine_X_Code OutPut&lt;br /&gt;       ,@o_Return_Value OutPut&lt;br /&gt;&lt;br /&gt;select  @o_Machine_X_Code Machine_Code&lt;br /&gt;       ,@o_Return_Value   Return_Value&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is the result set:&lt;br /&gt;&lt;br /&gt;Machine_Code Return_Value&lt;br /&gt;------------ ------------&lt;br /&gt;6002         1&lt;br /&gt;&lt;br /&gt;It is perfect. Let&#39;s check the Current_Code column of the table, Code_Generation_Table by executing the following code:&lt;br /&gt;&lt;br /&gt;select * from Code_Generation_Table&lt;br /&gt;where Location_Code = 3&lt;br /&gt;and  Machine_Brand = &#39;IBM&#39;&lt;br /&gt; &lt;br /&gt;The result set is as follows:&lt;br /&gt;Location_Code Machine_Brand Start_Code  Current_Code End_Code    Increment_Value&lt;br /&gt;------------- ------------  ----------- ------------ ----------- ---------------&lt;br /&gt;3             IBM           6001        6003         6005        1&lt;br /&gt;&lt;br /&gt;So the new Machine_X_Number generated in 6002 which was the missing code in Main_Table for Location_Code = 3 and Machine_Brand = &#39;IBM&#39;. And the Current_Code column has also been updated to the next possible Machine_X_Number which is 6003. The next possible Machine_X_Number should be equal to the output of the SP + Increment_Value and should exist within the range of Start_Code and End_Code. So the Scenario 3 has been satisfied.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The developer has satisifed all the functional requirements. Good job. Now what are the drawbacks in his code? Let&#39;s enlist them:&lt;br /&gt;&lt;br /&gt;1. He uses an SP and a UDF, two database objects for one purpose. This is a minor drawback though.&lt;br /&gt;2. Looping is being used. In a rare case, it may have just one iteration. But in almost all cases it would have multiple iterations upto a maximum of five iterations. We have a simple set of scenarios here. If the range between Start_Code and Stop_Code runs into several hundreds, the looping will happen those many times most of the time. And the invocation of the UDF would take place double those many times for generating just one new Machine_X_Number. &lt;br /&gt;3. The Update to Code_Generation_Table takes place as many times as the number of iterations which could run into several hundred times. So many UPDATE&#39;s just to generate one Machine_X_Number seems too much processing. It should be avoided. &lt;br /&gt;4. The code is accessing the Main_Table as many times as the number of iterations. In our case the Main_Table has just 22 rows. In the real situation, it actually has hundreds of thousands of rows. Why scan that table so many times just to generate one valid Machine_X_Number? &lt;br /&gt;5. The SP performs record-by-record operation or row-by-row. An attempt should be made to perform set based operation for faster and efficient execution. That would also reduce scans of the underlying tables.&lt;br /&gt;&lt;br /&gt;These are only a few drawbacks and there are others like usage of dynamic T-SQL which is not needed in this case, etc. We won&#39;t go into those detailed aspects in this article. We will concentrate more on logical programming.&lt;br /&gt;&lt;br /&gt;Let&#39;s try to satisfy the same scenarios after making some refinements to the SP and by eliminating the user-defined function altogether.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A refined version of the SP is given below:&lt;br /&gt;&lt;br /&gt;--This SP&#39;s purpose is to generate a new Machine_X_Code. It generates a new &lt;br /&gt;--Machine_X_Code from the Current_Code column of the table, Code_Generation_Table &lt;br /&gt;--while checking that the newly generated Machine_X_Code does not already exist&lt;br /&gt;--in the Machine_X_Code column of the table Main_Table. It also checks that a newly &lt;br /&gt;--generated Machine_X_Code lies between Start_Code and End_Code in the table, &lt;br /&gt;--Code_Generation_Table.&lt;br /&gt;--If the newly generated machine_x_code falls outside that range, then the machine_x_code&lt;br /&gt;--is reset to Start_Code. In this whole process it updates the Current_Code column&lt;br /&gt;--of the table, Code_Generation_Table with the newly generated Machine_X_Code once &lt;br /&gt;--or at the most twice. The function, F_Validate_Machine_Code has been eliminated altogether. &lt;br /&gt;--It also updates the the  Current_Code column of the table, Code_Generation_Table with &lt;br /&gt;--the next possible Machine_X_Code value. If at all, Looping occurs only twice in a rare case.&lt;br /&gt;--So effectively, there would be no looping except in a scenario where there would be two iterations &lt;br /&gt;--of the code when a new machine_x_code does not exist between the Current_Code and &lt;br /&gt;--the End_Code. In such a scenario, it would check the valid machine codes from Start_Code&lt;br /&gt;--through Current_Code in the second iteration.&lt;br /&gt;&lt;br /&gt;Create Procedure usp_Generate_Machine_X_Code&lt;br /&gt;@i_Location_Code Smallint,&lt;br /&gt;@i_Machine_Brand VarChar(10),&lt;br /&gt;@o_Machine_X_Code Integer OutPut,&lt;br /&gt;@o_Return_Value Smallint OutPut&lt;br /&gt;As&lt;br /&gt;Begin&lt;br /&gt;  Set nocount on&lt;br /&gt;  Set @o_Return_Value = 0&lt;br /&gt;/* WHILE Loop eliminated. Main_Table is now being referenced only once. New code below. &lt;br /&gt;  Set @o_Return_Value = 0&lt;br /&gt;&lt;br /&gt;  Declare @l_Increment_Value Smallint,&lt;br /&gt;          @l_Temp_Machine_X_Value Integer,&lt;br /&gt;          @l_Next_Machine_X_Value Integer,&lt;br /&gt;          @l_Start_Machine_X_Value Int,&lt;br /&gt;          @l_End_Machine_X_Value Integer,&lt;br /&gt;          @l_First_Machine_X_Value_Taken Integer,&lt;br /&gt;          @l_Affected_Rows Smallint,&lt;br /&gt;          @l_Execution_String VarChar(2000)&lt;br /&gt;&lt;br /&gt;  Select @l_Temp_Machine_X_Value  = Current_Code&lt;br /&gt;        ,@l_Increment_Value       = Increment_Value&lt;br /&gt;        ,@l_Start_Machine_X_Value = Start_Code&lt;br /&gt;        ,@l_End_Machine_X_Value   = End_Code&lt;br /&gt;  From DBO.Code_Generation_Table With (NoLock) &lt;br /&gt;  Where Location_Code = @i_Location_Code &lt;br /&gt;  And Machine_Brand   = @i_Machine_Brand&lt;br /&gt;  &lt;br /&gt;  Set @l_Temp_Machine_X_Value = DBO.F_Validate_Machine_Code (@l_Temp_Machine_X_Value, @l_Start_Machine_X_Value, @l_End_Machine_X_Value)&lt;br /&gt;&lt;br /&gt;  Set @l_First_Machine_X_Value_Taken = @l_Temp_Machine_X_Value&lt;br /&gt;&lt;br /&gt;  Set @l_Affected_Rows = 0&lt;br /&gt;&lt;br /&gt;  While @l_Affected_Rows = 0&lt;br /&gt;  Begin&lt;br /&gt;&lt;br /&gt;    Set @l_Next_Machine_X_Value = @l_Temp_Machine_X_Value + @l_Increment_Value &lt;br /&gt;&lt;br /&gt;    Set @l_Next_Machine_X_Value = DBO.F_Validate_Machine_Code (@l_Next_Machine_X_Value, @l_Start_Machine_X_Value, @l_End_Machine_X_Value)&lt;br /&gt;&lt;br /&gt;    Set @l_Execution_String = &#39;Update A  Set A.Current_Code = &#39; + Cast(@l_Next_Machine_X_Value as VarChar) +&lt;br /&gt;    &#39; From Dbo.Code_Generation_Table A &#39;+   &lt;br /&gt;    + &#39; Where Location_Code = &#39; + Cast(@i_Location_Code AS VarChar)  + &#39; And Machine_Brand = &#39; + &#39;&#39;&#39;&#39;+ @i_Machine_Brand +  &#39;&#39;&#39;&#39; +&lt;br /&gt;    &#39; And Not Exists (Select &#39; + &#39;&#39;&#39;&#39; &lt;br /&gt;    + &#39;x&#39; + &#39;&#39;&#39;&#39; + &#39; From  Main_Table &#39; +&lt;br /&gt;    &#39;Where Location_Code = &#39; + Cast(@i_Location_Code AS VarChar) +&lt;br /&gt;    &#39; AND Machine_Brand = &#39; + &#39;&#39;&#39;&#39; + @i_Machine_Brand +  &#39;&#39;&#39;&#39; +  &lt;br /&gt;    &#39; And Machine_X_Code = &#39; + &#39;&#39;&#39;&#39; + Replicate(&#39;0&#39;,10 - Len(@l_Temp_Machine_X_Value)) +  Cast(@l_Temp_Machine_X_Value as Varchar(10)) + &#39;&#39;&#39;&#39; + &#39;)&#39;&lt;br /&gt;&lt;br /&gt;    Exec (@l_Execution_String) &lt;br /&gt;&lt;br /&gt;    Set @l_Affected_Rows = @@Rowcount&lt;br /&gt;&lt;br /&gt;    If @l_Affected_Rows = 1&lt;br /&gt;    Begin&lt;br /&gt;       Select @o_Machine_X_Code = @l_Temp_Machine_X_Value, @o_Return_Value = 1&lt;br /&gt;       Break&lt;br /&gt;    End&lt;br /&gt;&lt;br /&gt;    Set @l_Temp_Machine_X_Value = @l_Temp_Machine_X_Value + @l_Increment_Value&lt;br /&gt;&lt;br /&gt;    Set @l_Temp_Machine_X_Value = DBO.F_Validate_Machine_Code (@l_Temp_Machine_X_Value, @l_Start_Machine_X_Value, @l_End_Machine_X_Value)&lt;br /&gt;&lt;br /&gt;    If @l_First_Machine_X_Value_Taken = @l_Temp_Machine_X_Value &lt;br /&gt;    Begin&lt;br /&gt;      -- For the current day all the Machine_X_Codes have been used up.&lt;br /&gt;      Select @l_Affected_Rows = 2, @o_Return_Value = -1 &lt;br /&gt;      Break&lt;br /&gt;    End&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;declare @l_next_Machine_X_Code int&lt;br /&gt;       ,@l_Start_Machine_X_Code int&lt;br /&gt;       ,@l_End_Machine_X_Code   int&lt;br /&gt;       ,@l_Current_Machine_X_Code int&lt;br /&gt;       ,@l_Increment_Value tinyint&lt;br /&gt;&lt;br /&gt;select  @l_Start_Machine_X_Code   = Start_Code&lt;br /&gt;       ,@l_End_Machine_X_Code     = End_Code&lt;br /&gt;       ,@l_Increment_Value        = Increment_Value&lt;br /&gt;       ,@l_Current_Machine_X_Code = Current_Code &lt;br /&gt;from Code_Generation_Table&lt;br /&gt;where Location_Code = @i_Location_Code&lt;br /&gt;and Machine_Brand = @i_Machine_Brand&lt;br /&gt;&lt;br /&gt;if ((@l_Current_Machine_X_Code &lt; @l_Start_Machine_X_Code) or (@l_Current_Machine_X_Code &gt; @l_End_Machine_X_Code))&lt;br /&gt;begin&lt;br /&gt;   Start_Processing:&lt;br /&gt;   update Code_Generation_Table&lt;br /&gt;   set @l_Current_Machine_X_Code = Current_Code = @l_Start_Machine_X_Code&lt;br /&gt;   where Location_Code = @i_Location_Code&lt;br /&gt;   and   Machine_Brand = @i_Machine_Brand&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;if object_id (&#39;tempdb.dbo.#Temp_Code_Table&#39;) is not null&lt;br /&gt;drop table #Temp_Code_Table&lt;br /&gt;&lt;br /&gt;create table #Temp_Code_Table&lt;br /&gt;(Existing_Machine_Codes int&lt;br /&gt;,Valid_machine_Codes    int &lt;br /&gt;,Serial_No              int identity(0,1))&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;insert into #Temp_Code_Table (Existing_Machine_Codes, Valid_machine_Codes)&lt;br /&gt;select convert(int, Machine_X_Code), @l_Current_Machine_X_Code from dbo.Main_Table&lt;br /&gt;where Location_Code = @i_Location_Code&lt;br /&gt;and Machine_Brand = @i_Machine_Brand&lt;br /&gt;order by Machine_X_Code&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;if ((select COUNT(1) from #Temp_Code_Table) = 0)&lt;br /&gt;begin&lt;br /&gt;   select @l_next_Machine_X_Code = @l_Current_Machine_X_Code&lt;br /&gt;   goto Final_Validation&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;update #Temp_Code_Table&lt;br /&gt;set Valid_machine_Codes = Valid_machine_Codes + (Serial_No * @l_Increment_Value)&lt;br /&gt;&lt;br /&gt;select @l_next_Machine_X_Code = min(a.Valid_machine_Codes) from #Temp_Code_Table a&lt;br /&gt;where a.Valid_machine_Codes not in (select b.Existing_Machine_Codes from #Temp_Code_Table b)&lt;br /&gt;and   a.Valid_machine_Codes between @l_Start_Machine_X_Code and @l_End_Machine_X_Code&lt;br /&gt;&lt;br /&gt;if (@l_next_Machine_X_Code is null)&lt;br /&gt;begin&lt;br /&gt;   select @l_next_Machine_X_Code = max(Valid_machine_Codes) + @l_Increment_Value from #Temp_Code_Table&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;Final_Validation:&lt;br /&gt;if (@l_next_Machine_X_Code &gt; @l_End_Machine_X_Code)&lt;br /&gt;begin&lt;br /&gt;   goto Start_Processing&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;if (@l_next_Machine_X_Code &lt; @l_Start_Machine_X_Code) or (@l_next_Machine_X_Code &gt; @l_End_Machine_X_Code)&lt;br /&gt;begin&lt;br /&gt;   select @l_next_Machine_X_Code = @l_Start_Machine_X_Code&lt;br /&gt;         ,@o_Return_Value = -1&lt;br /&gt;   set nocount off &lt;br /&gt;   if object_id (&#39;tempdb.dbo.#Temp_Code_Table&#39;) is not null&lt;br /&gt;   drop table #Temp_Code_Table&lt;br /&gt;   return&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;if (@l_next_Machine_X_Code &gt;= @l_Start_Machine_X_Code) and ((@l_next_Machine_X_Code &gt;= @l_Start_Machine_X_Code) and (@l_next_Machine_X_Code &lt;= @l_End_Machine_X_Code))&lt;br /&gt;begin&lt;br /&gt;   update Code_Generation_Table with (readpast, rowlock, xlock)&lt;br /&gt;   set Current_Code = case &lt;br /&gt;                        when @l_next_Machine_X_Code = @l_End_Machine_X_Code then @l_Start_Machine_X_Code&lt;br /&gt;                        else @l_next_Machine_X_Code + @l_Increment_Value&lt;br /&gt;                      end &lt;br /&gt;   where Location_Code = @i_Location_Code&lt;br /&gt;   and   Machine_Brand = @i_Machine_Brand&lt;br /&gt;&lt;br /&gt;   if (@@ROWCOUNT &lt;&gt; 0)&lt;br /&gt;   begin&lt;br /&gt;      select @o_Machine_X_Code = @l_next_Machine_X_Code&lt;br /&gt;            ,@o_Return_Value  = 1  &lt;br /&gt;   end &lt;br /&gt;   else &lt;br /&gt;      set @o_Return_Value  = -1&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;if object_id (&#39;tempdb.dbo.#Temp_Code_Table&#39;) is not null&lt;br /&gt;drop table #Temp_Code_Table&lt;br /&gt;&lt;br /&gt;set nocount off&lt;br /&gt;return&lt;br /&gt;End&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The new logic in the SP is given in the comments before the Create Procedure statement. The calls to function have been eliminated. This SP checks for valid Machine_X_Code values between Current_Code and End_Code. If not found, it checks for valid Machine_X_Code values between Start_Code and Current_Code. This is because there is a functional resttriction that the search should always start from the Current_Code only.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Let&#39;s now start from the beginning by emptynig (truncating) bith the tables, Code_Generation_Table and Main_Table and filling (inserting) the same data as above into them afresh by executing the following code: &lt;br /&gt;&lt;br /&gt;truncate table Code_Generation_Table&lt;br /&gt;truncate table Main_Table&lt;br /&gt;&lt;br /&gt;Go&lt;br /&gt;&lt;br /&gt;insert into Code_Generation_Table&lt;br /&gt;select 1, &#39;DELL&#39;, 1001, 1001, 1005, 1&lt;br /&gt;union all&lt;br /&gt;select 2, &#39;DELL&#39;, 4001, 4001, 4005, 1&lt;br /&gt;union all&lt;br /&gt;select 3, &#39;DELL&#39;, 7001, 7001, 7005, 1&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;HP&#39;, 2001, 2003, 2005, 1&lt;br /&gt;union all&lt;br /&gt;select 2, &#39;HP&#39;, 5001, 5003, 5005, 1&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;IBM&#39;, 3001, 3001, 3005, 1&lt;br /&gt;union all&lt;br /&gt;select 2, &#39;IBM&#39;, 8001, 8003, 8005, 1&lt;br /&gt;union all&lt;br /&gt;select 3, &#39;IBM&#39;, 6001, 6004, 6005, 1&lt;br /&gt;union all&lt;br /&gt;select 3, &#39;SUN&#39;, 6011, 6011, 6015, 1&lt;br /&gt;&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;insert into Main_Table (Location_Code, Machine_Brand, Machine_X_Code, Number_Of_CPUs)&lt;br /&gt;select 1, &#39;DELL&#39;, &#39;0000001001&#39;, 2&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;DELL&#39;, &#39;0000001002&#39;, 4&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;DELL&#39;, &#39;0000001003&#39;, 8&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;DELL&#39;, &#39;0000001004&#39;, 16&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;HP&#39;, &#39;0000002002&#39;, 2&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;HP&#39;, &#39;0000002003&#39;, 4&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;HP&#39;, &#39;0000002004&#39;, 8&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;HP&#39;, &#39;0000002005&#39;, 32&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;IBM&#39;, &#39;0000003003&#39;, 2&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;IBM&#39;, &#39;0000003004&#39;, 8&lt;br /&gt;union all&lt;br /&gt;select 1, &#39;IBM&#39;, &#39;0000003005&#39;, 32&lt;br /&gt;union all&lt;br /&gt;select 2, &#39;DELL&#39;, &#39;0000004001&#39;, 2&lt;br /&gt;union all&lt;br /&gt;select 2, &#39;DELL&#39;, &#39;0000004002&#39;, 4&lt;br /&gt;union all&lt;br /&gt;select 2, &#39;DELL&#39;, &#39;0000004003&#39;, 8&lt;br /&gt;union all&lt;br /&gt;select 3, &#39;DELL&#39;, &#39;0000007004&#39;, 16&lt;br /&gt;union all&lt;br /&gt;select 2, &#39;HP&#39;, &#39;0000005002&#39;, 2&lt;br /&gt;union all&lt;br /&gt;select 2, &#39;HP&#39;, &#39;0000005003&#39;, 4&lt;br /&gt;union all&lt;br /&gt;select 2, &#39;HP&#39;, &#39;0000005004&#39;, 8&lt;br /&gt;union all&lt;br /&gt;select 2, &#39;HP&#39;, &#39;0000005005&#39;, 32&lt;br /&gt;union all&lt;br /&gt;select 3, &#39;IBM&#39;, &#39;0000006001&#39;, 2&lt;br /&gt;union all&lt;br /&gt;select 3, &#39;IBM&#39;, &#39;0000006003&#39;, 2&lt;br /&gt;union all&lt;br /&gt;select 3, &#39;IBM&#39;, &#39;0000006004&#39;, 8&lt;br /&gt;union all&lt;br /&gt;select 3, &#39;IBM&#39;, &#39;0000006005&#39;, 32&lt;br /&gt;Go&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Having filled up the data, it&#39;s time to test this new version of the SP.&lt;br /&gt;&lt;br /&gt;Let us try to generate a new Machine_X_Code for the Location_Code = 1 and Brand_Name = &#39;Dell&#39;.&lt;br /&gt;&lt;br /&gt;TEST 1:&lt;br /&gt;Execute the following:&lt;br /&gt;&lt;br /&gt;select Location_Code, Machine_Brand, Machine_X_Code, Number_Of_CPUs from Main_Table &lt;br /&gt;where Location_Code = 1&lt;br /&gt;and  Machine_Brand = &#39;Dell&#39;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The result set is as follows:&lt;br /&gt;&lt;br /&gt;Location_Code Machine_Brand             Machine_X_Code Number_Of_CPUs&lt;br /&gt;------------- ------------------------- -------------- --------------&lt;br /&gt;1             DELL                      0000001001     2&lt;br /&gt;1             DELL                      0000001002     4&lt;br /&gt;1             DELL                      0000001003     8&lt;br /&gt;1             DELL                      0000001004     16&lt;br /&gt;&lt;br /&gt;So the next generated machine_x_code to has to be 1005. Remember zeroes are suffixed later and that is not so important right now. What is important is generating the new Machine_X_Code that meets all the requirements above. &lt;br /&gt;&lt;br /&gt;Now run the following code:&lt;br /&gt;&lt;br /&gt;select * from Code_Generation_Table&lt;br /&gt; &lt;br /&gt;The result set is as follows:&lt;br /&gt;Location_Code Machine_Brand Start_Code  Current_Code End_Code    Increment_Value&lt;br /&gt;------------- ------------  ----------- ------------ ----------- ---------------&lt;br /&gt;1             DELL          1001        1001         1005        1&lt;br /&gt;1             HP            2001        2003         2005        1&lt;br /&gt;1             IBM           3001        3001         3005        1&lt;br /&gt;2             DELL          4001        4001         4005        1&lt;br /&gt;2             HP            5001        5003         5005        1&lt;br /&gt;2             IBM           8001        8003         8005        1&lt;br /&gt;3             DELL          7001        7001         7005        1&lt;br /&gt;3             IBM           6001        6004         6005        1&lt;br /&gt;3             SUN           6011        6011         6015        1&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Since the codes (1001, 1002, 1003 and 1004) already exist in the Main_Table for Location_Code = 1 and Machine_Brand = &#39;Dell&#39;, the next one has to be 1005 as it exists in the Code_Generation_Table between Start_Code and End_Code. Let&#39;s see if the SP generates this machine code. Execute the following code to execute the SP:&lt;br /&gt;&lt;br /&gt;declare @i_Location_Code Smallint&lt;br /&gt;       ,@i_Machine_Brand VarChar(10)&lt;br /&gt;       ,@o_Machine_X_Code Integer &lt;br /&gt;       ,@o_Return_Value Smallint &lt;br /&gt;&lt;br /&gt;select @i_Location_Code = 1&lt;br /&gt;      ,@i_Machine_Brand = &#39;Dell&#39;&lt;br /&gt;      ,@o_Return_Value = 0&lt;br /&gt;&lt;br /&gt;execute usp_Generate_Machine_X_Code&lt;br /&gt;        @i_Location_Code &lt;br /&gt;       ,@i_Machine_Brand &lt;br /&gt;       ,@o_Machine_X_Code OutPut&lt;br /&gt;       ,@o_Return_Value OutPut&lt;br /&gt;&lt;br /&gt;select  @o_Machine_X_Code Machine_Code&lt;br /&gt;       ,@o_Return_Value   Return_Value&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is the result set:&lt;br /&gt;&lt;br /&gt;Machine_Code Return_Value&lt;br /&gt;------------ ------------&lt;br /&gt;1005         1&lt;br /&gt;&lt;br /&gt;We got the exact machine code. Now let&#39;s check the table Code_Generation_Table for the column Current_Code by executing the following code:&lt;br /&gt;&lt;br /&gt;select * from Code_Generation_Table&lt;br /&gt;where Location_Code = 1&lt;br /&gt;and Machine_Brand = &#39;Dell&#39;&lt;br /&gt; &lt;br /&gt;The result set is as follows:&lt;br /&gt;&lt;br /&gt;Location_Code Machine_Brand Start_Code  Current_Code End_Code    Increment_Value&lt;br /&gt;------------- ------------  ----------- ------------ ----------- ---------------&lt;br /&gt;1             DELL          1001        1001         1005        1&lt;br /&gt;&lt;br /&gt;You may note that the Current_Code has been updated. It has actually been initialized with the next possible Current_Code because 1005 has been taken by us via execution of the SP. So the next available value can be 1006. But since 1006 is outside the range of 1001 and 1005, the next possible value has to be re-initialized to the start_code which is 1001. So this condition has also been satisfied by the new version of the SP. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;TEST 2:&lt;br /&gt;Now let&#39;s test another condition:&lt;br /&gt;&lt;br /&gt;Scenario 1:&lt;br /&gt;&lt;br /&gt;For Location_Code = 2 and Machine_Brand = &#39;IBM&#39;, there is no record in the Main_Table. Let&#39;s check by executing the following code:&lt;br /&gt;&lt;br /&gt;select Location_Code, Machine_Brand, Machine_X_Code, Number_Of_CPUs from Main_Table &lt;br /&gt;where Location_Code = 2&lt;br /&gt;and  Machine_Brand = &#39;IBM&#39;&lt;br /&gt;&lt;br /&gt;(0 rows affected)&lt;br /&gt;&lt;br /&gt;Let&#39;s check in the Code_Generation_Table by executing the following code:&lt;br /&gt;&lt;br /&gt;select * from Code_Generation_Table&lt;br /&gt;where Location_Code = 2&lt;br /&gt;and  Machine_Brand = &#39;IBM&#39;&lt;br /&gt; &lt;br /&gt;The result set is as follows:&lt;br /&gt;Location_Code Machine_Brand Start_Code  Current_Code End_Code    Increment_Value&lt;br /&gt;------------- ------------  ----------- ------------ ----------- ---------------&lt;br /&gt;2             IBM           8001        8003         8005        1&lt;br /&gt;&lt;br /&gt;So as per the given condition, the new machine_x_code shoudl be 8003 and the Current_Code column of the table, Code_Generation_Table should get updated with 8004. So let&#39;s execute the SP from the developer by executng the following code:&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;declare @i_Location_Code Smallint&lt;br /&gt;       ,@i_Machine_Brand VarChar(10)&lt;br /&gt;       ,@o_Machine_X_Code Integer &lt;br /&gt;       ,@o_Return_Value Smallint &lt;br /&gt;&lt;br /&gt;select @i_Location_Code = 2&lt;br /&gt;      ,@i_Machine_Brand = &#39;IBM&#39;&lt;br /&gt;      ,@o_Return_Value = 0&lt;br /&gt;&lt;br /&gt;execute usp_Generate_Machine_X_Code&lt;br /&gt;        @i_Location_Code &lt;br /&gt;       ,@i_Machine_Brand &lt;br /&gt;       ,@o_Machine_X_Code OutPut&lt;br /&gt;       ,@o_Return_Value OutPut&lt;br /&gt;&lt;br /&gt;select  @o_Machine_X_Code Machine_Code&lt;br /&gt;       ,@o_Return_Value   Return_Value&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is the result set:&lt;br /&gt;&lt;br /&gt;Machine_Code Return_Value&lt;br /&gt;------------ ------------&lt;br /&gt;8003         1&lt;br /&gt;&lt;br /&gt;It is perfect. Let&#39;s check the Current_Code column of the table, Code_Generation_Table by executing the following code:&lt;br /&gt;&lt;br /&gt;select * from Code_Generation_Table&lt;br /&gt;where Location_Code = 2&lt;br /&gt;and  Machine_Brand = &#39;IBM&#39;&lt;br /&gt; &lt;br /&gt;The result set is as follows:&lt;br /&gt;Location_Code Machine_Brand Start_Code  Current_Code End_Code    Increment_Value&lt;br /&gt;------------- ------------  ----------- ------------ ----------- ---------------&lt;br /&gt;2             IBM           8001        8004         8005        1&lt;br /&gt;&lt;br /&gt;So the new Machine_X_Number generated in 8003 which is perfect. And the Crrent_Code column has also been updated to the next possible Machine_X_Number which is 8004. So the Scenario 1 has been satisfied by the new SP.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;TEST 3:&lt;br /&gt;Let&#39;s check out if Scenario 2 is satisfied:&lt;br /&gt;&lt;br /&gt;For Location_Code = 2 and Machine_Brand = &#39;HP&#39;, there are four records in the Main_Table with machine codes (0000005002, 0000005003, 0000005004 and 0000005005). Let&#39;s check by executing the following code:&lt;br /&gt;&lt;br /&gt;select Location_Code, Machine_Brand, Machine_X_Code, Number_Of_CPUs from Main_Table &lt;br /&gt;where Location_Code = 2&lt;br /&gt;and  Machine_Brand = &#39;HP&#39;&lt;br /&gt;&lt;br /&gt;Location_Code Machine_Brand             Machine_X_Code Number_Of_CPUs&lt;br /&gt;------------- ------------------------- -------------- --------------&lt;br /&gt;2             HP                        0000005002     2&lt;br /&gt;2             HP                        0000005003     4&lt;br /&gt;2             HP                        0000005004     8&lt;br /&gt;2             HP                        0000005005     32&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Let&#39;s check in the Code_Generation_Table by executing the following code:&lt;br /&gt;&lt;br /&gt;select * from Code_Generation_Table&lt;br /&gt;where Location_Code = 2&lt;br /&gt;and  Machine_Brand = &#39;HP&#39;&lt;br /&gt; &lt;br /&gt;The result set is as follows:&lt;br /&gt;Location_Code Machine_Brand Start_Code  Current_Code End_Code    Increment_Value&lt;br /&gt;------------- ------------  ----------- ------------ ----------- ---------------&lt;br /&gt;2             HP            5001        5003         5005        1&lt;br /&gt;&lt;br /&gt;So as per the given condition for Scenario 2, the new machine_x_code generated should be 5001 and the Current_Code column of the table, Code_Generation_Table should get updated with 8002. So let&#39;s execute the SP from the developer by executng the following code:&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;declare @i_Location_Code Smallint&lt;br /&gt;       ,@i_Machine_Brand VarChar(10)&lt;br /&gt;       ,@o_Machine_X_Code Integer &lt;br /&gt;       ,@o_Return_Value Smallint &lt;br /&gt;&lt;br /&gt;select @i_Location_Code = 2&lt;br /&gt;      ,@i_Machine_Brand = &#39;HP&#39;&lt;br /&gt;      ,@o_Return_Value = 0&lt;br /&gt;&lt;br /&gt;execute usp_Generate_Machine_X_Code&lt;br /&gt;        @i_Location_Code &lt;br /&gt;       ,@i_Machine_Brand &lt;br /&gt;       ,@o_Machine_X_Code OutPut&lt;br /&gt;       ,@o_Return_Value OutPut&lt;br /&gt;&lt;br /&gt;select  @o_Machine_X_Code Machine_Code&lt;br /&gt;       ,@o_Return_Value   Return_Value&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is the result set:&lt;br /&gt;&lt;br /&gt;Machine_Code Return_Value&lt;br /&gt;------------ ------------&lt;br /&gt;5001         1&lt;br /&gt;&lt;br /&gt;It is perfect. Let&#39;s check the Current_Code column of the table, Code_Generation_Table by executing the following code:&lt;br /&gt;&lt;br /&gt;select * from Code_Generation_Table&lt;br /&gt;where Location_Code = 2&lt;br /&gt;and  Machine_Brand = &#39;HP&#39;&lt;br /&gt; &lt;br /&gt;The result set is as follows:&lt;br /&gt;Location_Code Machine_Brand Start_Code  Current_Code End_Code    Increment_Value&lt;br /&gt;------------- ------------  ----------- ------------ ----------- ---------------&lt;br /&gt;2             HP            5001        5002         5005        1&lt;br /&gt;&lt;br /&gt;So the new Machine_X_Number generated in 5001 which is perfect. And the Current_Code column has also been updated to the next possible Machine_X_Number which is 5002. The next possible Machine_X_Number should be equal to the output of the SP + Increment_Value and should exists within the range of Start_Code and End_Code. So the Scenario 2 has been satisfied by the new SP.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;TEST 4:&lt;br /&gt;Let&#39;s check the Scenario 3, the final scenario:&lt;br /&gt;&lt;br /&gt;For Location_Code = 3 and Machine_Brand = &#39;IBM&#39;, there are four records in the Main_Table with machine codes (0000006001, 0000006003, 0000006004 and 0000006005). Let&#39;s check by executing the following code:&lt;br /&gt;&lt;br /&gt;select Location_Code, Machine_Brand, Machine_X_Code, Number_Of_CPUs from Main_Table &lt;br /&gt;where Location_Code = 3&lt;br /&gt;and  Machine_Brand = &#39;IBM&#39;&lt;br /&gt;&lt;br /&gt;Location_Code Machine_Brand             Machine_X_Code Number_Of_CPUs&lt;br /&gt;------------- ------------------------- -------------- --------------&lt;br /&gt;3             IBM                       0000006001     2&lt;br /&gt;3             IBM                       0000006003     2&lt;br /&gt;3             IBM                       0000006004     8&lt;br /&gt;3             IBM                       0000006005     32&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Let&#39;s check in the Code_Generation_Table by executing the following code:&lt;br /&gt;&lt;br /&gt;select * from Code_Generation_Table&lt;br /&gt;where Location_Code = 3&lt;br /&gt;and  Machine_Brand = &#39;IBM&#39;&lt;br /&gt; &lt;br /&gt;The result set is as follows:&lt;br /&gt;Location_Code Machine_Brand Start_Code  Current_Code End_Code    Increment_Value&lt;br /&gt;------------- ------------  ----------- ------------ ----------- ---------------&lt;br /&gt;3             IBM           6001        6004         6005        1&lt;br /&gt;&lt;br /&gt;So as per the given condition for Scenario 3, the new machine_x_code generated should be 6002 and the Current_Code column of the table, Code_Generation_Table should get updated with 6003. So let&#39;s execute the SP from the developer by executng the following code:&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;declare @i_Location_Code Smallint&lt;br /&gt;       ,@i_Machine_Brand VarChar(10)&lt;br /&gt;       ,@o_Machine_X_Code Integer &lt;br /&gt;       ,@o_Return_Value Smallint &lt;br /&gt;&lt;br /&gt;select @i_Location_Code = 3&lt;br /&gt;      ,@i_Machine_Brand = &#39;IBM&#39;&lt;br /&gt;      ,@o_Return_Value = 0&lt;br /&gt;&lt;br /&gt;execute usp_Generate_Machine_X_Code&lt;br /&gt;        @i_Location_Code &lt;br /&gt;       ,@i_Machine_Brand &lt;br /&gt;       ,@o_Machine_X_Code OutPut&lt;br /&gt;       ,@o_Return_Value OutPut&lt;br /&gt;&lt;br /&gt;select  @o_Machine_X_Code Machine_Code&lt;br /&gt;       ,@o_Return_Value   Return_Value&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here is the result set:&lt;br /&gt;&lt;br /&gt;Machine_Code Return_Value&lt;br /&gt;------------ ------------&lt;br /&gt;6002         1&lt;br /&gt;&lt;br /&gt;It is perfect. Let&#39;s check the Current_Code column of the table, Code_Generation_Table by executing the following code:&lt;br /&gt;&lt;br /&gt;select * from Code_Generation_Table&lt;br /&gt;where Location_Code = 3&lt;br /&gt;and  Machine_Brand = &#39;IBM&#39;&lt;br /&gt; &lt;br /&gt;The result set is as follows:&lt;br /&gt;Location_Code Machine_Brand Start_Code  Current_Code End_Code    Increment_Value&lt;br /&gt;------------- ------------  ----------- ------------ ----------- ---------------&lt;br /&gt;3             IBM           6001        6003         6005        1&lt;br /&gt;&lt;br /&gt;So the new Machine_X_Number generated in 6002 which was the missing code in Main_Table for Location_Code = 3 and Machine_Brand = &#39;IBM&#39;. And the Current_Code column has also been updated to the next possible Machine_X_Number which is 6003. The next possible Machine_X_Number should be equal to the output of the SP + Increment_Value and should exist within the range of Start_Code and End_Code. So the Scenario 3 has been satisfied by the new SP.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We too have satisifed all the functional requirements with less scan of the tables Main_Table and Code_Generation_Table. We are not looping except in one scenario where we loop only twice at the most. No matter what the range of values between Start_Code and End_COde, we would scan any table once or at the most twice. Update to Code_Generation_Table also happenns only two times at the most. So the new code can handle larger volumes of data than the original code.&lt;br /&gt;&lt;br /&gt;There is another method of optimiznig this SP further whereby there is only once scan of any table. May be in my next article, I would write about it as I am quite tired today. I feel any SP can be optimized no matter how optimized it already is. The dead end is always too distant. I hope you have enjoyed the article. Please let me know of your feedback which is always very welcome.</content><link rel='replies' type='application/atom+xml' href='http://sqlfinerpoints.blogspot.com/feeds/8182116612076608192/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3544169305649860217/8182116612076608192' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/8182116612076608192'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/8182116612076608192'/><link rel='alternate' type='text/html' href='http://sqlfinerpoints.blogspot.com/2008/09/tricky-scenario-with-t-sql.html' title='A Tricky Scenario with T-SQL'/><author><name>Suresh Kumar Maganti</name><uri>http://www.blogger.com/profile/16645888326754808866</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyuLq44DuP85JRUqfEDgaxcuih4-meamQYzD9Jk-DD_Z1Ul5oh9WXYLo1JF5VzNReeNSgttGQ5dWJgLibF42zcSqeHW9aWQUeDrNfd_ucgf5BQ7975u63-9Bd7VfUwkQ/s220/Suresh.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3544169305649860217.post-4905110054324074167</id><published>2008-03-13T15:54:00.000-05:00</published><updated>2008-07-10T15:53:12.999-05:00</updated><title type='text'>TSQL and Excel - Data Exchanges</title><content type='html'>We are all aware of the applicability of &quot;BCP&quot; for transporting data between external files (text files, Excel files, etc) and database tables, and &quot;Bulk Insert&quot; for sending data one way from external files into database tables. I love BCP. Sometimes, situations arise where we need data from tables to be sent to well formatted Excel spreadsheets and vice-versa. With BCP, one could not send in the Column Headers into the Spreadsheet (it can be done with queryout option but in a round about way) . We always need simple solutions, don&#39;t we?&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Let&#39;s complicate the requirement further. We have to write today&#39;s Top three stocks from the database on to a spreadsheet, i.e. the top three gainers in the Stock Market today. We have to also write the top three losing stocks in the Stock Market today into the same Excel spreadsheet but in a different tab. This single Excel spreadsheet with the two tabs, one for gainers and one for losers needs to be e-mailed to some of the investors daily. We would put the e-mailing issue aside as it is quite simple using SQL Server 2005 Database Mail or SQL Server 2000 SQL Mail.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The second requirement is that the data in both the Excel spreadsheet tabs needs to be overwritten with fresh data daily. The source of all data is just one database table which is fed daily. There are two more tabs in the spreadsheet that will have the consolidated gainers and losers respectively for all days as the days progress.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;It might have been so simple if this overwritting on two tabs (Gainers and Losers) and appending onto two other tabs (AllGainers and AllLosers) was not required. It would have also been so simple if we could have had four separate spreadsheets instead of four tabs in one single spreadsheet. Then we could have simply used the simple BCP and added a little bit of complication in the bcp command to include the Column Headers in there. But that is not the case as we need to keep the solution simple. Some people say, use CLR and do it row by row. Some say, use VC++.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The issue here is to find a simple solution and not complicate the solution further. We don&#39;t want assemblies and DLL&#39;s to be prepared, compiled, registered and then modified whenever something needs to be changed. So CLR, VC++ and the like are not an option. We are talking about efficient programming here. And since we are talking about programming and not about packages, DTS and SSIS are also not an option. We need minimum maintenance too. What do we do?&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We resort to our contemporary T-SQL database programming language. T-SQL is simple, neat, prolific, dynamic and very flexible. Why not explore OPENDATASOURCE and OPENROWSET? Let&#39;s go about solving the problem now.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;So let me prepare the data first. Let&#39;s create a table called Table_Stock_Value and populate the same. This article is just about concepts. So please don&#39;t bother about constraints, indexes and fillfactors right now. All symbols in the data are hypothetical.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;if exists (select 1 from sys.tables&lt;br /&gt;where name = &#39;Table_Stock_Value&#39;&lt;br /&gt;and type = &#39;U&#39;)&lt;br /&gt;drop table Table_Stock_Value&lt;br /&gt;go&lt;br /&gt;create table Table_Stock_Value&lt;br /&gt;(SerialNo int identity (1,1) not null&lt;br /&gt;,NASDAQ_SYMBOL varchar(4) not null&lt;br /&gt;,Company_Name varchar(25) not null&lt;br /&gt;,Change decimal(5,2)&lt;br /&gt;,Date_of_Change datetime not null default getdate())&lt;br /&gt;go&lt;br /&gt;insert into Table_Stock_Value (NASDAQ_SYMBOL, Company_Name, Change, Date_of_Change)&lt;br /&gt;&lt;br /&gt;--TOP GAINERS&lt;br /&gt;select &#39;ABCD&#39;, &#39;ABCD Inc&#39;, 2.50, getdate()-3&lt;br /&gt;union all&lt;br /&gt;select &#39;EFGH&#39;, &#39;EFGH Inc&#39;, 2.00, getdate()-3&lt;br /&gt;union all&lt;br /&gt;select &#39;IJKL&#39;, &#39;IJKL Inc&#39;, 1.50, getdate()-3&lt;br /&gt;union all&lt;br /&gt;select &#39;IJKL&#39;, &#39;IJKL Inc&#39;, 1.75, getdate()-2&lt;br /&gt;union all&lt;br /&gt;select &#39;MNOP&#39;, &#39;MNOP Inc&#39;, 1.60, getdate()-2&lt;br /&gt;union all&lt;br /&gt;select &#39;ABCD&#39;, &#39;ABCD Inc&#39;, 1.00, getdate()-2&lt;br /&gt;union all&lt;br /&gt;select &#39;QRST&#39;, &#39;QRST Inc&#39;, 3.50, getdate()-1&lt;br /&gt;union all&lt;br /&gt;select &#39;UVWX&#39;, &#39;UVWX Inc&#39;, 2.50, getdate()-1&lt;br /&gt;union all&lt;br /&gt;select &#39;ABCD&#39;, &#39;ABCD Inc&#39;, 1.25, getdate()-1&lt;br /&gt;union all&lt;br /&gt;select &#39;IJKL&#39;, &#39;IJKL Inc&#39;, 4.95, getdate()&lt;br /&gt;union all&lt;br /&gt;select &#39;ABCD&#39;, &#39;ABCD Inc&#39;, 2.50, getdate()&lt;br /&gt;union all&lt;br /&gt;select &#39;MNOP&#39;, &#39;MNOP Inc&#39;, 1.25, getdate()&lt;br /&gt;union all&lt;br /&gt;select &#39;ABCD&#39;, &#39;ABCD Inc&#39;, 1.90, getdate()+1&lt;br /&gt;union all&lt;br /&gt;select &#39;EFGH&#39;, &#39;EFGH Inc&#39;, 1.20, getdate()+1&lt;br /&gt;union all&lt;br /&gt;select &#39;QRST&#39;, &#39;QRST Inc&#39;, 1.10, getdate()+1&lt;br /&gt;union all&lt;br /&gt;select &#39;ABCD&#39;, &#39;ABCD Inc&#39;, 1.10, getdate()+2&lt;br /&gt;union all&lt;br /&gt;select &#39;UVWX&#39;, &#39;UVWX Inc&#39;, 1.00, getdate()+2&lt;br /&gt;union all&lt;br /&gt;select &#39;QRST&#39;, &#39;QRST Inc&#39;, 0.95, getdate()+2&lt;br /&gt;union all&lt;br /&gt;select &#39;ABCD&#39;, &#39;ABCD Inc&#39;, 1.50, getdate()+3&lt;br /&gt;union all&lt;br /&gt;select &#39;EFGH&#39;, &#39;EFGH Inc&#39;, 1.25, getdate()+3&lt;br /&gt;union all&lt;br /&gt;select &#39;IJKL&#39;, &#39;IJKL Inc&#39;, 1.00, getdate()+3&lt;br /&gt;&lt;br /&gt;UNION ALL&lt;br /&gt;--TOP LOSERS&lt;br /&gt;select &#39;BBBB&#39;, &#39;BBBB Inc&#39;, -1.50, getdate()-3&lt;br /&gt;union all&lt;br /&gt;select &#39;CCCC&#39;, &#39;CCCC Inc&#39;, -1.20, getdate()-3&lt;br /&gt;union all&lt;br /&gt;select &#39;DDDD&#39;, &#39;DDDD Inc&#39;, -1.10, getdate()-3&lt;br /&gt;union all&lt;br /&gt;select &#39;EEEE&#39;, &#39;EEEE Inc&#39;, -1.75, getdate()-2&lt;br /&gt;union all&lt;br /&gt;select &#39;FFFF&#39;, &#39;FFFF Inc&#39;, -1.60, getdate()-2&lt;br /&gt;union all&lt;br /&gt;select &#39;YYYY&#39;, &#39;YYYY Inc&#39;, -1.00, getdate()-2&lt;br /&gt;union all&lt;br /&gt;select &#39;IJKL&#39;, &#39;IJKL Inc&#39;, -0.70, getdate()-1&lt;br /&gt;union all&lt;br /&gt;select &#39;HHHH&#39;, &#39;HHHH Inc&#39;, -0.62, getdate()-1&lt;br /&gt;union all&lt;br /&gt;select &#39;CCCC&#39;, &#39;CCCC Inc&#39;, -0.25, getdate()-1&lt;br /&gt;union all&lt;br /&gt;select &#39;UUUU&#39;, &#39;UUUU Inc&#39;, -4.95, getdate()&lt;br /&gt;union all&lt;br /&gt;select &#39;LLLL&#39;, &#39;LLLL Inc&#39;, -2.50, getdate()&lt;br /&gt;union all&lt;br /&gt;select &#39;AAAA&#39;, &#39;AAAA Inc&#39;, -1.25, getdate()&lt;br /&gt;union all&lt;br /&gt;select &#39;UVWX&#39;, &#39;UVWX Inc&#39;, -1.90, getdate()+1&lt;br /&gt;union all&lt;br /&gt;select &#39;FFFF&#39;, &#39;FFFF Inc&#39;, -1.20, getdate()+1&lt;br /&gt;union all&lt;br /&gt;select &#39;CCCC&#39;, &#39;CCCC Inc&#39;, -1.10, getdate()+1&lt;br /&gt;union all&lt;br /&gt;select &#39;CCCC&#39;, &#39;CCCC Inc&#39;, -0.80, getdate()+2&lt;br /&gt;union all&lt;br /&gt;select &#39;BBBB&#39;, &#39;BBBB Inc&#39;, -0.75, getdate()+2&lt;br /&gt;union all&lt;br /&gt;select &#39;EEEE&#39;, &#39;EEEE Inc&#39;, -0.65, getdate()+2&lt;br /&gt;union all&lt;br /&gt;select &#39;EEEE&#39;, &#39;EEEE Inc&#39;, -1.50, getdate()+3&lt;br /&gt;union all&lt;br /&gt;select &#39;YYYY&#39;, &#39;YYYY Inc&#39;, -1.25, getdate()+3&lt;br /&gt;union all&lt;br /&gt;select &#39;QRST&#39;, &#39;QRST Inc&#39;, -1.00, getdate()+3&lt;br /&gt;go&lt;br /&gt;create clustered index ci_date on Table_Stock_Value (Date_of_Change) with fillfactor = 70&lt;br /&gt;go&lt;br /&gt;select * from Table_Stock_Value&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Next for one time only, we need to prepare an Excel spreadsheet with four tabs in it. The names of the tabs are, Gainers, Losers, AllGainers, and AllLosers. And each tab can have the column names same or different than in the table, Table_Stock_Value created above. For simplicity, let&#39;s keep the column names same in the tabs. I have created and kept the Excel spreadsheet in the folder, C:\SQLXL. I have named the Excel spreadsheet as Stock_Overview.xls.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Please remember to close the Excel sheet before the initial compilation of the SP and before each instance of execution of the SP.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now, let&#39;s set out to do what we need to do i.e. writing into the Excel spreadsheet&#39;s different tabs as per the requirements stated above. I have written the SP as follows. Please note that the SQL Server advanced configuration option, &#39;Ad Hoc Distributed Queries&#39; should be set to 1 to read from and write to the Excel Spreadsheet from within a database Stored Procedure:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;set ansi_warnings on&lt;br /&gt;go&lt;br /&gt;set ansi_nulls on&lt;br /&gt;Go&lt;br /&gt;execute sp_configure &#39;show advanced options&#39;, 1&lt;br /&gt;go&lt;br /&gt;reconfigure&lt;br /&gt;go&lt;br /&gt;execute sp_configure &#39;Ad Hoc Distributed Queries&#39;, 1&lt;br /&gt;go&lt;br /&gt;reconfigure&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;if exists (select 1 from sys.procedures&lt;br /&gt;where type = &#39;P&#39;&lt;br /&gt;and [NAME] = &#39;DBP_Prepare_Daily_Stock_Data&#39;)&lt;br /&gt;drop procedure [DBP_Prepare_Daily_Stock_Data]&lt;br /&gt;GO&lt;br /&gt;create procedure DBP_Prepare_Daily_Stock_Data&lt;br /&gt;@i_date datetime = null&lt;br /&gt;as&lt;br /&gt;begin&lt;br /&gt;declare @l_File_Exists int&lt;br /&gt;select @i_date = isnull(@i_date, getdate())&lt;br /&gt;execute master.dbo.XP_fileexist &#39;C:\SQLXL\Stock_Overview.xls&#39;, @l_File_Exists output&lt;br /&gt;if (@l_File_Exists = 1)&lt;br /&gt;begin&lt;br /&gt;--Gainers have to be overwritten everyday.&lt;br /&gt;UPDATE OPENDATASOURCE (&#39;Microsoft.Jet.OleDB.4.0&#39;,&lt;br /&gt;&#39;Data Source=&quot;C:\SQLXL\Stock_Overview.xls&quot;;&lt;br /&gt;extended Properties=Excel 8.0&#39;)...Gainers$&lt;br /&gt;SET SerialNo = &#39;&#39;&lt;br /&gt;,NASDAQ_SYMBOL = &#39;&#39;&lt;br /&gt;,Company_Name = &#39;&#39;&lt;br /&gt;,Change = &#39;&#39;&lt;br /&gt;,Date_of_Change = &#39;&#39;&lt;br /&gt;WHERE SerialNo is not null&lt;br /&gt;--Losers have to be overwritten everyday.&lt;br /&gt;UPDATE OPENDATASOURCE (&#39;Microsoft.Jet.OleDB.4.0&#39;,&lt;br /&gt;&#39;Data Source=&quot;C:\SQLXL\Stock_Overview.xls&quot;;&lt;br /&gt;extended Properties=Excel 8.0&#39;)...Losers$&lt;br /&gt;SET SerialNo = &#39;&#39;&lt;br /&gt;,NASDAQ_SYMBOL = &#39;&#39;&lt;br /&gt;,Company_Name = &#39;&#39;&lt;br /&gt;,Change = &#39;&#39;&lt;br /&gt;,Date_of_Change = &#39;&#39;&lt;br /&gt;WHERE SerialNo is not null&lt;br /&gt;--UPDATE/Overwrite the Gainers tab with top 3 gainers&#39; data for the given day, @i_date.&lt;br /&gt;insert into OPENROWSET(&#39;Microsoft.Jet.OLEDB.4.0&#39;,&lt;br /&gt;&#39;Excel 8.0;Database=C:\SQLXL\Stock_Overview.xls;&#39;,&lt;br /&gt;&#39;SELECT SerialNo, NASDAQ_SYMBOL, Company_Name, Change, Date_of_Change FROM [Gainers$A1:E1]&#39;)&lt;br /&gt;select top 3 SerialNo, NASDAQ_SYMBOL, Company_Name, Change, Date_of_Change&lt;br /&gt;from Table_Stock_Value with (nolock)&lt;br /&gt;where convert(varchar, Date_of_Change, 112) = convert(varchar, @i_date, 112)&lt;br /&gt;and Change &gt; 0.00&lt;br /&gt;order by Change desc&lt;br /&gt;--UPDATE/Overwrite the Losers tab with top 3 losers&#39; data for the given day, @i_date.&lt;br /&gt;insert into OPENROWSET(&#39;Microsoft.Jet.OLEDB.4.0&#39;,&lt;br /&gt;&#39;Excel 8.0;Database=C:\SQLXL\Stock_Overview.xls;&#39;,&lt;br /&gt;&#39;SELECT SerialNo, NASDAQ_SYMBOL, Company_Name, Change, Date_of_Change FROM [Losers$A1:E1]&#39;)&lt;br /&gt;select top 3 SerialNo, NASDAQ_SYMBOL, Company_Name, Change, Date_of_Change&lt;br /&gt;from Table_Stock_Value with (nolock)&lt;br /&gt;where convert(varchar, Date_of_Change, 112) = convert(varchar, @i_date, 112)&lt;br /&gt;and Change &lt; 0.00&lt;br /&gt;order by Change desc&lt;br /&gt;--UPDATE/APPEND the AllGainers tab with top 3 gainers&#39; data for the given day, @i_date.&lt;br /&gt;insert into OPENROWSET(&#39;Microsoft.Jet.OLEDB.4.0&#39;, &lt;br /&gt;&#39;Excel 8.0;Database=C:\SQLXL\Stock_Overview.xls;&#39;, &lt;br /&gt;&#39;SELECT SerialNo, NASDAQ_SYMBOL, Company_Name, Change, Date_of_Change FROM [AllGainers$A1:E1]&#39;) &lt;br /&gt;select top 3 SerialNo, NASDAQ_SYMBOL, Company_Name, Change, Date_of_Change &lt;br /&gt;from Table_Stock_Value with (nolock)&lt;br /&gt;where convert(varchar, Date_of_Change, 112) = convert(varchar, @i_date, 112)&lt;br /&gt;and Change &gt; 0.00&lt;br /&gt;order by Change desc&lt;br /&gt;insert into OPENROWSET(&#39;Microsoft.Jet.OLEDB.4.0&#39;, &lt;br /&gt;&#39;Excel 8.0;Database=C:\SQLXL\Stock_Overview.xls;&#39;, &lt;br /&gt;&#39;SELECT SerialNo, NASDAQ_SYMBOL, Company_Name, Change, Date_of_Change FROM [AllLosers$A1:E1]&#39;) &lt;br /&gt;select top 3 SerialNo, NASDAQ_SYMBOL, Company_Name, Change, Date_of_Change &lt;br /&gt;from Table_Stock_Value with (nolock)&lt;br /&gt;where convert(varchar, Date_of_Change, 112) = convert(varchar, @i_date, 112)&lt;br /&gt;and Change &lt; 0.00&lt;br /&gt;order by Change desc&lt;br /&gt;PRINT &#39;RESULTS HAVE BEEN WRITTEN TO C:\SQLXL\Stock_Overview.xls&#39;&lt;br /&gt;end&lt;br /&gt;else&lt;br /&gt;Begin&lt;br /&gt;Print &#39;The file Stock_Overview.xls should exist in the correct path, C:\SQLXL.&#39;&lt;br /&gt;PRINT &#39;RESULTS COULD NOT BE WRITTEN TO C:\SQLXL\Stock_Overview.xls. THE FILE IS OPEN OR DOES NOT EXIST.&#39;&lt;br /&gt;End&lt;br /&gt;end&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;The SP being ready now, let&#39;s check if the all the tabs in the Excel spreadsheet, &lt;br /&gt;C:\SQLXL\Stock_Overview.xls are empty:&lt;br /&gt;&lt;br /&gt;select * &lt;br /&gt;FROM OPENROWSET(&#39;Microsoft.Jet.OLEDB.4.0&#39;, &lt;br /&gt;&#39;Excel 8.0;DATABASE=C:\SQLXL\Stock_Overview.xls&#39;, &#39;Select * from [Gainers$]&#39;) &lt;br /&gt;go&lt;br /&gt;select * &lt;br /&gt;FROM OPENROWSET(&#39;Microsoft.Jet.OLEDB.4.0&#39;, &lt;br /&gt;&#39;Excel 8.0;DATABASE=C:\SQLXL\Stock_Overview.xls&#39;, &#39;Select * from [Losers$]&#39;) &lt;br /&gt;go&lt;br /&gt;select * &lt;br /&gt;FROM OPENROWSET(&#39;Microsoft.Jet.OLEDB.4.0&#39;, &lt;br /&gt;&#39;Excel 8.0;DATABASE=C:\SQLXL\Stock_Overview.xls&#39;, &#39;Select * from [AllGainers$]&#39;) &lt;br /&gt;go&lt;br /&gt;select * &lt;br /&gt;FROM OPENROWSET(&#39;Microsoft.Jet.OLEDB.4.0&#39;, &lt;br /&gt;&#39;Excel 8.0;DATABASE=C:\SQLXL\Stock_Overview.xls&#39;, &#39;Select * from [AllLosers$]&#39;) &lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;They are empty.&lt;br /&gt;&lt;br /&gt;Let&#39;s execute the SP for the earliest date in the table Table_Stock_Value, i.e. getdate()-3:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;declare @i_date datetime&lt;br /&gt;select @i_date = getdate()-3&lt;br /&gt;exec DBP_Prepare_Daily_Stock_Data @i_date&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;Now open the spreadsheet, C:\SQLXL\Stock_Overview.xls and check it out. The data has been written to the respective tabs for the given input date to the SP, DBP_Prepare_Daily_Stock_Data. &lt;br /&gt;&lt;br /&gt;You can also execute the following T-SQL code from your SQL Server database to check the same:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;select * &lt;br /&gt;FROM OPENROWSET(&#39;Microsoft.Jet.OLEDB.4.0&#39;, &lt;br /&gt;&#39;Excel 8.0;DATABASE=C:\SQLXL\Stock_Overview.xls&#39;, &#39;Select * from [Gainers$]&#39;) &lt;br /&gt;go&lt;br /&gt;select * &lt;br /&gt;FROM OPENROWSET(&#39;Microsoft.Jet.OLEDB.4.0&#39;, &lt;br /&gt;&#39;Excel 8.0;DATABASE=C:\SQLXL\Stock_Overview.xls&#39;, &#39;Select * from [Losers$]&#39;) &lt;br /&gt;go&lt;br /&gt;select * &lt;br /&gt;FROM OPENROWSET(&#39;Microsoft.Jet.OLEDB.4.0&#39;, &lt;br /&gt;&#39;Excel 8.0;DATABASE=C:\SQLXL\Stock_Overview.xls&#39;, &#39;Select * from [AllGainers$]&#39;) &lt;br /&gt;go&lt;br /&gt;select * &lt;br /&gt;FROM OPENROWSET(&#39;Microsoft.Jet.OLEDB.4.0&#39;, &lt;br /&gt;&#39;Excel 8.0;DATABASE=C:\SQLXL\Stock_Overview.xls&#39;, &#39;Select * from [AllLosers$]&#39;) &lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The data in the spreadsheet and the result sets above would match.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Let&#39;s execute the SP for the next date in the table Table_Stock_Value, i.e. getdate()-2:&lt;br /&gt;&lt;br /&gt;declare @i_date datetime&lt;br /&gt;select @i_date = getdate()-3&lt;br /&gt;exec DBP_Prepare_Daily_Stock_Data @i_date&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;Now open the spreadsheet, C:\SQLXL\Stock_Overview.xls and check it out. The data has been written to the respective tabs for the given input date to the SP, DBP_Prepare_Daily_Stock_Data. Please close the spreadsheet after reading its contents.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;To verfiy the results using T-SQL itself without opening the spreadsheet, you can use the following code too and verify the results:&lt;/p&gt;&lt;p&gt;&lt;br /&gt;select *&lt;br /&gt;FROM OPENROWSET(&#39;Microsoft.Jet.OLEDB.4.0&#39;,&lt;br /&gt;&#39;Excel 8.0;DATABASE=C:\SQLXL\Stock_Overview.xls&#39;, &#39;Select * from [Gainers$]&#39;)&lt;br /&gt;go&lt;br /&gt;select *&lt;br /&gt;FROM OPENROWSET(&#39;Microsoft.Jet.OLEDB.4.0&#39;,&lt;br /&gt;&#39;Excel 8.0;DATABASE=C:\SQLXL\Stock_Overview.xls&#39;, &#39;Select * from [Losers$]&#39;)&lt;br /&gt;go&lt;br /&gt;select *&lt;br /&gt;FROM OPENROWSET(&#39;Microsoft.Jet.OLEDB.4.0&#39;,&lt;br /&gt;&#39;Excel 8.0;DATABASE=C:\SQLXL\Stock_Overview.xls&#39;, &#39;Select * from [AllGainers$]&#39;)&lt;br /&gt;go&lt;br /&gt;select *&lt;br /&gt;FROM OPENROWSET(&#39;Microsoft.Jet.OLEDB.4.0&#39;,&lt;br /&gt;&#39;Excel 8.0;DATABASE=C:\SQLXL\Stock_Overview.xls&#39;, &#39;Select * from [AllLosers$]&#39;)&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;Note that we have met the requirement.&lt;br /&gt;&lt;br /&gt;Let&#39;s do it for getdate()-1 and getdate() as well.&lt;br /&gt;&lt;br /&gt;declare @i_date datetime&lt;br /&gt;select @i_date = getdate()-1&lt;br /&gt;exec DBP_Prepare_Daily_Stock_Data @i_date&lt;br /&gt;&lt;br /&gt;Now open the Excel Spreadsheet. You would find the Gainer and Losers tabs overwritten with the top 3 gainers and top 3 losers respectively for the date, getdate()-1. You would also notice that the same data has been appended respectively to AllGainers and AllLosers tabs within the same spreadsheet. So, the Gainers and Losers tabs always have three rows each and these rows pertain specifically to the given input date to the SP. The AllGainers and AllLosers tabs have their number of rows growing with each execution of the SP as new rows get appended behind the older rows thus building up a historical data for later analysis. And that&#39;s all we want.&lt;/p&gt;&lt;p&gt;Let&#39;s execute the SP for @i_date = getdate().&lt;br /&gt;&lt;br /&gt;declare @i_date datetime&lt;br /&gt;select @i_date = getdate()&lt;br /&gt;exec DBP_Prepare_Daily_Stock_Data @i_date&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;Now open the Excel Spreadsheet. You would find the Gainer and Losers tabs overwritten with the top 3 gainers and top 3 losers respectively for the given input date, getdate(). You would also notice that the same data has been appended respectively to AllGainers and AllLosers tabs within the same spreadsheet. So, the Gainers and Losers tabs always have three rows each and these rows pertain specifically to the given input date for top three gainers and losers respectively for the respective tabs. The AllGainers and AllLosers tabs have their number of rows growing with each execution as new rows get appended behind the older rows thus building up a historical data for later analysis. We are done with verifying that we have satisfied the requirements that we initially started out with.&lt;/p&gt;&lt;p&gt;You can checkout the contents of the spreasheet by just opening it from its location. Alternatively, you can use the following code. Please close the spreadsheet after physically reading its contents.:&lt;br /&gt;&lt;br /&gt;select * FROM OPENROWSET(&#39;Microsoft.Jet.OLEDB.4.0&#39;,&lt;br /&gt;&#39;Excel 8.0;DATABASE=C:\SQLXL\Stock_Overview.xls&#39;, &#39;Select * from [Gainers$]&#39;)&lt;br /&gt;go&lt;br /&gt;select *&lt;br /&gt;FROM OPENROWSET(&#39;Microsoft.Jet.OLEDB.4.0&#39;,&lt;br /&gt;&#39;Excel 8.0;DATABASE=C:\SQLXL\Stock_Overview.xls&#39;, &#39;Select * from [Losers$]&#39;)&lt;br /&gt;go&lt;br /&gt;select *&lt;br /&gt;FROM OPENROWSET(&#39;Microsoft.Jet.OLEDB.4.0&#39;,&lt;br /&gt;&#39;Excel 8.0;DATABASE=C:\SQLXL\Stock_Overview.xls&#39;, &#39;Select * from [AllGainers$]&#39;)&lt;br /&gt;go&lt;br /&gt;select *&lt;br /&gt;FROM OPENROWSET(&#39;Microsoft.Jet.OLEDB.4.0&#39;,&lt;br /&gt;&#39;Excel 8.0;DATABASE=C:\SQLXL\Stock_Overview.xls&#39;, &#39;Select * from [AllLosers$]&#39;)&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;Please remember to close the Excel sheet before each execution of the SP.&lt;br /&gt;&lt;br /&gt;Now let me throw a little bit of explanation behind overwriting and appending the Excel spreadsheet tabs: &lt;br /&gt;&lt;br /&gt;First of all, in order to read from and write to an external data source like the Excel spreadsheet in this case while not using the BCP or BULK INSERT commands, the advanced SQL Server configuration option &#39;Ad Hoc Distributed Queries&#39; must be set to 1. This way, writes to and reads from the Excel spreadsheet get enabled. This&#39;s what we did first.&lt;br /&gt;&lt;br /&gt;Next, we UPDATE any existing records in the Gainers and Losers tabs with an empty string, &#39;&#39;. This is equivalent to DELETEing all existing records in both these tabs. Even then, the empty characher string remains in the existing cells although it is not visible. &lt;br /&gt;&lt;br /&gt;The next step is to overwrite these existing cells in the Gainers and Losers tabs. For this purpose, we use the INSERT statement. Please note that we use the starting and ending cells of the first row in the Gainers and Losers tab in the OPENDATASOURCE statement (A1:E1). This will enable overwriting the existing cells. The smart thing here is that any empty strings &#39;&#39; are no more visible even if they exist somwhere else in the spreadsheet. &lt;br /&gt;&lt;br /&gt;For the tabs AllGainers and AllLosers, since we don&#39;t want to overwrite existing data but instead want to append new data to any existing data, we omit mentioning the starting row A1:E1 in the OPENDATASOURCE function of the INSERT statement. That ensures that new rows are always appended to the existing rows in these two tabs.&lt;br /&gt;&lt;br /&gt;You can use the spreadsheet and its tabs as if they were individual tables, for reading and performing calculations. You can apply all types of T-SQL constructs on that data to make aggregations, etc. Please note the sample T-SQL code above that we used to check the result sets in the tabs of the spreadsheet after executing the SP.&lt;br /&gt;&lt;br /&gt;One important point to note is that before compiling the SP, the spreadsheet with the appropriate tabs in it should exist in the correct path mentioned in the SP. Otherwise the SP will not get compiled and will instead throw an error.&lt;br /&gt;&lt;br /&gt;Another important point is that during compilation and execution of the SP, the spreadsheet itself should not be physically openend from the Windows Explorer.&lt;br /&gt;&lt;br /&gt;Ofcourse, sufficient scope exists for making improvisations to the SP to calculate the top stock for the month or for generating some special reports, etc. I am limited myself to just explaining the manipulation of an Excel spreadseet and its tabs using T-SQL. And the example has been designed to just show that. Using Database Mail to e-mail the spreadsheet is outside the scope of this article.&lt;br /&gt;&lt;br /&gt;I hope you like the technique of writing to and manipulating spreadsheets and their tabs using simple T-SQL. Please let me know of your comments and thoughts.</content><link rel='replies' type='application/atom+xml' href='http://sqlfinerpoints.blogspot.com/feeds/4905110054324074167/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3544169305649860217/4905110054324074167' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/4905110054324074167'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/4905110054324074167'/><link rel='alternate' type='text/html' href='http://sqlfinerpoints.blogspot.com/2008/03/tsql-and-excel-data-exchanges.html' title='TSQL and Excel - Data Exchanges'/><author><name>Suresh Kumar Maganti</name><uri>http://www.blogger.com/profile/16645888326754808866</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyuLq44DuP85JRUqfEDgaxcuih4-meamQYzD9Jk-DD_Z1Ul5oh9WXYLo1JF5VzNReeNSgttGQ5dWJgLibF42zcSqeHW9aWQUeDrNfd_ucgf5BQ7975u63-9Bd7VfUwkQ/s220/Suresh.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3544169305649860217.post-1719104011857760586</id><published>2008-03-10T13:27:00.000-05:00</published><updated>2008-09-08T17:32:34.997-05:00</updated><title type='text'>Simplifying Code: Updating Variables</title><content type='html'>A good programming practice means being able to use every available option one can to reduce code and at the same time increase speed of execution. A merit of blogging is that such methods can be explained but demontration of using the sample code as it is on large databases is not possible because of the obvious limitations on how much can be written inside one article. That fact however does not in any way takes away the applicability of such methods on large databases. In today&#39;s article, we will see how an unrelated construct/statement can help in reducing code and speeding up execution.&lt;br /&gt;&lt;br /&gt;First let us build the base table, Order_Master:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;create table Order_Master&lt;br /&gt;(Order_ID smallint not null&lt;br /&gt;,Begin_Date datetime not null&lt;br /&gt;,End_Date datetime not null&lt;br /&gt;,Delivery_Location varchar(50) not null)&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;Now, let&#39;s put in some data into the table, Order_Master:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Insert into Order_Master (Order_ID, Begin_Date, End_Date, Delivery_Location)&lt;br /&gt;select 1, getdate()-100, getdate()+100, &#39;New York&#39;&lt;br /&gt;union all&lt;br /&gt;select 2, getdate()-50, getdate()+50, &#39;Washington D.C&#39;&lt;br /&gt;union all&lt;br /&gt;select 3, getdate()-25, getdate()+25, &#39;Princeton&#39;&lt;br /&gt;union all&lt;br /&gt;select 4, getdate()-12, getdate()+12, &#39;Dallas&#39;&lt;br /&gt;union all&lt;br /&gt;select 5, getdate()-6, getdate()+6, &#39;Los Angeles&#39;&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;Let&#39;s read from the table, Order_Master:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;select * from Order_Master&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Order_ID         Begin_Date              End_Date                Delivery_Location&lt;br /&gt;--------         ----------------------- ----------------------- --------------------&lt;br /&gt;1                2007-12-01 14:06:04.087 2008-06-18 14:06:04.087 New York&lt;br /&gt;2                2008-01-20 14:06:04.087 2008-04-29 14:06:04.087 Washington D.C&lt;br /&gt;3                2008-02-14 14:06:04.087 2008-04-04 14:06:04.087 Princeton&lt;br /&gt;4                2008-02-27 14:06:04.087 2008-03-22 14:06:04.087 Dallas&lt;br /&gt;5                2008-03-04 14:06:04.087 2008-03-16 14:06:04.087 Los Angeles&lt;br /&gt;&lt;br /&gt;Now let&#39;s come to the scenario.&lt;br /&gt;&lt;br /&gt;&lt;p&gt;1. For a given Order_ID and for a given input date, we need to find out what the begin date and end date for that Order_ID are. &lt;/p&gt;&lt;p&gt;2. We also need to find out whether the given input date falls within the begin date and end date for that order ID. &lt;/p&gt;&lt;p&gt;3. The output needs to be the Begin Date, the end date and a return value which would be 1 if the given input date falls within the begin date and end date for that order ID or 0 if it doesn&#39;t. &lt;/p&gt;&lt;br /&gt;Given these requirements, a normal Stored Procedure would be like the following:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;if exists (select 1 from sys.procedures&lt;br /&gt;where type = &#39;P&#39;&lt;br /&gt;and user_name(schema_id) = &#39;dbo&#39;&lt;br /&gt;and [NAME] = &#39;DBP_ValidateOrderDates&#39;)&lt;br /&gt;drop procedure [dbo].[DBP_ValidateOrderDates]&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;CREATE PROCEDURE [dbo].[DBP_ValidateOrderDates]&lt;br /&gt;@i_OrderNo varchar(6),&lt;br /&gt;@i_ValidationDate datetime,&lt;br /&gt;@o_Return bit = 0 output,&lt;br /&gt;@o_BeginDate datetime = Null output,&lt;br /&gt;@o_EndDate datetime = Null output&lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;br /&gt;SET NOCOUNT ON;&lt;br /&gt;Select @o_BeginDate = Begin_Date&lt;br /&gt;,@o_EndDate = End_Date&lt;br /&gt;from dbo.Order_Master&lt;br /&gt;where Order_ID = @i_OrderNo&lt;br /&gt;IF @@rowcount = 0&lt;br /&gt;Begin&lt;br /&gt;Set @o_Return = 0&lt;br /&gt;End&lt;br /&gt;Else&lt;br /&gt;If (@i_ValidationDate &lt;= @o_EndDate and @i_ValidationDate &gt;= @o_BeginDate)&lt;br /&gt;Begin&lt;br /&gt;set @o_Return = 1&lt;br /&gt;End&lt;br /&gt;Else&lt;br /&gt;Begin&lt;br /&gt;set @o_Return = 0&lt;br /&gt;End&lt;br /&gt;Set nocount off&lt;br /&gt;END&lt;br /&gt;Go&lt;br /&gt;&lt;br /&gt;Now let&#39;s compile and then execute this SP for Order_ID = 1 and input date as today&#39;s date:&lt;br /&gt;&lt;br /&gt;--Declaring the variables:&lt;br /&gt;declare @i_OrderNo varchar(6),&lt;br /&gt;@i_ValidationDate datetime,&lt;br /&gt;@o_Return bit,&lt;br /&gt;@o_BeginDate datetime,&lt;br /&gt;@o_EndDate datetime&lt;br /&gt;&lt;br /&gt;--Assigning values to input variables/parameters:&lt;br /&gt;select @i_OrderNo = 1&lt;br /&gt;,@i_ValidationDate = getdate()&lt;br /&gt;,@o_Return = null&lt;br /&gt;&lt;br /&gt;--Executing the SP with the given input parameters:&lt;br /&gt;execute [dbo].[DBP_ValidateOrderDates]&lt;br /&gt;@i_OrderNo&lt;br /&gt;,@i_ValidationDate&lt;br /&gt;,@o_Return output&lt;br /&gt;,@o_BeginDate output&lt;br /&gt;,@o_EndDate output&lt;br /&gt;&lt;br /&gt;--Capturing the output:&lt;br /&gt;select&lt;br /&gt;@o_Return &#39;Return Value&#39;, @o_BeginDate &#39;Begin Date&#39;, @o_EndDate &#39;End Date&#39;&lt;br /&gt;&lt;br /&gt;Here is the result set as expected. We got the begin and end dates for the given Order_ID (1) and the return value is 1 because the input date falls between the begin and end dates for that Order_ID:&lt;br /&gt;&lt;br /&gt;Return Value Begin Date              End Date&lt;br /&gt;------------ ----------------------- -----------------------&lt;br /&gt;1            2007-12-01 14:06:04.087 2008-06-18 14:06:04.087&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now let&#39;s execute this SP for the same Order_ID (1) but with the input date not falling within the begin and end dates for that Order_ID.&lt;br /&gt;&lt;br /&gt;--Declaring the variables:&lt;br /&gt;declare @i_OrderNo varchar(6),&lt;br /&gt;@i_ValidationDate datetime,&lt;br /&gt;@o_Return bit,&lt;br /&gt;@o_BeginDate datetime,&lt;br /&gt;@o_EndDate datetime&lt;br /&gt;&lt;br /&gt;--Assigning values to input variables/parameters:&lt;br /&gt;select @i_OrderNo = 1&lt;br /&gt;,@i_ValidationDate = getdate()-200&lt;br /&gt;,@o_Return = null&lt;br /&gt;&lt;br /&gt;--Executing the SP with the given input parameters:&lt;br /&gt;execute [dbo].[DBP_ValidateOrderDates]&lt;br /&gt;@i_OrderNo&lt;br /&gt;,@i_ValidationDate&lt;br /&gt;,@o_Return output&lt;br /&gt;,@o_BeginDate output&lt;br /&gt;,@o_EndDate output&lt;br /&gt;&lt;br /&gt;--Capturing the output:&lt;br /&gt;select&lt;br /&gt;@o_Return &#39;Return Value&#39;, @o_BeginDate &#39;Begin Date&#39;, @o_EndDate &#39;End Date&#39;&lt;br /&gt;&lt;br /&gt;Here is the result set as expected. We got the begin and end dates for the given Order_ID (1) and the return value is 0 because the input date does not fall between the begin and end dates for that Order_ID:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Return Value Begin Date              End Date&lt;br /&gt;------------ ----------------------- -----------------------&lt;br /&gt;0            2007-12-01 14:06:04.087 2008-06-18 14:06:04.087&lt;br /&gt;&lt;br /&gt;Now let&#39;s take another possible situation. If we were to input an Order_ID (101) that doesn&#39;t exist in the Order_Master table, we should be getting nulls for the begin and end dates but the return value should be 0. Let&#39;s execute this scenario.&lt;br /&gt;&lt;br /&gt;--Declaring the variables:&lt;br /&gt;declare @i_OrderNo varchar(6),&lt;br /&gt;@i_ValidationDate datetime,&lt;br /&gt;@o_Return bit,&lt;br /&gt;@o_BeginDate datetime,&lt;br /&gt;@o_EndDate datetime&lt;br /&gt;&lt;br /&gt;--Assigning values to input variables/parameters:&lt;br /&gt;select @i_OrderNo = 101&lt;br /&gt;,@i_ValidationDate = getdate()&lt;br /&gt;,@o_Return = null&lt;br /&gt;&lt;br /&gt;--Executing the SP with the given input parameters:&lt;br /&gt;execute [dbo].[DBP_ValidateOrderDates]&lt;br /&gt;@i_OrderNo&lt;br /&gt;,@i_ValidationDate&lt;br /&gt;,@o_Return output&lt;br /&gt;,@o_BeginDate output&lt;br /&gt;,@o_EndDate output&lt;br /&gt;&lt;br /&gt;--Capturing the output:&lt;br /&gt;select&lt;br /&gt;@o_Return &#39;Return Value&#39;, @o_BeginDate &#39;Begin Date&#39;, @o_EndDate &#39;End Date&#39;&lt;br /&gt;&lt;br /&gt;The result set matches with the expected result:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Return Value Begin Date              End Date&lt;br /&gt;------------ ----------------------- -----------------------&lt;br /&gt;0            NULL                    NULL&lt;br /&gt;&lt;br /&gt;Since the input Order_ID, 101 does not exist in the table, the Begin and End dates are null. Since they are null, the input date can not fall between null begin and end dates and hence is 0.&lt;br /&gt;&lt;br /&gt;Issue:&lt;br /&gt;&lt;br /&gt;You may observe that there are so many constructs in this SP:&lt;br /&gt;&lt;br /&gt;One SELECT statement&lt;br /&gt;Three set variable statements&lt;br /&gt;One usage of @@rowcount to check existence of the record&lt;br /&gt;Three Begin-End blocks&lt;br /&gt;Two IF and one ELSE construct&lt;br /&gt;&lt;br /&gt;The SP seems perfectly logical. It has a very logical flow to satisfy all our requirements. But&lt;br /&gt;the question is &quot;Do we really need so many constructs to staify our requirements?&quot;. The answer is &quot;May be Not&quot;. Remember, all we have to do is assign proper value to the output variables/parameters based on the input variables and the data in the table, Order_Master.&lt;br /&gt;&lt;br /&gt;Let&#39;s try to simplify the same SP. Below, I have replace the IF BEGIN-END, IF-ELSE and @@rowcount with one UPDATE CASE statement. Let&#39;s see how it looks now:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;if exists (select 1 from sys.procedures&lt;br /&gt;where type = &#39;P&#39;&lt;br /&gt;and user_name(schema_id) = &#39;dbo&#39;&lt;br /&gt;and [NAME] = &#39;DBP_ValidateOrderDates&#39;)&lt;br /&gt;drop procedure [dbo].[DBP_ValidateOrderDates]&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;CREATE PROCEDURE [dbo].[DBP_ValidateOrderDates]&lt;br /&gt;@i_OrderNo varchar(6),&lt;br /&gt;@i_ValidationDate datetime,&lt;br /&gt;@o_Return bit = 0 output,&lt;br /&gt;@o_BeginDate datetime = Null output,&lt;br /&gt;@o_EndDate datetime = Null output&lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;br /&gt;SET NOCOUNT ON;&lt;br /&gt;update a&lt;br /&gt;set @o_Return = case&lt;br /&gt;when @i_ValidationDate between a.Begin_Date and a.End_Date then 1&lt;br /&gt;else 0&lt;br /&gt;end&lt;br /&gt;,@o_BeginDate = a.Begin_Date&lt;br /&gt;,@o_EndDate = a.End_Date&lt;br /&gt;from (Select Begin_Date, End_Date from dbo.Order_Master with (nolock)&lt;br /&gt;where Order_ID = @i_OrderNo) a&lt;br /&gt;&lt;br /&gt;Set nocount off&lt;br /&gt;END&lt;br /&gt;Go&lt;br /&gt;&lt;br /&gt;Doesn&#39;t the SP look short now? One UPDATE statement takes care of all possibilities and reduces code even though in the original SP, we are not updating any table for that matter. Neither are we updating the table Order_Master in this refined version of the original SP. All we are doing is updating the output variables / parameters based on the input parameters and the corresponding data in the table Order_Master. And that&#39;s all that is expected from this SP. So why take the longer route as in the original version? Not necessary.&lt;br /&gt;&lt;br /&gt;Now let&#39;s execute the refined SP:&lt;br /&gt;&lt;br /&gt;Now let&#39;s compile and then execute this SP for Order_ID = 1 and input date as today&#39;s date. This is the first case above:&lt;br /&gt;&lt;br /&gt;--Declaring the variables:&lt;br /&gt;declare @i_OrderNo varchar(6)&lt;br /&gt;,@i_ValidationDate datetime&lt;br /&gt;,@o_Return bit&lt;br /&gt;,@o_BeginDate datetime&lt;br /&gt;,@o_EndDate datetime&lt;br /&gt;&lt;br /&gt;--Assigning values to input variables/parameters:&lt;br /&gt;select @i_OrderNo = 1&lt;br /&gt;,@i_ValidationDate = getdate()&lt;br /&gt;,@o_Return = null&lt;br /&gt;&lt;br /&gt;--Executing the SP with the given input parameters:&lt;br /&gt;execute [dbo].[DBP_ValidateOrderDates]&lt;br /&gt;@i_OrderNo&lt;br /&gt;,@i_ValidationDate&lt;br /&gt;,@o_Return output&lt;br /&gt;,@o_BeginDate output&lt;br /&gt;,@o_EndDate output&lt;br /&gt;&lt;br /&gt;--Capturing the output:&lt;br /&gt;select&lt;br /&gt;@o_Return &#39;Return Value&#39;, @o_BeginDate &#39;Begin Date&#39;, @o_EndDate &#39;End Date&#39;&lt;br /&gt;&lt;br /&gt;Here is the result set as expected. We got the begin and end dates for the given Order_ID (1) and the return value is 1 because the input date falls between the begin and end dates for that Order_ID:&lt;br /&gt;&lt;br /&gt;Return Value Begin Date              End Date&lt;br /&gt;------------ ----------------------- -----------------------&lt;br /&gt;1            2007-12-01 14:06:04.087 2008-06-18 14:06:04.087&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now let&#39;s execute this SP for the same Order_ID (1) but with the input date not falling within the begin and end dates for that Order_ID. This is the second case above.&lt;br /&gt;&lt;br /&gt;--Declaring the variables:&lt;br /&gt;declare @i_OrderNo varchar(6)&lt;br /&gt;,@i_ValidationDate datetime&lt;br /&gt;,@o_Return bit&lt;br /&gt;,@o_BeginDate datetime&lt;br /&gt;,@o_EndDate datetime&lt;br /&gt;&lt;br /&gt;--Assigning values to input variables/parameters:&lt;br /&gt;select @i_OrderNo = 1&lt;br /&gt;,@i_ValidationDate = getdate()-200&lt;br /&gt;,@o_Return = null&lt;br /&gt;&lt;br /&gt;--Executing the SP with the given input parameters:&lt;br /&gt;execute [dbo].[DBP_ValidateOrderDates]&lt;br /&gt;@i_OrderNo&lt;br /&gt;,@i_ValidationDate&lt;br /&gt;,@o_Return output&lt;br /&gt;,@o_BeginDate output&lt;br /&gt;,@o_EndDate output&lt;br /&gt;&lt;br /&gt;--Capturing the output:&lt;br /&gt;select&lt;br /&gt;@o_Return &#39;Return Value&#39;, @o_BeginDate &#39;Begin Date&#39;, @o_EndDate &#39;End Date&#39;&lt;br /&gt;&lt;br /&gt;Here is the result set as expected. We got the begin and end dates for the given Order_ID (1) and the return value is 0 because the input date does not fall between the begin and end dates for that Order_ID(1):&lt;br /&gt;&lt;br /&gt;Return Value Begin Date              End Date&lt;br /&gt;------------ ----------------------- -----------------------&lt;br /&gt;0            2007-12-01 14:06:04.087 2008-06-18 14:06:04.087&lt;br /&gt;&lt;br /&gt;Now let&#39;s take another possible situation, i.e. the third situation above. If we were to input an Order_ID (101) that doesn&#39;t exist in the Order_Master table, we should be getting nulls for the begin and end dates but the return value should be &lt;br /&gt;0. Let&#39;s execute this scenario.&lt;br /&gt;&lt;br /&gt;--Declaring the variables:&lt;br /&gt;declare @i_OrderNo varchar(6)&lt;br /&gt;,@i_ValidationDate datetime&lt;br /&gt;,@o_Return bit&lt;br /&gt;,@o_BeginDate datetime&lt;br /&gt;,@o_EndDate datetime&lt;br /&gt;&lt;br /&gt;--Assigning values to input variables/parameters:&lt;br /&gt;select @i_OrderNo = 101&lt;br /&gt;,@i_ValidationDate = getdate()&lt;br /&gt;,@o_Return = null&lt;br /&gt;&lt;br /&gt;--Executing the SP with the given input parameters:&lt;br /&gt;execute [dbo].[DBP_ValidateOrderDates]&lt;br /&gt;@i_OrderNo&lt;br /&gt;,@i_ValidationDate&lt;br /&gt;,@o_Return output&lt;br /&gt;,@o_BeginDate output&lt;br /&gt;,@o_EndDate output&lt;br /&gt;&lt;br /&gt;--Capturing the output:&lt;br /&gt;select&lt;br /&gt;@o_Return &#39;Return Value&#39;, @o_BeginDate &#39;Begin Date&#39;, @o_EndDate &#39;End Date&#39;&lt;br /&gt;&lt;br /&gt;The result set matches with the expected result:&lt;br /&gt;&lt;br /&gt;Return Value Begin Date              End Date&lt;br /&gt;------------ ----------------------- -----------------------&lt;br /&gt;0            NULL                    NULL&lt;br /&gt;&lt;br /&gt;Since the input Order_ID, 101 does not exist in the table, the Begin and End dates are null. Since they are null, the input date can not fall between null begin and end dates and hence is 0.&lt;br /&gt;&lt;br /&gt;We get the same results with the refined version of the SP but in one statement that in multiple If&#39;s and ELSE&#39;s and @@rowcount.&lt;br /&gt;&lt;br /&gt;The UPDATE statement in this case doesn&#39;t perform any updates to the table Order_Master itself. It just updates the output parameters to the correct values.&lt;br /&gt;&lt;br /&gt;This is a very simple example of using unrelated operations (UPDATE) where SELECT seems the only solution and thus reducing the amount of code needed to be written and executed. This article just demonstrates that coding flexibility in Microsoft SQL Server&#39;s T-SQL is profound. Based on individual situation, there are numerous possibilities in T-SQL that can reduce code and execution time as well. We will follow some more of such techniques in the forthcoming articles.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div id=&quot;cse-search-results&quot;&gt;&lt;/div&gt;&lt;br /&gt;&lt;script type=&quot;text/javascript&quot;&gt;&lt;br /&gt;  var googleSearchIframeName = &quot;cse-search-results&quot;;&lt;br /&gt;  var googleSearchFormName = &quot;cse-search-box&quot;;&lt;br /&gt;  var googleSearchFrameWidth = 800;&lt;br /&gt;  var googleSearchDomain = &quot;www.google.com&quot;;&lt;br /&gt;  var googleSearchPath = &quot;/cse&quot;;&lt;br /&gt;&lt;/script&gt;&lt;br /&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;http://www.google.com/afsonline/show_afs_search.js&quot;&gt;&lt;/script&gt;</content><link rel='replies' type='application/atom+xml' href='http://sqlfinerpoints.blogspot.com/feeds/1719104011857760586/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3544169305649860217/1719104011857760586' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/1719104011857760586'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/1719104011857760586'/><link rel='alternate' type='text/html' href='http://sqlfinerpoints.blogspot.com/2008/03/simplifying-code-updating-variables.html' title='Simplifying Code: Updating Variables'/><author><name>Suresh Kumar Maganti</name><uri>http://www.blogger.com/profile/16645888326754808866</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyuLq44DuP85JRUqfEDgaxcuih4-meamQYzD9Jk-DD_Z1Ul5oh9WXYLo1JF5VzNReeNSgttGQ5dWJgLibF42zcSqeHW9aWQUeDrNfd_ucgf5BQ7975u63-9Bd7VfUwkQ/s220/Suresh.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3544169305649860217.post-8778135832561496954</id><published>2007-12-11T12:29:00.000-06:00</published><updated>2007-12-11T17:14:03.134-06:00</updated><title type='text'>MERGE statement in SQL Server 2008</title><content type='html'>Since the last few days, I have been spending some time on studying new features in the Katmai betas. One of the better coding introductions I like in Microsoft SQL Server 2008 is the MERGE statement. I like the way it provides option to UPDATE, DELETE and INSERT data simultaneously in a single T-SQL statement. &lt;br /&gt;&lt;br /&gt;       Using it for simple operations is a costly affair when compared to equivalent T-SQL code in 2000/2005. However using it for complex operations is simple and efficient compared to equivalent T-SQL code in 2000/2005. This is the paradox.  I find most prolific coders prefer to write compact code even if is is very complex rather than using lengthy but simple code. The reason most often is that the shorter the code, the faster it performs in most cases. Here is where the paradox associated with MERGE statement comes in handy.&lt;br /&gt;&lt;br /&gt;       Let me create a simple situation first:&lt;br /&gt;&lt;br /&gt;--Create a small table.&lt;br /&gt;create table abc&lt;br /&gt;(productid int identity (1,1)&lt;br /&gt;,Quantity decimal(4,0) default 100&lt;br /&gt;,Order_Date datetime default getdate())&lt;br /&gt;&lt;br /&gt;--Populate the tables, abc.&lt;br /&gt;&lt;br /&gt;insert into abc default values&lt;br /&gt;insert into abc (Quantity) values (125)&lt;br /&gt;insert into abc (Quantity) values (150)&lt;br /&gt;insert into abc (Quantity) values (175)&lt;br /&gt;insert into abc (Quantity) values (200)&lt;br /&gt;insert into abc (Quantity) values (225)&lt;br /&gt;insert into abc (Quantity) values (250)&lt;br /&gt;insert into abc (Quantity) values (275)&lt;br /&gt;insert into abc (Quantity) values (300)&lt;br /&gt;insert into abc (Quantity) values (325)&lt;br /&gt;insert into abc (Quantity) values (350)&lt;br /&gt;insert into abc (Quantity) values (375)&lt;br /&gt;&lt;br /&gt;--Check the contents of the table, abc.&lt;br /&gt;select * from abc   &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;--Add a nullable column to the table, abc. This one column is to be populated (updated) later.&lt;br /&gt;alter table abc&lt;br /&gt;add OrderID int null&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;--Now the MERGE construct updates the OrderID column of the table abc as follows.&lt;br /&gt;--Divide the ProductID by 2. If remainder comes out to be zero, then update&lt;br /&gt;--OrderID with 2. If remainder comes out to a non-zero value, then update&lt;br /&gt;--OrderID as ProductID minus the Quotient.&lt;br /&gt;--The code is given below:&lt;br /&gt;&lt;br /&gt;merge abc as master_table&lt;br /&gt;using (select ProductId, ProductId / @l_OrderID_value, ProductId % @l_OrderID_value&lt;br /&gt;           from abc) as input (ProductId, Quotient, Remainder)&lt;br /&gt;ON master_Table.ProductId = Input.ProductID&lt;br /&gt;When Matched&lt;br /&gt;THEN UPDATE set master_table.OrderID = case&lt;br /&gt;                                                                       when input.Remainder = 0 then @l_OrderID_value&lt;br /&gt;                                                                        else master_Table.ProductId - Input.Quotient&lt;br /&gt;                                                                                end;&lt;br /&gt;&lt;br /&gt;It was very structured but long. This was a simple operation and didn&#39;t need such length of code.&lt;br /&gt;&lt;br /&gt;--Now let&#39;s backup the data in the tables abc.&lt;br /&gt;&lt;br /&gt;select * into def from abc&lt;br /&gt;&lt;br /&gt;Now, let&#39;s write the equivalent T-SQL code as a replacement of the MERGE statement above.&lt;br /&gt;&lt;br /&gt;declare @l_OrderID_value int&lt;br /&gt;set @l_OrderID_value = 2&lt;br /&gt;&lt;br /&gt;update abc&lt;br /&gt;set OrderID = case&lt;br /&gt;                                 when ProductID%@l_OrderID_value = 0 then @l_OrderID_value&lt;br /&gt;                                 else ProductId - (ProductID/@l_OrderID_value)&lt;br /&gt;                         end;&lt;br /&gt;&lt;br /&gt;This was short, simple and just one UPDATE statement was required to do the entire operation.&lt;br /&gt;&lt;br /&gt;If you look for the query cost relative to batch when you run both the statements together and get actual execution plans, the MERGE costs 76% while the single UPDATE costs 24%. The MERGE statement had a SELECT from the table abc, then it uses a derived table, and then it does the UPDATE. In the second statement above, there is a direct UPDATE.  So the second statement is expected to be faster. So in this example, MERGE was not really useful. The above example was a simple case, so we won&#39;t bother ourselves much with MERGE in simple scenarios.&lt;br /&gt;&lt;br /&gt;Now, let&#39;s take up something slightly more involved where MERGE can be really useful.&lt;br /&gt;&lt;br /&gt;--Create a new table.&lt;br /&gt;create table abc_Summary&lt;br /&gt;(OrderID int&lt;br /&gt;,Order_Date datetime&lt;br /&gt;,Quantity int)&lt;br /&gt;&lt;br /&gt;--It is empty.&lt;br /&gt;select * from abc_Summary&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;MERGE abc_summary as TARGET_Table&lt;br /&gt;using (select OrderID, convert(varchar, Order_Date, 112), sum(Quantity) from abc&lt;br /&gt;            group by OrderID, convert(varchar, Order_Date, 112)) as Data (OrderID, Order_Date, Total_Quantity)&lt;br /&gt;on TARGET_Table.OrderID = Data.OrderID&lt;br /&gt;When NOT MATCHED THEN INSERT (OrderID, Order_Date, Quantity)&lt;br /&gt;                                               VALUES (Data.OrderID, Data.Order_Date, Data.Total_Quantity)&lt;br /&gt;WHEN MATCHED THEN&lt;br /&gt;                                              UPDATE SET TARGET_Table.Quantity = Data.Total_Quantity;&lt;br /&gt;&lt;br /&gt;--Verify that the empty table has gotten populated with aggregated data from the main table, abc.&lt;br /&gt;select * from abc_Summary&lt;br /&gt;&lt;br /&gt;--So this time the INSERT took place. Now lets UPDATE the column abc_Summary.Quantity to --zero. And then we will run the above code again.&lt;br /&gt;&lt;br /&gt;update abc_summary&lt;br /&gt;set Quantity = 0&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;--Verify that Quantity is 0.&lt;br /&gt;select * from abc_Summary&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;--Run the same code again:&lt;br /&gt;MERGE abc_summary as TARGET_Table&lt;br /&gt;using (select OrderID, convert(varchar, Order_Date, 112), sum(Quantity) from abc&lt;br /&gt;            group by OrderID, convert(varchar, Order_Date, 112)) as Data (OrderID, Order_Date, Total_Quantity)&lt;br /&gt;on TARGET_Table.OrderID = Data.OrderID&lt;br /&gt;When NOT MATCHED THEN INSERT (OrderID, Order_Date, Quantity)&lt;br /&gt;                                          VALUES (Data.OrderID, Data.Order_Date, Data.Total_Quantity)&lt;br /&gt;WHEN MATCHED THEN&lt;br /&gt;                                       UPDATE SET TARGET_Table.Quantity = Data.Total_Quantity;&lt;br /&gt;&lt;br /&gt;--Verify that the Quantity column in the abc_Summarfy table is updated now with actual summed quantities per OrderID from the parent table, abc.&lt;br /&gt;&lt;br /&gt;select * from abc_Summary&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Lets do something different now, a bit more involved. UPDATE the table abc_Summary such that OrderID&#39;s 1,2 and 3 are removed and Quantity is set to zero for the remaining three rows. Also add one dirty row to the abc_summary table. Note that the table abc_summary should contain only those OrderID&#39;s that already exist in the parent table, abc. Since the newly inserted OrderID in the dirty row does not exist in the parent table, abc, it should be deleted from abc_summary. So in all, what we are trying to do here is following:&lt;br /&gt;INSERT missing rows,&lt;br /&gt;UPDATE Quantity for existing three rows, and&lt;br /&gt;DELETE one dirty row.&lt;br /&gt;&lt;br /&gt;All this is to be accomplished in one query.&lt;br /&gt;Let&#39;s prepare the data now.&lt;br /&gt;&lt;br /&gt;--Preparing data.&lt;br /&gt;update abc_summary&lt;br /&gt;set Quantity = 0&lt;br /&gt;GO&lt;br /&gt;delete from abc_summary&lt;br /&gt;where OrderID in (1, 2, 3)&lt;br /&gt;GO&lt;br /&gt;insert into abc_summary (OrderID, Order_Date, Quantity)&lt;br /&gt;values (2001, getdate(), 50000)&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;--Check the data. OrderID&#39;s 4,5 and 6 have their Quantity zero&#39;ed out. OrderID&#39;s 1,2 and 3 no ---longer exist. A new OrderID 2001 has come in but it doesn&#39;t exist in parent table, abc.&lt;br /&gt;&lt;br /&gt;select * from abc_Summary&lt;br /&gt;&lt;br /&gt;--Now let&#39;s get the correct data into the table abc_summary in one single T-SQL query using the MERGE statement which issues INSERT, UPDATE and DELETE simultaneously.&lt;br /&gt;&lt;br /&gt;MERGE abc_summary as TARGET_Table&lt;br /&gt;using (select OrderID, convert(varchar, Order_Date, 112), sum(Quantity) from abc&lt;br /&gt;            group by OrderID, convert(varchar, Order_Date, 112)) as Data (OrderID, Order_Date, Total_Quantity)&lt;br /&gt;on TARGET_Table.OrderID = Data.OrderID&lt;br /&gt;When NOT MATCHED THEN INSERT (OrderID, Order_Date, Quantity)&lt;br /&gt;                                               VALUES (Data.OrderID, Data.Order_Date, Data.Total_Quantity)&lt;br /&gt;WHEN MATCHED THEN&lt;br /&gt;                                             UPDATE SET TARGET_Table.Quantity = Data.Total_Quantity&lt;br /&gt;WHEN SOURCE NOT MATCHED THEN&lt;br /&gt;                                             DELETE;&lt;br /&gt;&lt;br /&gt;--Check the data. OrderID&#39;s 4,5 and 6 have their Quantity UPDATEd correctly. --Rows for OrderID&#39;s 1,2 and 3 have been INSERTed now. The dirty row for OrderID 2001 has been DELETed as it didn&#39;t exist in parent table, abc.&lt;br /&gt;&lt;br /&gt;select * from abc_Summary&lt;br /&gt;&lt;br /&gt;So this time the INSERT took place and brought in the three mmissing ORDERID&#39;s (1,2,3) from&lt;br /&gt;the table abc into the table abc_Summary. Then it UPDATEd the abc_Summary.Quantity&lt;br /&gt;column for ORDERID&#39;s 3,4,5 from zero to the correct aggregated quantity from the&lt;br /&gt;parent table, abc. Lastly, it also DELETEd the erroneous row with ORDERID 2001 because this&lt;br /&gt;ORDERID did not exist in the parent table, abc. All these three operations were&lt;br /&gt;performed in one single query.    &lt;br /&gt;&lt;br /&gt;All this can be done without the MERGE statement as well. But it would be really very lengthy and expensive code. It would contain several IF-ELSE constructs, IF [NOT] EXISTS clauses, multiple instances of usage of the tables abc and abc_Summary, apart from the INSERT, UPDATE, DELETE statements. We don&#39;t want to do that, do we? The cost of that  query would be much more than the MERGE statement above. Let&#39;s verify the same. Let me try to build as short a code as possible to replace the MERGE statement above.  Please see the replacement code below:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;--Prepare the data first.&lt;br /&gt;update abc_summary&lt;br /&gt;set Quantity = 0&lt;br /&gt;GO&lt;br /&gt;delete from abc_summary&lt;br /&gt;where OrderID in (1, 2, 3)&lt;br /&gt;GO&lt;br /&gt;insert into abc_summary (OrderID, Order_Date, Quantity)&lt;br /&gt;values (2001, getdate(), 50000)&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;--Check the data.&lt;br /&gt;select * from abc_Summary&lt;br /&gt;&lt;br /&gt;--Now let&#39;s get the correct data into the table abc_summary.&lt;br /&gt;--UPDATE Quantity to the correct aggregated value.&lt;br /&gt;update abc_summary&lt;br /&gt;set abc_summary.Quantity = (select sum(abc.Quantity) from abc&lt;br /&gt;                                                      where abc_Summary.OrderID = abc.OrderID)&lt;br /&gt;where exists (select OrderID from abc&lt;br /&gt;                        where abc_Summary.OrderID = abc.OrderID)&lt;br /&gt;&lt;br /&gt;--Check the data. Quantity has been updated correctly.&lt;br /&gt;select * from abc_Summary&lt;br /&gt;&lt;br /&gt;--INSERT missing ORDERID&#39;s 1,2 and 3.&lt;br /&gt;insert into abc_summary (OrderID, Order_Date, Quantity)&lt;br /&gt;select OrderID, convert(varchar, Order_Date, 112), sum(Quantity) from abc&lt;br /&gt;where OrderID not in (select OrderID from abc_Summary)&lt;br /&gt;group by OrderID, convert(varchar, Order_Date, 112)&lt;br /&gt;&lt;br /&gt;--Check the data. Missing ORDERID&#39;s have been re-inserted correctly with the right Quantity.&lt;br /&gt;select * from abc_Summary&lt;br /&gt;&lt;br /&gt;--DELETE dirty row.&lt;br /&gt;delete from abc_Summary&lt;br /&gt;where OrderID not in (select OrderID from abc)&lt;br /&gt;&lt;br /&gt;--Check the data. The row with ORDERID 2001 has been deleted as required.&lt;br /&gt;select * from abc_Summary&lt;br /&gt;&lt;br /&gt;So multiple instances of joins between the parent table abc and the child table abc_Summary had to be used.&lt;br /&gt;&lt;br /&gt;The execution plan showed 40% cost for the MERGE statement and 60%  cost for the replacement statemenets above combined. Here we are talking about just va few rows of data. The benefits rise with increase in the number of rows. You may want to try it out.&lt;br /&gt;&lt;br /&gt;So in all, MERGE statement in Microsoft SQL Server 2008 is really good. It reduces code, does multiple operations in a single statement and is less expensive or is more efficient when we need to perform  a bit complex operations. Please note that the syntax for INSERT, UPDATE and DELETE constructs within the MERGE statement are slightly different from their original counterparts. The MERGE statement opens up a lot of possibilities. Imagine how interesting it would be to use within an AFTER TRIGGER to perform UPDATE, DELETE and INSERT operations on another table in one shot through the trigger. Although it is possible to do it without the MERGE statement as well, but it gets too lengthy and tedious as I had to implement at one of my clients for certain reasons. MERGE can make it compact, simple and efficient.&lt;br /&gt;&lt;br /&gt;In my next article, we will solve some puzzles.</content><link rel='replies' type='application/atom+xml' href='http://sqlfinerpoints.blogspot.com/feeds/8778135832561496954/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3544169305649860217/8778135832561496954' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/8778135832561496954'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/8778135832561496954'/><link rel='alternate' type='text/html' href='http://sqlfinerpoints.blogspot.com/2007/12/merge-statement-in-sql-server-2008.html' title='MERGE statement in SQL Server 2008'/><author><name>Suresh Kumar Maganti</name><uri>http://www.blogger.com/profile/16645888326754808866</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyuLq44DuP85JRUqfEDgaxcuih4-meamQYzD9Jk-DD_Z1Ul5oh9WXYLo1JF5VzNReeNSgttGQ5dWJgLibF42zcSqeHW9aWQUeDrNfd_ucgf5BQ7975u63-9Bd7VfUwkQ/s220/Suresh.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3544169305649860217.post-5105424651294492558</id><published>2007-11-14T09:22:00.000-06:00</published><updated>2007-11-14T11:49:27.119-06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="T-SQL comparison between 2000 and 2005."/><title type='text'>Some T-SQL code in 2005 and 2000.</title><content type='html'>I have sometimes been fazed by CTE (Common Table Expressions) and PIVOT in SQL Server 2005. I like the way they are structured but hate the way they perform. I tried comparing them with our old-style drived tables. And to my surprise, code written using derived tables performed much better that the code written using CTE or PIVOT or both combined. Let me share some code here using the SQL Server 2005 Adventureworks database.&lt;br /&gt;&lt;br /&gt;--Shows Amount per customer:&lt;br /&gt;&lt;br /&gt;WITH Value_from_Customer (CustomerID, SalesOrderID, Total_Value_of_Sale)&lt;br /&gt;AS&lt;br /&gt;(SELECT SOH.CustomerID, SOD.SalesOrderID, sum(SOD.LineTotal)&lt;br /&gt;FROM Sales.SalesOrderDetail AS SOD INNER JOIN Sales.SalesOrderHeader SOH&lt;br /&gt;ON (SOH.SalesOrderID = SOD.SalesOrderID)&lt;br /&gt;GROUP BY SOH.CustomerID, SOD.SalesOrderID&lt;br /&gt;)&lt;br /&gt;SELECT CustomerID&lt;br /&gt;,count(SalesOrderID) as Number_of_Sales&lt;br /&gt;,sum(Total_Value_of_Sale) Value_from_Customer&lt;br /&gt;FROM Value_from_Customer&lt;br /&gt;Group by CustomerID&lt;br /&gt;ORDER BY CustomerID&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;--The same would have been done in SQL Server 2000 as follows:&lt;br /&gt;&lt;br /&gt;SELECT CustomerID&lt;br /&gt;,count(SalesOrderID) as Number_of_Sales&lt;br /&gt;,sum(Total_Value_of_Sale) as Value_from_Customer&lt;br /&gt;from&lt;br /&gt;(SELECT SOH.CustomerID, SOD.SalesOrderID,&lt;br /&gt;sum (SOD.LineTotal) as Total_Value_of_Sale&lt;br /&gt;FROM Sales.SalesOrderDetail AS SOD INNER JOIN Sales.SalesOrderHeader SOH&lt;br /&gt;ON (SOH.SalesOrderID = SOD.SalesOrderID)&lt;br /&gt;GROUP BY SOH.CustomerID, SOD.SalesOrderID) abc&lt;br /&gt;Group by CustomerID&lt;br /&gt;ORDER BY CustomerID&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;The CTE above looks very structured and systematic. The equivalent query with derived table looks more like a block of buildings. But I have found in all my tests that derived tables always outperform CTE&#39;s. In fact, I can share a real story about it. One of my colleagues had a terabyte-plus sized database for a data warehouse. Somebody wrote a 200 line CTE that would fetch a few rows from a combination of several multi-million row tables. It gave the results but after executing for eight hours. It is hard to withstand eight hours when you know that you need just a few rows at the end. I suggested that CTE&#39;s in the query be replaced by regular derived tables. It was a big task but when it was done, the query gave out the same results in 55 seconds. That&#39;s the kind of difference I am talking about. After that some more improvements were done to make it finally run in 10 seconds. However, we would not go into the details of that story here but would stick to differences in performance between new and old T-SQL constructs.&lt;br /&gt;&lt;br /&gt;My sample queries for 2005 and 2000 above would show only marginal difference in execution times becuase the number of rows in the referenced tables is very small. When the number of rows are increased in the underlying tables, the difference in execution times keeps on rising with the 2000 style code outperforming the 2005 CTE code.&lt;br /&gt;&lt;br /&gt;Now let&#39;s take up a PIVOT construct.&lt;br /&gt;&lt;br /&gt;---Shows Sale per year by territory. PIVOT.&lt;br /&gt;&lt;br /&gt;SELECT YEAR_of_SALE&lt;br /&gt;, [1] AS Trtry_1&lt;br /&gt;, [2] AS Trtry_2&lt;br /&gt;, [3] AS Trtry_3&lt;br /&gt;, [4] AS Trtry_4&lt;br /&gt;, [5] AS Trtry_5&lt;br /&gt;, [6] AS Trtry_6&lt;br /&gt;FROM&lt;br /&gt;(SELECT TerritoryID, TotalDue, year (OrderDate) AS YEAR_of_SALE&lt;br /&gt;FROM Sales.SalesOrderHeader) SOH&lt;br /&gt;PIVOT (SUM (TotalDue) FOR TerritoryID IN ( [1], [2], [3], [4], [5], [6] )) AS pvt&lt;br /&gt;ORDER BY YEAR_of_SALE&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;--The same would have been done in SQL 2000 as follows:&lt;br /&gt;&lt;br /&gt;select YEAR_of_SALE&lt;br /&gt;,sum(Trtry_1) as Trtry_1&lt;br /&gt;,sum(Trtry_2) as Trtry_2&lt;br /&gt;,sum(Trtry_3) as Trtry_3&lt;br /&gt;,sum(Trtry_4) as Trtry_4&lt;br /&gt;,sum(Trtry_5) as Trtry_5&lt;br /&gt;,sum(Trtry_6) as Trtry_6&lt;br /&gt;from&lt;br /&gt;(select YEAR_of_SALE&lt;br /&gt;,isnull((select Total_Revenue where TerritoryID = 1), 0) as Trtry_1&lt;br /&gt;,isnull((select Total_Revenue where TerritoryID = 2), 0) as Trtry_2&lt;br /&gt;,isnull((select Total_Revenue where TerritoryID = 3), 0) as Trtry_3&lt;br /&gt;,isnull((select Total_Revenue where TerritoryID = 4), 0) as Trtry_4&lt;br /&gt;,isnull((select Total_Revenue where TerritoryID = 5), 0) as Trtry_5&lt;br /&gt;,isnull((select Total_Revenue where TerritoryID = 6), 0) as Trtry_6&lt;br /&gt;&lt;br /&gt;from&lt;br /&gt;(SELECT TerritoryID&lt;br /&gt;,year (OrderDate) AS YEAR_of_SALE&lt;br /&gt;,sum(TotalDue) Total_Revenue&lt;br /&gt;FROM Sales.SalesOrderHeader&lt;br /&gt;where TerritoryID between 1 and 6&lt;br /&gt;GROUP BY TerritoryID, year (OrderDate)) abc) def&lt;br /&gt;&lt;br /&gt;GROUP BY YEAR_of_SALE&lt;br /&gt;ORDER BY YEAR_of_SALE&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;Now as you can see, the 2005 query with PIVOT looks simple and neat. The equivalent query performing the same functionality but written using derived tables looks tedious. However, the second query with derived tables outperforms the first one with PIVOT. I agree that the first query with PIVOT requires much less coding effort and looks much less complex than the second one using derived tables. But what are clients interested in? Coding complexity or performance. It is always performance that everybody is interested in. When a customer clicks on a link, he/she expects results fast and doesn&#39;t really care what code is executing in the background. So we have to use code which performs better.&lt;br /&gt;&lt;br /&gt;Now lets take another example where I combine a CTE with a PIVOT structure. Please note that below the 2005 style query with CTE and PIVOT combined, I have an equivalent for 2000 using derived tables:&lt;br /&gt;&lt;br /&gt;--2005.&lt;br /&gt;--Find number of physical sales by year and their total value.&lt;br /&gt;--Break down the yearly value by territory upto six territories.&lt;br /&gt;&lt;br /&gt;WITH Value_of_Sale (SalesOrderID, Transaction_Date, Total_Value_of_Sale)&lt;br /&gt;AS&lt;br /&gt;(SELECT SOD.SalesOrderID, SOH.ModifiedDate, sum(SOD.LineTotal)&lt;br /&gt;FROM Sales.SalesOrderDetail AS SOD INNER JOIN Sales.SalesOrderHeader SOH&lt;br /&gt;ON (SOH.SalesOrderID = SOD.SalesOrderID)&lt;br /&gt;GROUP BY SOD.SalesOrderID, SOH.ModifiedDate&lt;br /&gt;)&lt;br /&gt;SELECT count(a.SalesOrderID) Total_Number_of_Sales&lt;br /&gt;,b.YEAR_of_SALE&lt;br /&gt;,sum(a.Total_Value_of_Sale) Total_Value_of_Sale&lt;br /&gt;,b.Trtry_1&lt;br /&gt;,b.Trtry_2&lt;br /&gt;,b.Trtry_3&lt;br /&gt;,b.Trtry_4&lt;br /&gt;,b.Trtry_5&lt;br /&gt;,b.Trtry_6&lt;br /&gt;FROM Value_of_Sale a&lt;br /&gt;inner join&lt;br /&gt;(SELECT YEAR_of_SALE&lt;br /&gt;,[1] AS Trtry_1&lt;br /&gt;,[2] AS Trtry_2&lt;br /&gt;,[3] AS Trtry_3&lt;br /&gt;,[4] AS Trtry_4&lt;br /&gt;,[5] AS Trtry_5&lt;br /&gt;,[6] AS Trtry_6&lt;br /&gt;FROM (SELECT TerritoryID, TotalDue, year (OrderDate) AS YEAR_of_SALE&lt;br /&gt;FROM&lt;br /&gt;Sales.SalesOrderHeader) SOH&lt;br /&gt;PIVOT (SUM (TotalDue) FOR TerritoryID IN ( [1], [2], [3], [4], [5], [6] ) ) AS pvt ) b&lt;br /&gt;&lt;br /&gt;on year(a.Transaction_Date) = b.YEAR_of_SALE&lt;br /&gt;Group By b.YEAR_of_SALE&lt;br /&gt;,b.Trtry_1&lt;br /&gt;,b.Trtry_2&lt;br /&gt;,b.Trtry_3&lt;br /&gt;,b.Trtry_4&lt;br /&gt;,b.Trtry_5&lt;br /&gt;,b.Trtry_6&lt;br /&gt;ORDER BY b.YEAR_of_SALE Asc&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;--The same would have been done in SQL Server 2000 as follows:&lt;br /&gt;---2000.&lt;br /&gt;&lt;br /&gt;select count(a.SalesOrderID) Number_of_Sales&lt;br /&gt;,b.YEAR_of_SALE&lt;br /&gt;,sum(a.Total_Value_of_Sale) Total_Value_of_Sale&lt;br /&gt;,b.Trtry_1&lt;br /&gt;,b.Trtry_2&lt;br /&gt;,b.Trtry_3&lt;br /&gt;,b.Trtry_4&lt;br /&gt;,b.Trtry_5&lt;br /&gt;,b.Trtry_6&lt;br /&gt;from&lt;br /&gt;&lt;br /&gt;(SELECT SOD.SalesOrderID&lt;br /&gt;,year(SOH.ModifiedDate) AS Transaction_Date&lt;br /&gt;,sum(SOD.LineTotal) AS Total_Value_of_Sale&lt;br /&gt;FROM Sales.SalesOrderDetail AS SOD INNER JOIN Sales.SalesOrderHeader SOH&lt;br /&gt;ON (SOH.SalesOrderID = SOD.SalesOrderID)&lt;br /&gt;GROUP BY SOD.SalesOrderID, year(SOH.ModifiedDate)&lt;br /&gt;) a&lt;br /&gt;INNER JOIN&lt;br /&gt;(select YEAR_of_SALE&lt;br /&gt;,sum(Trtry_1) as Trtry_1&lt;br /&gt;,sum(Trtry_2) as Trtry_2&lt;br /&gt;,sum(Trtry_3) as Trtry_3&lt;br /&gt;,sum(Trtry_4) as Trtry_4&lt;br /&gt;,sum(Trtry_5) as Trtry_5&lt;br /&gt;,sum(Trtry_6) as Trtry_6&lt;br /&gt;from&lt;br /&gt;(select YEAR_of_SALE&lt;br /&gt;,isnull((select Total_Revenue where TerritoryID = 1), 0) as Trtry_1&lt;br /&gt;,isnull((select Total_Revenue where TerritoryID = 2), 0) as Trtry_2&lt;br /&gt;,isnull((select Total_Revenue where TerritoryID = 3), 0) as Trtry_3&lt;br /&gt;,isnull((select Total_Revenue where TerritoryID = 4), 0) as Trtry_4&lt;br /&gt;,isnull((select Total_Revenue where TerritoryID = 5), 0) as Trtry_5&lt;br /&gt;,isnull((select Total_Revenue where TerritoryID = 6), 0) as Trtry_6&lt;br /&gt;from&lt;br /&gt;(SELECT TerritoryID&lt;br /&gt;,year (OrderDate) AS YEAR_of_SALE&lt;br /&gt;,sum(TotalDue) Total_Revenue&lt;br /&gt;FROM Sales.SalesOrderHeader&lt;br /&gt;where TerritoryID between 1 and 6&lt;br /&gt;GROUP BY TerritoryID, year (OrderDate)) abc) def&lt;br /&gt;GROUP BY YEAR_of_SALE) b&lt;br /&gt;&lt;br /&gt;on a.Transaction_Date = b.YEAR_of_SALE&lt;br /&gt;Group By b.YEAR_of_SALE&lt;br /&gt;,b.Trtry_1&lt;br /&gt;,b.Trtry_2&lt;br /&gt;,b.Trtry_3&lt;br /&gt;,b.Trtry_4&lt;br /&gt;,b.Trtry_5&lt;br /&gt;,b.Trtry_6&lt;br /&gt;ORDER BY b.YEAR_of_SALE Asc&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;The 2000 code with derived tables involves much more coding and code complexity than the equivalent one in 2005. But the 2000 code outperforms the 2005 code everytime. When I incease the number of rows in the underlying referenced tables within the two queries, the different in execution times gets wider.&lt;br /&gt;&lt;br /&gt;I am not denying SQL Server 2005 credit that it deserves. But I still love the SQL Server 2000 coding style becuase of its supeior performance. Please note that all the code was run on SQL Server 2005 Adventureworks database in 90 compatibility mode.&lt;br /&gt;&lt;br /&gt;However, there are a few coding improvements that I love in SQL Server 2005. These are the NTILE and ROW_NUMBER() OVER constructs. The reason why I like them is that not only are they short and simple, they also perform better than their equivalnt coding constructs in SQL Server 2000. Let me share some examples below.&lt;br /&gt;&lt;br /&gt;Let me create a situation first. An Engineering College conducts nationwide enterance exams. Thousands of students appear in the tests. Let&#39;s say only the top 12 scorers qualify for admission. The college first selects the top 12 scorers in the exam. The college then needs to rank these top 12 students based on their total score first. If for any two or more of the top 12 students the total scores match, then the one with the higher score in Mathematics is ranked higher. If total scores and score in Maths are also same, then the one with higher score in Physics is ranked higher. If the total score and individual scores in Maths, Physics and Chemistry also match, then the student with the higher age is to be ranked higher as per the College policy. Once ranks have bene allotted to the top 12 students, they are to be invited in groups/batches of four to permit them to elect their field of interest (Computer Engineering, Electronics Engineering, Electrical Engineering, Mechanical Engineering, Chemical Engineering, Civil Engineering, etc). Each group is based on the ranks of the students and needs to contain exactly four students.&lt;br /&gt;&lt;br /&gt;Taking these requirements into consideration, lets design a small table with the top twleve scorers as follows:&lt;br /&gt;&lt;br /&gt;--Create the table.&lt;br /&gt;create table Student_Ranks&lt;br /&gt;(Student_Name varchar(2)&lt;br /&gt;,Maths smallint&lt;br /&gt;,Physics smallint&lt;br /&gt;,Chemistry smallint&lt;br /&gt;,Total_Marks AS Maths + Physics + Chemistry&lt;br /&gt;,Age tinyint)&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;--Populate the table with marks.&lt;br /&gt;insert into Student_Ranks (Student_Name, Maths, Physics, Chemistry, Age)&lt;br /&gt;values (&#39;A&#39;, 90, 50, 70, 17)&lt;br /&gt;insert into Student_Ranks (Student_Name, Maths, Physics, Chemistry, Age)&lt;br /&gt;values (&#39;B&#39;, 70, 60, 90, 16)&lt;br /&gt;insert into Student_Ranks (Student_Name, Maths, Physics, Chemistry, Age)&lt;br /&gt;values (&#39;C&#39;, 50, 70, 90, 17)&lt;br /&gt;insert into Student_Ranks (Student_Name, Maths, Physics, Chemistry, Age)&lt;br /&gt;values (&#39;D&#39;, 50, 90, 70, 16)&lt;br /&gt;insert into Student_Ranks (Student_Name, Maths, Physics, Chemistry, Age)&lt;br /&gt;values (&#39;E&#39;, 100, 90, 45, 16)&lt;br /&gt;insert into Student_Ranks (Student_Name, Maths, Physics, Chemistry, Age)&lt;br /&gt;values (&#39;F&#39;, 90, 50, 70, 16)&lt;br /&gt;insert into Student_Ranks (Student_Name, Maths, Physics, Chemistry, Age)&lt;br /&gt;values (&#39;G&#39;, 45, 80, 60, 16)&lt;br /&gt;insert into Student_Ranks (Student_Name, Maths, Physics, Chemistry, Age)&lt;br /&gt;values (&#39;H&#39;, 52, 49, 35, 17)&lt;br /&gt;insert into Student_Ranks (Student_Name, Maths, Physics, Chemistry, Age)&lt;br /&gt;values (&#39;I&#39;, 75, 35, 45, 17)&lt;br /&gt;insert into Student_Ranks (Student_Name, Maths, Physics, Chemistry, Age)&lt;br /&gt;values (&#39;J&#39;, 35, 35, 25, 17)&lt;br /&gt;insert into Student_Ranks (Student_Name, Maths, Physics, Chemistry, Age)&lt;br /&gt;values (&#39;K&#39;, 10, 45, 35, 17)&lt;br /&gt;insert into Student_Ranks (Student_Name, Maths, Physics, Chemistry, Age)&lt;br /&gt;values (&#39;L&#39;, 5, 35, 56, 17)&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;select * from Student_Ranks&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;--SQL Server 2005 code for ranking students.&lt;br /&gt;--Ranks students by Total Marks. If equal, then checks for Maths, then Physics, then&lt;br /&gt;--Chemistry. If all equal, then ranks the older one up.&lt;br /&gt;&lt;br /&gt;select Row_Number() Over (Order by Total_Marks desc&lt;br /&gt;,Maths desc&lt;br /&gt;,Physics desc&lt;br /&gt;,Chemistry desc&lt;br /&gt;,Age Desc)&lt;br /&gt;as Student_Rank, Student_Name&lt;br /&gt;from&lt;br /&gt;Student_Ranks&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;--The code otherwise in SQL 2000 that would do the ranking.&lt;br /&gt;--Identity allowed only with SELECT INTO.&lt;br /&gt;--To gaurentee order, Top 100 Percent, Maxdop 1 and Order By are required.&lt;br /&gt;&lt;br /&gt;select top 100 percent Student_Name&lt;br /&gt;,Student_Rank = identity(int, 1, 1)&lt;br /&gt;into #Temp_Student_Ranks&lt;br /&gt;from Student_Ranks&lt;br /&gt;Order by Total_Marks desc, Maths desc, Physics desc, Chemistry desc, Age Desc&lt;br /&gt;option (maxdop 1)&lt;br /&gt;&lt;br /&gt;select * from #Temp_Student_Ranks&lt;br /&gt;GO&lt;br /&gt;--Clean up.&lt;br /&gt;Drop table #Temp_Student_Ranks&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;--The college has to call candidates for disciple allotment in batches of 4.&lt;br /&gt;--Who is in which batch/group as per their rank? The first group is called first, the second next&lt;br /&gt;--and so on.&lt;br /&gt;&lt;br /&gt;select NTILE(3) Over(Order by Total_Marks desc&lt;br /&gt;,Maths desc&lt;br /&gt;,Physics desc&lt;br /&gt;,Chemistry desc&lt;br /&gt;,Age Desc)&lt;br /&gt;as Batch, Student_Name from Student_Ranks&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;--In 2000, the same grouping of batches would have been done in the following way:&lt;br /&gt;select top 100 percent Student_Name&lt;br /&gt;,Student_Rank = identity(int, 1, 1)&lt;br /&gt;,0 as Batch&lt;br /&gt;into #Temp_Student_Ranks&lt;br /&gt;from Student_Ranks&lt;br /&gt;Order by Total_Marks desc, Maths desc, Physics desc, Chemistry desc, Age Desc&lt;br /&gt;option (maxdop 1)&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;update #Temp_Student_Ranks&lt;br /&gt;set Batch = case&lt;br /&gt;when Student_Rank between 1 and 4 then 1&lt;br /&gt;when Student_Rank between 5 and 8 then 2&lt;br /&gt;when Student_Rank between 9 and 12 then 3&lt;br /&gt;else 4&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;select Student_Name, Batch from #Temp_Student_Ranks&lt;br /&gt;GO&lt;br /&gt;--Clean up.&lt;br /&gt;Drop table #Temp_Student_Ranks&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;You may observe, how easy it is in 2005 to rank and group students using ROW_NUMBER OVER() and NTILE() respectively. The same holds good for other items as far as ranking and grouping is concerned and as far as I have tried. And it performs better with SQL Server 2005 code than with SQL Server 2000 style code.&lt;br /&gt;&lt;br /&gt;In my next article, I will come up with some tricky T-SQL code. Please let me know your opinions for scope of improvement.</content><link rel='replies' type='application/atom+xml' href='http://sqlfinerpoints.blogspot.com/feeds/5105424651294492558/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3544169305649860217/5105424651294492558' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/5105424651294492558'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/5105424651294492558'/><link rel='alternate' type='text/html' href='http://sqlfinerpoints.blogspot.com/2007/11/some-t-sql-code-in-2005-and-2000.html' title='Some T-SQL code in 2005 and 2000.'/><author><name>Suresh Kumar Maganti</name><uri>http://www.blogger.com/profile/16645888326754808866</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyuLq44DuP85JRUqfEDgaxcuih4-meamQYzD9Jk-DD_Z1Ul5oh9WXYLo1JF5VzNReeNSgttGQ5dWJgLibF42zcSqeHW9aWQUeDrNfd_ucgf5BQ7975u63-9Bd7VfUwkQ/s220/Suresh.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3544169305649860217.post-6406059301046978057</id><published>2007-10-12T11:25:00.000-05:00</published><updated>2008-11-17T12:47:21.876-06:00</updated><title type='text'>SQL Server: Making a good product.</title><content type='html'>I am a new blogger. Basically an SQL Server DBA and performance tuning guy, I am also interested in poetry, literature and music. The aim of this blog is to express ideas as they come, on any of these topics i.e. SQL Server, poetry, literature and music. All are welcome to join me and express their ideas. The thoughts and ideas expressed in this blog are personal to the blogger alone and nobody need to take any offence to it.&lt;br /&gt;&lt;br /&gt;I love MS SQL Server 2000 more than any version of SQL Server and over all other RDBMS&#39;s. It was a well made product, Performance wise and feature-wise it was a product in which every feature present worked well. I always feel that the people who developed it did it it with commitment and were very meritorious. It did not have too many bells and whistles but it worked much better than the so-called &#39;feature-rich&#39; RDBMS&#39;s from any vendor. It is so important to hire well-qualified people, for e.g people with a four year bachelor degree in engineering/technology or higher from premier institutes and a good experience atleast in the technical field. Then the product they make or are asked to make would be competent and work efficiently.&lt;br /&gt;These days, companies hire just about anybody based on a voluminous resume. The purpose is to save money because such people have to be paid less. In outsourcing scenarios, such people add to only to headcount and consequently to revenue of the contracted company. But what happens in the bargain is that although the assignment or the product may give results, it may not be efficient or robust. Making something and making something good are two different things. Companies get satisfied with making something and when experts start using these products after purchsing them, such products do not stand the test or pressures of production environments and the purchaser ends up hiring somebody else to make it better or purchasing higher and higher end hardware to make it work to his satisfaction. So in all, the customer loses his investment and gets poor returns. That is why it is so important to make a good product like SQL Server 2000. I remember a few years ago when I was running a poorly written code on SQL Server 7.0. It took around 13 minutes to get executed to completion. Then I ran the same lengthy query in SQL Server 2000. There was no difference in hardware or resource allocation between the two setups. The same query ran on SQL Server 2000 in 7 seconds. Of course I later optimized it and it ran in 00 seconds. But what my point here is that without any changes to code, SQL Server 2000 gave me drastic improvement out of the box with no inputs from my end. This is just one example. I have never seen something similar or other instances of performance improvement out of the box in any other version or RDBMS. Although my clients like to go to newer versions or switch RDBMS&#39;s and I participate in upgrades and migrations for them, I still feel that they should stick to a good database like SQL Server 2000 and not to get swayed by rhetoric, bells and whistles. In my opinion, nothing is greater than performance.&lt;br /&gt;I had another client who felt he had an excellent 64-bit hardware and yet the performance was not upto mark. Somebody asked him to upgrade the hardware further and the nclient was not interested. Users of this website i.e. his customers had to wait 10 to 15 seconds on an average for responses. I took their top 10 SP&#39;s and optimized them. Each one of these SP&#39;s was called thousands of times a day. After optimization, testing and promoting these 10 optimized SP&#39;s to production, the total CPU utilization came down from 312 hours to 49 hours. This was in SQL Server 2000. The beauty of this product was that it gave you ample flexibility to code with your imagination only being the limiting factor. Not the same with other DB&#39;s though.&lt;br /&gt;But now a days there are so many bells and whistles to RDBMS&#39;s that real T-SQL is getting drowned out. That is a sad state of affairs. T-SQL beats CLR in speed in almost all cases (except possibly string manipulation within variables) and yet nobody realises how important T-SQL is. In a good product, all other coding techniques should be part of T-SQL and NOT T-SQL being part of other methods. I again reiterate, simplicity, efficiency, flexibility and performance are more important than the cacophony that the bells and whistles make.&lt;br /&gt;One more thing I like about SQL Server 2000 was the small learning curve. You learn quickly and then you use your imagination to bring out the best possible results. I have seen in some other RDBMS&#39;s that the learning curve is so involved and long and flexibiliy so little that the learners keep learning till the next version comes out and then start learning the next version without getting ample time to apply with imagination what they learnt. This is sad and ridiculous. Products should be made keeping the interests of the customer in mind and not keeping the limitations in thoughts of the architects in mind.&lt;br /&gt;&lt;br /&gt;In my next post, we would look at some possibly cool coding in T-SQL. Bye for now.</content><link rel='replies' type='application/atom+xml' href='http://sqlfinerpoints.blogspot.com/feeds/6406059301046978057/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3544169305649860217/6406059301046978057' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/6406059301046978057'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3544169305649860217/posts/default/6406059301046978057'/><link rel='alternate' type='text/html' href='http://sqlfinerpoints.blogspot.com/2007/10/sql-server-making-good-product.html' title='SQL Server: Making a good product.'/><author><name>Suresh Kumar Maganti</name><uri>http://www.blogger.com/profile/16645888326754808866</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='30' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyuLq44DuP85JRUqfEDgaxcuih4-meamQYzD9Jk-DD_Z1Ul5oh9WXYLo1JF5VzNReeNSgttGQ5dWJgLibF42zcSqeHW9aWQUeDrNfd_ucgf5BQ7975u63-9Bd7VfUwkQ/s220/Suresh.jpg'/></author><thr:total>0</thr:total></entry></feed>