<?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 2008</title>
	<atom:link href="https://blog.developpez.com/sqlpro/pcategory/ms-sql-server/sql-server-2008/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>Une peste nommée NOLOCK&#8230; et comment l&#8217;éviter</title>
		<link>https://blog.developpez.com/sqlpro/p13181/langage-sql-norme/une-peste-nommee-nolock-et-comment-leviter</link>
		<comments>https://blog.developpez.com/sqlpro/p13181/langage-sql-norme/une-peste-nommee-nolock-et-comment-leviter#comments</comments>
		<pubDate>Sun, 08 Jul 2018 14:55:07 +0000</pubDate>
		<dc:creator><![CDATA[SQLpro]]></dc:creator>
				<category><![CDATA[Langage SQL (norme)]]></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[ISOLATION]]></category>
		<category><![CDATA[NOLOCK]]></category>
		<category><![CDATA[SNAPSHOT]]></category>
		<category><![CDATA[transaction]]></category>
		<category><![CDATA[verrou]]></category>
		<category><![CDATA[verrouilage]]></category>
		<category><![CDATA[verrous]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=818</guid>
		<description><![CDATA[L&#8217;utilisation du tag de table NOLOCK, hélas largement prisé par de nombreux développeurs, est, la plupart du temps, une absurdité conduisant à des résultats potentiellement faux. Est-il possible de faire autrement pour minimiser les blocages induit par les verrouillages dans SQL Server ? Réponse OUI ! À la suite de cette remarque : &#171;&#160;&#8230;je passe [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>L&rsquo;utilisation du tag de table NOLOCK, hélas largement prisé par de nombreux développeurs, est, la plupart du temps, une absurdité conduisant à des résultats potentiellement faux. Est-il possible de faire autrement pour minimiser les blocages induit par les verrouillages dans SQL Server ? Réponse OUI !<br />
<span id="more-818"></span><br />
À la suite de cette remarque :<br />
<em>&laquo;&nbsp;&#8230;je passe aussi sur le NOLOCK qui est une véritable connerie en production et donne des valeurs fausses !&nbsp;&raquo;</em><br />
Publiée ici :<br />
<a href="https://www.developpez.net/forums/d1870299/bases-donnees/ms-sql-server/developpement/utilisation-excessive-structure-conditionnelle-choix-multiples/#post10343427" rel="noopener" target="_blank"></a><br />
Mon interlocuteur me répondais :<br />
<em>&laquo;&nbsp;Merci pour cette précision. Comme vous abordez le sujet du NOLOCK effectivement j&rsquo;ai constaté certaines fois en production on avait des valeurs totalement erronées, mais je n&rsquo;arrivais pas à me l&rsquo;expliquer. J&rsquo;ai maintenant une piste et je vais creuser du côté du NOLOCK que nous utilisons beaucoup en production sur une base de données CRM 2013. Aussi par quoi pouvons nous remplacer le NOLOCK ??&nbsp;&raquo;</em></p>
<p>Je lui donnais donc l&rsquo;explication suivante&#8230;<br />
<strong><br />
Le &laquo;&nbsp;NOLOCK&nbsp;&raquo; peut parfaitement se remplacer par le niveau d&rsquo;isolation SNAPSHOT</strong> par exemple&#8230;</p>
<p>NOLOCK ne signifie pas &laquo;&nbsp;<em>je ne pose pas de verrou</em>&laquo;&nbsp;, mais plus précisément : &laquo;&nbsp;<em>j&rsquo;ignore qu&rsquo;il y a des verrous et je lit ce que je peut</em>&nbsp;&raquo; (ceci est plus connu dans la littérature professionnelle sous le nom de <em><strong>lecture dans le chaos</strong></em>&#8230;). Vous pouvez donc lire plusieurs fois les mêmes données ou encore ne pas lire certaines.</p>
<p><strong>Quid du mode SNAPSHOT ?</strong></p>
<p>SNAPSHOT fait du verrouillage optimiste pour les lectures. Par défaut SQL Server fait du verrouillage pessimiste, c&rsquo;est à dire que les lectures posent des verrous qui permettent d&rsquo;autres lectures concurrentes (verrous partagés, mode &laquo;&nbsp;SHARE&nbsp;&raquo;), mais empêchent les écritures des autres utilisateurs. Conceptuellement, le verrouillage optimiste (donc via le mode d’isolation SNAPSHOT) fait une copie (si besoin) des données avant de permettre leurs lectures. Comme vous travaillez sur une copie, les utilisateurs concurrent peuvent modifier les vraies ligne sans que cela vous concerne. Bien entendu il peut toujours y avoir des conflits, mais cela réduit considérablement le nombre de verrous bloquants tout en gardant l&rsquo;intégrité et la consistante de la base, ce qui n&rsquo;est pas du tout le cas du NOLOCK.</p>
<p><strong>Alors, à quoi sert le NOLOCK ? </strong></p>
<p>Cela peut servir pour certains cas ou l&rsquo;on désire des résultats dont on se fout de l&rsquo;intégrité ou de la précision. Souvent dans le domaine de la statistique.<br />
par exemple si vous voulez savoir combien les français ont d&rsquo;enfant par foyer et scrutez donc, pour ce faire, une table de 60 millions de lignes (population française), je ne suis pas sûr que dans un camembert représentant la chose, on fasse la distinction des quelques pixels de l&rsquo;imprécision de la 6e décimale du résultat liées à quelques mauvaises lectures de lignes !<br />
On utilise d&rsquo;ailleurs très souvent le NOLOCK pour des tâches d&rsquo;administration systèmes dans SQL Server afin d&rsquo;éviter tout blocage au risque de perdre quelques lignes, ce qui n&rsquo;a pas toujours une importance capitale lorsque l&rsquo;on veut remonter quelques informations concernant les 20% de requêtes les moins performantes !</p>
<p><strong>Mais alors, le SNAPSHOT fait la même chose en mieux ? </strong></p>
<p>Oui et non&#8230; Certes il permet de garantir l&rsquo;intégrité, la consistance, etc&#8230; mais son mécanisme est plus lourd (un peu moins rapide) et consomme des ressources pour générer les copies (SNAPSHOT). Rassurez-vous il est assez malin pour ne faire ces copies qui s&rsquo;il a besoin de les faire&#8230; En d&rsquo;autres termes si aucun utilisateur concurrent veut faire des modif sur le jeu de lignes que vous manipulez en mode SNAPSHOT, aucune copie ne sera générée. Mais si, lorsque vous avez démarré une lecture en mode SNAPSHOT, un utilisateur concurrent veut modifier des lignes, et bien la copie sera réalisée au cours de votre lecture sans que vous vous en aperceviez&#8230; En quelque sorte le système fonctionne à l&rsquo;envers&#8230; Bien évidemment il faut un espace de stockage pour ces copies et c&rsquo;est la base tempdb qui s&rsquo;y colle. Il faut donc, la dimensionner correctement&#8230;<br />
Tiens, à nouveau un travail de DBA !</p>
<p><strong>Des conflits ! Quels conflits ?</strong></p>
<p>J&rsquo;ai précédemment parlé de conflits possible avec l&rsquo;utilisation du mode SNAPSHOT. En effet, si vous voulez modifier des données lors d&rsquo;une transaction en mode d’isolation SNAPSHOT, c&rsquo;est possible, mais ces modifications seront faites sur la copie, et non sur la base originale, et seront reportées sur la base originale au moment du COMMIT. Deux hypothèses se font alors jour :<br />
■ soit aucune ligne visée par vos modifications faites en mode SNAPSHOT n&rsquo;a été modifié dans une autre session directement sur la base, alors la mise à jour est appliquée.<br />
Ou alors :<br />
■ s&rsquo;il existe au moins une ligne qui a été modifiée par une autre session entre temps sur la base originale, alors vos modifications ne pourront être appliquées aux données originales et vont êtres abandonnées.<br />
Dans ce dernier cas vous obtiendrez une message d&rsquo;erreur (3960) assez abscons, qui indique :<br />
<em>&laquo;&nbsp;La transaction d&rsquo;isolement d&rsquo;instantané a été abandonnée en raison d&rsquo;un conflit de mise à jour. &#8230;&nbsp;&raquo;</em></p>
<p>Notez qu&rsquo;il existe un autre tag plus intéressant que le NOLOCK&#8230;. Le <strong>READPAST</strong> !&#8230;  Qui fait presque la même chose, mais ignore tout ce qui est verrouillé !</p>
<p><strong>Les modes d&rsquo;isolation selon la norme SQL</strong></p>
<p>Sachez que le mode d’isolation SNAPSHOT ne fait pas partie de la norme SQL qui n&rsquo;autorise que les modes d&rsquo;isolation suivants :<br />
0 &#8211; READ UNCOMMITTED : lecture des données invalides (non &laquo;&nbsp;committées&nbsp;&raquo;)<br />
1 &#8211; READ COMMITTED : lecture de données valides (ce qui n&rsquo;empêche pas certaines anomalies transactionnelles comme la lecture non répétable, ou l&rsquo;apparition de lignes fantômes)<br />
2 &#8211; REPEATABLE READ : lecture répétable de données (ce qui n&rsquo;empêche pas certaines anomalies transactionnelles comme l&rsquo;apparition de lignes fantômes)<br />
3 &#8211; SERIALIZABLE : lecture en mode série (aucun accès concurrent aux tables lors des mises à jour, ce qui empêche toute anomalies)<br />
Évidemment plus on monte le niveau d’isolation, plus le verrouillage est important et moins les accès concurrents peuvent travailler.<br />
La norme SQL considère que les manipulation des données doivent porter sur des valeurs &laquo;&nbsp;vivantes&nbsp;&raquo;. Hors, en utilisant le mode SNAPSHOT et son versionnement des lignes, ont est appelé à lire potentiellement des valeurs antérieurs des données, ce n&rsquo;est donc pas une lecture de l&rsquo;état actuel des valeurs dans la base&#8230;</p>
<p>Pour informations, des SGBRD comme Oracle, ou PostGreSQL fonctionnent nativement en mode d&rsquo;isolation SNAPSHOT, autrement dit utilisent systématiquement le verrouillage optimiste et ne savent pas faire autres choses (verrouillage pessimiste en particulier) voir ne savent même pas utiliser certains autres niveau d&rsquo;isolation. Par exemple PostGreSQL ne permet pas de faires des transaction en mode READ UNCOMMITTED et Oracle ne permettent pas d&rsquo;utiliser le niveau d&rsquo;isolation REPEATABLE READ&#8230;</p>
<p><strong>Activer le mode s&rsquo;isolation SNAPSHOT</strong></p>
<p>Pour permettre ce mode d&rsquo;isolation, vous devez d&rsquo;abord autoriser la base à utiliser le mode SNAPSHOT. Ce paramétrage est à deux niveaux :</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">ALTER DATABASE { CURRENT | nom_base } SET ALLOW_SNAPSHOT_ISOLATION ON;</div></div>
<p>permet d&rsquo;utiliser à tout moment le mode d’isolation SNAPSHOT qui doit être introduit préalablement par la commande SET TRANSACTION ISOLATION SNAPSHOT ou par un tag de table (SNAPSHOT)<br />
&#8230; et :</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">ALTER DATABASE { CURRENT | nom_base } SET READ_COMMITTED_SNAPSHOT ON;</div></div>
<p>qui place d&rsquo;office toutes les lectures en mode SNAPSHOT (comme le fait Oracle ou PostGreSQL);<br />
<strong>ATTENTION</strong> : pour assurer le versionnement des lignes, SQL Server rajoute à toutes les lignes de toutes les tables une information de version qui est codé sur 14 octets. Ceci peut prendre du temps à mettre en œuvre, en particulier sur de grosses bases.<br />
<strong></p>
<p>Appendice :</strong></p>
<p>Pour une démonstration des effets du NOLOCK : <a href="http://mssqlserver.fr/les-dangers-du-nolock/" rel="noopener" target="_blank">http://mssqlserver.fr/les-dangers-du-nolock/</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>0</slash:comments>
		</item>
		<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>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>Métadonnées de l&#8217;utilisation d&#8217;une colonne</title>
		<link>https://blog.developpez.com/sqlpro/p13155/ms-sql-server/metadonnees-de-lutilisation-dune-colonne</link>
		<comments>https://blog.developpez.com/sqlpro/p13155/ms-sql-server/metadonnees-de-lutilisation-dune-colonne#comments</comments>
		<pubDate>Mon, 23 Oct 2017 07:07:13 +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[colonne]]></category>
		<category><![CDATA[contrainte]]></category>
		<category><![CDATA[index]]></category>
		<category><![CDATA[métadonnées]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=765</guid>
		<description><![CDATA[Certaines modifications de type de données d&#8217;une colonne nécessitent une suppression préalable des contraintes et index. Pour vous y aider, la procédure suivante indique dans quels objets (index ou contraintes) une colonne d&#8217;une table passée en argument est enrôlée. Dans le cas ou vous voudriez changer le type d&#8217;une telle colonne, il faudrait agir manuellement [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Certaines modifications de type de données d&rsquo;une colonne nécessitent une suppression préalable des contraintes et index. Pour vous y aider, la procédure suivante indique dans quels objets (index ou contraintes) une colonne d&rsquo;une table passée en argument est enrôlée.<br />
<span id="more-765"></span></p>
<p>Dans le cas ou vous voudriez changer le type d&rsquo;une telle colonne, il faudrait agir manuellement avec le scénario suivant :</p>
<p>1)	Scripter la création des contraintes par clic droit sur le nom de chacune des contraintes et les supprimer<br />
2)	Scripter la création des index par clic droit sur le nom de chacun des index et les supprimer<br />
3)	Modifier le type de données de la colonne avec la commande ALTER TABLE … ALTER COLUMN …<br />
4)	Recréer les index<br />
5)	Recréer les contraintes</p>
<p>Cette procédure à été créée pour vous y aider. Elle indique pour un couple d’information table/colonne dans quels index ou contrainte cette dernière est enrôlée&#8230;<br />
Cette procédure est créée en tant que procédure système afin d&rsquo;être exploitable dans n&rsquo;importe quelle base de l&rsquo;instance SQL Server.</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> master;<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<span style="color: #993333; font-weight: bold;">IF</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; <span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; sys<span style="color: #66cc66;">.</span>all_objects<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'sp__COLUMN_IS_USED'</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;schema_id <span style="color: #66cc66;">=</span> SCHEMA_ID<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'dbo'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">EXEC</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'ROP PROCEDURE dbo.sp__COLUMN_IS_USED;'</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<span style="color: #808080; font-style: italic;">--===========================================================================--</span><br />
<span style="color: #808080; font-style: italic;">-- METADONNÉES D'UTILISATION D'UNE COLONNE &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --</span><br />
<span style="color: #808080; font-style: italic;">--===========================================================================--</span><br />
<span style="color: #808080; font-style: italic;">-- Fredéric Brouard alias SQLpro &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;http://sqlpro.developpez.com --</span><br />
<span style="color: #808080; font-style: italic;">-- Société SQL SPOT - http://www.sqlspot.com &nbsp; &nbsp; &nbsp; &nbsp;2017-10-13 - version 1.0 --</span><br />
<span style="color: #808080; font-style: italic;">--===========================================================================--</span><br />
<br />
<br />
<span style="color: #808080; font-style: italic;">--▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄--</span><br />
<span style="color: #808080; font-style: italic;">-- PHASE 1 : création de la procédure &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;--</span><br />
<span style="color: #808080; font-style: italic;">--▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀--</span><br />
<br />
<br />
<span style="color: #993333; font-weight: bold;">USE</span> master;<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> dbo<span style="color: #66cc66;">.</span>sp__COLUMN_IS_USED @OBJ NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">261</span><span style="color: #66cc66;">&#41;</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; @COL NVARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">128</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">NULL</span><br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #808080; font-style: italic;">/******************************************************************************<br />
* METADONNÉES D'UTILISATION D'UNE COLONNE &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 dans quels objets est utilisé une &nbsp; &nbsp;*<br />
* colonne d'une table &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; <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-10-13 - 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) ainsi qu'un nom &nbsp;*<br />
* de colonne et renvoie un jeu de données contenant le descriptif des cas &nbsp; &nbsp; *<br />
* d'utilisation de cette colonne dans les index et les contraintes &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(261) : nom de table dont on veut connaître les index &nbsp;*<br />
* &nbsp; &nbsp;@COL nom de la colonne dont on veut connaître l'usage &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 />
* 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;COLUMN_NAME &nbsp; nom de la colonne passée en paramètre &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp;OBJECT_TYPE &nbsp; type d'objet (nature de la contrainte ou INDEX) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp;OBJECT_NAME &nbsp; nom de la contrainte ou de l'index &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br />
<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__COLUMN_IS_USED 'CC', 'Idx' &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 et contrainte de la table CC &nbsp; &nbsp; &nbsp; &nbsp;*<br />
* &nbsp; &nbsp; &nbsp; &nbsp;incluant la colonne Idx &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; &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> T <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> COLUMN_NAME<span style="color: #66cc66;">,</span> CONSTRAINT_TYPE <span style="color: #993333; font-weight: bold;">AS</span> OBJECT_TYPE<span style="color: #66cc66;">,</span> KCU<span style="color: #66cc66;">.</span>CONSTRAINT_NAME <span style="color: #993333; font-weight: bold;">AS</span> OBJECT_NAME <br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; INFORMATION_SCHEMA<span style="color: #66cc66;">.</span>KEY_COLUMN_USAGE <span style="color: #993333; font-weight: bold;">AS</span> KCU<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> INFORMATION_SCHEMA<span style="color: #66cc66;">.</span>TABLE_CONSTRAINTS <span style="color: #993333; font-weight: bold;">AS</span> TC<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> KCU<span style="color: #66cc66;">.</span>TABLE_SCHEMA <span style="color: #66cc66;">=</span> TC<span style="color: #66cc66;">.</span>TABLE_SCHEMA &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AND</span> KCU<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">TABLE_NAME</span> <span style="color: #66cc66;">=</span> TC<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">TABLE_NAME</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AND</span> KCU<span style="color: #66cc66;">.</span>CONSTRAINT_NAME <span style="color: #66cc66;">=</span> TC<span style="color: #66cc66;">.</span>CONSTRAINT_NAME<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;OBJECT_ID<span style="color: #66cc66;">&#40;</span>KCU<span style="color: #66cc66;">.</span>TABLE_SCHEMA <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'.'</span> <span style="color: #66cc66;">+</span> KCU<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">TABLE_NAME</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> OBJECT_ID<span style="color: #66cc66;">&#40;</span>@OBJ<span style="color: #66cc66;">&#41;</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;">REPLACE</span><span style="color: #66cc66;">&#40;</span>RTRIM<span style="color: #66cc66;">&#40;</span>LTRIM<span style="color: #66cc66;">&#40;</span>@COL<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: #66cc66;">=</span> COLUMN_NAME<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: #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>RTRIM<span style="color: #66cc66;">&#40;</span>LTRIM<span style="color: #66cc66;">&#40;</span>@COL<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: #66cc66;">,</span> CONSTRAINT_TYPE <span style="color: #993333; font-weight: bold;">AS</span> OBJECT_TYPE<span style="color: #66cc66;">,</span> CK<span style="color: #66cc66;">.</span>CONSTRAINT_NAME <span style="color: #993333; font-weight: bold;">AS</span> OBJECT_NAME <br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; INFORMATION_SCHEMA<span style="color: #66cc66;">.</span>CHECK_CONSTRAINTS <span style="color: #993333; font-weight: bold;">AS</span> CK<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> INFORMATION_SCHEMA<span style="color: #66cc66;">.</span>TABLE_CONSTRAINTS <span style="color: #993333; font-weight: bold;">AS</span> TC<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ON</span> CK<span style="color: #66cc66;">.</span>CONSTRAINT_SCHEMA <span style="color: #66cc66;">=</span> TC<span style="color: #66cc66;">.</span>CONSTRAINT_SCHEMA &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AND</span> CK<span style="color: #66cc66;">.</span>CONSTRAINT_NAME <span style="color: #66cc66;">=</span> TC<span style="color: #66cc66;">.</span>CONSTRAINT_NAME<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;OBJECT_ID<span style="color: #66cc66;">&#40;</span>TC<span style="color: #66cc66;">.</span>TABLE_SCHEMA <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'.'</span> <span style="color: #66cc66;">+</span> TC<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">TABLE_NAME</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> OBJECT_ID<span style="color: #66cc66;">&#40;</span>@OBJ<span style="color: #66cc66;">&#41;</span> <br />
&nbsp; <span style="color: #993333; font-weight: bold;">AND</span> &nbsp;CHECK_CLAUSE <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'%'</span> <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>RTRIM<span style="color: #66cc66;">&#40;</span>LTRIM<span style="color: #66cc66;">&#40;</span>@COL<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: #66cc66;">+</span> <span style="color: #ff0000;">'%'</span><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> c<span style="color: #66cc66;">.</span>name<span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'INDEX'</span><span style="color: #66cc66;">,</span> i<span style="color: #66cc66;">.</span>name <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>index_columns <span style="color: #993333; font-weight: bold;">AS</span> ic <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> ic<span style="color: #66cc66;">.</span>object_id <span style="color: #993333; font-weight: bold;">AND</span> i<span style="color: #66cc66;">.</span>index_id <span style="color: #66cc66;">=</span> ic<span style="color: #66cc66;">.</span>index_id <br />
&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; <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 <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 />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;i<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> OBJECT_ID<span style="color: #66cc66;">&#40;</span>@OBJ<span style="color: #66cc66;">&#41;</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;">REPLACE</span><span style="color: #66cc66;">&#40;</span>RTRIM<span style="color: #66cc66;">&#40;</span>LTRIM<span style="color: #66cc66;">&#40;</span>@COL<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: #66cc66;">=</span> c<span style="color: #66cc66;">.</span>name<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> &nbsp; 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> OBJECT_TYPE <span style="color: #993333; font-weight: bold;">WHEN</span> <span style="color: #ff0000;">'PRIMARY KEY'</span> <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHEN</span> <span style="color: #ff0000;">'UNIQUE'</span> <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #cc66cc;">2</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHEN</span> <span style="color: #ff0000;">'FOREIGN KEY'</span> <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #cc66cc;">3</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHEN</span> <span style="color: #ff0000;">'CHECK'</span> <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #cc66cc;">4</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHEN</span> <span style="color: #ff0000;">'INDEX'</span> <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #cc66cc;">5</span><br />
&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: #cc66cc;">99</span> <span style="color: #993333; font-weight: bold;">END</span><span style="color: #66cc66;">,</span> OBJECT_NAME; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<br />
<span style="color: #808080; font-style: italic;">--▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄--</span><br />
<span style="color: #808080; font-style: italic;">-- PHASE 2 : traduction en procédure système &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; --</span><br />
<span style="color: #808080; font-style: italic;">--▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀--</span><br />
<br />
<span style="color: #993333; font-weight: bold;">EXEC</span> sp_MS_marksystemobject <span style="color: #ff0000;">'sp__COLUMN_IS_USED'</span>;</div></div>
<p>Pour utiliser cette procédure, lancez la avec EXEC et passez le nom de table éventuellement préfixé par son schéma SQL ainsi que le nom de la colonne. 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">EXEC sp__COLUMN_IS_USED 'matable', 'macolonne';</div></div>
<p><a href="http://sqlpro.developpez.com/_fichierSQL/sp__COLUMN_IS_USED.txt" title="CODE SQL" rel="noopener" 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>
<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>
		<item>
		<title>Calcul de la &#171;&#160;bouding box&#160;&#187; appropriée pour l&#8217;indexation spatiale</title>
		<link>https://blog.developpez.com/sqlpro/p13125/ms-sql-server/sql-server-2008/calcul-de-la-bouding-box-appropriee-pour-lindexation-spatiale</link>
		<comments>https://blog.developpez.com/sqlpro/p13125/ms-sql-server/sql-server-2008/calcul-de-la-bouding-box-appropriee-pour-lindexation-spatiale#comments</comments>
		<pubDate>Mon, 09 Jan 2017 13:51:50 +0000</pubDate>
		<dc:creator><![CDATA[SQLpro]]></dc:creator>
				<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[bounding box]]></category>
		<category><![CDATA[indexation]]></category>
		<category><![CDATA[SIG]]></category>
		<category><![CDATA[spatial]]></category>
		<category><![CDATA[SQL server]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/sqlpro/?p=700</guid>
		<description><![CDATA[SQL Server dispose d&#8217;index spatiaux qui nécessitent l&#8217;utilisation d&#8217;une &#171;&#160;bounding box&#160;&#187; pour le type geometry. Une &#171;&#160;bounding box&#160;&#187; est en fait un rectangle qui délimite la surface à indexer. Mais comment déterminer les limites de cette &#171;&#160;boite&#160;&#187; ? Cet article vous propose une procédure stockée capable de calculer les meilleurs paramètres de ces &#171;&#160;bounding box&#160;&#187; [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>SQL Server dispose d&rsquo;index spatiaux qui nécessitent l&rsquo;utilisation d&rsquo;une &laquo;&nbsp;bounding box&nbsp;&raquo; pour le type geometry. Une &laquo;&nbsp;bounding box&nbsp;&raquo; est en fait un rectangle qui délimite la surface à indexer. Mais comment déterminer les limites de cette &laquo;&nbsp;boite&nbsp;&raquo; ? Cet article vous propose une procédure stockée capable de calculer les meilleurs paramètres de ces &laquo;&nbsp;bounding box&nbsp;&raquo; et vous donne le code de création des index spatiaux à ajouter à votre base.<br />
<span id="more-700"></span></p>
<div id="attachment_701" style="width: 829px" class="wp-caption aligncenter"><a href="http://blog.developpez.com/sqlpro/files/2017/01/La-France-Bounding-Box.jpg"><img src="http://blog.developpez.com/sqlpro/files/2017/01/La-France-Bounding-Box.jpg" alt="Bounding box pour l&#039;indexation géométrique de la france" width="819" height="770" class="size-full wp-image-701" /></a><p class="wp-caption-text">Bounding box pour l&rsquo;indexation géométrique de la france</p></div>
<p>Dans le principe, tous les points de toutes vos géométries dans une même colonne de table devraient être situés dans la bounding box de l&rsquo;index pour que certaines recherches bénéficient de l&rsquo;indexation. Pour ce faire il faut déterminer le point le plus bas, le plus haut (en y) le plus à gauche et le plus à droite (en x) de l&rsquo;ensemble des points formant toutes vos géométries.<br />
Pour effectuer ce calcul il existe une astuce qui consiste à calculer l&rsquo;enveloppe d&rsquo;une géométrie, comme le montre l&rsquo;image suivante :<br />
<div id="attachment_702" style="width: 1001px" class="wp-caption aligncenter"><a href="http://blog.developpez.com/sqlpro/files/2017/01/Bounding-box-Paris.jpg"><img src="http://blog.developpez.com/sqlpro/files/2017/01/Bounding-box-Paris.jpg" alt="Calcul de l&#039;enveloppe de Paris à l&#039;aide de la fonction STEnvelope" width="991" height="791" class="size-full wp-image-702" /></a><p class="wp-caption-text">Calcul de l&rsquo;enveloppe de Paris à l&rsquo;aide de la fonction STEnvelope</p></div><br />
Notez que le calcul des limites de Paris a été effectué par concaténation des polygones des différentes arrondissements dans une premier temps (geometry::UnionAggregate(GEOM)).</p>
<p>À partir de là on peut obtenir les valeurs min et max de X et Y, car, comme le rectangle est formé de 4 points, il suffit de prendre les coordonnées de X et Y du point 1 et du point 3, les points étant spécifiées par chainage !<br />
<div id="attachment_703" style="width: 963px" class="wp-caption aligncenter"><a href="http://blog.developpez.com/sqlpro/files/2017/01/Enveloppe-Paris.jpg"><img src="http://blog.developpez.com/sqlpro/files/2017/01/Enveloppe-Paris.jpg" alt="Enveloppe de Paris et ses points" width="953" height="553" class="size-full wp-image-703" /></a><p class="wp-caption-text">Enveloppe de Paris et ses points</p></div></p>
<p>Dès lors la méthode pour obtenir les coordonnées de la bounding box est d&rsquo;obtenir les valeurs de X et Y comme ceci :</p>
<li>MaGeometrie.STPointN(1).STX =&gt; Xmin</li>
<li>MaGeometrie.STPointN(1).STY =&gt; Ymin</li>
<li>MaGeometrie.STPointN(3).STX =&gt; Xmax</li>
<li>MaGeometrie.STPointN(3).STY =&gt; Ymax</li>
<p>Mais il faut concaténer toutes les entrées de toutes les géométries de la colonnes à l&rsquo;aide de la fonction EnvelopeAggregate.</p>
<p>la requête finale ressembles à celle-ci (calcul de la bounding box pour toutes les communes de la France métropolitaine) :</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;">WITH</span> 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> geometry::EnvelopeAggregate<span style="color: #66cc66;">&#40;</span>GEOM<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> GEO<br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; <span style="color: #66cc66;">&#91;</span>S_GEO<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>COMMUNE<span style="color: #66cc66;">&#93;</span> <br />
<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> geometry::EnvelopeAggregate<span style="color: #66cc66;">&#40;</span>GEO<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>STPointN<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>STX <span style="color: #993333; font-weight: bold;">AS</span> MIN_X<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;geometry::EnvelopeAggregate<span style="color: #66cc66;">&#40;</span>GEO<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>STPointN<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>STX <span style="color: #993333; font-weight: bold;">AS</span> MAX_X<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;geometry::EnvelopeAggregate<span style="color: #66cc66;">&#40;</span>GEO<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>STPointN<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>STY <span style="color: #993333; font-weight: bold;">AS</span> MIN_Y<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;geometry::EnvelopeAggregate<span style="color: #66cc66;">&#40;</span>GEO<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>STPointN<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span>STY <span style="color: #993333; font-weight: bold;">AS</span> MAX_Y <br />
<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; T</div></div>
<p>Au final nous pouvons automatiser ce calcul pour toutes les colonnes geometry de toute la base. Ceci conduit à la procédures suivantes :</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_AUTOBOUND_SPATIAL_INDEX @DELTA <span style="color: #993333; font-weight: bold;">FLOAT</span><br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<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>SCH sysname<span style="color: #66cc66;">,</span> TBL sysname<span style="color: #66cc66;">,</span> COL sysname<span style="color: #66cc66;">,</span> XMIN <span style="color: #993333; font-weight: bold;">FLOAT</span><span style="color: #66cc66;">,</span> XMAX <span style="color: #993333; font-weight: bold;">FLOAT</span><span style="color: #66cc66;">,</span> YMIN <span style="color: #993333; font-weight: bold;">FLOAT</span><span style="color: #66cc66;">,</span> YMAX <span style="color: #993333; font-weight: bold;">FLOAT</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #808080; font-style: italic;">-- le delate doit être 1 (bornes strictes) ou supérieur à 1</span><br />
<span style="color: #993333; font-weight: bold;">IF</span> @DELTA <span style="color: #66cc66;">&lt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">OR</span> @DELTA <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NULL</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SET</span> @DELTA <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">-- recherches des colonnes spatiales non indexées de type GEOMETRY</span><br />
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> @T <span style="color: #66cc66;">&#40;</span>SCH<span style="color: #66cc66;">,</span> TBL<span style="color: #66cc66;">,</span> COL<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> COLUMN_NAME<br />
<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><br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;DATA_TYPE <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'geometry'</span><br />
<span style="color: #993333; font-weight: bold;">EXCEPT</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</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> c<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>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> o<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;<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>spatial_indexes <span style="color: #993333; font-weight: bold;">AS</span> si<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> si<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> si<span style="color: #66cc66;">.</span>index_id<br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">JOIN</span> 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; <span style="color: #993333; font-weight: bold;">ON</span> i<span style="color: #66cc66;">.</span>object_id <span style="color: #66cc66;">=</span> ic<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> ic<span style="color: #66cc66;">.</span>index_id<br />
&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; <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; <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;<span style="color: #993333; font-weight: bold;">JOIN</span> sys<span style="color: #66cc66;">.</span>systypes <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> c<span style="color: #66cc66;">.</span>system_type_id <span style="color: #66cc66;">=</span> t<span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">TYPE</span> <br />
<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;t<span style="color: #66cc66;">.</span>name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'geometry'</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @TXY <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #66cc66;">&#40;</span>XMIN <span style="color: #993333; font-weight: bold;">FLOAT</span><span style="color: #66cc66;">,</span> XMAX <span style="color: #993333; font-weight: bold;">FLOAT</span><span style="color: #66cc66;">,</span> YMIN <span style="color: #993333; font-weight: bold;">FLOAT</span><span style="color: #66cc66;">,</span> YMAX <span style="color: #993333; font-weight: bold;">FLOAT</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> @TBL sysname<span style="color: #66cc66;">,</span> @SCH sysname<span style="color: #66cc66;">,</span> @COL sysname<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; @XMIN <span style="color: #993333; font-weight: bold;">FLOAT</span><span style="color: #66cc66;">,</span> @XMAX <span style="color: #993333; font-weight: bold;">FLOAT</span><span style="color: #66cc66;">,</span> @YMIN <span style="color: #993333; font-weight: bold;">FLOAT</span><span style="color: #66cc66;">,</span> @YMAX <span style="color: #993333; font-weight: bold;">FLOAT</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; @<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>;<br />
<br />
<span style="color: #993333; font-weight: bold;">DECLARE</span> C CURSOR <br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">LOCAL</span><br />
&nbsp; &nbsp;FORWARD_ONLY<br />
&nbsp; &nbsp;STATIC<br />
&nbsp; &nbsp;READ_ONLY<br />
<span style="color: #993333; font-weight: bold;">FOR</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SELECT</span> SCH<span style="color: #66cc66;">,</span> TBL<span style="color: #66cc66;">,</span> COL<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; @T;<br />
<br />
<span style="color: #993333; font-weight: bold;">OPEN</span> C;<br />
<br />
FETCH C <span style="color: #993333; font-weight: bold;">INTO</span> @SCH<span style="color: #66cc66;">,</span> @TBL<span style="color: #66cc66;">,</span> @COL;<br />
<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;">DELETE</span> <span style="color: #993333; font-weight: bold;">FROM</span> @TXY;<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SET</span> @<span style="color: #993333; font-weight: bold;">SQL</span> <span style="color: #66cc66;">=</span> N<span style="color: #ff0000;">'WITH T AS (SELECT geometry::EnvelopeAggregate(['</span> <span style="color: #66cc66;">+</span> @COL <span style="color: #66cc66;">+</span> N<span style="color: #ff0000;">']) AS GEO FROM ['</span> <span style="color: #66cc66;">+</span> @SCH &nbsp;<span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'].['</span> <span style="color: #66cc66;">+</span> @TBL <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">']) '</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">+</span> N<span style="color: #ff0000;">'SELECT geometry::EnvelopeAggregate(GEO).STPointN(1).STX / '</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>@DELTA <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> N<span style="color: #ff0000;">' AS MIN_X, '</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">+</span> N<span style="color: #ff0000;">'geometry::EnvelopeAggregate(GEO).STPointN(3).STX * '</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>@DELTA <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> N<span style="color: #ff0000;">' AS MAX_X, '</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">+</span> N<span style="color: #ff0000;">'geometry::EnvelopeAggregate(GEO).STPointN(1).STY / '</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>@DELTA <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> N<span style="color: #ff0000;">' AS MIN_Y, '</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">+</span> N<span style="color: #ff0000;">'geometry::EnvelopeAggregate(GEO).STPointN(3).STY * '</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span>@DELTA <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> N<span style="color: #ff0000;">' AS MAX_Y '</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">+</span> N<span style="color: #ff0000;">'FROM T;'</span><br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> @TXY<br />
&nbsp; &nbsp;<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 />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">UPDATE</span> T<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">SET</span> &nbsp; &nbsp;XMIN <span style="color: #66cc66;">=</span> TT<span style="color: #66cc66;">.</span>XMIN<span style="color: #66cc66;">,</span> XMAX <span style="color: #66cc66;">=</span> TT<span style="color: #66cc66;">.</span>XMAX<span style="color: #66cc66;">,</span> YMIN <span style="color: #66cc66;">=</span> TT<span style="color: #66cc66;">.</span>YMIN<span style="color: #66cc66;">,</span> YMAX <span style="color: #66cc66;">=</span> TT<span style="color: #66cc66;">.</span>YMAX<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">FROM</span> &nbsp; @T <span style="color: #993333; font-weight: bold;">AS</span> T<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">CROSS</span> <span style="color: #993333; font-weight: bold;">JOIN</span> @TXY <span style="color: #993333; font-weight: bold;">AS</span> TT<br />
&nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">WHERE</span> &nbsp;SCH <span style="color: #66cc66;">=</span> @SCH<br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AND</span> &nbsp;TBL <span style="color: #66cc66;">=</span> @TBL<br />
&nbsp; &nbsp; &nbsp;<span style="color: #993333; font-weight: bold;">AND</span> &nbsp;COL <span style="color: #66cc66;">=</span> @COL;<br />
&nbsp; &nbsp;FETCH C <span style="color: #993333; font-weight: bold;">INTO</span> @SCH<span style="color: #66cc66;">,</span> @TBL<span style="color: #66cc66;">,</span> @COL;<br />
<span style="color: #993333; font-weight: bold;">END</span><br />
<br />
CLOSE C;<br />
<br />
DEALLOCATE C;<br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> N<span style="color: #ff0000;">'CREATE SPATIAL INDEX XS_'</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">LEFT</span><span style="color: #66cc66;">&#40;</span>SCH<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">16</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> N<span style="color: #ff0000;">'_'</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">LEFT</span><span style="color: #66cc66;">&#40;</span>TBL<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> N<span style="color: #ff0000;">'_'</span> <span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">LEFT</span><span style="color: #66cc66;">&#40;</span>COL<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #66cc66;">&#41;</span> &nbsp;<span style="color: #66cc66;">+</span> N<span style="color: #ff0000;">'_'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<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;">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> NVARCHAR<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> N<span style="color: #ff0000;">'-'</span><span style="color: #66cc66;">,</span> N<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: #993333; font-weight: bold;">CONVERT</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">CHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">112</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">+</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><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">+</span> N<span style="color: #ff0000;">' &nbsp; ON ['</span> <span style="color: #66cc66;">+</span> SCH <span style="color: #66cc66;">+</span> N<span style="color: #ff0000;">'].['</span> <span style="color: #66cc66;">+</span> TBL <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">'] (['</span> <span style="color: #66cc66;">+</span> COL <span style="color: #66cc66;">+</span> N<span style="color: #ff0000;">']) '</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">+</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><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">+</span> N<span style="color: #ff0000;">' &nbsp; WITH ( BOUNDING_BOX = ('</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span> XMIN <span style="color: #993333; font-weight: bold;">AS</span> NVARCHAR<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 />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span> YMIN <span style="color: #993333; font-weight: bold;">AS</span> NVARCHAR<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 />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span> XMAX <span style="color: #993333; font-weight: bold;">AS</span> NVARCHAR<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 />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">+</span> <span style="color: #993333; font-weight: bold;">CAST</span><span style="color: #66cc66;">&#40;</span> YMAX <span style="color: #993333; font-weight: bold;">AS</span> NVARCHAR<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; @T; &nbsp;<br />
<br />
<span style="color: #993333; font-weight: bold;">GO</span></div></div>
<p>Le paramètre @DELTA sert à rajouter un peu de &quot;mou&quot; sur la périphérie de la bounding box et doit être égale à 1 (pas de &quot;mou&quot;) ou supérieur.<br />
par exemple avec 1.1, on ajoute 10% de distance en périphérie de la bounding box.</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/AutoBoundingSpatialIndex.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>
