<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Le blog de SQLpro &#187; SQL server</title>
	<atom:link href="https://blog.developpez.com/sqlpro/ptag/sql-server/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.developpez.com/sqlpro</link>
	<description>Le SQL pour SQL Server, PostGreSQL et tous les autres SGBDR</description>
	<lastBuildDate>Thu, 15 Oct 2020 12:59:17 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Extraire le code des requêtes d&#8217;un verrou mortel traqué par un graphe &#171;&#160;deadlock&#160;&#187; du profiler</title>
		<link>https://blog.developpez.com/sqlpro/p13176/ms-sql-server/extraire-le-code-des-requetes-dun-verrou-mortel-traque-par-un-graphe-deadlock-du-profiler</link>
		<comments>https://blog.developpez.com/sqlpro/p13176/ms-sql-server/extraire-le-code-des-requetes-dun-verrou-mortel-traque-par-un-graphe-deadlock-du-profiler#comments</comments>
		<pubDate>Thu, 22 Feb 2018 18:26:45 +0000</pubDate>
		<dc:creator><![CDATA[SQLpro]]></dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server 2014]]></category>
		<category><![CDATA[SQL Server 2016]]></category>
		<category><![CDATA[SQL Server 2017]]></category>
		<category><![CDATA[capture]]></category>
		<category><![CDATA[deadlock]]></category>
		<category><![CDATA[détection]]></category>
		<category><![CDATA[étreinte fatale]]></category>
		<category><![CDATA[interblocage]]></category>
		<category><![CDATA[lock]]></category>
		<category><![CDATA[log]]></category>
		<category><![CDATA[profiler]]></category>
		<category><![CDATA[SQL server]]></category>
		<category><![CDATA[trace]]></category>
		<category><![CDATA[verrou mortel]]></category>
		<category><![CDATA[victime]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=794</guid>
		<description><![CDATA[L&#8217;outil profiler de SQL Server permet de partir à la chasse au verrous mortel et peut récupérer le graphe du verrouillage des éléments concurrents et notamment les données technique de la victime. Mais ce graphe est en fait un document XML contenant toutes les informations sur les processus en jeu, la victime et les survivants. [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>L&rsquo;outil profiler de SQL Server permet de partir à la chasse au verrous mortel et peut récupérer le graphe du verrouillage des éléments concurrents et notamment les données technique de la victime. Mais ce graphe est en fait un document XML contenant toutes les informations sur les processus en jeu, la victime et les survivants. Il est alors intéressant d&rsquo;en extraire les requêtes fautives afin de corriger son code ou d&rsquo;indexer les tables, quelques un des moyens de se débarrasser des interblocages&#8230;<br />
<span id="more-794"></span><br />
Pour détecter les verrous mortels, il suffit de demander au profiler de tracer les événements de type &laquo;&nbsp;deadlock&nbsp;&raquo; de la catégorie &laquo;&nbsp;locks&nbsp;&raquo;, à savoir :</p>
<ul>
<li>Deadlock graph</li>
<li>Lock: Deadlock</li>
<li>Lock Deadlock Chain</li>
</ul>
<div id="attachment_797" style="width: 853px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2018/02/Profiler-Deadlock.jpg"><img src="http://blog.developpez.com/sqlpro/files/2018/02/Profiler-Deadlock.jpg" alt="Événements à cocher pour auditer les verrous mortels de SQL Server" width="843" height="540" class="size-full wp-image-797" /></a><p class="wp-caption-text">Événements à cocher pour auditer les verrous mortels de SQL Server</p></div>
<p>Une foi enregistrés, les données du profiler peuvent être vues par le client graphique sous cette forme :<br />
<div id="attachment_798" style="width: 1930px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2018/02/Profiler-SQL-Server-et-Deadlock.png"><img src="http://blog.developpez.com/sqlpro/files/2018/02/Profiler-SQL-Server-et-Deadlock.png" alt="Lecture des informations de verrous mortels dans le client graphique du profiler de SQL Server" width="1920" height="1040" class="size-full wp-image-798" /></a><p class="wp-caption-text">Lecture des informations de verrous mortels dans le client graphique du profiler de SQL Server</p></div></p>
<p>Néanmoins, il faut passer à l&rsquo;aide de sa souris sur chacune des bulles représentant les processus pour obtenir le texte des requêtes en jeu. Les rectangles visualisant les verrous, les pattes formant le graphe, qui lorsqu&rsquo;il présente un cycle contient un interblocage.</p>
<p>Dès lors, on peut récupérer les fichiers générés sous forme SQL via la fonction table sys.fn_trace_gettable dont le premier argument est le chemin de stockage des fichiers de trace du profiler et le second le numéro du fichier concerné. On peut aussi tous les obtenir en mettant le mot clef DEFAULT.<br />
L&rsquo;événement de graphe de verrouillage étant de classe 148, il suffit ensuite de filtrer sur ces lignes.<br />
Exemple :</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">SELECT *<br />
FROM &nbsp; sys.fn_trace_gettable('C:\SQL_TRACE\20180219_14H30_DEADLOCK_.trc', default)</div></div>
<p>La colonne TextData de cette table virtuelle contient alors le XML donnant toutes les informations sur le verrou mortel. En voici un exemple :<br />
<div id="attachment_799" style="width: 780px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2018/02/Deadlock-XML-SQL-Server.jpg"><img src="http://blog.developpez.com/sqlpro/files/2018/02/Deadlock-XML-SQL-Server.jpg" alt="Document XML montrant toutes les informations d&#039;un verrou mortel survenu dans SQL Server" width="770" height="702" class="size-full wp-image-799" /></a><p class="wp-caption-text">Document XML montrant toutes les informations d&rsquo;un verrou mortel survenu dans SQL Server</p></div></p>
<p>Avec une requête mêlant du SQL et du XML via XQuery et XPath, il est possible d&rsquo;extraire ce qui nous intéresse, à savoir, le texte des requêtes en jeu ainsi qu&rsquo;une indication disant quelle est la victime et les survivants. Et comme il n&rsquo;y a pas qu&rsquo;un seul interblocage en général, alors on les identifie par un numéro. la requête au final ressemble à cela :</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">WITH</span> <br />
TX <span style="color: #993333; font-weight: bold;">AS</span> <br />
<span style="color: #66cc66;">&#40;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> StartTime<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>TextData <span style="color: #993333; font-weight: bold;">AS</span> xml<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> TextData <br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>fn_trace_gettable<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'C:<span style="color: #000099; font-weight: bold;">\!</span> FB<span style="color: #000099; font-weight: bold;">\R</span>henus<span style="color: #000099; font-weight: bold;">\2</span>018-02-19<span style="color: #000099; font-weight: bold;">\2</span>0180219_14H30_DEADLOCK_.trc'</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;EventClass <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">148</span> <br />
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
TVM <span style="color: #993333; font-weight: bold;">AS</span> <br />
<span style="color: #66cc66;">&#40;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> StartTime<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;v<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">VALUE</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'(./inputbuf)[1]'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'nvarchar(max)'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> Query<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;i<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">VALUE</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'(./deadlock/@victim)[1]'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'varchar(32)'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> ProcessVictim<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;v<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">VALUE</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'(./@id)[1]'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'varchar(32)'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> ProcessID<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp;TX<br />
&nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">CROSS</span> APPLY TextData<span style="color: #66cc66;">.</span>nodes<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/deadlock-list'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> X<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">CROSS</span> APPLY TextData<span style="color: #66cc66;">.</span>nodes<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'/deadlock-list/deadlock/process-list/process'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> V<span style="color: #66cc66;">&#40;</span>v<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
TVV <span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #66cc66;">&#40;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> DENSE_RANK<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">OVER</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> StartTime<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> ID<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;StartTime<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;Query<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">CASE</span> <span style="color: #993333; font-weight: bold;">WHEN</span> ProcessVictim <span style="color: #66cc66;">=</span> ProcessID <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #ff0000;">'Victim!'</span> <span style="color: #993333; font-weight: bold;">ELSE</span> <span style="color: #ff0000;">'Alive'</span> <span style="color: #993333; font-weight: bold;">END</span> <span style="color: #993333; font-weight: bold;">AS</span> FinalState<br />
<span style="color: #993333; font-weight: bold;">FROM</span> TVM<br />
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
TQV <span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #66cc66;">&#40;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> ID<span style="color: #66cc66;">,</span> Query<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; TVV <br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;FinalState <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Victim!'</span><br />
<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> TVV<span style="color: #66cc66;">.</span>ID<span style="color: #66cc66;">,</span> TVV<span style="color: #66cc66;">.</span>StartTime<span style="color: #66cc66;">,</span> TVV<span style="color: #66cc66;">.</span>Query<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">CASE</span> <span style="color: #993333; font-weight: bold;">WHEN</span> TQV<span style="color: #66cc66;">.</span>ID <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #ff0000;">'Victim!'</span> <span style="color: #993333; font-weight: bold;">ELSE</span> <span style="color: #ff0000;">'Alive'</span> <span style="color: #993333; font-weight: bold;">END</span> <span style="color: #993333; font-weight: bold;">AS</span> FinalState <br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; TVV <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">OUTER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> TQV<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">ON</span> TVV<span style="color: #66cc66;">.</span>ID <span style="color: #66cc66;">=</span> TQV<span style="color: #66cc66;">.</span>ID <span style="color: #993333; font-weight: bold;">AND</span> TVV<span style="color: #66cc66;">.</span>Query <span style="color: #66cc66;">=</span> TQV<span style="color: #66cc66;">.</span>Query<br />
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #cc66cc;">1</span>;</div></div>
<p>Au final une telle requête permet de présenter les informations suivantes :<br />
<div id="attachment_807" style="width: 588px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2018/02/Resultat-requête-deadlock1.jpg"><img src="http://blog.developpez.com/sqlpro/files/2018/02/Resultat-requête-deadlock1.jpg" alt="Visualisation des requêtes en jeu dans un verrou mortel" width="578" height="383" class="size-full wp-image-807" /></a><p class="wp-caption-text">Visualisation des requêtes en jeu dans un verrou mortel</p></div><br />
Notez au passage le colonne &laquo;&nbsp;FinalState&nbsp;&raquo; indiquant quelle requête a été victime et celles ayant survécus.</p>
<p><a href="http://sqlpro.developpez.com/_fichierSQL/DEADLOCK-SQL.txt" title="Fichier du code" target="_blank">Le code ! Le code ! Le code ! Le code ! Le code ! Le code ! Le code ! Le code ! Le code ! Le code ! </a></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Frédéric Brouard, alias SQLpro, ARCHITECTE DE DONNÉES<br />
Expert &nbsp;S.G.B.D &nbsp;relationnelles &nbsp; et &nbsp; langage &nbsp;S.Q.L<br />
Moste &nbsp;Valuable &nbsp;Professionnal &nbsp;Microsoft &nbsp;SQL Server<br />
Société SQLspot &nbsp;: &nbsp;modélisation, conseil, formation,<br />
optimisation, &nbsp;audit, &nbsp;tuning, &nbsp;administration &nbsp;SGBDR<br />
Enseignant: CNAM PACA, ISEN Toulon, CESI Aix en Prov.</div></div>
<p>L&rsquo;entreprise <a href="http://www.sqlspot.com">SQL Spot</a><br />
<strong>Le site web sur le </strong><a href="http://sqlpro.developpez.com/">SQL et les SGBDR</a></p>
<p><img src="http://blog.developpez.com/media/Microsoft_MVP_logo_vertical Brouard 400.jpg" width="400" height="135" alt="MVP Microsoft SQL
Server" /></p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Estimation des IO (ES) en lecture et écriture table par table d&#8217;une base SQL</title>
		<link>https://blog.developpez.com/sqlpro/p13175/ms-sql-server/estimation-des-io-es-en-lecture-et-ecriture-table-par-table-dune-base-sql</link>
		<comments>https://blog.developpez.com/sqlpro/p13175/ms-sql-server/estimation-des-io-es-en-lecture-et-ecriture-table-par-table-dune-base-sql#comments</comments>
		<pubDate>Wed, 21 Feb 2018 18:07:18 +0000</pubDate>
		<dc:creator><![CDATA[SQLpro]]></dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server 2014]]></category>
		<category><![CDATA[SQL Server 2016]]></category>
		<category><![CDATA[SQL Server 2017]]></category>
		<category><![CDATA[accès]]></category>
		<category><![CDATA[écriture]]></category>
		<category><![CDATA[ES]]></category>
		<category><![CDATA[IO]]></category>
		<category><![CDATA[lecture]]></category>
		<category><![CDATA[métrique]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[SQL server]]></category>
		<category><![CDATA[Statistiques]]></category>
		<category><![CDATA[table]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=791</guid>
		<description><![CDATA[Les deux requêtes que je vous présente permettent d&#8217;estimer le nombres de pages lues et écrites table par table tenant compte de tous les index des tables. Attention : il ne s&#8217;agit pas d&#8217;une mesure exacte. Une telle mesure est impossible mais bien d&#8217;une estimation pour connaître les tables les plus écrites et celles les [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Les deux requêtes que je vous présente permettent d&rsquo;estimer le nombres de pages lues et écrites table par table tenant compte de tous les index des tables. Attention : il ne s&rsquo;agit pas d&rsquo;une mesure exacte. Une telle mesure est impossible mais bien d&rsquo;une estimation pour connaître les tables les plus écrites et celles les plus lues de manières relatives les unes aux autres.<br />
<span id="more-791"></span><br />
Elle se base sur le nombre d&rsquo;opération unitaire d&rsquo;accès effectué sur chaque table et index. En gros, il y a trois types d&rsquo;accès :<br />
1) un accès par balayage de la table ou de l&rsquo;index (scan) appelé &laquo;&nbsp;analyse&nbsp;&raquo; dans les plans de requête en français;<br />
2) un accès pour recherche dichotomique dans l&rsquo;index (seek) appelé &laquo;&nbsp;recherche&nbsp;&raquo; dans les plans de requête en français;<br />
3) un accès multi recherche dans l&rsquo;index (lookup) appelé &laquo;&nbsp;recherche de clés&nbsp;&raquo; dans les plans de requête en français;</p>
<p>Nous avons compter qu&rsquo;un SCAN balayait toutes les pages de l&rsquo;index ou de la table, ce qui est faux dans certains cas particulier (par exemple recherche TOP n)<br />
Nous avons compter qu&rsquo;un SEEK parcourait un nombre de page équivalent à la profondeur de l&rsquo;arbre ce qui n&rsquo;est pas vrai pour les recherches de type &laquo;&nbsp;range&nbsp;&raquo; (par exemple lors d&rsquo;un BETWEEN)<br />
Nous avons compter qu&rsquo;un LOOKUP était l&rsquo;équivalent de 10 seeks. Mais cela peut être plus ou moins et dans certains cas, ce n&rsquo;est même pas réellement l&rsquo;équivalent d&rsquo;un seek.</p>
<p>Ces approximations nous permettent quand même une bonne évaluation de la surface d&rsquo;attaque des données des tables en production, dans un but comparatif, voir les tables les plus accédées en lecture ou en écriture.</p>
<p><strong>La requête pour les lectures :</strong></p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">WITH</span> <br />
idx_details <span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #66cc66;">&#40;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> ius<span style="color: #66cc66;">.</span>object_id<span style="color: #66cc66;">,</span> ius<span style="color: #66cc66;">.</span>index_id<span style="color: #66cc66;">,</span> user_seeks<span style="color: #66cc66;">,</span> user_lookups<span style="color: #66cc66;">,</span> user_scans<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">COALESCE</span><span style="color: #66cc66;">&#40;</span>INDEXPROPERTY<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">.</span>object_id<span style="color: #66cc66;">,</span> i<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'IndexDepth '</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AS</span> index_depth<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>used_page_count<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> pages<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>dm_db_partition_stats <span style="color: #993333; font-weight: bold;">AS</span> s<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;s<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> ius<span style="color: #66cc66;">.</span>object_id <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;s<span style="color: #66cc66;">.</span>index_id &nbsp;<span style="color: #66cc66;">=</span> ius<span style="color: #66cc66;">.</span>index_id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> page_count<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>dm_db_index_usage_stats <span style="color: #993333; font-weight: bold;">AS</span> ius<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>indexes <span style="color: #993333; font-weight: bold;">AS</span> i<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> &nbsp;ius<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> i<span style="color: #66cc66;">.</span>object_id <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AND</span> ius<span style="color: #66cc66;">.</span>index_id &nbsp;<span style="color: #66cc66;">=</span> i<span style="color: #66cc66;">.</span>index_id<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;database_id <span style="color: #66cc66;">=</span> DB_ID<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">AND</span> &nbsp; &nbsp;ius<span style="color: #66cc66;">.</span>index_id &nbsp;<span style="color: #cc66cc;">0</span> <span style="color: #993333; font-weight: bold;">OR</span> user_lookups <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333; font-weight: bold;">OR</span> user_scans <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> &nbsp;<br />
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
TOPT <span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #66cc66;">&#40;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> s<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> TABLE_SCHEMA<span style="color: #66cc66;">,</span> o<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">TABLE_NAME</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>user_seeks <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">10</span> <span style="color: #66cc66;">*</span> user_lookups <span style="color: #66cc66;">*</span> index_depth<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>user_scans <span style="color: #66cc66;">*</span> page_count<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> IO_READS_ESTIMATE<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">100.0</span> <span style="color: #66cc66;">*</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>user_seeks <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">10</span> <span style="color: #66cc66;">*</span> user_lookups <span style="color: #66cc66;">*</span> index_depth<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>user_scans <span style="color: #66cc66;">*</span> page_count<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">NULLIF</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>user_seeks <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">10</span> <span style="color: #66cc66;">*</span> user_lookups <span style="color: #66cc66;">*</span> index_depth<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>user_scans <span style="color: #66cc66;">*</span> page_count<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">OVER</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> &nbsp;<span style="color: #993333; font-weight: bold;">AS</span> PERCENT_READS_ESTIMATE<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; idx_details <span style="color: #993333; font-weight: bold;">AS</span> id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>objects <span style="color: #993333; font-weight: bold;">AS</span> o<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> id<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> o<span style="color: #66cc66;">.</span>object_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>schemas <span style="color: #993333; font-weight: bold;">AS</span> s <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> s<span style="color: #66cc66;">.</span>schema_id <span style="color: #66cc66;">=</span> o<span style="color: #66cc66;">.</span>schema_id<br />
<span style="color: #993333; font-weight: bold;">GROUP</span> &nbsp;<span style="color: #993333; font-weight: bold;">BY</span> s<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span> o<span style="color: #66cc66;">.</span>name<br />
<span style="color: #993333; font-weight: bold;">HAVING</span> <span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>user_seeks <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">10</span> <span style="color: #66cc66;">*</span> user_lookups <span style="color: #66cc66;">*</span> index_depth<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>user_scans <span style="color: #66cc66;">*</span> page_count<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><br />
<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> TABLE_SCHEMA<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">TABLE_NAME</span><span style="color: #66cc66;">,</span> IO_READS_ESTIMATE<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>PERCENT_READS_ESTIMATE <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">DECIMAL</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> PERCENT_READS_ESTIMATE<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>PERCENT_READS_ESTIMATE<span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">OVER</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> PERCENT_READS_ESTIMATE <span style="color: #993333; font-weight: bold;">DESC</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">DECIMAL</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AS</span> CUMUL_PERCENT<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> sqlserver_start_time <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>dm_os_sys_info<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> datetime2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> SINCE<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; TOPT<br />
<span style="color: #993333; font-weight: bold;">ORDER</span> &nbsp;<span style="color: #993333; font-weight: bold;">BY</span> IO_READS_ESTIMATE <span style="color: #993333; font-weight: bold;">DESC</span>;</div></div>
<p><strong>La requête pour les écritures :</strong></p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">WITH</span> <br />
idx_details <span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #66cc66;">&#40;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> ius<span style="color: #66cc66;">.</span>object_id<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>user_updates <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> user_updates<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>dm_db_index_usage_stats <span style="color: #993333; font-weight: bold;">AS</span> ius<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>indexes <span style="color: #993333; font-weight: bold;">AS</span> i<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> ius<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> i<span style="color: #66cc66;">.</span>object_id <span style="color: #993333; font-weight: bold;">AND</span> ius<span style="color: #66cc66;">.</span>index_id <span style="color: #66cc66;">=</span> i<span style="color: #66cc66;">.</span>index_id<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;database_id <span style="color: #66cc66;">=</span> DB_ID<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">AND</span> &nbsp; &nbsp;ius<span style="color: #66cc66;">.</span>index_id &nbsp;<span style="color: #cc66cc;">0</span> <br />
<span style="color: #993333; font-weight: bold;">GROUP</span> &nbsp;<span style="color: #993333; font-weight: bold;">BY</span> ius<span style="color: #66cc66;">.</span>object_id<br />
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
TOPT <span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #66cc66;">&#40;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> s<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> TABLE_SCHEMA<span style="color: #66cc66;">,</span> o<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">TABLE_NAME</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;user_updates <span style="color: #993333; font-weight: bold;">AS</span> IO_WRITES_ESTIMATE<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #cc66cc;">100.0</span> <span style="color: #66cc66;">*</span> user_updates <span style="color: #66cc66;">/</span> <span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>user_updates<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">OVER</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AS</span> PERCENT_WRITES_ESTIMATE<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; idx_details <span style="color: #993333; font-weight: bold;">AS</span> id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>objects <span style="color: #993333; font-weight: bold;">AS</span> o<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> id<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> o<span style="color: #66cc66;">.</span>object_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>schemas <span style="color: #993333; font-weight: bold;">AS</span> s <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> s<span style="color: #66cc66;">.</span>schema_id <span style="color: #66cc66;">=</span> o<span style="color: #66cc66;">.</span>schema_id<br />
<span style="color: #993333; font-weight: bold;">GROUP</span> &nbsp;<span style="color: #993333; font-weight: bold;">BY</span> s<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span> o<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span> user_updates<br />
<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> TABLE_SCHEMA<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">TABLE_NAME</span><span style="color: #66cc66;">,</span> IO_WRITES_ESTIMATE<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>PERCENT_WRITES_ESTIMATE <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">DECIMAL</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AS</span> PERCENT_WRITES_ESTIMATE<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>PERCENT_WRITES_ESTIMATE<span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">OVER</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> PERCENT_WRITES_ESTIMATE <span style="color: #993333; font-weight: bold;">DESC</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">DECIMAL</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AS</span> CUMUL_PERCENT<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> sqlserver_start_time <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>dm_os_sys_info<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> datetime2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> SINCE<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; TOPT<br />
<span style="color: #993333; font-weight: bold;">ORDER</span> &nbsp;<span style="color: #993333; font-weight: bold;">BY</span> PERCENT_WRITES_ESTIMATE <span style="color: #993333; font-weight: bold;">DESC</span>;</div></div>
<p><em>Un exemple de résultat pour la lecture :</em></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">TABLE_SCHEMA &nbsp; TABLE_NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IO_READS_ESTIMATE &nbsp; &nbsp;PERCENT_READS_ESTIMATE &nbsp;CUMUL_PERCENT &nbsp; SINCE<br />
-------------- --------------------------- -------------------- ----------------------- --------------- ---------------------------<br />
S_GEO &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;COMMUNE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 748792 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 55.59 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 55.59 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2018-01-16 16:17:28<br />
S_RTE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TRONCON_ROUTE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 520044 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 38.61 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 94.19 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2018-01-16 16:17:28<br />
S_ADR &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CODE_INSEE_CODE_POSTAL &nbsp; &nbsp; &nbsp;42147 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3.13 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;97.32 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2018-01-16 16:17:28<br />
S_GEO &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DEPARTEMENT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 36057 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2.68 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;100.00 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2018-01-16 16:17:28</div></div>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Frédéric Brouard, alias SQLpro, ARCHITECTE DE DONNÉES<br />
Expert &nbsp;S.G.B.D &nbsp;relationnelles &nbsp; et &nbsp; langage &nbsp;S.Q.L<br />
Moste &nbsp;Valuable &nbsp;Professionnal &nbsp;Microsoft &nbsp;SQL Server<br />
Société SQLspot &nbsp;: &nbsp;modélisation, conseil, formation,<br />
optimisation, &nbsp;audit, &nbsp;tuning, &nbsp;administration &nbsp;SGBDR<br />
Enseignant: CNAM PACA, ISEN Toulon, CESI Aix en Prov.</div></div>
<p>L&rsquo;entreprise <a href="http://www.sqlspot.com">SQL Spot</a><br />
<strong>Le site web sur le </strong><a href="http://sqlpro.developpez.com/">SQL et les SGBDR</a></p>
<p><img src="http://blog.developpez.com/media/Microsoft_MVP_logo_vertical Brouard 400.jpg" width="400" height="135" alt="MVP Microsoft SQL
Server" /></p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Métrique de volumétrie du stockage par table, index et partition</title>
		<link>https://blog.developpez.com/sqlpro/p13171/ms-sql-server/metrique-de-volumetrie-du-stockage-par-table-index-et-partition</link>
		<comments>https://blog.developpez.com/sqlpro/p13171/ms-sql-server/metrique-de-volumetrie-du-stockage-par-table-index-et-partition#comments</comments>
		<pubDate>Wed, 07 Feb 2018 09:19:04 +0000</pubDate>
		<dc:creator><![CDATA[SQLpro]]></dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server 2014]]></category>
		<category><![CDATA[SQL Server 2016]]></category>
		<category><![CDATA[SQL Server 2017]]></category>
		<category><![CDATA[index]]></category>
		<category><![CDATA[métrique]]></category>
		<category><![CDATA[partition]]></category>
		<category><![CDATA[SQL server]]></category>
		<category><![CDATA[stockage]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=789</guid>
		<description><![CDATA[Voici une petite requête complémentaire pour mesurer les volumes stockées par partition, index et tables d&#8217;une base : SELECT s.name AS TABLE_SCHEMA, o.name AS TABLE_NAME, i.name AS INDEX_NAME, &#160; &#160; &#160; &#160;fs.name AS FILEGROUP_NAME, f.name AS FILE_NAME, f.physical_name, p.partition_number, &#160; &#160; &#160; &#160;p.ROWS, p.data_compression_desc, au.total_pages * 8 AS SIZE_TOTAL_KO, &#160; &#160; &#160; &#160;SUM&#40;au.total_pages * 8&#41; [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Voici une petite requête complémentaire pour mesurer les volumes stockées par partition, index et tables d&rsquo;une base :<br />
<span id="more-789"></span></p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> s<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> TABLE_SCHEMA<span style="color: #66cc66;">,</span> o<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">TABLE_NAME</span><span style="color: #66cc66;">,</span> i<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> INDEX_NAME<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;fs<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> FILEGROUP_NAME<span style="color: #66cc66;">,</span> f<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> FILE_NAME<span style="color: #66cc66;">,</span> f<span style="color: #66cc66;">.</span>physical_name<span style="color: #66cc66;">,</span> p<span style="color: #66cc66;">.</span>partition_number<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;p<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">ROWS</span><span style="color: #66cc66;">,</span> p<span style="color: #66cc66;">.</span>data_compression_desc<span style="color: #66cc66;">,</span> au<span style="color: #66cc66;">.</span>total_pages <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">8</span> <span style="color: #993333; font-weight: bold;">AS</span> SIZE_TOTAL_KO<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>au<span style="color: #66cc66;">.</span>total_pages <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">OVER</span><span style="color: #66cc66;">&#40;</span>PARTITION <span style="color: #993333; font-weight: bold;">BY</span> i<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span> s<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span> o<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> SIZE_TOTAL_INDEX_KO<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>au<span style="color: #66cc66;">.</span>total_pages <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">OVER</span><span style="color: #66cc66;">&#40;</span>PARTITION <span style="color: #993333; font-weight: bold;">BY</span> s<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span> o<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> SIZE_TOTAL_TABLE_KO<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>partitions <span style="color: #993333; font-weight: bold;">AS</span> p<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>allocation_units <span style="color: #993333; font-weight: bold;">AS</span> au<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> p<span style="color: #66cc66;">.</span>hobt_id <span style="color: #66cc66;">=</span> au<span style="color: #66cc66;">.</span>container_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>filegroups <span style="color: #993333; font-weight: bold;">AS</span> fs<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> au<span style="color: #66cc66;">.</span>data_space_id <span style="color: #66cc66;">=</span> fs<span style="color: #66cc66;">.</span>data_space_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>database_files <span style="color: #993333; font-weight: bold;">AS</span> f<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> fs<span style="color: #66cc66;">.</span>data_space_id <span style="color: #66cc66;">=</span> f<span style="color: #66cc66;">.</span>file_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>objects <span style="color: #993333; font-weight: bold;">AS</span> o<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> p<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> o<span style="color: #66cc66;">.</span>object_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>schemas <span style="color: #993333; font-weight: bold;">AS</span> s <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> o<span style="color: #66cc66;">.</span>schema_id <span style="color: #66cc66;">=</span> s<span style="color: #66cc66;">.</span>schema_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>indexes <span style="color: #993333; font-weight: bold;">AS</span> i<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> p<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> i<span style="color: #66cc66;">.</span>object_id <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AND</span> p<span style="color: #66cc66;">.</span>index_id <span style="color: #66cc66;">=</span> i<span style="color: #66cc66;">.</span>index_id;</div></div>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Frédéric Brouard, alias SQLpro, ARCHITECTE DE DONNÉES<br />
Expert &nbsp;S.G.B.D &nbsp;relationnelles &nbsp; et &nbsp; langage &nbsp;S.Q.L<br />
Moste &nbsp;Valuable &nbsp;Professionnal &nbsp;Microsoft &nbsp;SQL Server<br />
Société SQLspot &nbsp;: &nbsp;modélisation, conseil, formation,<br />
optimisation, &nbsp;audit, &nbsp;tuning, &nbsp;administration &nbsp;SGBDR<br />
Enseignant: CNAM PACA, ISEN Toulon, CESI Aix en Prov.</div></div>
<p>L&rsquo;entreprise <a href="http://www.sqlspot.com">SQL Spot</a><br />
<strong>Le site web sur le </strong><a href="http://sqlpro.developpez.com/">SQL et les SGBDR</a></p>
<p><img src="http://blog.developpez.com/media/Microsoft_MVP_logo_vertical Brouard 400.jpg" width="400" height="135" alt="MVP Microsoft SQL
Server" /></p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Vérification de la dernière sauvegarde d&#8217;une base</title>
		<link>https://blog.developpez.com/sqlpro/p13170/ms-sql-server/verification-de-la-derniere-sauvegarde-dune-base</link>
		<comments>https://blog.developpez.com/sqlpro/p13170/ms-sql-server/verification-de-la-derniere-sauvegarde-dune-base#comments</comments>
		<pubDate>Thu, 01 Feb 2018 10:51:55 +0000</pubDate>
		<dc:creator><![CDATA[SQLpro]]></dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server 2014]]></category>
		<category><![CDATA[SQL Server 2016]]></category>
		<category><![CDATA[SQL Server 2017]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[SQL server]]></category>
		<category><![CDATA[verification]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=787</guid>
		<description><![CDATA[Parmi les opérations importantes à systématiser, la vérification de consistance d&#8217;une sauvegarde est une opération à ne pas négliger. Mais compte tendu qu&#8217;une sauvegarde peut être multi-famille (donc répartie sur plusieurs fichiers), multi-support (donc redondées dans plusieurs destinations) ou intégré dans un &#171;&#160;device&#160;&#187; (donc noyée au milieu de plusieurs sauvegarde dans un fichier), la complexité [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Parmi les opérations importantes à systématiser, la vérification de consistance d&rsquo;une sauvegarde est une opération à ne pas négliger. Mais compte tendu qu&rsquo;une sauvegarde peut être multi-famille (donc répartie sur plusieurs fichiers), multi-support (donc redondées dans plusieurs destinations) ou intégré dans un &laquo;&nbsp;device&nbsp;&raquo; (donc noyée au milieu de plusieurs sauvegarde dans un fichier), la complexité augmente d&rsquo;autant. Voici donc une petite procédure pour ce faire&#8230;<br />
<span id="more-787"></span></p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">PROCEDURE</span> P_VERIFY_LAST_BACKUP @DB sysname<br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SET</span> NOCOUNT <span style="color: #993333; font-weight: bold;">ON</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @<span style="color: #993333; font-weight: bold;">SQL</span> NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> N<span style="color: #ff0000;">''</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">WITH</span> <br />
T0 <span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #66cc66;">&#40;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> TOP <span style="color: #cc66cc;">1</span> media_set_id<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">POSITION</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; msdb<span style="color: #66cc66;">.</span>dbo<span style="color: #66cc66;">.</span>backupset<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;database_name <span style="color: #66cc66;">=</span> @DB<br />
<span style="color: #993333; font-weight: bold;">ORDER</span> &nbsp;<span style="color: #993333; font-weight: bold;">BY</span> backup_start_date <span style="color: #993333; font-weight: bold;">DESC</span><br />
<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">=</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">+</span> N<span style="color: #ff0000;">'RESTORE VERIFYONLY FROM DISK = '</span><span style="color: #ff0000;">''</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">+</span> physical_device_name <span style="color: #66cc66;">+</span> N<span style="color: #ff0000;">''</span><span style="color: #ff0000;">' WITH FILE = '</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">POSITION</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> N<span style="color: #ff0000;">';'</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; msdb<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>backupmediafamily<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">AS</span> mf<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> T0 <span style="color: #993333; font-weight: bold;">ON</span> mf<span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>media_set_id<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> T0<span style="color: #66cc66;">.</span>media_set_id;<br />
<br />
PRINT @<span style="color: #993333; font-weight: bold;">SQL</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">EXEC</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">SQL</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p>Exemple d&rsquo;utilisation :</p>
<p>EXEC P_VERIFY_LAST_BACKUP &lsquo;master';</p>
<p><a href="http://sqlpro.developpez.com/_fichierSQL/SET_STATISTIS_IO_AGGREGATE.txt" title="Fichier du code" target="_blank">Le code !</a></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Frédéric Brouard, alias SQLpro, ARCHITECTE DE DONNÉES<br />
Expert &nbsp;S.G.B.D &nbsp;relationnelles &nbsp; et &nbsp; langage &nbsp;S.Q.L<br />
Moste &nbsp;Valuable &nbsp;Professionnal &nbsp;Microsoft &nbsp;SQL Server<br />
Société SQLspot &nbsp;: &nbsp;modélisation, conseil, formation,<br />
optimisation, &nbsp;audit, &nbsp;tuning, &nbsp;administration &nbsp;SGBDR<br />
Enseignant: CNAM PACA, ISEN Toulon, CESI Aix en Prov.</div></div>
<p>L&rsquo;entreprise <a href="http://www.sqlspot.com">SQL Spot</a><br />
<strong>Le site web sur le </strong><a href="http://sqlpro.developpez.com/">SQL et les SGBDR</a></p>
<p><img src="http://blog.developpez.com/media/Microsoft_MVP_logo_vertical Brouard 400.jpg" width="400" height="135" alt="MVP Microsoft SQL
Server" /></p>
<div id="attachment_590" style="width: 548px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg"><img src="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg" alt="Développez et administrez pour la performance avec SQL Server 2014" width="538" height="652" class="size-full wp-image-590" /></a><p class="wp-caption-text">Développez et administrez pour la performance avec SQL Server 2014</p></div>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Agrégation des statistiques d&#8217;IO d&#8217;une requête</title>
		<link>https://blog.developpez.com/sqlpro/p13157/ms-sql-server/agregation-des-statistiques-dio-dune-requete</link>
		<comments>https://blog.developpez.com/sqlpro/p13157/ms-sql-server/agregation-des-statistiques-dio-dune-requete#comments</comments>
		<pubDate>Thu, 28 Dec 2017 18:55:58 +0000</pubDate>
		<dc:creator><![CDATA[SQLpro]]></dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[SQL Server 2000]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server 2014]]></category>
		<category><![CDATA[SQL Server 2016]]></category>
		<category><![CDATA[SQL Server 2017]]></category>
		<category><![CDATA[ES]]></category>
		<category><![CDATA[IO]]></category>
		<category><![CDATA[performance requête]]></category>
		<category><![CDATA[SQL server]]></category>
		<category><![CDATA[STATISTICS IO]]></category>
		<category><![CDATA[statistiques entrées sorties]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=778</guid>
		<description><![CDATA[Si le paramétrage SET STATISTICS IO ON de Microsoft SQL Server permet de savoir, table par table les statistiques d&#8217;entrées/sorties (IO pour Input/Output) des requêtes, il n&#8217;est pas toujours facile de s&#8217;y repérer lorsque la requête est complexe et fait appel à de multiples tables. Voici une procédure qui en effectue la synthèse. Rappelons que [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Si le paramétrage <strong>SET STATISTICS IO ON</strong> de Microsoft SQL Server permet de savoir, table par table les statistiques d&rsquo;entrées/sorties (IO pour Input/Output) des requêtes, il n&rsquo;est pas toujours facile de s&rsquo;y repérer lorsque la requête est complexe et fait appel à de multiples tables. Voici une procédure qui en effectue la synthèse.<br />
<span id="more-778"></span><br />
Rappelons que les statistiques IO sont le nombre de pages lues par le moteur SQL pour exécuter une requête. Le paramètre de session STATISTICS IO peut être mis à ON ou OFF. Par défaut il est à OFF. Mis sur ON, il permet, dans SQL Server Management Studio (SSMS), de voir le nombre de pages accédées pour une requête ou un lot de requête. C&rsquo;est une série de message d&rsquo;une ligne commençant par &laquo;&nbsp;Table&#8230;&nbsp;&raquo; qui suit immédiatement le message indiquant le nombre de lignes affectées par la requête.<br />
<div id="attachment_780" style="width: 530px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2017/12/STATISTICS_IO.jpg"><img src="http://blog.developpez.com/sqlpro/files/2017/12/STATISTICS_IO.jpg" alt="SQL Server onglet message STATISTICS IO" width="520" height="310" class="size-full wp-image-780" /></a><p class="wp-caption-text">SQL Server onglet message STATISTICS IO</p></div> </p>
<p>Néanmoins il est assez difficile de lire tout cela et d&rsquo;en déduire une tendance globale.<br />
Ne serait-ce pas plus intéressante de les présenter de cette manière :<br />
<div id="attachment_781" style="width: 989px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2017/12/STATISTICS_IO_GLOBAL.jpg"><img src="http://blog.developpez.com/sqlpro/files/2017/12/STATISTICS_IO_GLOBAL.jpg" alt="SQL Server STATISTICS IO aggregate" width="979" height="329" class="size-full wp-image-781" /></a><p class="wp-caption-text">Statistiques IO SQL Server avec totaux et agrégation</p></div></p>
<p>La petite procédure que voici permet justement d&rsquo;effectuer cette tâche. Il suffit de lui passer le texte complet des message d&rsquo;IO et elle vous retournera tout cela, formaté, totalisé et agrégé !</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">USE</span> msdb;<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> SCHEMA S_DBA;<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">PROCEDURE</span> S_DBA<span style="color: #66cc66;">.</span>P_SPLIT_STATS_IO @<span style="color: #993333; font-weight: bold;">DATA</span> NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #993333; font-weight: bold;">SET</span> NOCOUNT <span style="color: #993333; font-weight: bold;">ON</span>;<br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @LINE NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @<span style="color: #993333; font-weight: bold;">SQL</span> NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> N<span style="color: #ff0000;">''</span>;<br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @L <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #66cc66;">&#40;</span>LINE NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">800</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @T <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">TABLE_NAME</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sysname<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SCAN_COUNT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LOGICAL_READS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PHYSICAL_READS &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; READ_AHEAD_READS &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LOB_LOGICAL_READS &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LOB_PHYSICAL_READS &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LOB_READ_AHEAD_READS &nbsp; <span style="color: #993333; font-weight: bold;">BIGINT</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
WHILE CHARINDEX<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">NCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">13</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">NCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @<span style="color: #993333; font-weight: bold;">DATA</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SET</span> @LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">LEFT</span><span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">DATA</span><span style="color: #66cc66;">,</span> CHARINDEX<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">NCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">13</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">NCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @<span style="color: #993333; font-weight: bold;">DATA</span><span style="color: #66cc66;">&#41;</span> &nbsp;<span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> @L <span style="color: #993333; font-weight: bold;">SELECT</span> @LINE;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SET</span> @<span style="color: #993333; font-weight: bold;">DATA</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">RIGHT</span><span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">DATA</span><span style="color: #66cc66;">,</span> LEN<span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">DATA</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> CHARINDEX<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">NCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">13</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">NCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @<span style="color: #993333; font-weight: bold;">DATA</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> @L <span style="color: #993333; font-weight: bold;">SELECT</span> @<span style="color: #993333; font-weight: bold;">DATA</span>;<br />
<span style="color: #993333; font-weight: bold;">DELETE</span> @L <span style="color: #993333; font-weight: bold;">WHERE</span> LINE <span style="color: #66cc66;">=</span> N<span style="color: #ff0000;">''</span> <span style="color: #993333; font-weight: bold;">OR</span> LINE <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">LIKE</span> N<span style="color: #ff0000;">'%Table %'</span> <span style="color: #993333; font-weight: bold;">COLLATE</span> French_BIN;<br />
<br />
<span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; @L <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;LINE <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'%Nombre d'</span><span style="color: #ff0000;">'analyses%'</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #808080; font-style: italic;">--&gt; français</span><br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Table '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">','</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">' lectures logiques '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">' lectures physiques '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">' lectures anticipées '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'de données d'</span><span style="color: #ff0000;">'objets volumineux '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'.'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; @L <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;LINE <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'%Scan count%'</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #808080; font-style: italic;">--&gt; anglais</span><br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Table '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'. Scan count '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">','</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">' logical reads '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">' physical reads '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">' read-ahead reads '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'lob'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> @L <span style="color: #993333; font-weight: bold;">SET</span> LINE <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>LINE<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'.'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
<br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">=</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'SELECT '</span> <span style="color: #66cc66;">+</span> LINE <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">' UNION ALL '</span> <span style="color: #993333; font-weight: bold;">FROM</span> @L;<br />
<span style="color: #993333; font-weight: bold;">SET</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">LEFT</span><span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">SQL</span><span style="color: #66cc66;">,</span> LEN<span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">SQL</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">10</span><span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> @T<br />
<span style="color: #993333; font-weight: bold;">EXEC</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">SQL</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">WITH</span> <br />
T <span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #66cc66;">&#40;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; @T<br />
<span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'°°° TOTAL °°°'</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>SCAN_COUNT<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>LOGICAL_READS<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>PHYSICAL_READS<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>READ_AHEAD_READS<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>LOB_LOGICAL_READS<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>LOB_PHYSICAL_READS<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>LOB_READ_AHEAD_READS<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; @T<br />
<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <br />
<span style="color: #993333; font-weight: bold;">FROM</span> T<br />
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #993333; font-weight: bold;">CASE</span> <span style="color: #993333; font-weight: bold;">WHEN</span> <span style="color: #993333; font-weight: bold;">TABLE_NAME</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'°°° TOTAL °°°'</span> <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">ELSE</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333; font-weight: bold;">END</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;LOGICAL_READS <span style="color: #993333; font-weight: bold;">DESC</span>; &nbsp;<br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>SCAN_COUNT<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> TOTAL_SCANS<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>LOGICAL_READS <span style="color: #66cc66;">+</span> LOB_LOGICAL_READS<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> TOTAL_LOGICAL_READS<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SUM</span><span style="color: #66cc66;">&#40;</span>PHYSICAL_READS <span style="color: #66cc66;">+</span> READ_AHEAD_READS <span style="color: #66cc66;">+</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;LOB_PHYSICAL_READS <span style="color: #66cc66;">+</span> LOB_READ_AHEAD_READS<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> TOTAL_PHYSICAL_READS<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; @T;<br />
<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p><strong>Exemple d&rsquo;utilisation :</strong></p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">EXECUTE</span> msdb<span style="color: #66cc66;">.</span>S_DBA<span style="color: #66cc66;">.</span>P_SPLIT_STATS_IO <span style="color: #ff0000;">'Table '</span><span style="color: #ff0000;">'PARAM_EAC'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 13, lectures logiques 26, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'Worktable'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 0, lectures logiques 0, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'VAL_PARAM_EAC'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 1, lectures logiques 59, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'ESP'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 49, lectures logiques 31, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'TL_EXP_COOP'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 49, lectures logiques 178, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'UC'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 49, lectures logiques 5633, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'TL_PP_UC'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 49, lectures logiques 1254, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'PG'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 49, lectures logiques 85289, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'Worktable'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 0, lectures logiques 0, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'PG'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 1, lectures logiques 437, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'PP'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 49, lectures logiques 7271, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'EXP'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 49, lectures logiques 1996, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.<br />
Table '</span><span style="color: #ff0000;">'VAR'</span><span style="color: #ff0000;">'. Nombre d'</span><span style="color: #ff0000;">'analyses 49, lectures logiques 866, lectures physiques 0, lectures anticipées 0, lectures logiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures physiques de données d'</span><span style="color: #ff0000;">'objets volumineux 0, lectures anticipées de données d'</span><span style="color: #ff0000;">'objets volumineux 0.'</span></div></div>
<p>Le résultat :<br />
<div id="attachment_782" style="width: 986px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2017/12/STATISTICS_IO_GLOBAL-results.jpg"><img src="http://blog.developpez.com/sqlpro/files/2017/12/STATISTICS_IO_GLOBAL-results.jpg" alt="Les statistiques d&#039;IO d&#039;une requête, sus forme tabulaire, totalisés et agrégés" width="976" height="337" class="size-full wp-image-782" /></a><p class="wp-caption-text">Les statistiques d&rsquo;IO d&rsquo;une requête, sous forme tabulaire, totalisés et agrégés</p></div></p>
<p><strong>QUELQUES EXPLICATIONS SUR LES MÉTRIQUES</strong></p>
<li><strong>analyses</strong> (scan count) : nombre de fois ou l&rsquo;exécuteur de requête a du pénétrer dans un index de la table ou dans la table (en cas de parallélisme, autant de fois que de threads&#8230;)</li>
<li><strong>lectures logiques</strong> (logical reads) : nombre de pages lues en RAM</li>
<li><strong>lectures physiques</strong> (physical reads) : nombre de pages lues depuis le disque (avec transfert en RAM) en mode aléatoire.</li>
<li><strong>lectures anticipées</strong> (read-ahead physical read) : nombre de pages lues depuis le disque (avec transfert en RAM) en mode continu.</li>
<li>&#8230;<strong>d&rsquo;objets volumineux</strong> (lob &#8230;) : nombre de pages &#8230; pour les &laquo;&nbsp;Large OBjects&nbsp;&raquo;.
</li>
<p><strong>NOTA </strong>: cet outil fonctionne que votre serveur soit en anglais ou français.</p>
<p><strong>LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE</strong><br />
<a href="http://sqlpro.developpez.com/_fichierSQL/SET_STATISTIS_IO_AGGREGATE.txt" title="Fichier du code" target="_blank">Le code !</a></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Frédéric Brouard, alias SQLpro, ARCHITECTE DE DONNÉES<br />
Expert &nbsp;S.G.B.D &nbsp;relationnelles &nbsp; et &nbsp; langage &nbsp;S.Q.L<br />
Moste &nbsp;Valuable &nbsp;Professionnal &nbsp;Microsoft &nbsp;SQL Server<br />
Société SQLspot &nbsp;: &nbsp;modélisation, conseil, formation,<br />
optimisation, &nbsp;audit, &nbsp;tuning, &nbsp;administration &nbsp;SGBDR<br />
Enseignant: CNAM PACA, ISEN Toulon, CESI Aix en Prov.</div></div>
<p>L&rsquo;entreprise <a href="http://www.sqlspot.com">SQL Spot</a><br />
<strong>Le site web sur le </strong><a href="http://sqlpro.developpez.com/">SQL et les SGBDR</a></p>
<p><img src="http://blog.developpez.com/media/Microsoft_MVP_logo_vertical Brouard 400.jpg" width="400" height="135" alt="MVP Microsoft SQL
Server" /></p>
<div id="attachment_590" style="width: 548px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg"><img src="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg" alt="Développez et administrez pour la performance avec SQL Server 2014" width="538" height="652" class="size-full wp-image-590" /></a><p class="wp-caption-text">Développez et administrez pour la performance avec SQL Server 2014</p></div>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Une fonction de comptage d&#8217;occurrence</title>
		<link>https://blog.developpez.com/sqlpro/p13156/ms-sql-server/une-fonction-de-comptage-doccurrence</link>
		<comments>https://blog.developpez.com/sqlpro/p13156/ms-sql-server/une-fonction-de-comptage-doccurrence#comments</comments>
		<pubDate>Thu, 16 Nov 2017 17:51:42 +0000</pubDate>
		<dc:creator><![CDATA[SQLpro]]></dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[SQL Server 2000]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server 2014]]></category>
		<category><![CDATA[SQL Server 2016]]></category>
		<category><![CDATA[SQL Server 2017]]></category>
		<category><![CDATA[caractères]]></category>
		<category><![CDATA[comptage]]></category>
		<category><![CDATA[fonction]]></category>
		<category><![CDATA[occurrence]]></category>
		<category><![CDATA[SQL server]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=776</guid>
		<description><![CDATA[La petite fonction qui suit permet de compter le nombre de fois ou un caractère est présent dans une chaine de caractères. CREATE FUNCTION F_COUNT_CHAR&#40;@STR NVARCHAR&#40;MAX&#41;, @CHR NCHAR&#40;1&#41;&#41; RETURNS INT WITH RETURNS NULL ON NULL INPUT AS BEGIN DECLARE @NBR INT, @POS INT; SELECT @NBR = 0, @POS = CHARINDEX&#40;@CHR, @STR&#41;; WHILE @POS &#62; 0 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>La petite fonction qui suit permet de compter le nombre de fois ou un caractère est présent dans une chaine de caractères.</p>
<p><span id="more-776"></span></p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> F_COUNT_CHAR<span style="color: #66cc66;">&#40;</span>@STR NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @CHR <span style="color: #993333; font-weight: bold;">NCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">RETURNS</span> <span style="color: #993333; font-weight: bold;">INT</span><br />
<span style="color: #993333; font-weight: bold;">WITH</span> <span style="color: #993333; font-weight: bold;">RETURNS</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">INPUT</span><br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @NBR <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">,</span> @POS <span style="color: #993333; font-weight: bold;">INT</span>;<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @NBR <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> @POS <span style="color: #66cc66;">=</span> CHARINDEX<span style="color: #66cc66;">&#40;</span>@CHR<span style="color: #66cc66;">,</span> @STR<span style="color: #66cc66;">&#41;</span>;<br />
WHILE @POS <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SET</span> @NBR <span style="color: #66cc66;">=</span> @NBR <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SET</span> @POS <span style="color: #66cc66;">=</span> CHARINDEX<span style="color: #66cc66;">&#40;</span>@CHR<span style="color: #66cc66;">,</span> @STR<span style="color: #66cc66;">,</span> @POS <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
<span style="color: #993333; font-weight: bold;">RETURN</span> @NBR;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p>Exemples d&rsquo;utilisation :</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">-- exemple &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- résultat</span><br />
<span style="color: #808080; font-style: italic;">------------------------------------------- ------------</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span>F_COUNT_CHAR<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'abracadabra'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'a'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #808080; font-style: italic;">--&gt; 5</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span>F_COUNT_CHAR<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'a'</span><span style="color: #66cc66;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">--&gt; 0</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span>F_COUNT_CHAR<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'a'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">--&gt; 0</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span>F_COUNT_CHAR<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">--&gt; 0</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span>F_COUNT_CHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'a'</span><span style="color: #66cc66;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">--&gt; NULL</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span>F_COUNT_CHAR<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'a'</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #808080; font-style: italic;">--&gt; NULL</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span>F_COUNT_CHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">--&gt; NULL</span></div></div>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Frédéric Brouard, alias SQLpro, ARCHITECTE DE DONNÉES<br />
Expert &nbsp;S.G.B.D &nbsp;relationnelles &nbsp; et &nbsp; langage &nbsp;S.Q.L<br />
Moste &nbsp;Valuable &nbsp;Professionnal &nbsp;Microsoft &nbsp;SQL Server<br />
Société SQLspot &nbsp;: &nbsp;modélisation, conseil, formation,<br />
optimisation, &nbsp;audit, &nbsp;tuning, &nbsp;administration &nbsp;SGBDR<br />
Enseignant: CNAM PACA, ISEN Toulon, CESI Aix en Prov.</div></div>
<p>L&rsquo;entreprise <a href="http://www.sqlspot.com">SQL Spot</a><br />
<strong>Le site web sur le </strong><a href="http://sqlpro.developpez.com/">SQL et les SGBDR</a></p>
<p><img src="http://blog.developpez.com/media/Microsoft_MVP_logo_vertical Brouard 400.jpg" width="400" height="135" alt="MVP Microsoft SQL
Server" /></p>
<div id="attachment_590" style="width: 548px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg"><img src="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg" alt="Développez et administrez pour la performance avec SQL Server 2014" width="538" height="652" class="size-full wp-image-590" /></a><p class="wp-caption-text">Développez et administrez pour la performance avec SQL Server 2014</p></div>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Les tables temporelles avec SQL Server (présentation)</title>
		<link>https://blog.developpez.com/sqlpro/p13141/langage-sql-norme/les-tables-temporelles-avec-sql-server-presentation</link>
		<comments>https://blog.developpez.com/sqlpro/p13141/langage-sql-norme/les-tables-temporelles-avec-sql-server-presentation#comments</comments>
		<pubDate>Wed, 25 Jan 2017 14:00:36 +0000</pubDate>
		<dc:creator><![CDATA[SQLpro]]></dc:creator>
				<category><![CDATA[Langage SQL (norme)]]></category>
		<category><![CDATA[SQL Server 2016]]></category>
		<category><![CDATA[historisation]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[SQL server]]></category>
		<category><![CDATA[tables temporelles]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=751</guid>
		<description><![CDATA[Arrivée avec la norme SQL 2011, le concept de tables temporelles permet une historisation automatique des données et propose des opérateurs temporels pour &#171;&#160;voir&#160;&#187; vos données telles qu&#8217;elles étaient à un point ou une période du temps passé. Paradoxe : plus besoin de faire des sauvegardes ! Cette présentation montré au Microsoft Cloud Summit 2017 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Arrivée avec la norme SQL 2011, le concept de tables temporelles permet une historisation automatique des données et propose des opérateurs temporels pour &laquo;&nbsp;voir&nbsp;&raquo; vos données telles qu&rsquo;elles étaient à un point ou une période du temps passé. Paradoxe : plus besoin de faire des sauvegardes ! Cette présentation montré au Microsoft Cloud Summit 2017 à paris le 24 janvier 2017, vous montre l&rsquo;essentiel sur le sujet, avec SQL Server 2016 et est assortie de nombreux exemples.<br />
<span id="more-751"></span></p>
<p>La présentation PowerPoint sous forme PDF est téléchargeable ici : <a href="http://mssqlserver.fr/wp-content/uploads/2017/01/MSCloudSummit2017-SQL-et-les-tables-temporelles-Commentaires.pdf" title="SQL Server et les tables temporelles" target="_blank">SQL Server et les tables temporelles</a><br />
<strong><br />
Les exemples l&rsquo;accompagnant, sont téléchargeable ici :</strong></p>
<p><a href="http://mssqlserver.fr/wp-content/uploads/2017/01/DEMO-001-table-temporelles-sans-historique.txt" target="_blank">DEMO 001 &#8211; table temporelles sans historique (fichier SQL)</a><br />
<a href="http://mssqlserver.fr/wp-content/uploads/2017/01/DEMO-002-table-temporelles-avec-historique.txt" target="_blank">DEMO 002 &#8211; table temporelles avec historique (fichier SQL)</a><br />
<a href="DEMO 003 – stockage (fichier SQL)" target="_blank">DEMO 003 &#8211; stockage (fichier SQL)</a><br />
<a href="http://mssqlserver.fr/wp-content/uploads/2017/01/DEMO-004-R%C3%A9cup%C3%A9ration.txt" target="_blank">DEMO 004 &#8211; Récupération (fichier SQL)</a><br />
<a href="http://mssqlserver.fr/wp-content/uploads/2017/01/DEMO-005-Purge.txt" target="_blank">DEMO 005 &#8211; Purge (fichier SQL)</a><br />
<a href="http://mssqlserver.fr/wp-content/uploads/2017/01/DEMO-005b-Purge-m%C3%A9tadonn%C3%A9es-blocage.txt" target="_blank">DEMO 005b &#8211; Purge métadonnées blocage (fichier SQL)</a><br />
<a href="http://mssqlserver.fr/wp-content/uploads/2017/01/DEMO-006-interrogation.txt" target="_blank">DEMO 006 &#8211; interrogation (fichier SQL)</a><br />
<a href="http://mssqlserver.fr/wp-content/uploads/2017/01/DEMO-007-In-Memory.txthttp://" target="_blank">DEMO 007 &#8211; In Memory (fichier SQL)</a><br />
<a href="http://mssqlserver.fr/wp-content/uploads/2017/01/DEMO-ANNEXE-temporal-tables.txt" target="_blank">DEMO ANNEXE temporal tables (fichier SQL)</a></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Frédéric Brouard, alias SQLpro, ARCHITECTE DE DONNÉES<br />
Expert &nbsp;S.G.B.D &nbsp;relationnelles &nbsp; et &nbsp; langage &nbsp;S.Q.L<br />
Most &nbsp;Valuable &nbsp;Professionnal &nbsp;Microsoft &nbsp;SQL Server<br />
Société SQLspot &nbsp;: &nbsp;modélisation, conseil, formation,<br />
optimisation, &nbsp;audit, &nbsp;tuning, &nbsp;administration &nbsp;SGBDR<br />
Enseignant: CNAM PACA, ISEN Toulon, CESI Aix en Prov.</div></div>
<p>L&rsquo;entreprise <a href="http://www.sqlspot.com">SQL Spot</a><br />
<strong>Le site web sur le </strong><a href="http://sqlpro.developpez.com/">SQL et les SGBDR</a></p>
<p><img src="http://blog.developpez.com/media/Microsoft_MVP_logo_vertical Brouard 400.jpg" width="400" height="135" alt="MVP Microsoft SQL
Server" /></p>
<div id="attachment_590" style="width: 548px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg"><img src="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg" alt="Développez et administrez pour la performance avec SQL Server 2014" width="538" height="652" class="size-full wp-image-590" /></a><p class="wp-caption-text">Développez et administrez pour la performance avec SQL Server 2014</p></div>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Métadonnées des index (sp__helpindex)</title>
		<link>https://blog.developpez.com/sqlpro/p13139/ms-sql-server/sql-server-2012/metadonnees-des-index-sp__helpindex</link>
		<comments>https://blog.developpez.com/sqlpro/p13139/ms-sql-server/sql-server-2012/metadonnees-des-index-sp__helpindex#comments</comments>
		<pubDate>Tue, 17 Jan 2017 15:51:08 +0000</pubDate>
		<dc:creator><![CDATA[SQLpro]]></dc:creator>
				<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server 2014]]></category>
		<category><![CDATA[SQL Server 2016]]></category>
		<category><![CDATA[descriptif]]></category>
		<category><![CDATA[index]]></category>
		<category><![CDATA[métadonnée]]></category>
		<category><![CDATA[sp_helpindex]]></category>
		<category><![CDATA[SQL server]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=744</guid>
		<description><![CDATA[La procédure stockée master.sys.sp_helpindex, décrivant les index d&#8217;une table, fournit par Microsoft, est aujourd&#8217;hui fortement obsolète et Microsoft n&#8217;a pas souhaité donné suite aux demandes de modification (1). Voici une procédure inspirée de cette dernière bien plus complète et prenant en compte tous les types d&#8217;index (spatiaux, XML, columstore, fulltext). (1) Dans l&#8217;outil CONNECT consacré [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>La procédure stockée master.sys.sp_helpindex, décrivant les index d&rsquo;une table, fournit par Microsoft, est aujourd&rsquo;hui fortement obsolète et Microsoft n&rsquo;a pas souhaité donné suite aux demandes de modification (1). Voici une procédure inspirée de cette dernière bien plus complète et prenant en compte tous les types d&rsquo;index (spatiaux, XML, columstore, fulltext).<br />
<span id="more-744"></span><br />
<strong>(1)</strong> Dans l&rsquo;outil CONNECT consacré aux amélioration de SQL Server, il est dit par Microsoft, que, malgré les multiples demandes, la procédure stockée <a href="https://connect.microsoft.com/SQLServer/feedback/details/577526/sp-helpindex-to-show-included-columns" title="CONNECT : sp_helpindex, demande d'amélioration fermées" target="_blank">sp_helpindex</a>, ne sera pas améliorée&#8230;<br />
Cependant cette procédure est bien pratique pour comparer les index d&rsquo;une même table afin de savoir s&rsquo;il n&rsquo;existe pas déjà tel ou tel index, au moment d&rsquo;en créer de nouveaux, ou encore, s&rsquo;il existe des index inclus ou redondants.<br />
Voici donc une nouvelle procédure intitulée sp__helpindex (notez le double blanc souligné afin de ne pas écraser l&rsquo;ancienne) qui permet de pallier à la déficience de l&rsquo;ancienne et qui rajoute :</p>
<ul>
<li>d&rsquo;un point de vu &laquo;&nbsp;horizontal&nbsp;&raquo; : nom de table, colonnes incluses, filtre et l&rsquo;essentiel de la clause WITH</li>
<li>d&rsquo;un point de vue vertical : les index columnstore, xml, spatiaux et fulltext</li>
</ul>
<p><strong>Voici le code de cette procédure :</strong></p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">PROCEDURE</span> dbo<span style="color: #66cc66;">.</span>sp__HELPINDEX @OBJ NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">133</span><span style="color: #66cc66;">&#41;</span> <br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #808080; font-style: italic;">/******************************************************************************<br />
* METADONNÉES DES INDEX &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* Procédure listant sous forme de tables les métadonnées logique des index &nbsp; &nbsp;*<br />
******************************************************************************* &nbsp; <br />
* Frédéric Brouard - SQLpro@SQLspot.com - Sté SQL SPOT http://www.sqlspot.com *<br />
* Plus d'info. sur http://sqlpro.developpez.com &nbsp; - &nbsp;2017-01-17 - version 1.0 * &nbsp; <br />
******************************************************************************* &nbsp; <br />
* Cette procédure prend en argument le nom d'une table avec son schéma SQL &nbsp; &nbsp;*<br />
* (sinon le schéma par défaut de l'utilisateur qui la lance) et renvoie un &nbsp; &nbsp;*<br />
* jeu de données contenant le descriptif logique de constitution d'un index &nbsp; *<br />
* ou de tous les index de la base &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* NOTA substitut à la procédure stockée système sp_helpindex &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
*******************************************************************************<br />
* ATTENTION &nbsp;: procédure système ! Exécutable depuis n'importe quelle base &nbsp; &nbsp;*<br />
* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* Paramètre en entrée : &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* &nbsp; &nbsp;@OBJ type NVARCHAR(133) : nom de table dont on veut connaître les index &nbsp;*<br />
* &nbsp; &nbsp; &nbsp; si vide, la procédure renvoie la liste de tous les index &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* COLONNE de la table en sortie : &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* &nbsp; &nbsp;TABLE_NAME : nom de la table en deux parties (schéma SQL + nom) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp;INDEX_NAME : nom de l'index (1) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp;INDEX TYPE : type d'index &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp;INDEX_STORAGE : type et nom de l'espace de stockage &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp;INDEX_KEY : liste des colonnes composant la clef d'index (2) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* &nbsp; &nbsp;INDEX_INCLUDE : liste des colonnes incluses (3) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp;INDEX_FILTER : filtre d'index s'il y a lieu &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp;INDEX_WITH : principaux paramètres de la clause WITH &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* NOTA : &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; (1) certains index n'ont pas de nom. C'est le cas des index &quot;fulltext&quot; &nbsp; &nbsp;*<br />
* &nbsp; (2) certains index n'ont pas de clef. C'est le cas des index &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp; &nbsp; &quot;columnstore&quot; et &quot;fulltext&quot;. Dans ce cas les colonnes indexées &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp; &nbsp; figurent dans la clause INDEX_INCLUDE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* &nbsp; (3) pour les index CLUSTERED de type BTree, toutes les colonnes autre &nbsp; &nbsp; *<br />
* &nbsp; &nbsp; &nbsp; que celles de la clef étant présente, la valeur présentée est : &nbsp; &nbsp; &nbsp; *<br />
* &nbsp; &nbsp; &nbsp; * - (INDEX_KEY) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* &nbsp; &nbsp; &nbsp; pour les index CLUSTERED COLUMNSTORE toutes les colonnes de la table &nbsp;*<br />
* &nbsp; &nbsp; &nbsp; étant présente, la valeur présentée est : * &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* EXEMPLE : &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* &nbsp; &nbsp;EXEC dbo.sp__HELPINDEX NULL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp;... donne la liste de tous les index de la base ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* <br />
* &nbsp; &nbsp;EXEC dbo.sp__HELPINDEX &nbsp;'[client]' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
* &nbsp; &nbsp;... donne la liste des index de la table client ... &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;* <br />
* &nbsp; &nbsp;EXEC dbo.sp__HELPINDEX &nbsp;'ventes.client' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp;... donne la liste des index de la table client du schéma SQL vente... &nbsp; * <br />
* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
******************************************************************************/</span> <br />
<br />
<span style="color: #993333; font-weight: bold;">SET</span> NOCOUNT <span style="color: #993333; font-weight: bold;">ON</span>;<br />
<span style="color: #993333; font-weight: bold;">WITH</span> <br />
T0 <span style="color: #993333; font-weight: bold;">AS</span> <br />
<span style="color: #66cc66;">&#40;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> i<span style="color: #66cc66;">.</span><span style="color: #ff0000;">&quot;type&quot;</span><span style="color: #66cc66;">,</span> s<span style="color: #66cc66;">.</span>name <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'.'</span> <span style="color: #66cc66;">+</span> o<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">TABLE_NAME</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;i<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> INDEX_NAME<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">CASE</span> <span style="color: #993333; font-weight: bold;">WHEN</span> i<span style="color: #66cc66;">.</span>is_unique <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #ff0000;">'UNIQUE '</span> <span style="color: #993333; font-weight: bold;">ELSE</span> <span style="color: #ff0000;">''</span> <span style="color: #993333; font-weight: bold;">END</span> <span style="color: #66cc66;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">CASE</span> <span style="color: #993333; font-weight: bold;">WHEN</span> xi<span style="color: #66cc66;">.</span>xml_index_type <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #ff0000;">'PRIMARY '</span> <span style="color: #993333; font-weight: bold;">ELSE</span> <span style="color: #ff0000;">''</span> <span style="color: #993333; font-weight: bold;">END</span> <span style="color: #66cc66;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;i<span style="color: #66cc66;">.</span>type_desc <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">CASE</span> <span style="color: #993333; font-weight: bold;">WHEN</span> xi<span style="color: #66cc66;">.</span>xml_index_type <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #ff0000;">' FOR '</span> <span style="color: #66cc66;">+</span> xi<span style="color: #66cc66;">.</span>secondary_type_desc <span style="color: #993333; font-weight: bold;">ELSE</span> <span style="color: #ff0000;">''</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">END</span> <span style="color: #993333; font-weight: bold;">AS</span> INDEX_TYPE<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">COALESCE</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'FILEGROUP: '</span> <span style="color: #66cc66;">+</span> fg<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'PARTITION: '</span> <span style="color: #66cc66;">+</span> ps<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">COLLATE</span> database_default <span style="color: #66cc66;">+</span><span style="color: #ff0000;">'('</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">+</span> STUFF<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> N<span style="color: #ff0000;">', '</span> <span style="color: #66cc66;">+</span> cp<span style="color: #66cc66;">.</span>name &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>index_columns <span style="color: #993333; font-weight: bold;">AS</span> icp<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">OUTER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">COLUMNS</span> <span style="color: #993333; font-weight: bold;">AS</span> cp<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">ON</span> icp<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> cp<span style="color: #66cc66;">.</span>object_id<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AND</span> icp<span style="color: #66cc66;">.</span>column_id <span style="color: #66cc66;">=</span> cp<span style="color: #66cc66;">.</span>column_id <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;icp<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> i<span style="color: #66cc66;">.</span>object_id<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AND</span> icp<span style="color: #66cc66;">.</span>index_id <span style="color: #66cc66;">=</span> i<span style="color: #66cc66;">.</span>index_id &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AND</span> icp<span style="color: #66cc66;">.</span>partition_ordinal <span style="color: #66cc66;">&gt;=</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">ORDER</span> &nbsp;<span style="color: #993333; font-weight: bold;">BY</span> icp<span style="color: #66cc66;">.</span>partition_ordinal &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">FOR</span> XML PATH<span style="color: #66cc66;">&#40;</span>N<span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span><span style="color: #ff0000;">')'</span> <span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AS</span> INDEX_STORAGE<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;LTRIM<span style="color: #66cc66;">&#40;</span>STUFF<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> N<span style="color: #ff0000;">', '</span> &nbsp;<span style="color: #66cc66;">+</span> CONCAT<span style="color: #66cc66;">&#40;</span>c<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">' '</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">CASE</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">WHEN</span> i<span style="color: #66cc66;">.</span>type_desc <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'XML'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'SPATIAL'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'COLUMNSTORE'</span><span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #ff0000;">''</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">WHEN</span> is_descending_key <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #ff0000;">'DESC'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">WHEN</span> is_descending_key <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #ff0000;">'ASC'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">ELSE</span> <span style="color: #ff0000;">''</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">END</span><span style="color: #66cc66;">&#41;</span> &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>index_columns <span style="color: #993333; font-weight: bold;">AS</span> ic <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">COLUMNS</span> <span style="color: #993333; font-weight: bold;">AS</span> c <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> ic<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> c<span style="color: #66cc66;">.</span>object_id <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AND</span> ic<span style="color: #66cc66;">.</span>column_id <span style="color: #66cc66;">=</span> c<span style="color: #66cc66;">.</span>column_id<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;ic<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> i<span style="color: #66cc66;">.</span>object_id <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;ic<span style="color: #66cc66;">.</span>index_id <span style="color: #66cc66;">=</span> i<span style="color: #66cc66;">.</span>index_id <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;is_included_column <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ORDER</span> &nbsp;<span style="color: #993333; font-weight: bold;">BY</span> key_ordinal &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FOR</span> XML PATH<span style="color: #66cc66;">&#40;</span>N<span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> INDEX_KEY<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;LTRIM<span style="color: #66cc66;">&#40;</span>STUFF<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> N<span style="color: #ff0000;">', '</span> <span style="color: #66cc66;">+</span> c<span style="color: #66cc66;">.</span>name &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>index_columns <span style="color: #993333; font-weight: bold;">AS</span> ic <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">COLUMNS</span> <span style="color: #993333; font-weight: bold;">AS</span> c <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> ic<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> c<span style="color: #66cc66;">.</span>object_id <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AND</span> ic<span style="color: #66cc66;">.</span>column_id <span style="color: #66cc66;">=</span> c<span style="color: #66cc66;">.</span>column_id<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;ic<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> i<span style="color: #66cc66;">.</span>object_id <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;ic<span style="color: #66cc66;">.</span>index_id <span style="color: #66cc66;">=</span> i<span style="color: #66cc66;">.</span>index_id <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;is_included_column <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ORDER</span> &nbsp;<span style="color: #993333; font-weight: bold;">BY</span> c<span style="color: #66cc66;">.</span>name &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FOR</span> XML PATH<span style="color: #66cc66;">&#40;</span>N<span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> INDEX_INCLUDE<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;i<span style="color: #66cc66;">.</span>filter_definition <span style="color: #993333; font-weight: bold;">AS</span> INDEX_FILTER<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'FILL_FACTOR = '</span> &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">.</span>fill_factor <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">', PAD_INDEX = '</span> &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">.</span>is_padded <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">CHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">', ALLOW_ROWLOCK = '</span> &nbsp;<span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">.</span>allow_row_locks <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">CHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">', ALLOW_PAGELOCK = '</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">.</span>allow_page_locks <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">CHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AS</span> INDEX_WITH<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>indexes <span style="color: #993333; font-weight: bold;">AS</span> i<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>objects <span style="color: #993333; font-weight: bold;">AS</span> o<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> i<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> o<span style="color: #66cc66;">.</span>object_id<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>schemas &nbsp;<span style="color: #993333; font-weight: bold;">AS</span> s &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">ON</span> o<span style="color: #66cc66;">.</span>schema_id <span style="color: #66cc66;">=</span> s<span style="color: #66cc66;">.</span>schema_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>data_spaces <span style="color: #993333; font-weight: bold;">AS</span> ds<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> i<span style="color: #66cc66;">.</span>data_space_id <span style="color: #66cc66;">=</span> ds<span style="color: #66cc66;">.</span>data_space_id<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">OUTER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>filegroups <span style="color: #993333; font-weight: bold;">AS</span> fg<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">ON</span> ds<span style="color: #66cc66;">.</span>data_space_id <span style="color: #66cc66;">=</span> fg<span style="color: #66cc66;">.</span>data_space_id <span style="color: #993333; font-weight: bold;">AND</span> ds<span style="color: #66cc66;">.</span><span style="color: #ff0000;">&quot;type&quot;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'FG'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">OUTER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>partition_schemes <span style="color: #993333; font-weight: bold;">AS</span> ps<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">ON</span> ds<span style="color: #66cc66;">.</span>data_space_id <span style="color: #66cc66;">=</span> ps<span style="color: #66cc66;">.</span>data_space_id <span style="color: #993333; font-weight: bold;">AND</span> ds<span style="color: #66cc66;">.</span><span style="color: #ff0000;">&quot;type&quot;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'PS'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">OUTER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>partition_functions <span style="color: #993333; font-weight: bold;">AS</span> pf<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> ps<span style="color: #66cc66;">.</span>function_id <span style="color: #66cc66;">=</span> pf<span style="color: #66cc66;">.</span>function_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">OUTER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>xml_indexes <span style="color: #993333; font-weight: bold;">AS</span> xi<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> i<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> xi<span style="color: #66cc66;">.</span>object_id<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AND</span> i<span style="color: #66cc66;">.</span>index_id <span style="color: #66cc66;">=</span> xi<span style="color: #66cc66;">.</span>index_id<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;o<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">COALESCE</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">NULLIF</span><span style="color: #66cc66;">&#40;</span>OBJECT_ID<span style="color: #66cc66;">&#40;</span>@OBJ<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> o<span style="color: #66cc66;">.</span>object_id<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">AND</span> &nbsp; &nbsp;i<span style="color: #66cc66;">.</span>index_id <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><br />
<span style="color: #993333; font-weight: bold;">AND</span> &nbsp; &nbsp;o<span style="color: #66cc66;">.</span>is_ms_shipped <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">TABLE_NAME</span><span style="color: #66cc66;">,</span> INDEX_NAME<span style="color: #66cc66;">,</span> INDEX_TYPE<span style="color: #66cc66;">,</span> INDEX_STORAGE<span style="color: #66cc66;">,</span> INDEX_KEY<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">CASE</span> <span style="color: #ff0000;">&quot;type&quot;</span> <span style="color: #993333; font-weight: bold;">WHEN</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #ff0000;">'* - (INDEX_KEY)'</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">WHEN</span> <span style="color: #cc66cc;">5</span> <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #ff0000;">'*'</span> <span style="color: #993333; font-weight: bold;">END</span> <span style="color: #993333; font-weight: bold;">AS</span> INDEX_INCLUDE<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;INDEX_FILTER<span style="color: #66cc66;">,</span> INDEX_WITH<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; T0<br />
<span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> s<span style="color: #66cc66;">.</span>name <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'.'</span> <span style="color: #66cc66;">+</span> o<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">TABLE_NAME</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AS</span> INDEX_NAME<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'FULL TEXT'</span> <span style="color: #993333; font-weight: bold;">AS</span> INDEX_TYPE<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'FULLTEXT CATALOG: '</span> <span style="color: #66cc66;">+</span> ftc<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">AS</span> INDEX_STORAGE<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AS</span> INDEX_KEY<span style="color: #66cc66;">,</span> &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp;LTRIM<span style="color: #66cc66;">&#40;</span>STUFF<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> N<span style="color: #ff0000;">', '</span> <span style="color: #66cc66;">+</span> c<span style="color: #66cc66;">.</span>name &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>fulltext_index_columns <span style="color: #993333; font-weight: bold;">AS</span> ftic <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">COLUMNS</span> <span style="color: #993333; font-weight: bold;">AS</span> c<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> c<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> ftic<span style="color: #66cc66;">.</span>object_id <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AND</span> c<span style="color: #66cc66;">.</span>column_id <span style="color: #66cc66;">=</span> ftic<span style="color: #66cc66;">.</span>column_id<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;ftic<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> fti<span style="color: #66cc66;">.</span>object_id &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FOR</span> XML PATH<span style="color: #66cc66;">&#40;</span>N<span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> INDEX_INCLUDE<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AS</span> INDEX_FILTER<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'CHANGE_TRACKING = '</span> <span style="color: #66cc66;">+</span> change_tracking_state_desc <span style="color: #66cc66;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">', STOPLIST = '</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">CASE</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHEN</span> fti<span style="color: #66cc66;">.</span>stoplist_id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #ff0000;">'SYSTEM'</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHEN</span> fti<span style="color: #66cc66;">.</span>stoplist_id <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #ff0000;">'OFF'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ELSE</span> <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> name <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>fulltext_stoplists <span style="color: #993333; font-weight: bold;">AS</span> ftsl<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;fti<span style="color: #66cc66;">.</span>stoplist_id <span style="color: #66cc66;">=</span> ftsl<span style="color: #66cc66;">.</span>stoplist_id<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">END</span> <span style="color: #66cc66;">+</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">COALESCE</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">', SEARCH PROPERTY LIST = '</span> <span style="color: #66cc66;">+</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> name <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>registered_search_property_lists <span style="color: #993333; font-weight: bold;">AS</span> ftpl<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;fti<span style="color: #66cc66;">.</span>property_list_id <span style="color: #66cc66;">=</span> ftpl<span style="color: #66cc66;">.</span>property_list_id<span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">COLLATE</span> French_BIN<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> INDEX_WITH <br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>fulltext_indexes <span style="color: #993333; font-weight: bold;">AS</span> fti<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>objects <span style="color: #993333; font-weight: bold;">AS</span> o<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> fti<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> o<span style="color: #66cc66;">.</span>object_id<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>schemas <span style="color: #993333; font-weight: bold;">AS</span> s &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">ON</span> o<span style="color: #66cc66;">.</span>schema_id <span style="color: #66cc66;">=</span> s<span style="color: #66cc66;">.</span>schema_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>fulltext_catalogs <span style="color: #993333; font-weight: bold;">AS</span> ftc<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> fti<span style="color: #66cc66;">.</span>fulltext_catalog_id <span style="color: #66cc66;">=</span> ftc<span style="color: #66cc66;">.</span>fulltext_catalog_id<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;o<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">COALESCE</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">NULLIF</span><span style="color: #66cc66;">&#40;</span>OBJECT_ID<span style="color: #66cc66;">&#40;</span>@OBJ<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> o<span style="color: #66cc66;">.</span>object_id<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">ORDER</span> &nbsp;<span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #993333; font-weight: bold;">TABLE_NAME</span><span style="color: #66cc66;">,</span> INDEX_KEY<span style="color: #66cc66;">,</span> INDEX_INCLUDE; &nbsp;<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p>Il convient de la créer dans la base master et de la rendre procédure système, à l&rsquo;aide de la commande :</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">EXEC sp_MS_marksystemobject 'sp__HELPINDEX';</div></div>
<p><strong>Les colonnes du jeu de données en sortie de procédure sont :</strong></p>
<ul>
<li>TABLE_NAME : nom de la table en deux parties (schéma SQL + nom) </li>
<li>INDEX_NAME : nom de l&rsquo;index. Certains index n&rsquo;ont pas de nom. C&rsquo;est en particulier le cas des index &laquo;&nbsp;fulltext&nbsp;&raquo; </li>
<li>INDEX TYPE : type d&rsquo;index</li>
<li>INDEX_STORAGE : type et nom de l&rsquo;espace de stockage</li>
<li>INDEX_KEY : liste des colonnes composant la clef d&rsquo;index. Certains index n&rsquo;ont pas de clef. C&rsquo;est le cas des index &laquo;&nbsp;columnstore&nbsp;&raquo; et &laquo;&nbsp;fulltext&nbsp;&raquo;. Dans ce cas les colonnes indexées figurent dans la clause INDEX_INCLUDE </li>
<li>INDEX_INCLUDE : liste des colonnes incluses. Pour les index CLUSTERED de type BTree, toutes les colonnes autre que celles de la clef étant présente, la valeur qui y figure est :  * &#8211; (INDEX_KEY). Pour les index CLUSTERED COLUMNSTORE toutes les colonnes de la table étant présente dans l&rsquo;index, la valeur qui y figure est : *</li>
<li>INDEX_FILTER : filtre d&rsquo;index s&rsquo;il y a lieu</li>
<li>INDEX_WITH : principaux paramètres de la clause WITH </li>
</ul>
<p>Si vous l&rsquo;exécutez sans spécifier le nom d&rsquo;une table (le paramètre sera donc NULL) alors la procédure sort la liste des tous les index de toutes les tables, vues indexées comprises.</p>
<p>Vous pouvez transformer aisément cette procédure en une fonction table en ligne avantageuse.</p>
<p><strong>LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE</strong><br />
<a href="http://sqlpro.developpez.com/_fichierSQL/sp__helpindex.txt" title="Fichier du code" target="_blank">Le code !</a></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Frédéric Brouard, alias SQLpro, ARCHITECTE DE DONNÉES<br />
Expert &nbsp;S.G.B.D &nbsp;relationnelles &nbsp; et &nbsp; langage &nbsp;S.Q.L<br />
Moste &nbsp;Valuable &nbsp;Professionnal &nbsp;Microsoft &nbsp;SQL Server<br />
Société SQLspot &nbsp;: &nbsp;modélisation, conseil, formation,<br />
optimisation, &nbsp;audit, &nbsp;tuning, &nbsp;administration &nbsp;SGBDR<br />
Enseignant: CNAM PACA, ISEN Toulon, CESI Aix en Prov.</div></div>
<p>L&rsquo;entreprise <a href="http://www.sqlspot.com">SQL Spot</a><br />
<strong>Le site web sur le </strong><a href="http://sqlpro.developpez.com/">SQL et les SGBDR</a></p>
<p><img src="http://blog.developpez.com/media/Microsoft_MVP_logo_vertical Brouard 400.jpg" width="400" height="135" alt="MVP Microsoft SQL
Server" /></p>
<div id="attachment_590" style="width: 548px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg"><img src="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg" alt="Développez et administrez pour la performance avec SQL Server 2014" width="538" height="652" class="size-full wp-image-590" /></a><p class="wp-caption-text">Développez et administrez pour la performance avec SQL Server 2014</p></div>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Audit trail générique</title>
		<link>https://blog.developpez.com/sqlpro/p13128/ms-sql-server/sql-server-2005/audit-trail-generique</link>
		<comments>https://blog.developpez.com/sqlpro/p13128/ms-sql-server/sql-server-2005/audit-trail-generique#comments</comments>
		<pubDate>Fri, 13 Jan 2017 14:53:54 +0000</pubDate>
		<dc:creator><![CDATA[SQLpro]]></dc:creator>
				<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server 2014]]></category>
		<category><![CDATA[SQL Server 2016]]></category>
		<category><![CDATA[audit]]></category>
		<category><![CDATA[audit trail]]></category>
		<category><![CDATA[pistage]]></category>
		<category><![CDATA[SQL server]]></category>
		<category><![CDATA[traçabilité]]></category>
		<category><![CDATA[trace]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=741</guid>
		<description><![CDATA[L&#8217; &#171;&#160;audit trail&#160;&#187;, littéralement &#171;&#160;piste d&#8217;audit&#160;&#187; est un audit destiné à pister les événements qui se passe dans un système. Dans une base de données, il est, la plupart du temps, destiné à vérifier ce qui s&#8217;est passé, notamment sur le plan des valeurs avant ou après la modification. Il peut à la fois servir [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>L&rsquo; &laquo;&nbsp;audit trail&nbsp;&raquo;, littéralement &laquo;&nbsp;piste d&rsquo;audit&nbsp;&raquo; est un audit destiné à pister les événements qui se passe dans un système. Dans une base de données, il est, la plupart du temps, destiné à vérifier ce qui s&rsquo;est passé, notamment sur le plan des valeurs avant ou après la modification. Il peut à la fois servir pour la sécurité (qui à fait quoi ?), comme sur le plan fonctionnel (pourquoi cette valeur ?, À quelle date un tel changement ?&#8230;).<br />
En dehors des outils lourds comme CDC (Change Data Capture) et Change Tracking (tous deux destiné à savoir <strong>quoi</strong>, donc fonctionnel) ou Database Audit (destiné à savoir <strong>qui</strong>, donc sécurité), voici une méthode basée sur un déclencheur et une seule et unique table, facile et rapide à mettre en œuvre et qui permet de tracer qui et quoi&#8230;<br />
<span id="more-741"></span><br />
la principe est simple : chaque table reçoit un unique déclencheur lancé sur tous les événements de mise à jour (INSERT, UPDATE, DELETE) et concatène les images <em>avant </em>et <em>après </em>des données (tables inserted et deleted) puis les transforme en un unique document XML, quelque soit le nombre de lignes. Il est alors facile de stocker cette information das une ligne d&rsquo;une table, accompagnée de métadonnées, notamment, date et heure de survenance, application cliente, compte de connexion, utilisateur SQL, machine hôte, login windows, etc&#8230; </p>
<p>Il ne reste plus qu&rsquo;à lire la table de trace pour savoir qui à fait quoi et quand ! Comme les données sont sous forme XML, il est possible des les interroger par des requêtes SQL mêlant XQuery et XPath. Une astuce consistant à publier certaines vues pour les demandes de contrôle les plus fréquentes  (en général quelques tables).</p>
<p>Les objets sont créé dans un schéma SQL de nom S_ADT et la table à scruter est la table S_ADT.T_MAJ.</p>
<p>Voici l&rsquo;ensemble du code pour se faire&#8230;</p>
<p><strong>1 &#8211; LA TABLE</strong></p>
<p>Pour recueillir les données tracées</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> SCHEMA S_ADT<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> T_MAJ<br />
<span style="color: #66cc66;">&#40;</span>MAJ_ID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">BIGINT</span> <span style="color: #993333; font-weight: bold;">IDENTITY</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_DHU &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DATETIME2 <span style="color: #993333; font-weight: bold;">DEFAULT</span> SYSUTCDATETIME<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_DHL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DATETIME2 <span style="color: #993333; font-weight: bold;">DEFAULT</span> SYSDATETIME<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_CNX &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sysname <span style="color: #993333; font-weight: bold;">DEFAULT</span> &nbsp; SYSTEM_USER<span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_USR &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sysname <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">USER</span><span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_APPLICATION &nbsp; &nbsp;NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_HOST &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_WINLOGIN &nbsp; &nbsp; &nbsp; NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_WINDOMAIN &nbsp; &nbsp; &nbsp;NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_SCHEMA &nbsp; &nbsp; &nbsp; &nbsp; NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_OBJECT &nbsp; &nbsp; &nbsp; &nbsp; NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_TYPE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">CHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp;MAJ_DATA &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XML<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p><strong>2 &#8211; LE TRIGGER</strong></p>
<p>Exemple de création de trigger sur une table pour tracer tous les événements</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TRIGGER</span> E_auditTrail_???<br />
<span style="color: #993333; font-weight: bold;">ON</span> ???<br />
<span style="color: #993333; font-weight: bold;">FOR</span> <span style="color: #993333; font-weight: bold;">INSERT</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">UPDATE</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">DELETE</span><br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #993333; font-weight: bold;">SET</span> NOCOUNT <span style="color: #993333; font-weight: bold;">ON</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @S sysname<span style="color: #66cc66;">,</span> @O sysname<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; @A NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @H NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @L NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @D NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @S <span style="color: #66cc66;">=</span> s<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span> @O <span style="color: #66cc66;">=</span> o<span style="color: #66cc66;">.</span>name<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>objects <span style="color: #993333; font-weight: bold;">AS</span> o<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>schemas <span style="color: #993333; font-weight: bold;">AS</span> s<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> o<span style="color: #66cc66;">.</span>schema_id <span style="color: #66cc66;">=</span> s<span style="color: #66cc66;">.</span>schema_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>objects <span style="color: #993333; font-weight: bold;">AS</span> t<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> t<span style="color: #66cc66;">.</span>parent_object_id <span style="color: #66cc66;">=</span> o<span style="color: #66cc66;">.</span>object_id<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;t<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> @@PROCID;<br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @A program_name<span style="color: #66cc66;">,</span> @H host_name<span style="color: #66cc66;">,</span> @L login_name<span style="color: #66cc66;">,</span> @D domain_name <br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>dm_exec_sessions<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;session_id <span style="color: #66cc66;">=</span> @@SPID;<br />
<br />
<span style="color: #808080; font-style: italic;">-- cas d'insertion</span><br />
<span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> deleted<span style="color: #66cc66;">&#41;</span> <br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> S_ADT<span style="color: #66cc66;">.</span>T_MAJ <span style="color: #66cc66;">&#40;</span>MAJ_SCHEMA<span style="color: #66cc66;">,</span> MAJ_OBJECT<span style="color: #66cc66;">,</span> MAJ_TYPE<span style="color: #66cc66;">,</span> MAJ_DATA<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MAJ_APPLICATION<span style="color: #66cc66;">,</span> MAJ_HOST<span style="color: #66cc66;">,</span> MAJ_WINLOGIN<span style="color: #66cc66;">,</span> MAJ_WINDOMAIN<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SELECT</span> @S<span style="color: #66cc66;">,</span> @O<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'I'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> inserted <span style="color: #993333; font-weight: bold;">FOR</span> XML AUTO<span style="color: #66cc66;">,</span> ELEMENTS<span style="color: #66cc66;">,</span> ROOT<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'insert'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">BINARY</span> BASE64<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> XML<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @A<span style="color: #66cc66;">,</span> @H<span style="color: #66cc66;">,</span> @L<span style="color: #66cc66;">,</span> @D;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">RETURN</span>;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">-- cas de la suppression</span><br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
<span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> inserted<span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> S_ADT<span style="color: #66cc66;">.</span>T_MAJ <span style="color: #66cc66;">&#40;</span>MAJ_SCHEMA<span style="color: #66cc66;">,</span> MAJ_OBJECT<span style="color: #66cc66;">,</span> MAJ_TYPE<span style="color: #66cc66;">,</span> MAJ_DATA<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MAJ_APPLICATION<span style="color: #66cc66;">,</span> MAJ_HOST<span style="color: #66cc66;">,</span> MAJ_WINLOGIN<span style="color: #66cc66;">,</span> MAJ_WINDOMAIN<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SELECT</span> @S<span style="color: #66cc66;">,</span> @O<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'D'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> deleted <span style="color: #993333; font-weight: bold;">FOR</span> XML AUTO<span style="color: #66cc66;">,</span> ELEMENTS<span style="color: #66cc66;">,</span> ROOT<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'delete'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">BINARY</span> BASE64<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> XML<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @A<span style="color: #66cc66;">,</span> @H<span style="color: #66cc66;">,</span> @L<span style="color: #66cc66;">,</span> @D;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">RETURN</span>;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">-- cas de la modification</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> S_ADT<span style="color: #66cc66;">.</span>T_MAJ <span style="color: #66cc66;">&#40;</span>MAJ_SCHEMA<span style="color: #66cc66;">,</span> MAJ_OBJECT<span style="color: #66cc66;">,</span> MAJ_TYPE<span style="color: #66cc66;">,</span> MAJ_DATA<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MAJ_APPLICATION<span style="color: #66cc66;">,</span> MAJ_HOST<span style="color: #66cc66;">,</span> MAJ_WINLOGIN<span style="color: #66cc66;">,</span> MAJ_WINDOMAIN<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SELECT</span> @S<span style="color: #66cc66;">,</span> @O<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'I'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'OLD'</span> <span style="color: #993333; font-weight: bold;">AS</span> _old_<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> deleted <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'NEW'</span> <span style="color: #993333; font-weight: bold;">AS</span> _new_<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> inserted<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> _auditTrail_<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FOR</span> XML AUTO<span style="color: #66cc66;">,</span> ELEMENTS<span style="color: #66cc66;">,</span> ROOT<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'update'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">BINARY</span> BASE64<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> XML<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @A<span style="color: #66cc66;">,</span> @H<span style="color: #66cc66;">,</span> @L<span style="color: #66cc66;">,</span> @D;<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p>Pour vous faciliter la tâche, quelques scripts complémentaires&#8230;</p>
<p><strong>3 &#8211; Un batch de création des triggers sur toutes les tables&#8230;</strong></p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">DECLARE</span> @<span style="color: #993333; font-weight: bold;">SQL</span> NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <br />
<span style="color: #ff0000;">'CREATE TRIGGER E_auditTrail_~1<br />
ON ~2<br />
FOR INSERT, UPDATE, DELETE<br />
AS<br />
SET NOCOUNT ON;<br />
<br />
DECLARE @S sysname, @O sysname, <br />
&nbsp; &nbsp; &nbsp; &nbsp; @A NVARCHAR(128), @H NVARCHAR(128), @L NVARCHAR(128), @D NVARCHAR(128);<br />
<br />
SELECT @S = s.name, @O = o.name<br />
FROM &nbsp; sys.objects AS o<br />
&nbsp; &nbsp; &nbsp; &nbsp;JOIN sys.schemas AS s<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ON o.schema_id = s.schema_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;JOIN sys.objects AS t<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ON t.parent_object_id = o.object_id<br />
WHERE &nbsp;t.object_id = @@PROCID;<br />
<br />
SELECT @A program_name, @H host_name, @L login_name, @D domain_name <br />
FROM &nbsp; sys.dm_exec_sessions<br />
WHERE &nbsp;session_id = @@SPID;<br />
<br />
IF NOT EXISTS(SELECT * FROM deleted) --&gt; cas d'</span><span style="color: #ff0000;">'insertion<br />
BEGIN<br />
&nbsp; &nbsp;INSERT INTO S_ADT.T_MAJ (MAJ_SCHEMA, MAJ_OBJECT, MAJ_TYPE, MAJ_DATA,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MAJ_APPLICATION, MAJ_HOST, MAJ_WINLOGIN, MAJ_WINDOMAIN)<br />
&nbsp; &nbsp;SELECT @S, @O, '</span><span style="color: #ff0000;">'I'</span><span style="color: #ff0000;">', <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CAST((SELECT * FROM inserted FOR XML AUTO, ELEMENTS, ROOT('</span><span style="color: #ff0000;">'insert'</span><span style="color: #ff0000;">'), BINARY BASE64) AS XML),<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @A, @H, @L, @D;<br />
&nbsp; &nbsp;RETURN;<br />
END;<br />
<br />
BEGIN<br />
IF NOT EXISTS(SELECT * FROM inserted) --&gt; cas de la suppression<br />
&nbsp; &nbsp;INSERT INTO S_ADT.T_MAJ (MAJ_SCHEMA, MAJ_OBJECT, MAJ_TYPE, MAJ_DATA,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MAJ_APPLICATION, MAJ_HOST, MAJ_WINLOGIN, MAJ_WINDOMAIN)<br />
&nbsp; &nbsp;SELECT @S, @O, '</span><span style="color: #ff0000;">'D'</span><span style="color: #ff0000;">', <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CAST((SELECT * FROM deleted FOR XML AUTO, ELEMENTS, ROOT('</span><span style="color: #ff0000;">'delete'</span><span style="color: #ff0000;">'), BINARY BASE64) AS XML),<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @A, @H, @L, @D;<br />
&nbsp; &nbsp;RETURN;<br />
END;<br />
<br />
-- cas de la modif<br />
&nbsp; &nbsp;INSERT INTO S_ADT.T_MAJ (MAJ_SCHEMA, MAJ_OBJECT, MAJ_TYPE, MAJ_DATA,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MAJ_APPLICATION, MAJ_HOST, MAJ_WINLOGIN, MAJ_WINDOMAIN)<br />
&nbsp; &nbsp;SELECT @S, @O, '</span><span style="color: #ff0000;">'I'</span><span style="color: #ff0000;">', <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CAST((SELECT * <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FROM &nbsp; (SELECT '</span><span style="color: #ff0000;">'OLD'</span><span style="color: #ff0000;">' AS _old_, * FROM deleted <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UNION ALL<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SELECT '</span><span style="color: #ff0000;">'NEW'</span><span style="color: #ff0000;">' AS _new_, * FROM inserted) AS _auditTrail_<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FOR XML AUTO, ELEMENTS, ROOT('</span><span style="color: #ff0000;">'update'</span><span style="color: #ff0000;">'), BINARY BASE64) AS XML),<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @A, @H, @L, @D;'</span><br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @T NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">261</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @N <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">38</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> @CMD NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> C CURSOR <br />
<span style="color: #993333; font-weight: bold;">LOCAL</span> FORWARD_ONLY STATIC READ_ONLY<br />
<span style="color: #993333; font-weight: bold;">FOR</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'['</span> <span style="color: #66cc66;">+</span> TABLE_SCHEMA <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'].['</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">TABLE_NAME</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">']'</span> <span style="color: #993333; font-weight: bold;">AS</span> T<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>NEWID<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">38</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'-'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'_'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'-'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'_'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> N<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; INFORMATION_SCHEMA<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">TABLES</span> <span style="color: #993333; font-weight: bold;">AS</span> T<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;TABLE_TYPE <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'BASE TABLE'</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AND</span> &nbsp; &nbsp;TABLE_SCHEMA &nbsp;<span style="color: #ff0000;">'S_ADT'</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AND</span> &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">EXISTS</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; INFORMATION_SCHEMA<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">COLUMNS</span> <span style="color: #993333; font-weight: bold;">AS</span> C<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;T<span style="color: #66cc66;">.</span>TABLE_SCHEMA <span style="color: #66cc66;">=</span> C<span style="color: #66cc66;">.</span>TABLE_SCHEMA<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;T<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">TABLE_NAME</span> <span style="color: #66cc66;">=</span> C<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">TABLE_NAME</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;C<span style="color: #66cc66;">.</span>DATA_TYPE <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'text'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'ntext'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'image'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'xml'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'geometry'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'geography'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">OPEN</span> C;<br />
FETCH C <span style="color: #993333; font-weight: bold;">INTO</span> @T<span style="color: #66cc66;">,</span> @N;<br />
WHILE @@FETCH_STATUS <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
<span style="color: #993333; font-weight: bold;">BEGIN</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SET</span> @CMD <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">SQL</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'~1'</span><span style="color: #66cc66;">,</span> @N<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'~2'</span><span style="color: #66cc66;">,</span> @T<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">EXEC</span> <span style="color: #66cc66;">&#40;</span>@CMD<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;FETCH C <span style="color: #993333; font-weight: bold;">INTO</span> @T<span style="color: #66cc66;">,</span> @N;<br />
<span style="color: #993333; font-weight: bold;">END</span>;<br />
CLOSE C;<br />
DEALLOCATE C;<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p><strong>4 &#8211; Un batch de suppression de tous les triggers de tracabilité mis en place</strong></p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">DECLARE</span> @<span style="color: #993333; font-weight: bold;">SQL</span> NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> N<span style="color: #ff0000;">''</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">=</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'DROP TRIGGER ['</span> <span style="color: #66cc66;">+</span> s<span style="color: #66cc66;">.</span>name <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'].'</span> <span style="color: #66cc66;">+</span> d<span style="color: #66cc66;">.</span>name <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">';'</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>triggers <span style="color: #993333; font-weight: bold;">AS</span> d<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>objects <span style="color: #993333; font-weight: bold;">AS</span> o<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> d<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> o<span style="color: #66cc66;">.</span>object_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>schemas <span style="color: #993333; font-weight: bold;">AS</span> s<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> o<span style="color: #66cc66;">.</span>schema_id <span style="color: #66cc66;">=</span> s<span style="color: #66cc66;">.</span>schema_id<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;d<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'E?_auditTrail?_%'</span> ESCAPE <span style="color: #ff0000;">'?'</span><br />
&nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;LEN<span style="color: #66cc66;">&#40;</span>d<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">49</span><br />
&nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;<span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">RIGHT</span><span style="color: #66cc66;">&#40;</span>d<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">36</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'_'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">LIKE</span> REPLICATE<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'[0-9A-F]'</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">EXEC</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">SQL</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p><strong>5 &#8211; Un batch de désactivation des déclencheurs de traçabilité</strong></p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">DECLARE</span> @<span style="color: #993333; font-weight: bold;">SQL</span> NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> N<span style="color: #ff0000;">''</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">=</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'DISABLE TRIGGER ['</span> <span style="color: #66cc66;">+</span> s<span style="color: #66cc66;">.</span>name <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'].'</span> <span style="color: #66cc66;">+</span> d<span style="color: #66cc66;">.</span>name <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">' ON ['</span> <span style="color: #66cc66;">+</span> s<span style="color: #66cc66;">.</span>name <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'].['</span> <span style="color: #66cc66;">+</span> t<span style="color: #66cc66;">.</span>name &nbsp;<span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'];'</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>triggers <span style="color: #993333; font-weight: bold;">AS</span> d<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>objects <span style="color: #993333; font-weight: bold;">AS</span> o<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> d<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> o<span style="color: #66cc66;">.</span>object_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>schemas <span style="color: #993333; font-weight: bold;">AS</span> s<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> o<span style="color: #66cc66;">.</span>schema_id <span style="color: #66cc66;">=</span> s<span style="color: #66cc66;">.</span>schema_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>objects <span style="color: #993333; font-weight: bold;">AS</span> t<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> d<span style="color: #66cc66;">.</span>parent_id <span style="color: #66cc66;">=</span> t<span style="color: #66cc66;">.</span>object_id<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;d<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'E?_auditTrail?_%'</span> ESCAPE <span style="color: #ff0000;">'?'</span><br />
&nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;LEN<span style="color: #66cc66;">&#40;</span>d<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">49</span><br />
&nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;<span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">RIGHT</span><span style="color: #66cc66;">&#40;</span>d<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">36</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'_'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">LIKE</span> REPLICATE<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'[0-9A-F]'</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">EXEC</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">SQL</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p><strong>6 &#8211; Un batch de réactivation des déclencheurs de tracabilité</strong></p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">DECLARE</span> @<span style="color: #993333; font-weight: bold;">SQL</span> NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> N<span style="color: #ff0000;">''</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">=</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'ENABLE TRIGGER ['</span> <span style="color: #66cc66;">+</span> s<span style="color: #66cc66;">.</span>name <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'].'</span> <span style="color: #66cc66;">+</span> d<span style="color: #66cc66;">.</span>name <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">' ON ['</span> <span style="color: #66cc66;">+</span> s<span style="color: #66cc66;">.</span>name <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'].['</span> <span style="color: #66cc66;">+</span> t<span style="color: #66cc66;">.</span>name &nbsp;<span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'];'</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>triggers <span style="color: #993333; font-weight: bold;">AS</span> d<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>objects <span style="color: #993333; font-weight: bold;">AS</span> o<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> d<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> o<span style="color: #66cc66;">.</span>object_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>schemas <span style="color: #993333; font-weight: bold;">AS</span> s<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> o<span style="color: #66cc66;">.</span>schema_id <span style="color: #66cc66;">=</span> s<span style="color: #66cc66;">.</span>schema_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>objects <span style="color: #993333; font-weight: bold;">AS</span> t<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> d<span style="color: #66cc66;">.</span>parent_id <span style="color: #66cc66;">=</span> t<span style="color: #66cc66;">.</span>object_id<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;d<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'E?_auditTrail?_%'</span> ESCAPE <span style="color: #ff0000;">'?'</span><br />
&nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;LEN<span style="color: #66cc66;">&#40;</span>d<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">49</span><br />
&nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;<span style="color: #993333; font-weight: bold;">REPLACE</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">RIGHT</span><span style="color: #66cc66;">&#40;</span>d<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">36</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'_'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">LIKE</span> REPLICATE<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'[0-9A-F]'</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">32</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">EXEC</span> <span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">SQL</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p><strong>NOTA :</strong></p>
<p>Conseil important : si vous avez attribué des privilèges très fermés, il faut autoriser les utilisateurs à voir les DMV d&rsquo;état du serveur pour recueillir les données système, sinon les déclencheurs vont planter.pour cela vous devez attribuer le privilège &laquo;&nbsp;VIEW SERVER STATE&nbsp;&raquo; à tous les utilisateurs de la base à travers leur compte de connexion.<br />
Vous pouvez faire ceci à l&rsquo;aide du script SQL suivant :</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">DECLARE</span> @<span style="color: #993333; font-weight: bold;">SQL</span> NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> N<span style="color: #ff0000;">'USE master;'</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">=</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'GRANT VIEW SERVER STATE TO ['</span> <span style="color: #66cc66;">+</span> L<span style="color: #66cc66;">.</span>name <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'];'</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>database_principals <span style="color: #993333; font-weight: bold;">AS</span> U<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">INNER</span> <span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>server_principals <span style="color: #993333; font-weight: bold;">AS</span> L<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">ON</span> L<span style="color: #66cc66;">.</span>sid <span style="color: #66cc66;">=</span> U<span style="color: #66cc66;">.</span>sid<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;L<span style="color: #66cc66;">.</span>name &nbsp;<span style="color: #ff0000;">'sa'</span><br />
<span style="color: #993333; font-weight: bold;">AND</span> &nbsp; &nbsp;U<span style="color: #66cc66;">.</span>name <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'dbo'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'INFORMATION_SCHEMA'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'sys'</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">EXEC</span> &nbsp;<span style="color: #66cc66;">&#40;</span>@<span style="color: #993333; font-weight: bold;">SQL</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p><strong>LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE</strong><br />
<a href="http://sqlpro.developpez.com/_fichierSQL/AuditTrail.txt" title="Fichier du code" target="_blank">Le code !</a></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Frédéric Brouard, alias SQLpro, ARCHITECTE DE DONNÉES<br />
Expert &nbsp;S.G.B.D &nbsp;relationnelles &nbsp; et &nbsp; langage &nbsp;S.Q.L<br />
Moste &nbsp;Valuable &nbsp;Professionnal &nbsp;Microsoft &nbsp;SQL Server<br />
Société SQLspot &nbsp;: &nbsp;modélisation, conseil, formation,<br />
optimisation, &nbsp;audit, &nbsp;tuning, &nbsp;administration &nbsp;SGBDR<br />
Enseignant: CNAM PACA, ISEN Toulon, CESI Aix en Prov.</div></div>
<p>L&rsquo;entreprise <a href="http://www.sqlspot.com">SQL Spot</a><br />
<strong>Le site web sur le </strong><a href="http://sqlpro.developpez.com/">SQL et les SGBDR</a></p>
<p><img src="http://blog.developpez.com/media/Microsoft_MVP_logo_vertical Brouard 400.jpg" width="400" height="135" alt="MVP Microsoft SQL
Server" /></p>
<div id="attachment_590" style="width: 548px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg"><img src="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg" alt="Développez et administrez pour la performance avec SQL Server 2014" width="538" height="652" class="size-full wp-image-590" /></a><p class="wp-caption-text">Développez et administrez pour la performance avec SQL Server 2014</p></div>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Déblocage d&#8217;une instance SQL Server bloquée</title>
		<link>https://blog.developpez.com/sqlpro/p13127/ms-sql-server/sql-server-2005/deblocage-dune-instance-bloquee</link>
		<comments>https://blog.developpez.com/sqlpro/p13127/ms-sql-server/sql-server-2005/deblocage-dune-instance-bloquee#comments</comments>
		<pubDate>Thu, 12 Jan 2017 14:12:46 +0000</pubDate>
		<dc:creator><![CDATA[SQLpro]]></dc:creator>
				<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server 2014]]></category>
		<category><![CDATA[SQL Server 2016]]></category>
		<category><![CDATA[attente]]></category>
		<category><![CDATA[blocage]]></category>
		<category><![CDATA[SQL server]]></category>
		<category><![CDATA[verrou]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=727</guid>
		<description><![CDATA[Il arrive, souvent à cause d&#8217;un mauvais développement (voir en gras italique plus loin), qu&#8217;un serveur SQL se bloque du fait des verrous. Une session en bloque autre qui en bloque plusieurs autres&#8230; et c&#8217;est l&#8217;effet boule de neige. La plupart du temps, les développeurs ou pseudo DBA réagissent mal en relaçant le service SQL [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Il arrive, souvent à cause d&rsquo;un mauvais développement (voir en <em><strong>gras italique</strong></em> plus loin), qu&rsquo;un serveur SQL se bloque du fait des verrous. Une session en bloque autre qui en bloque plusieurs autres&#8230; et c&rsquo;est l&rsquo;effet boule de neige. La plupart du temps, les développeurs ou pseudo DBA réagissent mal en relaçant le service SQL Server ce qui fait perdre une grande partie des mises à jour demandées, empêchent les utilisateurs de travailler et pour peu qu&rsquo;une longue transaction n&rsquo;ai pas encore enregistrées les données, rend indisponible la base après le redémarrage du serveur, souvent pendant de longues minutes&#8230; Tout cela pouvant être évité avec un peu d&rsquo;analyse et surtout la bonne requête que je vous présente, destinées à débloquer sans frustrer !<br />
<span id="more-727"></span><br />
Un blocage est une situation normale dans une base de données, comme dans la vie courante. Les feux rouges, l&rsquo;accès à des toilettes, les portiques de sécurité des aéroports, sont autant de points ou vous pouvez vous retrouvé bloqué pendant un certain temps, et cela est normal et fait partie de la vie. Les bases de données devant assurer la cohérence entre ceux qui lisent les données et ceux qui les écrivent, doivent poser des verrous afin de bloquer les utilisateurs concurrents dans certaines situations. Le principe est le suivant :</p>
<ul>
<li>un utilisateur qui <strong>lit</strong> des données, doit bloquer l&rsquo;accès aux utilisateurs qui veulent modifier les mêmes données, mais n&rsquo;empêchent pas d&rsquo;autres utilisateurs de lire en même temps les mêmes données. Dans ce cas la base pose un <strong>verrou partagé</strong> (shared), qui peut être pessimiste (posé préventivement) ou optimiste (actionné après coup).</li>
<li>un utilisateur qui <strong>écrit</strong> des données doit empêcher toute lecture et toute écriture concurrente. On parle alors de <strong>verrou exclusif</strong>.</li>
</ul>
<p>Si une session doit faire différentes modification au sein d&rsquo;une même transaction, les verrous vont être maintenus jusqu&rsquo;à ce qu&rsquo;intervienne la finalisation de la transaction, par un COMMIT (validation) ou un ROLLBACK (annulation). Pendant ce temps, si d&rsquo;autres utilisateurs doivent accéder à ces données, il faut qu&rsquo;ils patientent. C&rsquo;est la notion de blocage. Un blocage est donc une situation normale dans une base de données, comme c&rsquo;est le cas de madame Michu qui patiente dans sa 4L au feu rouge pour laisser passer les automobiliste de la voie latérale.<br />
Ce qui n&rsquo;est pas toujours normal, c&rsquo;est un blocage qui dure, longtemps, voire, très, très longtemps&#8230; mais jamais de manière éternelle, car la situation doit se débouquer à la fin du traitement bloquant, c&rsquo;est une certitude !<br />
Dans ce cas, il est possible que d&rsquo;autres utilisateurs se retrouvent bloqués, alors que même, ils ont déjà entamé des modification de données, susceptible d&rsquo;entrainer d&rsquo;autres blocages ! D’où l&rsquo;effet boule de neige&#8230; Là encore cette situation peut être normale, si le bloqueur de tête met à jour une très importante quantité de données alors que beaucoup d&rsquo;utilisateurs sont en train de parcourir ces mêmes données ! Mais dans bien des cas, c&rsquo;est une situation anormale, généralement due à des mauvaises pratiques de développement, tel que <em><strong>des transaction démarré côté client, des transactions avec interactions de l&rsquo;utilisateur, le forçage de verrous (les SGBDR se débrouillent généralement mieux tous seuls), des transactions non finalisées, l&rsquo;usage d&rsquo;un niveau d&rsquo;isolation inappropriée, l&rsquo;utilisation de code itératif eu lieu de code ensembliste, l&rsquo;oubli de poser les index adéquats, l&rsquo;utilisation de tables obèses, le recours systématique aux curseur, l’usage immodéré des déclencheurs</strong></em>, etc !<br />
Bref, la question est, en présence d&rsquo;un tel blocage, comment faire ?</p>
<p>LA SOLUTION</p>
<p>Elle consiste tout simplement à recherche qui bloque qui, et remonter jusqu&rsquo;à bloqueur de tête pour le forcer à abandonner son traitement. Problème, cela nécessite de parcourir récursivement l&rsquo;arbre de blocage de blocage&#8230; Second problème, il peut y avoir plusieurs chaines de blocage !<br />
La DMV sys.dm_exec_requests permet de savoir qui bloque qui dans l&rsquo;état actuel de l&rsquo;instance. Les deux colonnes qui nous intéressent pour ce faire sont :</p>
<ul>
<li>session_id : l&rsquo;identifiant de la session en cours</li>
<li>blocking_session_id : l&rsquo;identifiant de la session qui bloque</li>
</ul>
<p><div id="attachment_737" style="width: 688px" class="wp-caption aligncenter"><a href="http://blog.developpez.com/sqlpro/files/2017/01/blocage_session_chaine_arbre_sql_server1.jpg"><img src="http://blog.developpez.com/sqlpro/files/2017/01/blocage_session_chaine_arbre_sql_server1.jpg" alt="Différentes chaines de blocage de sessions dans SQL Server (en fait des arbres)" width="678" height="403" class="size-full wp-image-737" /></a><p class="wp-caption-text">Différentes chaines de blocage de sessions dans SQL Server (en fait des arbres)</p></div><br />
Dans la figure ci avant, les sessions 53 et 57 sont les leaders et bloquent respectivement :</p>
<ul>
<li>pour le 53, 11 session avec une profondeur maximale de 5</li>
<li>pour le 57, 3 sessions avec une profondeur maximale de 3</li>
</ul>
<p>Il faudrait débloquer en priorité le 53, attendre quelques instants pour voir si cela ne débloque pas le 57.</p>
<p>À partir de ces seules deux informations (session_id et blocking_session_id), nous pouvons remonter jusqu’aux bloqueurs de tête. L&rsquo;écriture de cette requête doit procéder en deux temps :<br />
rechercher les bloquers de tête, c&rsquo;est à dire ceux qui ne sont pas bloqués par d&rsquo;autres, mais qui en bloque d&rsquo;autres. Une fois ces &laquo;&nbsp;leaders&nbsp;&raquo; trouvés, il suffit de descendre dans l&rsquo;arbre de blocage pour comptabiliser le nombre de sessions bloquées et la profondeur du blocage.<br />
Il suffit alors de tuer les &laquo;&nbsp;lead blockers&nbsp;&raquo; les plus prégnants, c&rsquo;est à dire en général ceux qui bloquent le plus de session, ceux qui ont la plus grande profondeur de blocage, ceux dont le temps de traitement a été le plus long, ceux ayant consommé le plus d&rsquo;IO, ceux ayant la requête la moins critique&#8230;. À vous de choisir !</p>
<p>La requête !</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">WITH</span><br />
T_SESSION <span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #66cc66;">&#40;</span><br />
<span style="color: #808080; font-style: italic;">-- on récupère les sessions en cours des utilisateurs</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> session_id<span style="color: #66cc66;">,</span> blocking_session_id<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>dm_exec_requests <span style="color: #993333; font-weight: bold;">AS</span> tout<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;session_id <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">50</span><br />
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
T_LEAD <span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #66cc66;">&#40;</span><br />
<span style="color: #808080; font-style: italic;">-- on recherche les bloqueurs de tête</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> session_id<span style="color: #66cc66;">,</span> blocking_session_id<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; T_SESSION <span style="color: #993333; font-weight: bold;">AS</span> tout<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;session_id <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">50</span><br />
&nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;blocking_session_id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
&nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;<span style="color: #993333; font-weight: bold;">EXISTS</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; T_SESSION <span style="color: #993333; font-weight: bold;">AS</span> tin<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;tin<span style="color: #66cc66;">.</span>blocking_session_id <span style="color: #66cc66;">=</span> tout<span style="color: #66cc66;">.</span>session_id<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
T_CHAIN <span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #66cc66;">&#40;</span><br />
<span style="color: #808080; font-style: italic;">-- requête récursive pour trouver les chaines de blocage</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> session_id <span style="color: #993333; font-weight: bold;">AS</span> lead_session_id<span style="color: #66cc66;">,</span> session_id<span style="color: #66cc66;">,</span> blocking_session_id<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AS</span> p<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; T_LEAD<br />
<span style="color: #993333; font-weight: bold;">UNION</span> &nbsp;<span style="color: #993333; font-weight: bold;">ALL</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> C<span style="color: #66cc66;">.</span>lead_session_id<span style="color: #66cc66;">,</span> S<span style="color: #66cc66;">.</span>session_id<span style="color: #66cc66;">,</span> S<span style="color: #66cc66;">.</span>blocking_session_id<span style="color: #66cc66;">,</span> p<span style="color: #66cc66;">+</span><span style="color: #cc66cc;">1</span> <br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; T_CHAIN <span style="color: #993333; font-weight: bold;">AS</span> C<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> T_SESSION <span style="color: #993333; font-weight: bold;">AS</span> S<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> C<span style="color: #66cc66;">.</span>session_id <span style="color: #66cc66;">=</span> S<span style="color: #66cc66;">.</span>blocking_session_id<br />
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
T_WEIGHT <span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #66cc66;">&#40;</span><br />
<span style="color: #808080; font-style: italic;">-- calculs finaux</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> lead_session_id <span style="color: #993333; font-weight: bold;">AS</span> LEAD_BLOCKER<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AS</span> BLOCKED_SESSION_COUNT<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span>p<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AS</span> BLOCKED_DEEP<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'KILL '</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>lead_session_id <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">';'</span> <span style="color: #993333; font-weight: bold;">AS</span> SQL_CMD<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; T_CHAIN<br />
<span style="color: #993333; font-weight: bold;">GROUP</span> &nbsp;<span style="color: #993333; font-weight: bold;">BY</span> lead_session_id<br />
<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> T<span style="color: #66cc66;">.*,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;DB_NAME<span style="color: #66cc66;">&#40;</span>r<span style="color: #66cc66;">.</span>database_id<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> database_name<span style="color: #66cc66;">,</span> host_name<span style="color: #66cc66;">,</span> program_name<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;nt_user_name<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;q<span style="color: #66cc66;">.</span>text <span style="color: #993333; font-weight: bold;">AS</span> sql_command<span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;DATEDIFF<span style="color: #66cc66;">&#40;</span>ms<span style="color: #66cc66;">,</span> last_request_start_time<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">COALESCE</span><span style="color: #66cc66;">&#40;</span>last_request_end_time<span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> duration_ms<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;s<span style="color: #66cc66;">.</span>open_transaction_count<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;r<span style="color: #66cc66;">.</span>cpu_time<span style="color: #66cc66;">,</span> r<span style="color: #66cc66;">.</span>reads<span style="color: #66cc66;">,</span> r<span style="color: #66cc66;">.</span>writes<span style="color: #66cc66;">,</span> r<span style="color: #66cc66;">.</span>logical_reads<span style="color: #66cc66;">,</span> r<span style="color: #66cc66;">.</span>total_elapsed_time <br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; T_WEIGHT <span style="color: #993333; font-weight: bold;">AS</span> T<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>dm_exec_sessions <span style="color: #993333; font-weight: bold;">AS</span> s <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> T<span style="color: #66cc66;">.</span>LEAD_BLOCKER <span style="color: #66cc66;">=</span> s<span style="color: #66cc66;">.</span>session_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>dm_exec_requests <span style="color: #993333; font-weight: bold;">AS</span> r <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> s<span style="color: #66cc66;">.</span>session_id <span style="color: #66cc66;">=</span> r<span style="color: #66cc66;">.</span>session_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">OUTER</span> APPLY sys<span style="color: #66cc66;">.</span>dm_exec_sql_text<span style="color: #66cc66;">&#40;</span>sql_handle<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> q<br />
<span style="color: #993333; font-weight: bold;">ORDER</span> &nbsp;<span style="color: #993333; font-weight: bold;">BY</span> BLOCKED_SESSION_COUNT <span style="color: #993333; font-weight: bold;">DESC</span><span style="color: #66cc66;">,</span> BLOCKED_DEEP <span style="color: #993333; font-weight: bold;">DESC</span>;</div></div>
<p>Cette requête présente les sessions bloquant les autres sessions, en déterminant la session à la tête d&rsquo;une chaine de blocage (LEAD_BLOCKER), avec le nombre de sessions bloquées (BLOCKED_SESSION_COUNT) et la longueur maximale de la chaîne de blocage (BLOCKED_DEEP). Ceci permet de déterminer quelle session est à annuler en priorité en cas de blocage intempestif et d&rsquo;utiliser la commande KILL (SQL_CMD) qui termine la session fautive en forçant un ROLLBACK. Les autres colonnes donnent des informations sur la requête qui a bloqué (texte SQL, base, origine du code, métriques diverses&#8230;) </p>
<p><strong>ATTENTION </strong><br />
Ne pas confondre blocage et interblocage (ou étreinte fatale, ou encore verrou mortel&#8230;). Dans un interblocage, une session bloque des ressources qu&rsquo;une autre session veut obtenir, tandis que cette même session demandeuse a bloqué des objets que la première a besoin. Dans ce cas, nous pouvons atteindre éternellement, car le blocage ne peut être résolu par l&rsquo;attente. Le seul moyen consiste donc à sacrifier une des victimes de ce blocage et lui forçant une annulation de la transaction. Ceci est détecté automatiquement dans les bons SGBDR, qui procèdent automatiquement à l&rsquo;achèvement d&rsquo;une des victimes. Dans SQL Server, le message est intentionnellement significatif : <em>&laquo;&nbsp;La transaction (ID de processus %1!) a été bloquée sur les ressources &#8230; par un autre processus et a été choisie comme victime. Réexécutez la transaction.&nbsp;&raquo;</em></p>
<p><strong>LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE * LE CODE</strong><br />
<a href="http://sqlpro.developpez.com/_fichierSQL/LeadBlocker.txt" title="Fichier du code" target="_blank">Le code !</a></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">Frédéric Brouard, alias SQLpro, ARCHITECTE DE DONNÉES<br />
Expert &nbsp;S.G.B.D &nbsp;relationnelles &nbsp; et &nbsp; langage &nbsp;S.Q.L<br />
Moste &nbsp;Valuable &nbsp;Professionnal &nbsp;Microsoft &nbsp;SQL Server<br />
Société SQLspot &nbsp;: &nbsp;modélisation, conseil, formation,<br />
optimisation, &nbsp;audit, &nbsp;tuning, &nbsp;administration &nbsp;SGBDR<br />
Enseignant: CNAM PACA, ISEN Toulon, CESI Aix en Prov.</div></div>
<p>L&rsquo;entreprise <a href="http://www.sqlspot.com">SQL Spot</a><br />
<strong>Le site web sur le </strong><a href="http://sqlpro.developpez.com/">SQL et les SGBDR</a></p>
<p><img src="http://blog.developpez.com/media/Microsoft_MVP_logo_vertical Brouard 400.jpg" width="400" height="135" alt="MVP Microsoft SQL
Server" /></p>
<div id="attachment_590" style="width: 548px" class="wp-caption alignnone"><a href="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg"><img src="http://blog.developpez.com/sqlpro/files/2015/09/Couverture-livre-SQL-server-Eyrolles.jpg" alt="Développez et administrez pour la performance avec SQL Server 2014" width="538" height="652" class="size-full wp-image-590" /></a><p class="wp-caption-text">Développez et administrez pour la performance avec SQL Server 2014</p></div>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
