<?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-7234868290168795296</id><updated>2024-09-07T16:54:25.191-07:00</updated><category term="troubleshooting"/><category term="Performance"/><category term="Logins and Privileges"/><category term="Data files of Database"/><category term="Memory and its issues"/><category term="Backups and Restore"/><category term="Indexing in SQL Server"/><category term="database objects"/><category term="Useful Funcions"/><category term="linked Servers"/><category term="SQL Server Installation"/><category term="SQL Server Objects"/><category term="Query"/><category term="database growth"/><category term="Code Development"/><category term="Architecture"/><category term="DB Migration"/><category term="Log shipipng"/><category term="Mirroring"/><category term="SQL Server Replication"/><category term="Single usermode"/><category term="Errors and Resolution"/><category term="Migration"/><category term="Read LDF file"/><category term="Server Properties"/><category term="User Migration"/><category term="trace SPID loginname"/><category term="Database refresh"/><category term="Error Log"/><category term="Express edition backup"/><category term="Keys and Constraints"/><category term="MSDB Jobs"/><category term="Orphan Users"/><category term="Patch Installation Error in SQL Server"/><category term="Primay key and foreign key relation"/><category term="Read backup file"/><category term="Reorganize index"/><category term="Service Broker"/><category term="User-Defined DB Roles"/><category term="XP_CMDSHELL"/><category term="excel to sqlserver"/><category term="optimized indexing"/><category term="troubleshooting SQL Server jobs"/><category term="CMD Proxy"/><category term="Capture Sessions"/><category term="Cluster Installation"/><category term="DBCC"/><category term="Delete duplicate records"/><category term="Dirty Pages"/><category term="ENDPOINTS IN SQL SERVER"/><category term="Edition Upgrade"/><category term="Locks Concept in SQL Server"/><category term="Mail Profile in SQL Server"/><category term="Network issues"/><category term="Orphan logins"/><category term="PORTNUMBER of SQL Server"/><category term="RAID CONCEPT"/><category term="Schema"/><category term="Telnet"/><category term="Temp Database Overview"/><category term="Triggers"/><category term="Truncate tables"/><category term="WHITEPAPERS"/><category term="Whatisgoingon"/><category term="blob index"/><category term="delete data in tables"/><category term="fragmentation in indexes"/><category term="iss"/><category term="locks in SQL Server"/><category term="missing indexes"/><category term="relational database"/><category term="space issues"/><category term="table size"/><category term="trace issues"/><title type='text'>Manage-SQLServer</title><subtitle type='html'>Keep Learning and Keep Sharing. All the posts are informative for learning and to carry day to day DBA tasks.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://manage-sqlserver.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>140</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-7701542575906866802</id><published>2017-01-24T07:20:00.001-08:00</published><updated>2017-01-24T07:22:25.103-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="WHITEPAPERS"/><title type='text'>Analyzing I/O performances in SQL Server</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3 style=&quot;text-align: center;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Analyzing I/O performances in SQL Server&lt;/span&gt;&lt;/u&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10pt; text-align: justify;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;One of the most common reason behind
performance degradation is deficient amount of storage subsystem performance.
There are lot of reasons for low performance in storage, but calculating it and
knowing what needs to be controlled and handled is always an essential task.
Therefore, there are three major measures, which are quite necessary for
improving performance of I/O subsystem. In the following section, we are going
to discuss these three metrics and proceed further with analyzing I/O
performance for SQL server.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; text-align: justify;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;line-height: normal; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;line-height: normal; text-align: center;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10.0pt;&quot;&gt;Description of Three Metrics&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; text-align: justify;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10.0pt;&quot;&gt;Latency&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormalCxSpMiddle&quot; style=&quot;line-height: normal; margin-left: .5in; mso-add-space: auto; mso-list: l2 level1 lfo2; text-align: justify; text-indent: -.25in;&quot;&gt;
&lt;!--[if !supportLists]--&gt;&lt;span style=&quot;color: blue;&quot;&gt;●&lt;span style=&quot;font-size: 7pt; font-stretch: normal; font-variant-numeric: normal; line-height: normal;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10.0pt;&quot;&gt;It is the first
metric, which is the time required by I/O to complete and is considered as &lt;i&gt;service&lt;/i&gt; or &lt;i&gt;response time&lt;/i&gt;. &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormalCxSpMiddle&quot; style=&quot;margin-left: .5in; mso-add-space: auto; mso-list: l2 level1 lfo2; text-indent: -.25in;&quot;&gt;
&lt;!--[if !supportLists]--&gt;&lt;span style=&quot;color: blue;&quot;&gt;●&lt;span style=&quot;font-size: 7pt; font-stretch: normal; font-variant-numeric: normal; line-height: normal;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10.0pt; line-height: 115%;&quot;&gt;The working of this measurement starts working when the OS gets
initialized, sends request to the disk controller, and stops when drive
finishes the request processing. &lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormalCxSpMiddle&quot; style=&quot;line-height: normal; margin-left: .5in; mso-add-space: auto; mso-list: l2 level1 lfo2; text-align: justify; text-indent: -.25in;&quot;&gt;
&lt;!--[if !supportLists]--&gt;&lt;span style=&quot;color: blue;&quot;&gt;●&lt;span style=&quot;font-size: 7pt; font-stretch: normal; font-variant-numeric: normal; line-height: normal;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10.0pt;&quot;&gt;The Reads are
finished when OS receives data and writes are accomplished when drive informs
OS, which has received the data.&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormalCxSpMiddle&quot; style=&quot;margin-left: .5in; mso-add-space: auto; mso-list: l2 level1 lfo2; text-indent: -.25in;&quot;&gt;
&lt;!--[if !supportLists]--&gt;&lt;span style=&quot;color: blue;&quot;&gt;●&lt;span style=&quot;font-size: 7pt; font-stretch: normal; font-variant-numeric: normal; line-height: normal;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10.0pt; line-height: 115%;&quot;&gt;Depending upon the caching policy and hardware support, the data might
still be present in DRAM cache
for writes. Thus, write-back caching is more faster than that of write-through
caching. However, this will require a backup of battery for a disk controller. &lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; text-align: justify;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10.0pt;&quot;&gt;Input/Output Operations per Second (IOPS)&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormalCxSpMiddle&quot; style=&quot;line-height: normal; margin-left: .5in; mso-add-space: auto; mso-list: l1 level1 lfo3; text-align: justify; text-indent: -.25in;&quot;&gt;
&lt;!--[if !supportLists]--&gt;&lt;span style=&quot;color: blue;&quot;&gt;●&lt;span style=&quot;font-size: 7pt; font-stretch: normal; font-variant-numeric: normal; line-height: normal;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10.0pt;&quot;&gt;Understand the
concept of this second metric with help of following example:&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; text-align: justify;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;“&lt;i&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10.0pt;&quot;&gt;Suppose
there is a constant latency of 1 ms. It means that a drive will be able to
process one thousand IOs in a second with a 1 queue depth&lt;/span&gt;&lt;/i&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10.0pt;&quot;&gt;”&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; text-align: justify;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10.0pt;&quot;&gt;This denotes that as many IOs are
appended in the queue, correspondingly the latency also gets increased.&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormalCxSpMiddle&quot; style=&quot;line-height: normal; margin-left: .5in; mso-add-space: auto; mso-list: l3 level1 lfo1; text-align: justify; text-indent: -.25in;&quot;&gt;
&lt;!--[if !supportLists]--&gt;&lt;span style=&quot;color: blue;&quot;&gt;●&lt;span style=&quot;font-size: 7pt; font-stretch: normal; font-variant-numeric: normal; line-height: normal;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10.0pt;&quot;&gt;One of the major
advantage of flash storage is that it will be able to read/write multiple NAND
channels in parallel format, which will be in absence of electro-mechanical
rotating parts for slowing down disk access. &lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormalCxSpMiddle&quot; style=&quot;margin-left: .5in; mso-add-space: auto; mso-list: l3 level1 lfo1; text-indent: -.25in;&quot;&gt;
&lt;!--[if !supportLists]--&gt;&lt;span style=&quot;color: blue;&quot;&gt;●&lt;span style=&quot;font-size: 7pt; font-stretch: normal; font-variant-numeric: normal; line-height: normal;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10.0pt; line-height: 115%;&quot;&gt;If a person is knowing queue depth and size of transferring then,
he/she can transfer IOPS to MB/sec and then MB/sec to latency.&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormalCxSpMiddle&quot; style=&quot;line-height: normal; margin-left: .5in; mso-add-space: auto; mso-list: l3 level1 lfo1; text-align: justify; text-indent: -.25in;&quot;&gt;
&lt;!--[if !supportLists]--&gt;&lt;span style=&quot;color: blue;&quot;&gt;●&lt;span style=&quot;font-size: 7pt; font-stretch: normal; font-variant-numeric: normal; line-height: normal;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10.0pt;&quot;&gt;IOPS &amp;nbsp; is equal to depth divided by the latency and
IOPS itself is not considered as size of transferring for a single disk.&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; text-align: justify;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10.0pt;&quot;&gt;Sequential Throughput&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormalCxSpMiddle&quot; style=&quot;line-height: normal; margin-left: .5in; mso-add-space: auto; mso-list: l0 level1 lfo4; text-align: justify; text-indent: -.25in;&quot;&gt;
&lt;!--[if !supportLists]--&gt;&lt;span style=&quot;color: blue;&quot;&gt;●&lt;span style=&quot;font-size: 7pt; font-stretch: normal; font-variant-numeric: normal; line-height: normal;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10.0pt;&quot;&gt;It is the rate
with which one can transfer data in the server. Its unit is MB/sec or GB/sec
and if the value is in MB/sec then, it is equal to IOPS times of transfer size.&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormalCxSpMiddle&quot; style=&quot;line-height: normal; margin-left: .5in; mso-add-space: auto; mso-list: l0 level1 lfo4; text-align: justify; text-indent: -.25in;&quot;&gt;
&lt;!--[if !supportLists]--&gt;&lt;span style=&quot;color: blue;&quot;&gt;●&lt;span style=&quot;font-size: 7pt; font-stretch: normal; font-variant-numeric: normal; line-height: normal;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10.0pt;&quot;&gt;In an enterprise
storage, sequential disk often gets short-changed both by storage vendors and
administrators. &lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormalCxSpMiddle&quot; style=&quot;line-height: normal; margin-left: .5in; mso-add-space: auto; mso-list: l0 level1 lfo4; text-align: justify; text-indent: -.25in;&quot;&gt;
&lt;!--[if !supportLists]--&gt;&lt;span style=&quot;color: blue;&quot;&gt;●&lt;span style=&quot;font-size: 7pt; font-stretch: normal; font-variant-numeric: normal; line-height: normal;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10.0pt;&quot;&gt;Sequential
throughput is vital for many database activities related to the server. This
activity can involve backups and restores, creation and rebuilds of indexes,
etc.&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;line-height: normal; text-align: center;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10.0pt;&quot;&gt;Workload Metrics of the SQL Server I/O&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;line-height: normal; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; text-align: justify;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10.0pt;&quot;&gt;Talking about performance of the SQL
server and I/O, there are multiple facts that users should monitor over time.
One should be aware about read vs write ratio for their workload of all files
of a user database and tempdb. Remember that, this ratio is not unique for all
types of the SQL server and workloads.&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; text-align: justify;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10.0pt;&quot;&gt;The server user should view typical rate
of I/O for sequential throughput and IOPS. One can determine IOPS by read/sec
and write/sec of the Windows Performance Monitor (PerfMon) and on the other
hand, one can view sequential throughput by disk read bytes/sec and disk write
bytes/sec. One should operate PerfMon to calculate average disk sec/read and
average sec/write, which are read and write latency at the level of disk.&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;line-height: normal; text-align: center;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10.0pt;&quot;&gt;Conclusion&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;line-height: normal; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; text-align: justify;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: 10.0pt;&quot;&gt;Analyzing I/O performance for SQL server
is a best practice to maintain the performance speed of the server. Each and
every server user must be aware about the metrics used for measuring the
performance of the application. This will also help in monitoring the server
performance and hence, reduce any interruption while working.&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;line-height: normal; text-align: justify;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp;--&lt;/span&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;span style=&quot;color: blue;&quot;&gt;Author : Daniel Jones&lt;/span&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;o:p&gt;&lt;span style=&quot;color: blue;&quot;&gt;Email Id: &lt;a href=&quot;mailto:Daniel.jones.0543@gmail.com&quot;&gt;Daniel.jones.0543@gmail.com&lt;/a&gt;&lt;/span&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/7701542575906866802'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/7701542575906866802'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2017/01/analyzing-io-performances-in-sql-server.html' title='Analyzing I/O performances in SQL Server'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-3823631354131154006</id><published>2017-01-01T15:17:00.000-08:00</published><updated>2017-01-01T15:17:39.232-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Code Development"/><title type='text'>Compare two table structures in SQL Server</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3 style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;How to compare two tables structure wise&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/h3&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: blue; font-weight: normal;&quot;&gt;Comparing two table structures is an important work when we have multiple environments for a application and it uses all the environments at different time or in different zones.&lt;/span&gt;&lt;/h3&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: blue; font-weight: normal;&quot;&gt;This is important when some applications give issues in one environment and doesn&#39;t give in another environment.&amp;nbsp;&lt;/span&gt;&lt;/h3&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: blue; font-weight: normal;&quot;&gt;&amp;nbsp;This is very easy if we buy some tools like Redgate compare which makes the life of DBA easy by comparing the complete objects of a database.&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: blue; font-weight: normal;&quot;&gt;In the below I have shown a very simple way to compare the two table structures.&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: blue; font-weight: normal;&quot;&gt;In the below query, search for &quot;Table1&quot; and &quot;Table2&quot; and change it with your respective table names.&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: #ffffcc; border: 1px solid #000000; height: 500px; overflow-x: scroll; overflow-y: scroll; text-align: Left; width: 550px;&quot;&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: brown;&quot;&gt;select * from (&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;SELECT&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;[column].COLUMN_NAME AS [Column_Name],&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;[column].DATA_TYPE AS [datatype],&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;[column].CHARACTER_MAXIMUM_LENGTH AS [Character_Length],&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;[column].NUMERIC_PRECISION AS Numeric_precision,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;[column].IS_NULLABLE AS [nullable]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;FROM&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;INFORMATION_SCHEMA.TABLES [table] INNER JOIN&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;INFORMATION_SCHEMA.COLUMNS [column] ON [table].TABLE_NAME = [column].TABLE_NAME&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;WHERE&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;[table].TABLE_TYPE = &#39;BASE TABLE&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;AND [table].TABLE_NAME =&#39;Table1&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;)A&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;except&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;select * from (&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;SELECT&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;[column].COLUMN_NAME AS [Column_Name],&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;[column].DATA_TYPE AS [datatype],&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;[column].CHARACTER_MAXIMUM_LENGTH AS [Character_Length],&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;[column].NUMERIC_PRECISION AS Numeric_precision,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;[column].IS_NULLABLE AS [nullable]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;FROM&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;INFORMATION_SCHEMA.TABLES [table] INNER JOIN&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;INFORMATION_SCHEMA.COLUMNS [column] ON [table].TABLE_NAME = [column].TABLE_NAME&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;WHERE&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;[table].TABLE_TYPE = &#39;BASE TABLE&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;AND [table].TABLE_NAME =&#39;Table2&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;)B&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/3823631354131154006'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/3823631354131154006'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2017/01/compare-two-table-structures-in-sql.html' title='Compare two table structures in SQL Server'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-8442893209935773437</id><published>2017-01-01T14:44:00.000-08:00</published><updated>2017-01-01T14:44:43.593-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Code Development"/><category scheme="http://www.blogger.com/atom/ns#" term="Keys and Constraints"/><title type='text'>Find the current value of identity columns in the entire database</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Script to find the current value of identity columns in the entire SQL Server database&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/h3&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-weight: normal;&quot;&gt;Identity constraints plays a vital role in populating the data into the tables from application. This becomes more vital when we have heavy transaction oriented tables and that too participating in some sort of replication as part of high availability.&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-weight: normal;&quot;&gt;Identity constraints data population is also related to the data type of column.&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;h3&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-weight: normal;&quot;&gt;For example, if there is any column having data type as &#39;int&#39; then the maximum value for that column would be&amp;nbsp;2147483647. We have to keep looking at the value of that column so that it doesn&#39;t reach the max value and application doesn&#39;t starve.&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h3&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-weight: normal;&quot;&gt;Please create an alert of the below script and ensure to get the results periodically to monitor it.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: #ffffcc; border: 1px solid #000000; height: 250px; overflow-x: scroll; overflow-y: scroll; text-align: Left; width: 550px;&quot;&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: brown;&quot;&gt;SELECT &amp;nbsp;QUOTENAME(SCHEMA_NAME(t.schema_id)) + &#39;.&#39; + &amp;nbsp;QUOTENAME(t.name) AS TableName,&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;c.name AS ColumnName,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;IDENT_CURRENT(SCHEMA_NAME(t.schema_id) &amp;nbsp;+ &#39;.&#39; + t.name) AS CurrentIdentityValue,&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;IDENT_INCR (SCHEMA_NAME(t.schema_id) + &#39;.&#39; + t.name) as Identity_increment&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;FROM &amp;nbsp; &amp;nbsp;sys.columns AS c&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;INNER JOIN &amp;nbsp;sys.tables AS t ON t.[object_id] = c.[object_id]&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;WHERE &amp;nbsp; c.is_identity = 1&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;-- write here the table name if you want to filter for specific table&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: brown;&quot;&gt;and t.name like &#39;%TABLE_NAME%&#39;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/8442893209935773437'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/8442893209935773437'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2017/01/find-current-value-of-identity-columns.html' title='Find the current value of identity columns in the entire database'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-2212969168617842303</id><published>2017-01-01T14:17:00.000-08:00</published><updated>2017-01-01T14:17:07.751-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Code Development"/><category scheme="http://www.blogger.com/atom/ns#" term="Triggers"/><title type='text'>Enable or Disable triggers of SQL Server tables</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Script to Enable or Disable triggers of SQL Server tables&lt;/span&gt;&lt;/u&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;DBA need a script to enable or disable triggers on one or multiple tables of SQL Server depending upon the demand from application or development teams.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;Usually we disable triggers when we archive data or do a bulk insert into a particular table.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;Please find the below code to generate enable or disable trigger script. In the below code,&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;
three steps are commented.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;
To enable triggers, uncomment line 2.&lt;/span&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;To disable triggers, uncomment line 3&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;To put a condition for a particular table, uncomment line 8.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: #ffffcc; border: 1px solid #000000; height: 200px; overflow-x: scroll; overflow-y: scroll; text-align: Left; width: 550px;&quot;&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;Declare @input varchar(10)&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;
&lt;/span&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;--set @input =&#39;ENABLE&#39;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;
&lt;/span&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;--set @input=&#39;DISABLE&#39;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;select +@input+&#39; TRIGGER [&#39; +DB_NAME()+&#39;].[&#39;+SCHEMA_NAME(a.schema_id)+&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;
&lt;/span&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&#39;].[&#39;+b.name+&#39;] ON [&#39; +DB_NAME()+&#39;].[&#39;+SCHEMA_NAME(a.schema_id)+&#39;].[&#39;+a.name+&#39;]&#39;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;
&lt;div&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;from sys.objects a inner join sys.triggers b&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;on a.object_id=b.parent_id&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;--where a.name like &#39;objectname/tablename&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;order by a.name&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/2212969168617842303'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/2212969168617842303'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2017/01/enable-or-disable-triggers-of-sql.html' title='Enable or Disable triggers of SQL Server tables'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-7706878485289038242</id><published>2016-12-30T15:48:00.002-08:00</published><updated>2016-12-30T15:48:57.683-08:00</updated><title type='text'>Backup all the databases of a SQL Server Instance</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Backup all the databases of SQL Server Instance&amp;nbsp;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: blue;&quot;&gt;Code to backup all the databases of SQL Server Instance using the features of SQL Server like compression.&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Code is written in order to check the version and use the compression intelligently.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #ffffcc; border: 1px solid #000000; height: 500px; overflow-x: scroll; overflow-y: scroll; text-align: Left; width: 550px;&quot;&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Declare @C1 Cursor&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Declare @path nvarchar(250)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Declare @name nvarchar(100)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Declare @dt nvarchar(14)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Declare @sql1 nvarchar(250)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Declare @sql2 nvarchar(250)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Declare @sql3 nvarchar(250)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;set @path=&#39;D:\testing\&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;set @C1=CURSOR For&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;select distinct name from sys.databases where name not like &#39;tempdb&#39; and state_desc like &#39;ONLINE&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;open @c1&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;set @dt= (select CONVERT(varchar(10),getdate(),112)+replace(CONVERT(varchar(10),getdate(),108),&#39;:&#39;,&#39;&#39;))&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;fetch next from @c1 into @name&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;while @@FETCH_STATUS=0&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Begin&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;if (@@VERSION like &#39;%2000%&#39; or (@@VERSION like &#39;%2005%&#39;) or (@@version like &#39;%Express%&#39;))&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;Begin&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;Print &#39;Starting database backup &#39;+@name&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;set @sql1=&#39;backup database &#39;+@name+&#39; to disk =&#39;&#39;&#39;+@path+@name+&#39;_&#39;+@dt+&#39;.bak&#39;&#39; with stats=1,description=&#39;&#39;backup database before patching activity&#39;&#39;&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;Print(@sql1)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;Exec (@sql1)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;Print @name+&#39; Database &amp;nbsp;backup completed successfully&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;Print &#39;#####################################################&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;End&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;Else&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;Begin&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;Print &#39;Starting database backup &#39;+@name&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;set @sql1=&#39;backup database &#39;+@name+&#39; to disk =&#39;&#39;&#39;+@path+@name+&#39;_&#39;+@dt+&#39;.bak&#39;&#39; with stats=1,compression,description=&#39;&#39;backup database before patching activity&#39;&#39;&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;Print(@sql1)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;Exec (@sql1)&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;Print @name+&#39; Database &amp;nbsp;backup completed successfully&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;Print &#39;#####################################################&#39;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;End&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Fetch next from @c1 into @name&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;End&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;close @C1&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;deallocate @c1&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/7706878485289038242'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/7706878485289038242'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2016/12/backup-all-databases-of-sql-server.html' title='Backup all the databases of a SQL Server Instance'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-6356545507420158943</id><published>2016-12-30T12:08:00.002-08:00</published><updated>2016-12-30T13:00:23.610-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Code Development"/><title type='text'>find disk space details from SQL Server</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Finding disk occupancy details from SQL Server&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;To find disk space details we have straight forward extended procedure which gives details very quickly.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;That is XP_fixeddrives stored procedure which gives the below output.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&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/AVvXsEiQMZhdV9tVMKRr6Publbe6p8ec2Y9sapFHE7yEL_GidlBib8dP0O_HZB8kphMTby4yxIMio-rns-y4UbDmmCn4m8bDwyECq0DJIARoV_g3mP_HTfcLClHAg4pwfaJaLsvxYP-7Lv3lzED0/s1600/drive1.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQMZhdV9tVMKRr6Publbe6p8ec2Y9sapFHE7yEL_GidlBib8dP0O_HZB8kphMTby4yxIMio-rns-y4UbDmmCn4m8bDwyECq0DJIARoV_g3mP_HTfcLClHAg4pwfaJaLsvxYP-7Lv3lzED0/s1600/drive1.JPG&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;To get a better picture so that you can present the output directly to business or users, please use the below code which shows output in much fashion.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Ensure below step is performed first.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Use master&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;go&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;sp_configure &#39;xp_cmdshell&#39;,1&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;go&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;reconfigure&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;go&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Actual code for finding the disk space details.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;border: 1px solid rgb(204 , 204 , 204); font-stretch: normal; height: 400px; overflow-y: auto; width: 530px;&quot;&gt;
&lt;span style=&quot;color: brown;&quot;&gt;
create table #temp1 (Drive Char(1),[MB Free] bigint,[MB Total] bigint)&lt;br /&gt;
insert into #temp1(Drive,[MB Free]) exec master..xp_fixeddrives&lt;br /&gt;
Declare @var varchar(1)&lt;br /&gt;
Declare @sql nvarchar(1000)&lt;br /&gt;
Declare @c1 cursor&lt;br /&gt;
set @c1 = CURSOR For select Drive from #temp1&lt;br /&gt;
open @c1&lt;br /&gt;
fetch &amp;nbsp;next from @c1 into @var&lt;br /&gt;
while @@fetch_status =0&lt;br /&gt;
Begin&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown;&quot;&gt;&lt;br /&gt;
create table #temp2(output nvarchar(100))&lt;br /&gt;
set @sql = &#39;insert &amp;nbsp;#temp2 exec master..xp_cmdshell &#39;&#39;fsutil volume diskfree &#39;+ @var +&#39;:&#39;&#39;&#39;&lt;br /&gt;
&amp;nbsp;execute sp_executesql @sql&lt;br /&gt;
update #temp1&lt;br /&gt;
set [MB Total] = (select cast((SUBSTRING(output,Len(LEFT(output,CHARINDEX(&#39;:&#39;,output)))+1,LEN(output))) as bigint)/1024/1024 as Total_space_MB from #temp2 where output is not null&lt;br /&gt;
and output like &#39;Total # of bytes%&#39;)&lt;br /&gt;
where Drive = @var&lt;br /&gt;
drop table #temp2&lt;br /&gt;
fetch &amp;nbsp;next from @c1 into @var&lt;br /&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown;&quot;&gt;End&lt;br /&gt;
Close @c1&lt;br /&gt;
Deallocate @c1&lt;br /&gt;
Select Drive,[MB Free],[MB Total],cast(([MB Free]*100/[MB Total]) as decimal(4,2)) &#39;Percent of Free Space&#39; from #temp1&lt;br /&gt;
Drop table #temp1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown;&quot;&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Above code will give the below output.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&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/AVvXsEgrmOA0jBVHyYi_Tu8EFpBgpvaHkotU-UCwmoOP3D6rig1V4KqthI36q44_qBa-KFF4YckNhLz_bNzVNoUpDkQohcotmtA52PZQ29d6fyFRoAT3XnAnBqMbgEEYVOaznMjhboFlhmnhxICW/s1600/drive2.JPG&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;91&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgrmOA0jBVHyYi_Tu8EFpBgpvaHkotU-UCwmoOP3D6rig1V4KqthI36q44_qBa-KFF4YckNhLz_bNzVNoUpDkQohcotmtA52PZQ29d6fyFRoAT3XnAnBqMbgEEYVOaznMjhboFlhmnhxICW/s320/drive2.JPG&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/6356545507420158943'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/6356545507420158943'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2016/12/find-disk-space-details-from-sql-server.html' title='find disk space details from SQL Server'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQMZhdV9tVMKRr6Publbe6p8ec2Y9sapFHE7yEL_GidlBib8dP0O_HZB8kphMTby4yxIMio-rns-y4UbDmmCn4m8bDwyECq0DJIARoV_g3mP_HTfcLClHAg4pwfaJaLsvxYP-7Lv3lzED0/s72-c/drive1.JPG" height="72" width="72"/></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-7060042620526034599</id><published>2016-12-30T11:42:00.000-08:00</published><updated>2016-12-30T11:42:36.110-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Code Development"/><title type='text'>truncate all the data in the tables of a database</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;How to truncate all the tables in a database&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;We do get scenarios where we get request to truncate all the data in the tables and provide only the structure of it. These kind of scenarios do arise at times of testing or building any project.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Truncating all the data is not so easy as there might be some relational tables which makes the job tough. We have to delete the child table data first then handle the parent table to ensure we don&#39;t get any errors while deleting/truncating the data.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Other way around is drop the foreign keys in the database by taking the backup of them and then delete or truncate the data. Recreate the foreign keys to ensure the structure of the RDBMS is intact.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Below are the steps to proceed further.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;1. Take the backup of Foreign keys in the database by following the below link.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;a href=&quot;http://manage-sqlserver.blogspot.co.uk/2016/12/code-to-generate-all-foreign-keys-of.html&quot;&gt;http://manage-sqlserver.blogspot.co.uk/2016/12/code-to-generate-all-foreign-keys-of.html&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;2. Now run the below code to truncate the data.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp; a. You can modify the code by if you want to truncate or delete specific tables or objects by applying the proper conditions in where clause.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp;b. I have commented the execute step in the below cursor. First check the code and see the output as i have given the print output option. Remove the commented lines and then re-execute the code to truncate the data.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;border: 1px solid rgb(204, 204, 204); font-stretch: normal; height: 380px; overflow: auto; width: 530px;&quot;&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: brown;&quot;&gt;Declare @c1 cursor&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown;&quot;&gt;declare @name nvarchar(100)&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown;&quot;&gt;declare @sql nvarchar(150)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown;&quot;&gt;set @C1 = CURSOR for&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown;&quot;&gt;select distinct &#39;[&#39;+SCHEMA_NAME(schema_id)+&#39;].[&#39;+name+&#39;]&#39; from sys.objects where type = &#39;U&#39;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown;&quot;&gt;open @C1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown;&quot;&gt;fetch next from @C1 into @name&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown;&quot;&gt;while (@@fetch_status=0)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown;&quot;&gt;Begin&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown;&quot;&gt;set @sql= &#39;Truncate table &#39;+@name&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown;&quot;&gt;--exec (@sql)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown;&quot;&gt;Print @name+&#39; truncated successfully &#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown;&quot;&gt;fetch next from @C1 into @name&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown;&quot;&gt;End&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown;&quot;&gt;close @C1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown;&quot;&gt;Deallocate @C1&lt;/span&gt;&lt;span style=&quot;color: brown;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;3. Now re-create all the foreign keys as stated in the 1st step.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Thanks.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/7060042620526034599'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/7060042620526034599'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2016/12/truncate-all-data-in-tables-of-database.html' title='truncate all the data in the tables of a database'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-7663166001853053596</id><published>2016-12-30T06:42:00.000-08:00</published><updated>2016-12-30T06:42:44.438-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="DB Migration"/><category scheme="http://www.blogger.com/atom/ns#" term="Keys and Constraints"/><category scheme="http://www.blogger.com/atom/ns#" term="Primay key and foreign key relation"/><category scheme="http://www.blogger.com/atom/ns#" term="troubleshooting"/><title type='text'>Code to generate all foreign keys of SQL Server database</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;How to draft all the foreign keys of SQL Server database&lt;/span&gt;&lt;/u&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;DBA do end up in a situation where we need to copy all the foreign keys of the SQL Server to perform certain operations.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;Like:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;Archiving activity (When data is not consistent and gives constraint error when deleting records)&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;Recreating database due to fail over&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;Multiple publications going under re snapshot due to a release work&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;Any other application demand due to which FK&#39;s have to remove and then recreated.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;Generally we do take a full backup of the database for any of the activities that are mentioned above, but that would should be last scenario where have to use the full backup restore method as it is time consuming.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;Take the backup of entire FK&#39;s is better approach and then compare it once any of the above activity &amp;nbsp;is done. We can easily put back the missing FK&#39;s so that application logic is undisturbed.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;Please use the below code to generate all the FK&#39;s in the entire database. A simple action keyword will create two kinds of scripts.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;border: 1px solid rgb(204 , 204 , 204); font-stretch: normal; height: 400px; overflow-x: scroll; overflow-y: scroll; width: 530px;&quot;&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;DECLARE @schema_name sysname;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;DECLARE @table_name sysname;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;DECLARE @constraint_name sysname;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;DECLARE @constraint_object_id int;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;DECLARE @referenced_object_name sysname;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;DECLARE @is_disabled bit;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;DECLARE @is_not_for_replication bit;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;DECLARE @is_not_trusted bit;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;DECLARE @delete_referential_action tinyint;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;DECLARE @update_referential_action tinyint;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;DECLARE @tsql nvarchar(4000);&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;DECLARE @tsql2 nvarchar(4000);&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;DECLARE @fkCol sysname;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;DECLARE @pkCol sysname;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;DECLARE @col1 bit;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;DECLARE @action char(6);&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;font-size: x-small;&quot;&gt;--SET @action = &#39;DROP&#39;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;--SET @action = &#39;CREATE&#39;;&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;font-size: x-small;&quot;&gt;DECLARE FKcursor CURSOR FOR&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;select OBJECT_SCHEMA_NAME(parent_object_id)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;, OBJECT_NAME(parent_object_id), name, OBJECT_NAME(referenced_object_id)&lt;/span&gt;&lt;br /&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;, is_disabled, is_not_for_replication, is_not_trusted&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;, delete_referential_action, update_referential_action&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;from sys.foreign_keys&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;order by 1,2;&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;font-size: x-small;&quot;&gt;OPEN FKcursor;&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;font-size: x-small;&quot;&gt;FETCH NEXT FROM FKcursor INTO @schema_name, @table_name, @constraint_name&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;, @referenced_object_name, @constraint_object_id&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;, @is_disabled, @is_not_for_replication, @is_not_trusted&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;, @delete_referential_action, @update_referential_action;&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;font-size: x-small;&quot;&gt;WHILE @@FETCH_STATUS = 0&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;BEGIN&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; IF @action &amp;lt;&amp;gt; &#39;CREATE&#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SET @tsql = &#39;ALTER TABLE &#39;&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; + QUOTENAME(@schema_name) + &#39;.&#39; + QUOTENAME(@table_name)&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; + &#39; DROP CONSTRAINT &#39; + QUOTENAME(@constraint_name) + &#39;;&#39;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; ELSE&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; BEGIN&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SET @tsql = &#39;ALTER TABLE &#39;&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; + QUOTENAME(@schema_name) + &#39;.&#39; + QUOTENAME(@table_name)&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; + CASE @is_not_trusted&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt; &amp;nbsp;WHEN 0 THEN &#39; WITH CHECK &#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;ELSE &#39; WITH NOCHECK &#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;END&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;+ &#39; ADD CONSTRAINT &#39; + QUOTENAME(@constraint_name)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt; &amp;nbsp;+ &#39; FOREIGN KEY (&#39;&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;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SET @tsql2 = &#39;&#39;;&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;font-size: x-small;&quot;&gt;DECLARE ColumnCursor CURSOR FOR&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;select COL_NAME(fk.parent_object_id, fkc.parent_column_id)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;, COL_NAME(fk.referenced_object_id, fkc.referenced_column_id)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;from sys.foreign_keys fk&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;inner join sys.foreign_key_columns fkc&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;on fk.object_id = fkc.constraint_object_id&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;where fkc.constraint_object_id = @constraint_object_id&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;order by fkc.constraint_column_id;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;OPEN ColumnCursor;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;SET @col1 = 1;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;FETCH NEXT FROM ColumnCursor INTO @fkCol, @pkCol;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;WHILE @@FETCH_STATUS = 0&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;BEGIN&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt; &amp;nbsp; &amp;nbsp;IF (@col1 = 1)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt; &amp;nbsp; &amp;nbsp;SET @col1 = 0&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;ELSE&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;BEGIN&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;SET @tsql = @tsql + &#39;,&#39;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;SET @tsql2 = @tsql2 + &#39;,&#39;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;END;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt; &amp;nbsp; &amp;nbsp;SET @tsql = @tsql + QUOTENAME(@fkCol);&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;SET @tsql2 = @tsql2 + QUOTENAME(@pkCol);&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;FETCH NEXT FROM ColumnCursor INTO @fkCol, @pkCol;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;END;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;CLOSE ColumnCursor;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;DEALLOCATE ColumnCursor;&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;font-size: x-small;&quot;&gt;SET @tsql = @tsql + &#39; ) REFERENCES &#39; + QUOTENAME(@schema_name) + &#39;.&#39; + QUOTENAME(@referenced_object_name)+ &#39; (&#39; + @tsql2 + &#39;)&#39;; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; SET @tsql = @tsql+ &#39; ON UPDATE &#39; +&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;CASE&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;@update_referential_action&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;WHEN 0 THEN &#39;NO ACTION &#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; WHEN 1 THEN &#39;CASCADE &#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; WHEN 2 THEN &#39;SET NULL &#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; ELSE &#39;SET DEFAULT &#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; END&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; + &#39; ON DELETE &#39; +&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;CASE&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;@delete_referential_action&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; WHEN 0 THEN &#39;NO ACTION &#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; WHEN 1 THEN &#39;CASCADE &#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; WHEN 2 THEN &#39;SET NULL &#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; ELSE &#39;SET DEFAULT &#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; END&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; + CASE&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;@is_not_for_replication&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; WHEN 1 THEN &#39; NOT FOR REPLICATION &#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; ELSE &#39;&#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; END&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; + &#39;;&#39;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; END;&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;font-size: x-small;&quot;&gt;PRINT @tsql;&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;font-size: x-small;&quot;&gt;IF @action = &#39;CREATE&#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;BEGIN&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;SET @tsql = &#39;ALTER TABLE &#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; + QUOTENAME(@schema_name) + &#39;.&#39; + QUOTENAME(@table_name)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; + CASE @is_disabled&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; WHEN 0 THEN &#39; CHECK &#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ELSE &#39; NOCHECK &#39;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; END&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; + &#39;CONSTRAINT &#39; + QUOTENAME(@constraint_name)&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; + &#39;;&#39;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; PRINT @tsql;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; END;&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;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; FETCH NEXT FROM FKcursor INTO @schema_name, @table_name, @constraint_name&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; , @referenced_object_name, @constraint_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; , @is_disabled, @is_not_for_replication, @is_not_trusted&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; , @delete_referential_action, @update_referential_action;&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;font-size: x-small;&quot;&gt;END;&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;font-size: x-small;&quot;&gt;CLOSE FKcursor;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: x-small;&quot;&gt;DEALLOCATE FKcursor;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/7663166001853053596'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/7663166001853053596'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2016/12/code-to-generate-all-foreign-keys-of.html' title='Code to generate all foreign keys of SQL Server database'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-5286734849741637927</id><published>2016-12-29T06:23:00.000-08:00</published><updated>2016-12-29T06:33:12.366-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Performance"/><category scheme="http://www.blogger.com/atom/ns#" term="troubleshooting"/><category scheme="http://www.blogger.com/atom/ns#" term="troubleshooting SQL Server jobs"/><category scheme="http://www.blogger.com/atom/ns#" term="XP_CMDSHELL"/><title type='text'>Enable SQL Account to execute XP_CMDSHELL</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;How to Provide execute privileges to the SQL Account on XP_CMDSHELL procedure&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: small;&quot;&gt;xp_cmdshell procedure is an extended stored procedure of the system which cannot be directly called by the SQL authenticated user. This stored procedure can perform work on the areas where SQL Server directly cannot get involved.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: small;&quot;&gt;You can actually perform the windows operation using the command like.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: small;&quot;&gt;1. Copy/Move/Create/Delete files on the disk&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: small;&quot;&gt;2. Create/Modify/Exec the Windows level jobs and other application related tasks&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: small;&quot;&gt;Above two are just some sort of examples. You can actually write your code to implement any kind of windows operation from SQL Server using this stored procedure.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: small;&quot;&gt;As this SP is doing windows work, only WINDOWS Authenticated user can perform this operation who has access to the server on which SQL Server is installed. This kind of access is usually restricted at DBA level and application teams do face some issues when trying to use this functionality.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: small;&quot;&gt;We have to perform the below operations so that SQL Authenticated user can access the XP_CMDSHELL extended stored procedure.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: &amp;quot;verdana&amp;quot; , sans-serif; font-size: small;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;div style=&quot;border: 1px solid rgb(204, 204, 204); font-stretch: normal; height: 400px; overflow: auto; width: 530px;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: small;&quot;&gt;USE master&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: small;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: small;&quot;&gt;/*-- To allow advanced options to be changed.*/&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: small;&quot;&gt;EXEC sp_configure &#39;show advanced options&#39;, 1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: small;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: small;&quot;&gt;/*-- To update the currently configured value for advanced options.*/&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: small;&quot;&gt;RECONFIGURE&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: small;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: small;&quot;&gt;/*-- To enable the feature.*/&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: small;&quot;&gt;EXEC sp_configure &#39;xp_cmdshell&#39;, 1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: small;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: small;&quot;&gt;/*-- To update the currently configured value for this feature.*/&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: small;&quot;&gt;RECONFIGURE&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: small;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: small;&quot;&gt;EXEC sp_xp_cmdshell_proxy_account &#39;domainname\User&#39;,&#39;UserPwd&#39;;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: small;&quot;&gt;/*--Above domain account should be existing in the sql server and has required access (controlserver permission)*/&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: small;&quot;&gt;Go&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: small;&quot;&gt;Create user [sql_authenticated_login_name] for [sql_authenticated_login_name];&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: brown; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: small;&quot;&gt;/*--SQL Login which needs to access xp_cmdshell should have user in master database*/&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: small;&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: small;&quot;&gt;Grant EXEC on XP_CMDSHELL to [sql_authenticated_login_name];&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace; font-size: small;&quot;&gt;Go&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/5286734849741637927'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/5286734849741637927'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2016/12/enable-sql-account-to-execute-xpcmdshell.html' title='Enable SQL Account to execute XP_CMDSHELL'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-258257325359261622</id><published>2016-04-12T11:06:00.000-07:00</published><updated>2016-04-12T11:06:10.680-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Performance"/><category scheme="http://www.blogger.com/atom/ns#" term="troubleshooting"/><category scheme="http://www.blogger.com/atom/ns#" term="Whatisgoingon"/><title type='text'>What is going on in SQL Server now</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;u&gt;Finding what is going on now in SQL Server&lt;/u&gt;&lt;/span&gt;&lt;/h2&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Generally being a DBA, we are usually asked with one questions &quot;what is going on&quot; with database server whenever any performance issue or any thing happens in application.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;DBA will jump in and try to find out&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;1. Who all are logged in&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;2. What all they doing&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;3. Since how long a session is running or what is the longest running session&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;4. Who is consuming more memory&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;5. Who is blocking who&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;6. If it is system task (dbcc, backup, restore etc) then how long will it run&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;7. What is the query being fired&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;8. How many reads or writes being done by a session&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;9. who owns which session&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;10.What is the hostname&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;I generally create a view in the maintenance database so that i don&#39;t need waste my time much in finding above parameters.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;In panic situation many develop tendency to forget things and we do lot of messy things instead of finding the value in a simple.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Create the below view in any one the maintenance database and just select it whenever demand arises to see the current statistics.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;border: 1px solid rgb(204, 204, 204); font-stretch: normal; height: 400px; overflow: auto; width: 530px;&quot;&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;Create view Whatisgoingon&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;as&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;SELECT &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;OBJECT_NAME(objectid) as ObjectName &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;,SUBSTRING(stateText.text, (statement_start_offset/2)+1, &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;((CASE statement_end_offset &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;WHEN -1 THEN DATALENGTH(stateText.text) &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;ELSE statement_end_offset &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;END - statement_start_offset)/2) + 1) AS statement_text &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;,DB_Name(database_id) as DatabaseName &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;,req.cpu_time AS CPU_Time &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;,cast(cast(DATEDIFF(second, last_request_start_time, getdate()) as float)/60 as decimal(10,3)) AS RunningMinutes &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;,req.Percent_Complete &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;,sess.HOST_NAME as RunningFrom &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;,LEFT(CLIENT_INTERFACE_NAME, 25) AS RunningBy &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;,sess.session_id AS SessionID &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;,req.blocking_session_id AS BlockingWith &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;,req.reads &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;,req.writes &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;,sess.[program_name] &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;,sess.login_name &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;,sess.status &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;,sess.last_request_start_time &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;,req.logical_reads &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;FROM &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;sys.dm_exec_requests req &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;INNER JOIN sys.dm_exec_sessions sess ON sess.session_id = req.session_id &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;AND sess.is_user_process = 1 &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;CROSS APPLY &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;sys.dm_exec_sql_text(sql_handle) AS stateText&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;When ever demand arises just query:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Select * from whatisgoingon&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Hope it helps.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/258257325359261622'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/258257325359261622'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2016/04/what-is-going-on-in-sql-server-now.html' title='What is going on in SQL Server now'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-149862934992620174</id><published>2016-04-12T10:53:00.000-07:00</published><updated>2016-04-12T10:53:18.588-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Telnet"/><category scheme="http://www.blogger.com/atom/ns#" term="troubleshooting"/><title type='text'>How to telnet multiple windows server</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: red;&quot;&gt;How do we telnet multiple windows server&lt;/span&gt;&lt;/b&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;b&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;In my recent project i got a requirement to set up a shared SQL Server from where i have to connect to multiple servers and perform the task related to day to day DBA activities.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;This was a newly built server and therefore i have to struggle alot to find on what ports my database servers are working and whether they are accessible from this shared server or not ?&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;This was the biggest huddle to cross, so that i can install SQL Client and start working from it.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;We can check the port connectivity by using below command.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;CMD &amp;gt; Telnet HOSTNAME PORT_NUMBER&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Eg: Telnet 191.10.10.1 1433&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;By doing above operation we can find out whether port 1433 is accessible to connect 191.10.10.1 server from the source location where we are executing it. It&#39;s a command prompt task.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Now how to achieve this for more than 500 servers ? We cannot do manually for hours together and get irritated.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;I did lot of googling on this and came up with below solution. This is best way to proceed.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;By using Powershell scripting.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;&lt;u&gt;Step 1&lt;/u&gt;&lt;/b&gt;: Login to the Shared server from where you want to check connectivity&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;&lt;u&gt;Step 2&lt;/u&gt;&lt;/b&gt;: Open a notepad and copy the below script by changing &quot;Servername&quot; and port 1433 as per &amp;nbsp; &amp;nbsp; your need.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;border: 1px solid rgb(204, 204, 204); font-stretch: normal; height: 400px; overflow: auto; width: 530px;&quot;&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;$test = @(&#39;servername1:1433&#39;,&#39;servername2:1433&#39;,&#39;servername3:1433&#39;)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;Foreach ($t in $test)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;&amp;nbsp; $source = $t.Split(&#39;:&#39;)[0]&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;&amp;nbsp; $port = $t.Split(&#39;:&#39;)[1]&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;&amp;nbsp; Write-Host &quot;Connecting to $source on port $port&quot;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;&amp;nbsp; try&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;&amp;nbsp; {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;&amp;nbsp; &amp;nbsp; $socket = New-Object System.Net.Sockets.TcpClient($source, $port)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;&amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;&amp;nbsp; catch [Exception]&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;&amp;nbsp; {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;&amp;nbsp; &amp;nbsp; Write-Host $_.Exception.GetType().FullName&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;&amp;nbsp; &amp;nbsp; Write-Host $_.Exception.Message&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;&amp;nbsp; }&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;&amp;nbsp; Write-Host &quot;Connected`n&quot;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue; font-family: monospace;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 26px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;u style=&quot;font-weight: bold;&quot;&gt;Step 3&lt;/u&gt;: Now save the above script as &quot;Powershell_script.ps1&quot;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Don&#39;t forget to change the server names and port numbers as per your need.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;&lt;u&gt;Step 4&lt;/u&gt;: &lt;/b&gt;Go to Command prompt and run the below script&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;CMD &amp;gt; powershell -File Pwoershell_script.ps1&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Above script will give result like&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Connecting to Servername on port 1433&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;Connected&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;If you want to log the complete output, especially when you are checking for hundreds of servers.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Use below command.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;CMD &amp;gt; powershell -File Pwoershell_script.ps1 &amp;gt;&amp;gt;Output.log&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;You will find the output file in the same path where your command prompt is prompting now.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Hope it helps.&lt;/span&gt;&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/149862934992620174'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/149862934992620174'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2016/04/how-to-telnet-multiple-windows-server.html' title='How to telnet multiple windows server'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-5631009878867459719</id><published>2015-10-22T10:56:00.001-07:00</published><updated>2015-10-22T10:56:03.482-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Capture Sessions"/><category scheme="http://www.blogger.com/atom/ns#" term="Performance"/><category scheme="http://www.blogger.com/atom/ns#" term="troubleshooting"/><title type='text'>Capture current session details in SQL Server</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Find the active sessions in SQL Server Instance in detailed&lt;/span&gt;&lt;/u&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;When ever performance of SQL Server has started degrading very first thing a DBA should do is&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Check the current active sessions and their details like&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; a. CPU usage&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; b. Memory Usage&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; c. Query being execution&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; d. how long query is being executed&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; e. Any locks on sessions&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; f. who is the user of session&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; g. From where the connection is coming&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; h. When session is started&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; i. &amp;nbsp;Reads / writes caused&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; j. &amp;nbsp;session id&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Above all the details can be retrieved easily from the DMV&#39;s of SQL Server. We just need to join in a very simple to accomplish the task.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Below is the query which i use in my day to day operational work to figure out bottle necks.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;border: 1px solid #ccc; font: 16px/26px Georgia, Garamond, Serif; height: 400px; overflow: auto; width: 530px;&quot;&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;OBJECT_NAME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;objectid&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;ObjectName&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;SUBSTRING&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;stateText.&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;TEXT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, (&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;statement_start_offset&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)+&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;br /&gt;((&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CASE &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;statement_end_offset&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;WHEN &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;1 &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;THEN &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;DATALENGTH&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;stateText.&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;TEXT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;ELSE &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;statement_end_offset&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;END &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;- &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;statement_start_offset&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)/&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) + &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;statement_text&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;DB_NAME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;database_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;DatabaseName&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;req.cpu_time &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;CPU_Time&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;DATEDIFF&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;second&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;last_request_start_time&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;()) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS FLOAT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)/&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;60 &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS DECIMAL&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;10&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;RunningMinutes&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;req.Percent_Complete&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sess.&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;HOST_NAME &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;RunningFrom&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;LEFT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;CLIENT_INTERFACE_NAME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;25&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;RunningBy&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sess.session_id &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;SessionID&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;req.blocking_session_id &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;BlockingWith&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;req.reads&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;req.writes&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sess.[program_name]&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sess.login_name&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sess.status&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sess.last_request_start_time&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;req.logical_reads&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sys.dm_exec_requests req&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INNER JOIN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sys.dm_exec_sessions sess &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;ON &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sess.session_id &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;req.session_id&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;AND &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sess.is_user_process &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;1&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;CROSS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;APPLY&lt;br /&gt;sys.dm_exec_sql_text&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sql_handle&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;stateText&lt;/span&gt;&lt;/code&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;I usually create a view using the above code in the DBA technical support database and query it easily when ever is needed.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Hope it will help.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/5631009878867459719'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/5631009878867459719'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2015/10/capture-current-session-details-in-sql.html' title='Capture current session details in SQL Server'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-1915412138220983915</id><published>2015-10-22T07:21:00.001-07:00</published><updated>2015-10-22T07:22:59.683-07:00</updated><title type='text'>Complete Database details of SQL Server</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;Find the complete details of all the databases hosted in SQL Server Instance&lt;/span&gt;&lt;/u&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Some times to find the database details we need to run multiple queries.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Below one query will retrieve the following information which helps in quick understanding of the databases hosted in a particular SQL Server Instance.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;1. DB Name&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;2. Logical File Name&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;3. File Size&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;4. Physical name and Path&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;5. Size&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;6. Percentage of Growth&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;7. Occupied Space&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;8. Free Space&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;9. Status&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;10. Recovery model&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;border: 1px solid #ccc; font: 16px/26px Georgia, Garamond, Serif; height: 500px; overflow: auto; width: 530px;&quot;&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECLARE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@DBInfo &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;TABLE&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;( &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;ServerName &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;DatabaseName &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: grey;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;FileSizeMB &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;LogicalFileName &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SYSNAME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;PhysicalFileName &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;NVARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;520&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;Status &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SYSNAME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;Updateability &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SYSNAME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;RecoveryMode &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SYSNAME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;FreeSpaceMB &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;FreeSpacePct &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;FreeSpacePages &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;Autogrow &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;PollDate &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DATETIME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECLARE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@command &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@command &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;Use [&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;?&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;] SELECT&lt;br /&gt;@@servername as ServerName,&lt;br /&gt;&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&#39;&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;?&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&#39;&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39; AS DatabaseName,&lt;br /&gt;CAST(sys.database_files.size/128.0 AS int) AS FileSize,&lt;br /&gt;sys.database_files.name AS LogicalFileName, sys.database_files.physical_name AS PhysicalFileName,&lt;br /&gt;CONVERT(sysname,DatabasePropertyEx(&#39;&#39;?&#39;&#39;,&#39;&#39;Status&#39;&#39;)) AS Status,&lt;br /&gt;CONVERT(sysname,DatabasePropertyEx(&#39;&#39;?&#39;&#39;,&#39;&#39;Updateability&#39;&#39;)) AS Updateability,&lt;br /&gt;CONVERT(sysname,DatabasePropertyEx(&#39;&#39;?&#39;&#39;,&#39;&#39;Recovery&#39;&#39;)) AS RecoveryMode,&lt;br /&gt;CAST(sys.database_files.size/128.0 - CAST(FILEPROPERTY(sys.database_files.name, &#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&#39;&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;SpaceUsed&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&#39;&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39; ) AS int)/128.0 AS int) AS FreeSpaceMB,&lt;br /&gt;CAST(100 * (CAST (((sys.database_files.size/128.0 -CAST(FILEPROPERTY(sys.database_files.name,&lt;br /&gt;&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&#39;&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;SpaceUsed&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&#39;&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39; ) AS int)/128.0)/(sys.database_files.size/128.0))&lt;br /&gt;AS decimal(4,2))) AS varchar(8)) + &#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&#39;&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;%&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&#39;&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39; AS FreeSpacePct,&lt;br /&gt;CASE WHEN is_percent_growth = 0&amp;nbsp;&amp;nbsp;THEN LTRIM(STR(growth * 8.0 / 1024,10,1)) + &#39;&#39; MB,&#39;&#39;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;ELSE &#39;&#39;By &#39;&#39; + CAST(growth AS VARCHAR) + &#39;&#39; percent, &#39;&#39;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;END +&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;CASE WHEN max_size = -1 THEN &#39;&#39;unrestricted growth&#39;&#39;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br /&gt;ELSE &#39;&#39;restricted growth to &#39;&#39; +&amp;nbsp;&amp;nbsp;LTRIM(STR(max_size * 8.0 / 1024,10,1)) + &#39;&#39; MB&#39;&#39;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;END AS Autogrow,&lt;br /&gt;GETDATE() as PollDate FROM sys.database_files&#39; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INSERT INTO &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@DBInfo&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;ServerName&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;DatabaseName&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;FileSizeMB&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;LogicalFileName&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;PhysicalFileName&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Status&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Updateability&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;RecoveryMode&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;FreeSpaceMB&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;FreeSpacePct&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Autogrow&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;PollDate&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;EXEC &lt;/span&gt;&lt;span style=&quot;color: darkred;&quot;&gt;sp_MSforeachdb &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@command&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;ServerName&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;DatabaseName&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;FileSizeMB&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;LogicalFileName&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;PhysicalFileName&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Status&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Updateability&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;RecoveryMode&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;FreeSpaceMB&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;FreeSpacePct&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Autogrow&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;PollDate&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@DBInfo&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;WHERE &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;DatabaseName &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;NOT &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;IN &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;Master&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;Model&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;tempdb&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;ORDER BY&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;ServerName&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;DatabaseName&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/code&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/1915412138220983915'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/1915412138220983915'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2015/10/complete-database-details-of-sql-server.html' title='Complete Database details of SQL Server'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-1518025179187709754</id><published>2015-10-21T07:24:00.001-07:00</published><updated>2015-10-21T07:25:28.590-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="database growth"/><category scheme="http://www.blogger.com/atom/ns#" term="Performance"/><category scheme="http://www.blogger.com/atom/ns#" term="table size"/><category scheme="http://www.blogger.com/atom/ns#" term="troubleshooting"/><title type='text'>Size of all the tables in a database of SQL Server</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;How to find the size of all the tables residing in a database&lt;/span&gt;&lt;/u&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Gathering the table wise size data is very important in DBA support life cycle.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Being a DBA, we need to have command on the database growth and table wise growth to get the proper estimates as part of capacity management.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Below query helps us in find the complete details of the tables in particular database.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;It will provide the number of rows and size of the table along with index size.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;border: 1px solid #ccc; font: 16px/26px Georgia, Garamond, Serif; height: 550px; overflow: auto; width: 550px;&quot;&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET NOCOUNT ON&lt;br /&gt;DECLARE&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: #434343;&quot;&gt;@tablename &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;250&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECLARE&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: black;&quot;&gt;db_cursor &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;CURSOR FOR&lt;br /&gt;SELECT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;table_schema&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;.&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;table_name&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM&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: black;&quot;&gt;INFORMATION_SCHEMA.TABLES&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;WHERE&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: black;&quot;&gt;table_type&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;BASE TABLE&#39;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;OPEN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;db_cursor&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;FETCH &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;NEXT &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;db_cursor &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INTO &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@tablename&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;WHILE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@@FETCH_STATUS &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;0&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;BEGIN&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DECLARE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@sqltableusage &lt;br /&gt;&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;&quot;&gt;TABLE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;name &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;250&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;rows &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;reserved &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;), &lt;br /&gt;&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: black;&quot;&gt;data &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;index_used &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;unused &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;))&lt;br /&gt;&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;&quot;&gt;INSERT INTO &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@sqltableusage&lt;br /&gt;&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;&quot;&gt;EXEC &lt;/span&gt;&lt;span style=&quot;color: darkred;&quot;&gt;sp_spaceused &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@tablename&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;FETCH &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;NEXT &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;db_cursor &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INTO &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@tablename&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: #434343;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;END&lt;br /&gt;CLOSE &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;db_cursor&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;DEALLOCATE &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;db_cursor&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/code&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;name&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;rows&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;REPLACE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;reserved&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;KB&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Reserved_KB&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;REPLACE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;KB&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Data_KB&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;REPLACE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;index_used&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;KB&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Index_Used_KB&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;REPLACE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;unused&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;KB&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Unused_KB&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECIMAL&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;14&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;REPLACE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;reserved&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;KB&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)))/&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;1024 &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Reserved_MB&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECIMAL&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;14&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;REPLACE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;data&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;KB&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)))/&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;1024 &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Data_MB&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECIMAL&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;14&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;REPLACE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;index_used&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;KB&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)))/&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;1024 &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Index_Used_MB&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECIMAL&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;14&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;REPLACE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;unused&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;KB&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)))/&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;1024 &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Unused_MB&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@sqltableusage&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/1518025179187709754'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/1518025179187709754'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2015/10/size-of-all-tables-in-database-of-sql.html' title='Size of all the tables in a database of SQL Server'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-6207740431223372140</id><published>2015-10-21T07:12:00.003-07:00</published><updated>2015-10-21T07:13:39.724-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data files of Database"/><category scheme="http://www.blogger.com/atom/ns#" term="database growth"/><title type='text'>Find Database size details in SQL Server</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;How to find current database size details in SQL Server&lt;/span&gt;&lt;/u&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;u&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Every database will have free space in it depending upon the growth style and type of activities going on the database.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;If database has more delete operations on it and growth is depending upon the percentage then some times we may see huge free space in the database.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Below query will give the details.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;border: 1px solid #ccc; font: 16px/26px Georgia, Garamond, Serif; height: 200px; overflow: auto; width: 550px;&quot;&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Report_Date &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;FILE_ID &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[FileId]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;br /&gt;&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: black;&quot;&gt;a.name &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[LogicalName]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;br /&gt;&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: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECIMAL&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;ROUND&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.size&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;128.000&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[FileSize(MB)]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;br /&gt;&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: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECIMAL&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;ROUND&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;FILEPROPERTY&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.name&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;br /&gt;&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;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;SpaceUsed&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)/&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;128.000&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[SpaceUsed(MB)]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECIMAL&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;ROUND&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.size&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;FILEPROPERTY&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.name&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;br /&gt;&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;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;SpaceUsed&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;))/&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;128.000&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[FreeSpace(MB)]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;ROUND&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECIMAL&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;ROUND&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;FILEPROPERTY&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.name&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;br /&gt;&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;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;SpaceUsed&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)/&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;128.000&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;))/ &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECIMAL&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;ROUND&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.size&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;128.000&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)))*&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[Pct_SpaceUsed]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;br /&gt;&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: magenta;&quot;&gt;ROUND&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECIMAL&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;ROUND&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.size&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;FILEPROPERTY&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.name&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;br /&gt;&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;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;SpaceUsed&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;))/&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;128.000&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;))/ &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECIMAL&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;12&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;ROUND&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.size&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;128.000&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)))*&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[Pct_FreeSpace] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sys.database_files a &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;LEFT &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;OUTER &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;JOIN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sys.data_spaces b &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;ON&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.data_space_id &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;b.data_space_id&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/6207740431223372140'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/6207740431223372140'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2015/10/find-database-size-details-in-sql-server.html' title='Find Database size details in SQL Server'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-8208691601528115219</id><published>2015-10-21T06:00:00.002-07:00</published><updated>2015-10-21T06:10:54.524-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="iss"/><category scheme="http://www.blogger.com/atom/ns#" term="Memory and its issues"/><category scheme="http://www.blogger.com/atom/ns#" term="Performance"/><title type='text'>RAM occupied by SQL Server Database</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;color: #cc0000;&quot;&gt;How much size of RAM is occupied by each database in SQL Server Database&lt;/span&gt;&lt;/u&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;u&gt;&lt;span style=&quot;color: #cc0000;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Memory one of the important bottle neck in troubleshooting the performance of SQL Server.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;It is very evident that the pages which are&amp;nbsp;retrieved from RAM directly will increase the performance of any application rather than pages retrieving from DISK to RAM.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Our first preference is to find out how much space is being occupied by each database in the buffer cache (which is nothing but physical RAM).&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Below is the query to find the space details occupied by database in RAM.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;border: 1px solid #ccc; font: 16px/26px Georgia, Garamond, Serif; height: 300px; overflow: scroll; width: 550px;&quot;&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECLARE&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@total_buffer &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@total_buffer &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;cntr_value&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sys.dm_os_performance_counters&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;WHERE&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;RTRIM&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[object_name]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) LIKE &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;%Buffer Manager&#39;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;AND &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;counter_name &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;Total Pages&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;;&lt;br /&gt;;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;WITH &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;src &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;database_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;db_buffer_pages &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;COUNT_BIG&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(*)&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sys.dm_os_buffer_descriptors&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;GROUP BY&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;database_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[db_name] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CASE &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[database_id] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;WHEN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;32767&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&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;&quot;&gt;THEN&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;Resource DB&#39;&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;&quot;&gt;ELSE &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;DB_NAME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[database_id]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;END&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;br /&gt;&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: black;&quot;&gt;db_buffer_pages&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;db_buffer_MB &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;db_buffer_pages &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;/ &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;128&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&amp;nbsp;&amp;nbsp; &lt;br /&gt;&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: black;&quot;&gt;db_buffer_percent &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECIMAL&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;db_buffer_pages &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;* &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;100.0 &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;/ &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@total_buffer&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;src&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;ORDER BY&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;db_buffer_MB &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;;&lt;/span&gt;&lt;/code&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;After identifying the database which is consuming more space in RAM, now it&#39;s turn to identify the table which is taking more space in that particular database.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;div style=&quot;border: 1px solid #ccc; font: 16px/26px Georgia, Garamond, Serif; height: 300px; overflow: scroll; width: 550px;&quot;&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;USE &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[DB_NAME]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;GO&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;WITH &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;src &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[Object] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;o.name&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[Type] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;o.type_desc&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[Index] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &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: magenta;&quot;&gt;COALESCE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;i.name&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[Index_Type] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;i.type_desc&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;p. &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;[object_id]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;p.index_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;au.allocation_unit_id&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM&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: black;&quot;&gt;sys.partitions &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;p &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;INNER JOIN&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sys.allocation_units&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;au&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ON &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;p.hobt_id &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;au.container_id&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;INNER JOIN&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sys.objects &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;o&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;ON &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;p.[object_id] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;o.[object_id]&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;INNER JOIN&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sys.indexes &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;i&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;ON &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;o.[object_id] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;i.[object_id]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;AND &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;p.index_id &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;i.index_id&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;WHERE&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;au.[type] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;IN &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AND &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;o.is_ms_shipped &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;src.[Object]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;src.[Type]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;src.[Index]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;src.Index_Type&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&amp;nbsp;&amp;nbsp; &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; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;buffer_pages &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;COUNT_BIG&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;b.page_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;buffer_mb &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;COUNT_BIG&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;b.page_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) / &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;128&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;src&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;INNER JOIN&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sys.dm_os_buffer_descriptors &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;b&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;ON &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;src.allocation_unit_id&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;b.allocation_unit_id&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;WHERE&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;b.database_id &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;DB_ID&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;()&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;GROUP BY&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;src.[Object]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;src.[Type]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;src.[Index]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;src.Index_Type&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;ORDER BY&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;buffer_pages &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DESC&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Now it will be clear regarding which table pages are more in physical RAM.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Obviously this table is being&amp;nbsp;retrieved more than any other table in the database.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Now it&#39;s up to you how you proceed for further troubleshooting. You need to check what type of queries being fired from the application or users.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Question yourself&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;1. Is this table&amp;nbsp;retrieving more than it is needed&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;2. Why do we need all the column data in the RAM&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;3. Can we create the index and extract only those columns data which is needed.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Now proceed the way where your question takes you to solve the issues.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;Please post your issues or concerns so that together we can work and solve it.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/8208691601528115219'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/8208691601528115219'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2015/10/ram-occupied-by-sql-server-database.html' title='RAM occupied by SQL Server Database'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-2654439662124273907</id><published>2015-10-13T11:40:00.002-07:00</published><updated>2015-10-13T12:19:30.341-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Indexing in SQL Server"/><category scheme="http://www.blogger.com/atom/ns#" term="missing indexes"/><category scheme="http://www.blogger.com/atom/ns#" term="optimized indexing"/><category scheme="http://www.blogger.com/atom/ns#" term="Performance"/><title type='text'>Find the Missing Indexes in SQL Server Database</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;color: blue;&quot;&gt;Missing Index Analysis in SQL Server&lt;/span&gt;&lt;/u&gt;&lt;/h2&gt;
&lt;div&gt;
I am not here to describe what is index and what is its impact on SQL Server.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
There will be missing indexes in any SQL Server database if proper indexes are not created or any application related queries are deployed in the system.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Below Query helps in finding the missing indexes of a particular database.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;Use [DBNAME]&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;GO&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;[&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Sch.name&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;].[&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Tab.[name]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;]&#39; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;TableName&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Ind.[name] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;IndexName&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;SUBSTRING&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;br /&gt;( &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;, &#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;AC.name &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sys.[tables] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;T &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INNER JOIN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sys.[indexes] I&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;ON &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;T.[object_id] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;I.[object_id]&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;INNER JOIN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sys.[index_columns] IC &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;ON &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;I.[object_id] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;IC.[object_id] &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;AND&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&amp;nbsp;I.[index_id] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;IC.[index_id] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INNER JOIN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sys.[all_columns] AC &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;ON&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;T.[object_id] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;AC.[object_id]&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;AND &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;IC.[column_id] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;AC.[column_id] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;WHERE&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;Ind.[object_id] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;I.[object_id] &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;AND &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Ind.index_id &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;I.index_id &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;AND&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;IC.is_included_column &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;0&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;ORDER BY &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;IC.key_ordinal&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;FOR &amp;nbsp;XML &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;PATH&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)), &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;8000&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;KeyCols&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: magenta;&quot;&gt;SUBSTRING&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(( &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;, &#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;AC.name &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sys.[tables] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;T &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INNER JOIN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sys.[indexes] I&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;ON &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;T.[object_id] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;I.[object_id] &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INNER JOIN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sys.[index_columns] IC &amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;ON &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;I.[object_id] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;IC.[object_id] &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;AND &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;I.[index_id] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;IC.[index_id]&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;INNER JOIN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sys.[all_columns] AC&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;ON &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;T.[object_id] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;AC.[object_id] &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;AND &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;IC.[column_id] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;AC.[column_id]&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;WHERE &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Ind.[object_id] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;I.[object_id] &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;AND &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Ind.index_id &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;I.index_id&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: grey;&quot;&gt;AND &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;IC.is_included_column &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;1&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;ORDER BY &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;IC.key_ordinal&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;FOR &amp;nbsp;XML &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;PATH&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)), &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;8000&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;IncludeCols&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sys.[indexes] Ind &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INNER JOIN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sys.[tables] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Tab &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;ON &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Tab.[object_id] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Ind.[object_id]&amp;nbsp;&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;INNER JOIN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sys.[schemas] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Sch &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;ON &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Sch.[schema_id] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Tab.[schema_id]&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;ORDER BY &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;TableName&lt;/span&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;GO&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/code&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/code&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;Above query will give the proposed indexes on the database to improve the performance.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;Confused how to put the indexes in create statement syntax. Here is the simplest way to create all the missing indexes in one shot.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;Below Query will help in finding the missing indexes and create the syntax as well.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;


&lt;br /&gt;
&lt;div class=&quot;scrollbox&quot;&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: green;&quot;&gt;--Create Missing Indexes---&lt;br /&gt;-----------------------------------&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;SERVERPROPERTY&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;ServerName&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[nvarchar]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;256&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[SQLServer]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;db.[database_id] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[DatabaseID]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;db.[name] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[DatabaseName]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;id.[object_id] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[ObjectID]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;id.[statement] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[FullyQualifiedObjectName]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;id.[equality_columns] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[EqualityColumns]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;id.[inequality_columns] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[InEqualityColumns]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;id.[included_columns] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[IncludedColumns]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;gs.[unique_compiles] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[UniqueCompiles]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;gs.[user_seeks] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[UserSeeks]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;gs.[user_scans] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[UserScans]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;gs.[last_user_seek] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[LastUserSeekTime]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;gs.[last_user_scan] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[LastUserScanTime]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;gs.[avg_total_user_cost] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[AvgTotalUserCost]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;gs.[avg_user_impact] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[AvgUserImpact]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;gs.[system_seeks] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[SystemSeeks]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;gs.[system_scans] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[SystemScans]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;gs.[last_system_seek] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[LastSystemSeekTime]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;gs.[last_system_scan] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[LastSystemScanTime]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;gs.[avg_total_system_cost] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[AvgTotalSystemCost]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;gs.[avg_system_impact] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[AvgSystemImpact]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;gs.[user_seeks] &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;* &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;gs.[avg_total_user_cost] &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;* (&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;gs.[avg_user_impact] &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;* &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;0.01&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[IndexAdvantage]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;CREATE INDEX [IXNC_&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;OBJECT_NAME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;id.[object_id]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;db.[database_id]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) + &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;_&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;REPLACE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;REPLACE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;REPLACE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;ISNULL&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;id.[equality_columns]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;, &#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;_&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;[&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;]&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) + &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CASE&lt;br /&gt;&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;&quot;&gt;WHEN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;id.[equality_columns] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;IS &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;NOT 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;AND &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;id.[inequality_columns] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;IS &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;NOT 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;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;THEN &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;_&#39;&lt;br /&gt;&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;&quot;&gt;ELSE &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;br /&gt;&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;&quot;&gt;END &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;REPLACE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;REPLACE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;REPLACE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;ISNULL&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;id.[inequality_columns]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;, &#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;_&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;[&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;), &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;]&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) + &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;_&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;LEFT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;NEWID&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;() &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[nvarchar]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;64&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)), &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) + &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;]&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39; ON &#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;id.[statement] &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39; (&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;ISNULL&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;id.[equality_columns]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) + &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CASE&lt;br /&gt;&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;&quot;&gt;WHEN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;id.[equality_columns] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;IS &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;NOT 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;AND &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;id.[inequality_columns] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;IS &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;NOT 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;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;THEN &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;,&#39;&lt;br /&gt;&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;&quot;&gt;ELSE &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;br /&gt;&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;&quot;&gt;END &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;ISNULL&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;id.[inequality_columns]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) + &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;)&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;ISNULL&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39; INCLUDE (&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;id.[included_columns] &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+ &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;)&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;, &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[ProposedIndex]&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CURRENT_TIMESTAMP &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[smalldatetime]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[CollectionDate]&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[sys].[dm_db_missing_index_group_stats] gs &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;WITH &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;NOLOCK&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;INNER JOIN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[sys].[dm_db_missing_index_groups] ig &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;WITH &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;NOLOCK&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;ON &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;gs.[group_handle] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;ig.[index_group_handle]&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;INNER JOIN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[sys].[dm_db_missing_index_details] id &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;WITH &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;NOLOCK&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;ON &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;ig.[index_handle] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;id.[index_handle]&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;INNER JOIN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[sys].[databases] db &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;WITH &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;NOLOCK&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;ON &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;db.[database_id] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;id.[database_id]&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;WHERE &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;id.[database_id] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;6 &lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;-- Remove this to see for entire instance&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;ORDER BY &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[IndexAdvantage] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DESC&lt;br /&gt;OPTION &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;RECOMPILE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;Being a DBA, my suggestion is to go through the proposed indexes and analyze whether it will seriously help or not. More indexes may kill the performance some times. Pay double attention when doing this activity.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;Get back to me in case of the script doesn&#39;t work or if needed help :)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Verdana, sans-serif;&quot;&gt;&lt;span style=&quot;font-size: 12px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/2654439662124273907'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/2654439662124273907'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2015/10/find-missing-indexes-in-sql-server.html' title='Find the Missing Indexes in SQL Server Database'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-4310302719021510399</id><published>2015-10-13T02:58:00.001-07:00</published><updated>2015-10-13T08:01:38.783-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="blob index"/><category scheme="http://www.blogger.com/atom/ns#" term="fragmentation in indexes"/><category scheme="http://www.blogger.com/atom/ns#" term="Indexing in SQL Server"/><category scheme="http://www.blogger.com/atom/ns#" term="optimized indexing"/><category scheme="http://www.blogger.com/atom/ns#" term="Reorganize index"/><title type='text'>OPTIMIZED INDEXING IN SQL SERVER</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;color: #660000;&quot;&gt;OPTIMIZED INDEXING IN SQL SERVER&lt;/span&gt;&lt;/u&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;u&gt;&lt;span style=&quot;color: #660000;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;Indexing is an important task to keep the database performance good. But there &amp;nbsp;are many issues with indexing which we being a DBA have to pay attention.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;&lt;u&gt;Points to remember indexing:&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;1. Indexing regularly increases the i/o on disk. More disk activity is always dangerous for the CPU.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;2. Choosing correct fill factor is mandatory for the performance.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;3. Fragmentation percentage should be rightly considered for reorganizing or rebuilding indexes.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;4. &amp;nbsp;All the indexes cannot be rebuild with ONLINE operation. Blob indexes should come under offline indexes.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;5. Online Indexing comes under Enterprise\Developer edition.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;6. Page count of indexes is important to consider for large tables.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;7. Logging of data is important to know which index has taken more time for getting rebuild or reorg.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;If a table is very large then huge amount of fragmented pages also do not come under more than 20% fragmentation percentage.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;Keeping above point in view, Below code is built in such a way&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;1. It will keep the logging of every index in msdb database.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;2. Depending upon edition of SQL Server, code will be intelligent enough to run in any edition.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;3. BLOB indexes will be reindex&#39;d offline only.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: blue; font-family: Verdana, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style=&quot;color: green;&quot;&gt;---NEW INDEXING CODE (LIVE)----&lt;br /&gt;------------------------------------------------&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECLARE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@C1 &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;CURSOR&lt;br /&gt;DECLARE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Tname &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;NVARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;MAX&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECLARE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Iname &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;NVARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;MAX&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECLARE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Frag &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INT&lt;br /&gt;DECLARE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@PCount &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INT&lt;br /&gt;DECLARE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@FCount &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INT&lt;br /&gt;DECLARE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Obj_id &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;bigint&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECLARE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Ind_id &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INT&lt;br /&gt;DECLARE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt1 &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DATETIME&lt;br /&gt;DECLARE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt2 &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DATETIME&lt;br /&gt;DECLARE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@edition &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;NVARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECLARE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@version &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;NVARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECLARE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@SQL1 &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;NVARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;MAX&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECLARE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@SQL2 &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;NVARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;MAX&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECLARE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@SQL3 &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;NVARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;MAX&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECLARE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@SQL4 &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;NVARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;MAX&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECLARE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dbname &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;NVARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;MAX&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;DECLARE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@schname &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;NVARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;MAX&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;IF &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;NOT EXISTS(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;1 &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;msdb.dbo.sys.objects &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;WHERE &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;name &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;LIKE &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;Index_Operation&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;BEGIN&lt;br /&gt;CREATE TABLE &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;msdb.dbo.Index_Operation &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;DB_NAME &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;NVARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;MAX&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Obj_id bigint&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Ind_id &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Index_name &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;NVARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;MAX&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Frag_Percent &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Page_Count &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Fragments_Count &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Ind_Operation &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;20&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Start_time &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DATETIME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;End_time &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;DATETIME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Time_Taken_Sec &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;FLOAT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;TYPE CHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;))&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;END&lt;br /&gt;&lt;br /&gt;SELECT &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;* &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INTO &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;#indices &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT &lt;br /&gt;DISTINCT &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;OBJECT_NAME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;OBJECT_ID&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Tname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;b.name Iname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;SUM&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.avg_fragmentation_in_percent&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)/&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;COUNT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;index_level&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Frag&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;DB_NAME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.database_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;dbname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;OBJECT_SCHEMA_NAME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;OBJECT_ID&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;schname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;OBJECT_ID&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.index_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.avg_fragment_size_in_pages&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;page_count&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;fragment_count&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sys.dm_db_index_physical_stats&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;DB_ID&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(), NULL,NULL, NULL,NULL) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;JOIN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;sys.indexes &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;b &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;WITH&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;nolock&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;ON &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;OBJECT_ID &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;b.&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;OBJECT_ID&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;AND &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.index_id &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;b.index_id &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;WHERE &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.database_id &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;DB_ID&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;() AND &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;b.name &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;IS &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;NOT NULL&lt;br /&gt;AND &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.avg_fragmentation_in_percent &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&amp;lt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;0 &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;AND &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.page_count&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;1000 &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;AND &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;fragment_count&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;100&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;AND &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;b.allow_page_locks&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;1 &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;AND &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;b.name &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;NOT LIKE &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;%merge%&#39; &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;GROUP BY &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;OBJECT_ID&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;b.name&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.database_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.index_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;a.avg_fragment_size_in_pages&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;page_count&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;fragment_count&lt;br /&gt; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;ind&lt;br /&gt; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;&amp;nbsp;SET &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@C1&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= CURSOR FOR&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Tname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Iname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;Frag&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;dbname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;schname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[object_id]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;index_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;page_count&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;fragment_count &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;#indices&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/code&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;* &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INTO &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;#indcolumns &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;OBJECT_ID&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;A.TABLE_NAME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[OBJECT_ID]&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;A.TABLE_SCHEMA&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;A.TABLE_NAME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;A.COLUMN_NAME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;A.DATA_TYPE &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;INFORMATION_SCHEMA.COLUMNS A &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;WHERE &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;DATA_TYPE &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;IN &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;LOWER&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;FILESTREAM&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;LOWER&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;XML&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;LOWER&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;VARBINARY&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;LOWER&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;TEXT&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;LOWER&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;NTEXT&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;LOWER&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;IMAGE&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)) &lt;br /&gt;OR&lt;br /&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;DATA_TYPE &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;IN &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;LOWER&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;VARCHAR&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;LOWER&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;NVARCHAR&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)) AND &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;CHAR&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;ACTER_MAXIMUM_LENGTH &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)) &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;A&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/code&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@edition&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;SERVERPROPERTY&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;Edition&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)))&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@version&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;= &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT &lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CONVERT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;NVARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;SUBSTRING&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@@version&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;CHARINDEX&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;-&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@@version&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)-&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)))&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;OPEN &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@C1&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;FETCH &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;next &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@C1 &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INTO &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Tname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Iname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Frag&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dbname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@schname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Obj_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Ind_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@PCount&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@FCount&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/code&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;WHILE &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@@FETCH_STATUS&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;BEGIN&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IF&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@FCount&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;500&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&lt;br /&gt;&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;&quot;&gt;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;&amp;nbsp;&amp;nbsp;&amp;nbsp; IF &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;( &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@edition &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;LIKE &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;%Enterprise%&#39; &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;AND &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@version &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;LIKE &lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;%2008%&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;BEGIN&amp;nbsp;&amp;nbsp;&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; IF &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;EXISTS (&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SELECT &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[OBJECT_ID] &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;#indcolumns &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;WHERE &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;[OBJECT_ID]&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Obj_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BEGIN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;TRY&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@SQL1&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;ALTER INDEX [&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Iname &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;] on&amp;nbsp;&amp;nbsp;[&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dbname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;].[&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@schname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;].[&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Tname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;] REBUILD&#39;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;PRINT &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@SQL1&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt1&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;EXEC &lt;/span&gt;&lt;span style=&quot;color: darkred;&quot;&gt;sp_executesql &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@SQL1&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt2&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INSERT INTO &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;msdb.dbo.Index_Operation &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dbname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Obj_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Ind_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Iname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Frag&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@PCount&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@FCount&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;REBUILD&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt1&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;DATEDIFF&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;MILLISECOND&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt1&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS FLOAT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)/&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;1000&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;B&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;END &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;TRY&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;BEGIN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;CATCH&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt1&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt2&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INSERT INTO &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;msdb.dbo.Index_Operation &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dbname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Obj_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Ind_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Iname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Frag&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@PCount&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@FCount&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;FAILED&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt1&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,NULL,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;B&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;END &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;CATCH&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;END&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; ELSE&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; 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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BEGIN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;TRY&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@SQL2&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;ALTER INDEX [&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Iname &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;] on&amp;nbsp;&amp;nbsp;[&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dbname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;].[&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@schname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;].[&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Tname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;] REBUILD with (FILLFACTOR= 80 , SORT_IN_TEMPDB = ON, ONLINE = ON) &#39;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;PRINT &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@SQL2&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt1&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;EXEC &lt;/span&gt;&lt;span style=&quot;color: darkred;&quot;&gt;sp_executesql &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@SQL2&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt2&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INSERT INTO &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;msdb.dbo.Index_Operation &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dbname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Obj_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Ind_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Iname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Frag&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@PCount&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@FCount&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;REBUILD&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt1&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;DATEDIFF&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;MILLISECOND&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt1&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS FLOAT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)/&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;1000&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;I&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;END &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;TRY&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;BEGIN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;CATCH&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt1&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt2&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INSERT INTO &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;msdb.dbo.Index_Operation &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dbname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Obj_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Ind_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Iname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Frag&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@PCount&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@FCount&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;FAILED&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt1&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,NULL,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;I&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;END &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;CATCH&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;END&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; &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; END&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; ELSE &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; BEGIN&amp;nbsp;&amp;nbsp;&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BEGIN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;TRY&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@SQL3&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;ALTER INDEX [&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Iname &lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;] on&amp;nbsp;&amp;nbsp;[&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dbname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;].[&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@schname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;].[&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Tname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;] REBUILD with (FILLFACTOR= 80 , SORT_IN_TEMPDB = ON) &#39;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;PRINT &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@SQL3&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt1&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;EXEC &lt;/span&gt;&lt;span style=&quot;color: darkred;&quot;&gt;sp_executesql &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@SQL3&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt2&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INSERT INTO &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;msdb.dbo.Index_Operation &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dbname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Obj_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Ind_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Iname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Frag&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@PCount&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@FCount&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;REBUILD&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt1&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;DATEDIFF&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;MILLISECOND&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt1&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS FLOAT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)/&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;1000&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;I&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;END &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;TRY&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;BEGIN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;CATCH&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt1&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt2&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INSERT INTO &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;msdb.dbo.Index_Operation &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dbname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Obj_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Ind_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Iname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Frag&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@PCount&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@FCount&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;FAILED&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt1&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,NULL,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;I&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;END &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;CATCH&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;END&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; &lt;br /&gt;&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ELSE&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&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;&amp;nbsp;&amp;nbsp;&amp;nbsp; BEGIN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;TRY&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@SQL4&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;ALTER INDEX [&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Iname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;] on&amp;nbsp;&amp;nbsp;[&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dbname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;].[&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@schname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;].[&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Tname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;] REORGANIZE&amp;nbsp;&amp;nbsp;&#39;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;PRINT &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@SQL4&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt1&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;EXEC &lt;/span&gt;&lt;span style=&quot;color: darkred;&quot;&gt;sp_executesql &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@SQL4&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt2&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INSERT INTO &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;msdb.dbo.Index_Operation &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dbname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Obj_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Ind_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Iname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Frag&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@PCount&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@FCount&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;REORGANIZE&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt1&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;DATEDIFF&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;MILLISECOND&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt1&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;AS FLOAT&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)/&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;1000&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;I&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;END &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;TRY&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;BEGIN &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;CATCH&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt1&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;SET &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt2&lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INSERT INTO &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;msdb.dbo.Index_Operation &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;VALUES&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dbname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Obj_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Ind_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Iname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Frag&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@PCount&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@FCount&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;FAILED&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt1&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dt2&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,NULL,&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;I&#39;&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&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; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;END &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;CATCH&lt;br /&gt;&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;&quot;&gt;END&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FETCH &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;next &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;FROM &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@C1 &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;INTO &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Tname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Iname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Frag&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@dbname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@schname&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Obj_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@Ind_id&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@PCount&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@FCount&lt;br /&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;END&lt;br /&gt;&lt;br /&gt;CLOSE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@C1&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;DELETE FROM &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;msdb.dbo.Index_Operation &lt;/span&gt;&lt;span style=&quot;color: blue;&quot;&gt;WHERE &lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;End_time&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;&amp;lt;=&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;GETDATE&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;()-&lt;/span&gt;&lt;span style=&quot;color: black;&quot;&gt;31&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;DROP TABLE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;#indices&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;DROP TABLE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;#indcolumns&lt;/span&gt;&lt;/code&gt;&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;DEALLOCATE &lt;/span&gt;&lt;span style=&quot;color: #434343;&quot;&gt;@C1&lt;/span&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: #434343;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/code&gt;
&lt;code style=&quot;font-size: 12px;&quot;&gt;&lt;span style=&quot;color: #434343;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/code&gt;&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/4310302719021510399'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/4310302719021510399'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2015/10/optimized-indexing-in-sql-server.html' title='OPTIMIZED INDEXING IN SQL SERVER'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-8836786694313675267</id><published>2014-06-24T13:16:00.004-07:00</published><updated>2014-06-27T12:17:15.114-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Backups and Restore"/><category scheme="http://www.blogger.com/atom/ns#" term="Database refresh"/><category scheme="http://www.blogger.com/atom/ns#" term="DB Migration"/><category scheme="http://www.blogger.com/atom/ns#" term="Orphan Users"/><category scheme="http://www.blogger.com/atom/ns#" term="User Migration"/><category scheme="http://www.blogger.com/atom/ns#" term="User-Defined DB Roles"/><title type='text'>Create Users script from a database</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;color: #274e13;&quot;&gt;USER CREATE SCRIPT FROM DATABASE&lt;/span&gt;&lt;/u&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;u&gt;&lt;span style=&quot;color: #274e13;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div&gt;
We need to preserve the users information from a database before refresh/restore task.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Below script will be used to generate a script out the database having all the user information. This script will generate &quot;User Creation script&quot;.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
It will be conditional script. If user is already existing in the destination or refreshed database then it will skip else it will create the user.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #ffffcc; border: 1px solid #000000; height: 500px; overflow-x: scroll; overflow-y: scroll; text-align: Left; width: 550px;&quot;&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;br /&gt;
&amp;nbsp;Declare @C1 Cursor&lt;br /&gt;
&amp;nbsp;Declare @Uname nvarchar(200)&lt;br /&gt;
&amp;nbsp;Declare @Lname nvarchar(200)&lt;br /&gt;
&amp;nbsp;Declare @type char(1)&lt;br /&gt;
&amp;nbsp;set @C1 = Cursor for&lt;br /&gt;
&amp;nbsp;select name,SUSER_SNAME(sid) AS LOGIN_NAME,type from sys.database_principals&lt;br /&gt;
&amp;nbsp;where type in (&#39;U&#39;,&#39;S&#39;,&#39;G&#39;) and SUSER_SNAME(sid) is NOT NULL and principal_id&amp;gt;4&lt;br /&gt;
&amp;nbsp;and name not like &#39;#%&#39;
and name not like &#39;NT AUTHORITY%&#39;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;OPEN @C1&lt;br /&gt;
&amp;nbsp;FETCH NEXT FROM @C1 into @Uname,@Lname,@type&lt;br /&gt;
&amp;nbsp;WHILE (@@FETCH_STATUS=0)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; Begin&lt;br /&gt;
&amp;nbsp; &amp;nbsp; PRINT &#39;IF NOT EXISTS (Select name from sys.sysusers where name=&#39;&#39;&#39;+@Uname+&#39;&#39;&#39;) create User  [&#39;+@Uname+&#39;]&#39;+&#39; FOR LOGIN [&#39;+@Lname+&#39;];&#39;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;If (@type=&#39;S&#39;)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; PRINT &#39;IF EXISTS (Select name from sys.sysusers where name=&#39;&#39;&#39;+@Uname+&#39;&#39;&#39;) exec sp_change_users_login &#39;&#39;update_one&#39;&#39;,[&#39;+@Uname+&#39;],[&#39;+@Lname+&#39;];&#39;&lt;br /&gt;
&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; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FETCH NEXT FROM @C1 into @Uname,@Lname,@type&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; End &lt;br /&gt;
&amp;nbsp;Close @C1&lt;br /&gt;
&amp;nbsp; Deallocate @C1

&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/8836786694313675267'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/8836786694313675267'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2014/06/create-users-script-from-database.html' title='Create Users script from a database'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-8037625733614308687</id><published>2014-06-21T13:58:00.002-07:00</published><updated>2014-06-21T13:58:50.394-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="DB Migration"/><category scheme="http://www.blogger.com/atom/ns#" term="Logins and Privileges"/><category scheme="http://www.blogger.com/atom/ns#" term="User Migration"/><title type='text'>Find Users with privileges other than database roles</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;color: #274e13;&quot;&gt;USERS HAVING PRIVILEGES ON DATABASE OBJECTS&lt;/span&gt;&lt;/u&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Privileges can be provided to users of a database in 3 ways.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
1. By assigning the user to a fixed database role&lt;/div&gt;
&lt;div&gt;
2. By assigning the user to a User defined database role&lt;/div&gt;
&lt;div&gt;
3. By granting privilege to a user directly on the database objects.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
In this area of blog, we will cover the third part. The other two points are also covered in the blog.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Below is the query provided to track down the privileges on database objects for all the users in a particular database of SQL Server.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
This will be very helpful in case of database refresh and database restore activity to retain the privileges of Users on a database.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Script will be generated in the last column of the query output. Do save it :)&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #ffffcc; border: 1px solid #000000; height: 400px; overflow-x: scroll; overflow-y: scroll; text-align: Left; width: 550px;&quot;&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;SELECT&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;USER_NAME(p.grantee_principal_id) AS principal_name,&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;SUSER_SNAME(dp.sid) AS LOGIN_NAME,&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;dp.principal_id,&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;dp.type_desc AS principal_type_desc,&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;p.class_desc,&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;OBJECT_NAME(p.major_id) AS [object_name],&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;p.permission_name,&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;p.state_desc AS permission_state_desc,&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&#39;grant &amp;nbsp;&#39;+p.permission_name +&#39; on object::[&#39;+OBJECT_NAME(p.major_id)+&#39;] to &#39;+USER_NAME(p.grantee_principal_id)+&#39;&#39; as &#39;script&#39;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;from &lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;sys.database_permissions p&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;inner JOIN&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;sys.database_principals dp&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;on &amp;nbsp; &amp;nbsp;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;p.grantee_principal_id = dp.principal_id&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;where dp.type_desc &amp;nbsp;&amp;lt;&amp;gt;&#39;DATABASE_ROLE&#39; and &lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;dp.principal_id&amp;gt;4 and OBJECT_NAME(p.major_id) is not null&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/8037625733614308687'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/8037625733614308687'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2014/06/find-users-with-privileges-other-than.html' title='Find Users with privileges other than database roles'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-4118086615081266456</id><published>2014-06-21T13:35:00.000-07:00</published><updated>2014-06-21T13:38:52.296-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Database refresh"/><category scheme="http://www.blogger.com/atom/ns#" term="DB Migration"/><category scheme="http://www.blogger.com/atom/ns#" term="Logins and Privileges"/><category scheme="http://www.blogger.com/atom/ns#" term="User Migration"/><category scheme="http://www.blogger.com/atom/ns#" term="User-Defined DB Roles"/><title type='text'>Find user defined database roles and its privileges</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;color: #274e13;&quot;&gt;HOW TO FIND USER DEFINED DATABASE ROLES AND THE PRIVILEGES ASSOCIATED WITH THE ROLE&lt;/span&gt;&lt;/u&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;u&gt;&lt;span style=&quot;color: #274e13;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div&gt;
There can be many user defined database roles in a database which are created by a DBA depending upon the requirement from support teams.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Instead of granting a particular access to any user&amp;nbsp;, it is always to better to create a database role and grant the privileges to the role. DBA will have more control on security when lots of users want similar kind of access.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;u style=&quot;color: #274e13;&quot;&gt;Challenge:&lt;/u&gt;&lt;span style=&quot;font-weight: normal;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;Challenge for a DBA comes when refresh of a database takes place having lots of user defined roles and backup used is the old one which doesn&#39;t have any. We have to make sure after refresh all the roles and Users are intact as it was before the refresh.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;To achieve the above defined goal, we should save the db roles with its respective privileges by scripting them in order to create them once the refresh/restore is performed by a DBA.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-weight: normal;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
Below query is used to find the user defined roles with its respective privileges and also provide the script to be used as next course of action.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: #ffffcc; border: 1px solid #000000; height: 400px; overflow-x: scroll; overflow-y: scroll; text-align: Left; width: 550px;&quot;&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;select&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;name as principal_name,&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;SUSER_SNAME(sid) AS LOGIN_NAME , &lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;principal_id,&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;type_desc,&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&#39;NULL&#39; [Class Desc],&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&#39;NULL&#39; [Object Name],&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&#39;NULL&#39; [Permission Name],&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&#39;NULL&#39; [Permission Type],&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&#39;sp_addrole [&#39;+name+&#39;]&#39; collate &amp;nbsp;SQL_Latin1_General_CP1_CI_AS AS [Script]&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;from sys.database_principals&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;where type=&#39;R&#39; and is_fixed_role =0 and name not like &#39;public&#39; and&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;principal_id in&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;(select distinct grantee_principal_id from sys.database_permissions)&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;UNION ALL&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;SELECT&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;USER_NAME(p.grantee_principal_id) AS &amp;nbsp;principal_name,&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;SUSER_SNAME(dp.sid) AS LOGIN_NAME,&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;dp.principal_id,&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;dp.type_desc AS principal_type_desc,&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;p.class_desc,&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;OBJECT_NAME(p.major_id) &amp;nbsp;AS [object_name],&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;p.permission_name,&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;p.state_desc AS permission_state_desc,&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;Case &lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;when OBJECT_NAME(p.major_id) is not null&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;then &#39;grant &#39;+p.permission_name +&#39; on &amp;nbsp;object::[&#39;+OBJECT_NAME(p.major_id)+&#39;] to &amp;nbsp;&#39;+USER_NAME(p.grantee_principal_id)+&#39;&#39;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;when OBJECT_NAME(p.major_id) is null &lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;then &#39;grant &#39;+p.permission_name +&#39; to &#39;+USER_NAME(p.grantee_principal_id)+&#39;&#39;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;End &amp;nbsp;as &#39;script&#39;&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;from sys.database_permissions p&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;inner JOIN sys.database_principals dp on &lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;p.grantee_principal_id = dp.principal_id where dp.type_desc like&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&#39;DATABASE_ROLE&#39; and dp.principal_id&amp;gt;0&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; GO&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/4118086615081266456'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/4118086615081266456'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2014/06/find-user-defined-database-roles-and.html' title='Find user defined database roles and its privileges'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-9202249344675929954</id><published>2014-06-21T12:59:00.000-07:00</published><updated>2014-06-21T13:01:09.181-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Primay key and foreign key relation"/><category scheme="http://www.blogger.com/atom/ns#" term="relational database"/><title type='text'>Find the relational tables</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;color: #274e13;&quot;&gt;TABLES HAVING RELATION WITH OTHER TABLES IN SQL SERVER&lt;/span&gt;&lt;/u&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;u&gt;&lt;span style=&quot;color: #274e13;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div&gt;
In a good relational database having normal forms being implemented will have relational tables.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
This is possible by creating primary and foreign keys between the tables.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Below Query is used to find the tables having primary key and foreign key relations.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #ffffcc; border: 1px solid #000000; height: 200px; overflow-x: scroll; overflow-y: scroll; text-align: Left; width: 550px;&quot;&gt;
&lt;div style=&quot;width: 100%;&quot;&gt;
&amp;nbsp;SELECT 
     OBJECT_NAME (referenced_object_id)                      AS Parent_Table , &amp;nbsp;OBJECT_NAME(parent_object_id)                          AS Child_Table , &amp;nbsp;COL_NAME(referenced_object_id,referenced_column_id)    AS Parent_Column
    , &amp;nbsp;COL_NAME(parent_object_id,parent_column_id)            AS Child_Column
    , &amp;nbsp;OBJECT_NAME(constraint_object_id)                      AS FK_Name
FROM&lt;br /&gt;
&amp;nbsp;sys.foreign_key_columns &lt;br /&gt;
&amp;nbsp;ORDER BY Parent_Table, Child_Table

&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/9202249344675929954'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/9202249344675929954'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2014/06/find-relational-tables.html' title='Find the relational tables'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-2059527929056925862</id><published>2014-06-21T12:25:00.000-07:00</published><updated>2014-06-21T12:27:23.462-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Logins and Privileges"/><category scheme="http://www.blogger.com/atom/ns#" term="Orphan logins"/><category scheme="http://www.blogger.com/atom/ns#" term="Orphan Users"/><title type='text'>How to Fix the Orphan users of a database</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;u&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;FIX THE ORPHAN USERS OF DATABASE&lt;/span&gt;&lt;/u&gt;&lt;/h2&gt;
&lt;div&gt;
&lt;u&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div&gt;
Orphan Users are those users whose login exist in the SQL Server but mapping would have lost due to refresh of database.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
When ever refresh of database is performed or restore of database on another SQL Server Instance chances of Orphan users arise.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Below Query will fix all the Orphan users at one go.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;background-color: #ffffcc; border: 1px solid #000000; height: 600px; overflow-x: scroll; overflow-y: scroll; text-align: Left; width: 550px;&quot;&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; set nocount on&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; declare @Action &amp;nbsp; &amp;nbsp;varchar(10) &amp;nbsp;-- &#39;&#39; (default) / UPDATE&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; set @Action=&#39;&#39; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; declare @ret &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;int,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @cfixesupdate &amp;nbsp; int, &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;-- count of fixes by update&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @loginsid &amp;nbsp; &amp;nbsp; &amp;nbsp; varbinary(85),&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @txtloginsid &amp;nbsp; &amp;nbsp;varchar(256),&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @name &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sysname,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @binpwd &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; varbinary(256),&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @txtpwd &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sysname,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @tmpstr &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; varchar (256),&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; @status &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; int&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; -- SET INITIAL VALUES --&lt;br /&gt;
&amp;nbsp; &amp;nbsp; select &amp;nbsp;@cfixesupdate &amp;nbsp; = 0&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; -- CHECK PERMISSIONS --&lt;br /&gt;
&amp;nbsp; &amp;nbsp; if not is_member(&#39;db_owner&#39;) = 1&lt;br /&gt;
&amp;nbsp; &amp;nbsp; begin&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; raiserror(15247,-1,-1)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return&lt;br /&gt;
&amp;nbsp; &amp;nbsp; end&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; -- PREPARE CURSOR FOR SYSUSERS TABLE IN CURRENT DATABASE --&lt;br /&gt;
&amp;nbsp; &amp;nbsp; DECLARE login_curs CURSOR FOR&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; select [name], sid, [password] from sysusers&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; where issqluser = 1&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; and (sid is not null and sid &amp;lt;&amp;gt; 0x0)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; and suser_sname(sid) is null&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; and [name] &amp;lt;&amp;gt; (&#39;dbo&#39;)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; order by [name]&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; OPEN login_curs&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; FETCH NEXT FROM login_curs INTO @name, @loginsid, @binpwd&lt;br /&gt;
&amp;nbsp; &amp;nbsp; IF (@@fetch_status = -1)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; BEGIN&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; PRINT &#39;No login(s) found.&#39;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; CLOSE login_curs&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; DEALLOCATE login_curs&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; RETURN&lt;br /&gt;
&amp;nbsp; &amp;nbsp; END&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; -- Create a temp table to hold any database login scripts that need to be generated&lt;br /&gt;
&amp;nbsp; &amp;nbsp; -- and manually run by the user.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; if exists (select * from tempdb..sysobjects where name like &#39;#sqlstmts%&#39;) drop table #sqlstmts&lt;br /&gt;
&amp;nbsp; &amp;nbsp; create table #sqlstmts&lt;br /&gt;
&amp;nbsp; &amp;nbsp; (&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sqlid &amp;nbsp; int,&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sqlname varchar(50),&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; sqlstmt varchar(1000)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; )&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; -- PROCESS ALL USERS IN CURSOR --&lt;br /&gt;
&amp;nbsp; &amp;nbsp; WHILE (@@fetch_status &amp;lt;&amp;gt; -1)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; BEGIN&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; IF (@@fetch_status &amp;lt;&amp;gt; -2)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; BEGIN&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; set @cfixesupdate = @cfixesupdate + 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; -- Increment the number of successfully repaired users.&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; -- NOTE: &amp;nbsp;SYSLOGINS is a view for the SYSXLOGINS table.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; set @ret = null &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; -- init return value&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; select @ret = 1 from master.dbo.syslogins where&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; loginname = @name &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; -- match login name&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; and isntname = 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;-- cannot use nt logins&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; --print &#39;User: &#39; + cast(@name as varchar(25)) + &#39; -- @RET: &#39; + cast(@ret as varchar(10))&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if @ret is not null &amp;nbsp; &amp;nbsp; -- database login exists&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; begin&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; -- REPAIR THE USER LOGIN (SID) --&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if upper(@Action) = &#39;UPDATE&#39;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; begin -- Attempt to repair the user login.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; exec @ret = sp_change_users_login &#39;update_one&#39;, @name, @name&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if @ret = 0&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; print @name + &#39;: repaired &#39;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; else&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; print @name + &#39;: failed &#39;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; end -- if UPDATE...&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; else &amp;nbsp;-- Only generate the EXEC statements. &amp;nbsp;Users must execute these statements manually.&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; --print &#39;exec sp_change_users_login &#39;&#39;update_one&#39;&#39;, &#39;&#39;&#39; + @name + &#39;&#39;&#39;, &#39;&#39;&#39; + @name + &#39;&#39;&#39;&#39;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; set @tmpstr = &#39;exec sp_change_users_login &#39;&#39;update_one&#39;&#39;, &#39;&#39;&#39; + @name + &#39;&#39;&#39;, &#39;&#39;&#39; + @name + &#39;&#39;&#39;&#39;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; insert into #sqlstmts values (0, @name, @tmpstr)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; end -- else&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; end -- If @ret is not null...&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; NextUser: &amp;nbsp; &amp;nbsp; &amp;nbsp; -- Get the next user&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FETCH NEXT FROM login_curs INTO @name, @loginsid, @binpwd&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; end&lt;br /&gt;
&amp;nbsp; &amp;nbsp; end&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; CLOSE login_curs&lt;br /&gt;
&amp;nbsp; &amp;nbsp; DEALLOCATE login_curs&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; print &#39;&#39;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; print &#39;---------------------------------------&#39;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; print &#39;Total users: &#39; + cast(@cfixesupdate as varchar(8))&lt;br /&gt;
&amp;nbsp; &amp;nbsp; print &#39;---------------------------------------&#39;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; -- Dump the SQL statements created by this script.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; select sqlid as &#39;Seq&#39;, sqlname as &#39;User&#39;, sqlstmt as &#39;Run Me&#39; from #sqlstmts order by sqlid, sqlname&lt;br /&gt;
&amp;nbsp; &amp;nbsp; drop table #sqlstmts&lt;br /&gt;
&lt;br /&gt;
GO&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
We have to run the above query in the database where restore or refresh is performed. &lt;br /&gt;
&lt;br /&gt;
The above script will generate a query in &#39;Run Me&#39; Column &lt;br /&gt;
&lt;br /&gt;
Copy all consolidated script at once and execute in the database to fix the Orphan users &lt;br /&gt;
&lt;br /&gt;
Thanks :)&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/2059527929056925862'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/2059527929056925862'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2014/06/how-to-fix-orphan-users-of-database.html' title='How to Fix the Orphan users of a database'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-550837798577536569</id><published>2014-06-20T14:15:00.001-07:00</published><updated>2014-06-21T12:42:28.314-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="delete data in tables"/><category scheme="http://www.blogger.com/atom/ns#" term="Indexing in SQL Server"/><category scheme="http://www.blogger.com/atom/ns#" term="troubleshooting"/><category scheme="http://www.blogger.com/atom/ns#" term="Truncate tables"/><title type='text'>TRUNCATE DATA FROM TABLES</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2&gt;
&lt;u&gt;&lt;span style=&quot;color: #38761d;&quot;&gt;TRUNCATE ALL THE DATA FROM TABLES&lt;/span&gt;&lt;/u&gt;&lt;/h2&gt;
&lt;div&gt;
We do get the requirement some time to clean up all the data from the tables of a relational database.&lt;/div&gt;
&lt;div&gt;
We cannot simply truncate the data table by table in relational database due to relationship between primary key tables and foreign key tables.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Best thing to do in this scenario is to drop all the constraints from the database and truncate the data. We can later of create the constraints to make sure the metadata remains the same even after removing the complete data from the database tables.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
So, how to proceed now:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #274e13;&quot;&gt;&lt;u&gt;Step 1: COPY ALL THE CONSTRAINTS&lt;/u&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
Below Script is used for creating a script for &quot;Create constraints&quot; as well as &quot;Dropping Constraints&quot;, Please focus on the &lt;b&gt;&lt;span style=&quot;color: red;&quot;&gt;red coloured code &lt;/span&gt;&lt;/b&gt;in between and remove to comment for which ever action you wanted to perform. Copy both the action scripts output and execute the &quot;Drop&quot; action.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: #ffffcc; border: 1px solid #000000; height: 600px; overflow-x: scroll; overflow-y: scroll; text-align: Left; width: 550px;&quot;&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;div&gt;
DECLARE @schema_name sysname;&lt;/div&gt;
&lt;div&gt;
DECLARE @table_name sysname;&lt;/div&gt;
&lt;div&gt;
DECLARE @constraint_name sysname;&lt;/div&gt;
&lt;div&gt;
DECLARE @constraint_object_id int;&lt;/div&gt;
&lt;div&gt;
DECLARE @referenced_object_name sysname;&lt;/div&gt;
&lt;div&gt;
DECLARE @is_disabled bit;&lt;/div&gt;
&lt;div&gt;
DECLARE @is_not_for_replication bit;&lt;/div&gt;
&lt;div&gt;
DECLARE @is_not_trusted bit;&lt;/div&gt;
&lt;div&gt;
DECLARE @delete_referential_action tinyint;&lt;/div&gt;
&lt;div&gt;
DECLARE @update_referential_action tinyint;&lt;/div&gt;
&lt;div&gt;
DECLARE @tsql nvarchar(4000);&lt;/div&gt;
&lt;div&gt;
DECLARE @tsql2 nvarchar(4000);&lt;/div&gt;
&lt;div&gt;
DECLARE @fkCol sysname;&lt;/div&gt;
&lt;div&gt;
DECLARE @pkCol sysname;&lt;/div&gt;
&lt;div&gt;
DECLARE @col1 bit;&lt;/div&gt;
&lt;div&gt;
DECLARE @action char(6);&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red;&quot;&gt;--SET @action = &#39;DROP&#39;;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: red;&quot;&gt;--SET @action = &#39;CREATE&#39;;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
DECLARE FKcursor CURSOR FOR&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; select OBJECT_SCHEMA_NAME(parent_object_id)&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;, OBJECT_NAME(parent_object_id), name, OBJECT_NAME(referenced_object_id)&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;, object_id&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;, is_disabled, is_not_for_replication, is_not_trusted&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;, delete_referential_action, update_referential_action&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; from sys.foreign_keys&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; order by 1,2;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
OPEN FKcursor;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
FETCH NEXT FROM FKcursor INTO @schema_name, @table_name, @constraint_name&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; , @referenced_object_name, @constraint_object_id&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; , @is_disabled, @is_not_for_replication, @is_not_trusted&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; , @delete_referential_action, @update_referential_action;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
WHILE @@FETCH_STATUS = 0&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
BEGIN&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; IF @action &amp;lt;&amp;gt; &#39;CREATE&#39;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SET @tsql = &#39;ALTER TABLE &#39;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; + QUOTENAME(@schema_name) + &#39;.&#39; + QUOTENAME(@table_name)&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; + &#39; DROP CONSTRAINT &#39; + QUOTENAME(@constraint_name) + &#39;;&#39;;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; ELSE&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; BEGIN&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SET @tsql = &#39;ALTER TABLE &#39;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; + QUOTENAME(@schema_name) + &#39;.&#39; + QUOTENAME(@table_name)&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; + CASE @is_not_trusted&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&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; WHEN 0 THEN &#39; WITH CHECK &#39;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&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; ELSE &#39; WITH NOCHECK &#39;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; END&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; + &#39; ADD CONSTRAINT &#39; + QUOTENAME(@constraint_name)&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; + &#39; FOREIGN KEY (&#39;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SET @tsql2 = &#39;&#39;;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; DECLARE ColumnCursor CURSOR FOR&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; select COL_NAME(fk.parent_object_id, fkc.parent_column_id)&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;, COL_NAME(fk.referenced_object_id, fkc.referenced_column_id)&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; from sys.foreign_keys fk&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; inner join sys.foreign_key_columns fkc&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; on fk.object_id = fkc.constraint_object_id&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; where fkc.constraint_object_id = @constraint_object_id&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; order by fkc.constraint_column_id;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; OPEN ColumnCursor;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SET @col1 = 1;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FETCH NEXT FROM ColumnCursor INTO @fkCol, @pkCol;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; WHILE @@FETCH_STATUS = 0&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; BEGIN&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; IF (@col1 = 1)&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SET @col1 = 0&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ELSE&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; BEGIN&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SET @tsql = @tsql + &#39;,&#39;;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SET @tsql2 = @tsql2 + &#39;,&#39;;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; END;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SET @tsql = @tsql + QUOTENAME(@fkCol);&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SET @tsql2 = @tsql2 + QUOTENAME(@pkCol);&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; FETCH NEXT FROM ColumnCursor INTO @fkCol, @pkCol;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; END;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; CLOSE ColumnCursor;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; DEALLOCATE ColumnCursor;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SET @tsql = @tsql + &#39; ) REFERENCES &#39; + QUOTENAME(@schema_name) + &#39;.&#39; + QUOTENAME(@referenced_object_name)&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; + &#39; (&#39; + @tsql2 + &#39;)&#39;; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SET @tsql = @tsql&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; + &#39; ON UPDATE &#39; + CASE @update_referential_action&lt;/div&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; WHEN 0 THEN &#39;NO ACTION &#39;&lt;/div&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; WHEN 1 THEN &#39;CASCADE &#39;&lt;/div&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; WHEN 2 THEN &#39;SET NULL &#39;&lt;/div&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; ELSE &#39;SET DEFAULT &#39;&lt;/div&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; END&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; + &#39; ON DELETE &#39; + CASE @delete_referential_action&lt;/div&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; WHEN 0 THEN &#39;NO ACTION &#39;&lt;/div&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; WHEN 1 THEN &#39;CASCADE &#39;&lt;/div&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; WHEN 2 THEN &#39;SET NULL &#39;&lt;/div&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; ELSE &#39;SET DEFAULT &#39;&lt;/div&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; END&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; + CASE @is_not_for_replication&lt;/div&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; WHEN 1 THEN &#39; NOT FOR REPLICATION &#39;&lt;/div&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; ELSE &#39;&#39;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; END&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; + &#39;;&#39;;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; END;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; PRINT @tsql;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; IF @action = &#39;CREATE&#39;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; BEGIN&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; SET @tsql = &#39;ALTER TABLE &#39;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; + QUOTENAME(@schema_name) + &#39;.&#39; + QUOTENAME(@table_name)&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; + CASE @is_disabled&lt;/div&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; WHEN 0 THEN &#39; CHECK &#39;&lt;/div&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; ELSE &#39; NOCHECK &#39;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; END&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; + &#39;CONSTRAINT &#39; + QUOTENAME(@constraint_name)&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; + &#39;;&#39;;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; PRINT @tsql;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; END;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; FETCH NEXT FROM FKcursor INTO @schema_name, @table_name, @constraint_name&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; , @referenced_object_name, @constraint_object_id&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; , @is_disabled, @is_not_for_replication, @is_not_trusted&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; , @delete_referential_action, @update_referential_action;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
END;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
CLOSE FKcursor;&lt;/div&gt;
&lt;div&gt;
DEALLOCATE FKcursor;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;div&gt;
-----------------------------------------------------&lt;/div&gt;
&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #274e13;&quot;&gt;&lt;u&gt;Step 2 : Once constraints are dropped, truncate the tables using the below script&lt;/u&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;div style=&quot;background-color: #ffffcc; border: 1px solid #000000; height: 300px; overflow-x: scroll; overflow-y: scroll; text-align: Left; width: 550px;&quot;&gt;
&lt;div style=&quot;width: 250%;&quot;&gt;
&lt;div&gt;
&amp;nbsp;Declare @c1 cursor&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;declare @name nvarchar(100)&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;declare @sql nvarchar(150)&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;set @C1 = CURSOR for&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;select distinct &#39;[&#39;+SCHEMA_NAME(schema_id)+&#39;].[&#39;+name+&#39;]&#39; from sys.objects where type = &#39;U&#39;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;open @C1&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;fetch next from @C1 into @name&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;while (@@fetch_status=0)&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; Begin&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; set @sql= &#39;Truncate table &#39;+@name&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; exec (@sql)&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Print @name+&#39; truncated successfully &#39;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;fetch next from @C1 into @name&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;End&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;close @C1&lt;/div&gt;
&lt;div&gt;
&amp;nbsp;Deallocate @C1&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;div&gt;
-----------------------------------------------------------------&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: #274e13;&quot;&gt;&lt;u&gt;Step 3: Create constraints&amp;nbsp;&lt;/u&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
Now create back the constraints using the &quot;Create Constraints&quot; script generated at step 1.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
We are done with the activity now.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Thank You&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/550837798577536569'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/550837798577536569'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2014/06/truncate-data-from-tables.html' title='TRUNCATE DATA FROM TABLES'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry><entry><id>tag:blogger.com,1999:blog-7234868290168795296.post-5330539328774392097</id><published>2013-07-25T05:09:00.000-07:00</published><updated>2013-07-25T05:11:20.907-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="ENDPOINTS IN SQL SERVER"/><category scheme="http://www.blogger.com/atom/ns#" term="Performance"/><category scheme="http://www.blogger.com/atom/ns#" term="PORTNUMBER of SQL Server"/><category scheme="http://www.blogger.com/atom/ns#" term="troubleshooting"/><title type='text'>Endpoints in SQL Server</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2 style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: red;&quot;&gt;What is an ENDPOINT in SQL SERVER&lt;/span&gt;&lt;/h2&gt;
&lt;br /&gt;
&lt;b style=&quot;background-color: lime;&quot;&gt;ENDPOINT is a gateway&lt;/b&gt; to enter into the SQL SERVER.&lt;br /&gt;
&lt;br /&gt;
Each and every connection established to the SQL SERVER has to come through an ENDPOINT.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;For Eg:&lt;/u&gt;&lt;/b&gt; Assume SQL SERVER as a GATED COMMUNITY where you have only one main entrance to enter. That is through Main Gate. This Gate is called as ENDPOINT to the GATED COMMUNITY.&lt;br /&gt;
&lt;br /&gt;
Similary, SQL Server will have ENDPOINTS through which it allows client connections to enter.&lt;br /&gt;
&lt;br /&gt;
ENDPOINTS is establised on PORT NUMBERS which are the GATEWAY from the firewall to enter into the WINDOWS SERVER where SQL Server is installed.&lt;br /&gt;
&lt;br /&gt;
We have to have a clear distinguishing idea on ENDPOINTS / PORTS / PROTOCOLS.&lt;br /&gt;
&lt;br /&gt;
&lt;b style=&quot;background-color: lime;&quot;&gt;PROTOCOL is a WAY to connect to SQL Server&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
More information on protocols can be explored below&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://manage-sqlserver.blogspot.in/2012/08/find-protocol-used-by-session-to.html&quot;&gt;&lt;b style=&quot;background-color: white;&quot;&gt;http://manage-sqlserver.blogspot.in/2012/08/find-protocol-used-by-session-to.html&lt;/b&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
PORT NUMBER is dedicated number provided by INTERNET AUTHORITY to each and every different Application/Software.&lt;br /&gt;
&lt;br /&gt;
If we are supposed to create any dynamic ports for SQL SERVER or any new endpoints, good practise is to consider port above 50000.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Port details as per given server can be find:&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://manage-sqlserver.blogspot.in/2012/10/find-ports-information-in-server.html&quot;&gt;&lt;b style=&quot;background-color: white;&quot;&gt;http://manage-sqlserver.blogspot.in/2012/10/find-ports-information-in-server.html&lt;/b&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Query to find the ENDPOINTS:&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;Select * from sys.endpoints&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;color: blue;&quot;&gt;Select * from sys.tcp_endpoints&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
/* to find TCP ENDPOINTS, this will include the dynamically created endpoints as well */&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Creating a dynamic Endpoint:&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;Create endpoint endpointname&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;state=started&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;as TCP&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;(Listener_port= portnumber , Listener_ip=All)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;for TSQL();&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;&lt;b&gt;GO&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
/* Changes needed in the above syntax are&lt;br /&gt;
&lt;b&gt;endpointname&lt;/b&gt; = Valid ENDPOINT name&lt;br /&gt;
&lt;b&gt;portnumber&lt;/b&gt; = give any port number that is up (check with NETWORK TEAM) Give above 50000 to avoid any confusion&lt;br /&gt;
&lt;b&gt;Listener_ip&lt;/b&gt; = ALL (will accept all connections from outside), Particular IP = Will accept only that IP connection.&lt;br /&gt;
*/&lt;br /&gt;
&lt;br /&gt;
Above syntax will create a dynamic endpoint.&lt;br /&gt;
&lt;br /&gt;
Hope the above information helps&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/5330539328774392097'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7234868290168795296/posts/default/5330539328774392097'/><link rel='alternate' type='text/html' href='http://manage-sqlserver.blogspot.com/2013/07/endpoints-in-sql-server.html' title='Endpoints in SQL Server'/><author><name>ssarmadh</name><uri>http://www.blogger.com/profile/06420907668459969181</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author></entry></feed>