<?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 SQL Server d&#039;ElSüket &#187; DBCC</title>
	<atom:link href="https://blog.developpez.com/elsuket/ptag/dbcc/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.developpez.com/elsuket</link>
	<description>Nicolas Souquet - Expert SQL Server</description>
	<lastBuildDate>Mon, 05 Apr 2021 07:32:41 +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>Vous êtes sûr d&#8217;avoir les BACKUP avec CHECKSUM parce que vous utilisez l&#8217;option COMPRESSION ? Vérifiez une seconde fois !</title>
		<link>https://blog.developpez.com/elsuket/p11990/snippets/backup_checksum_compression</link>
		<comments>https://blog.developpez.com/elsuket/p11990/snippets/backup_checksum_compression#comments</comments>
		<pubDate>Sun, 26 May 2013 13:32:34 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Moteur de base de données SQL Server]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[BACKUP]]></category>
		<category><![CDATA[CHECKSUM]]></category>
		<category><![CDATA[COMPRESSION]]></category>
		<category><![CDATA[DBCC]]></category>
		<category><![CDATA[DBCC IND]]></category>
		<category><![CDATA[DBCC PAGE]]></category>
		<category><![CDATA[maintenance plan]]></category>
		<category><![CDATA[plan de maintenance]]></category>
		<category><![CDATA[sauvegarde]]></category>
		<category><![CDATA[sys.dm_db_database_page_allocations]]></category>
		<category><![CDATA[sys.messages]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/elsuket/?p=427</guid>
		<description><![CDATA[Après avoir vu comment compresser toutes les sauvegardes de base de données dès SQL Server 2008, il est intéressant de trouver comment activer l&#8217;option CHECKSUM de l&#8217;instruction BACKUP. A ce sujet, la documentation indique : Indique que l&#8217;opération de sauvegarde &#8230; <a href="https://blog.developpez.com/elsuket/p11990/snippets/backup_checksum_compression">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Après avoir vu comment <a href="http://blog.developpez.com/elsuket/p9040/moteur-de-base-de-donnees-sql-server/compresser_tous_les_backups_sans_modifie_2008">compresser toutes les sauvegardes de base de données</a> dès SQL Server 2008, il est intéressant de trouver comment activer l&rsquo;option CHECKSUM de l&rsquo;instruction BACKUP.</p>
<p>A ce sujet, la documentation indique :</p>
<blockquote><p> Indique que l&rsquo;opération de sauvegarde vérifie dans chaque page les informations de somme de contrôle et de page endommagée, si elles sont activées et disponibles, et génère une somme de contrôle pour l&rsquo;ensemble de la sauvegarde. <strong>Ceci est le comportement par défaut pour une sauvegarde compressée</strong>.</p></blockquote>
<p>Vérifions donc si cela est vrai : nous allons créer une base de données de test, et nous allons corrompre une de ses pages afin de vérifier le comportement de la sauvegarde. En effet, l&rsquo;option CHECKSUM permet de détecter la corruption de données au moment de la sauvegarde. Cela permet en plus de ne pas se retrouver dans l’inconfortable situation de devoir restaurer une base de données à partir d&rsquo;un fichier de sauvegarde corrompu.<br />
<span id="more-427"></span><br />
<img src="http://img12.imageshack.us/img12/5913/iconarrowp.gif" alt="" /> <strong>La vérification de l&rsquo;intégrité des pages au niveau de la base de données</strong></p>
<p>Par défaut, lorsqu&rsquo;on crée une nouvelle base de données sous SQL Server 2005 et suivants, la vérification de l&rsquo;intégrité des pages lues à partir du disque se fait à l&rsquo;aide d&rsquo;une somme de contrôle. Celle-ci est calculée à partir du contenu de la page, puis écrite dans l&rsquo;en-tête de celle-ci au moment de l&rsquo;écriture sur disque. Lorsque la page est ensuite lue à partir du disque, la somme de contrôle est recalculée, puis comparée à celle qui est dans l&rsquo;en-tête de la page : si les valeurs sont identiques, alors la page est intègre; à l&rsquo;inverse, on obtient l&rsquo;erreur 824, qui est écrite à la fois dans les journaux de SQL Server et dans le journal d&rsquo;événements de Windows.<br />
Enfin lorsqu&rsquo;on restaure une base de données qui a été créée sous une version antérieure à 2005, il est important de changer cette option de TORN_PAGE_DETECTION à PAGE_VERIFY, car la première est conservée par défaut. L&rsquo;option TORN_PAGE_DETECTION stocke deux bits pour chaque secteur de 512 octets de toute page, dont la taille est 8192 octets. De la même façon, ces 16 * 2 bits sont stockés dans l&rsquo;en-tête de la page, et comparés à la relecture de la page. Comme cette option est moins stricte que CHECKSUM, il est donc recommandé d&rsquo;utiliser cette dernière option.</p>
<p><img src="http://img12.imageshack.us/img12/5913/iconarrowp.gif" alt="" /> <strong>Le test</strong></p>
<p>Nous créons donc la base de données sous SQL Server 2012, puis nous vérifions l&rsquo;option du contrôle de l&rsquo;intégrité des pages :</p>
<div class="codecolorer-container tsql vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="tsql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">DATABASE</span> TEST_CHECKSUM;<br />
GO<br />
<br />
<span style="color: #0000FF;">USE</span> TEST_CHECKSUM<br />
GO<br />
<br />
<span style="color: #0000FF;">SELECT</span>&nbsp; name<br />
&nbsp; &nbsp; &nbsp; &nbsp; , page_verify_option_desc<br />
<span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; sys.<span style="color: #202020;">databases</span><br />
<span style="color: #0000FF;">WHERE</span> &nbsp; name <span style="color: #808080;">=</span> <span style="color: #FF0000;">'TEST_CHECKSUM'</span></div></td></tr></tbody></table></div>
<p><img src="http://img843.imageshack.us/img843/1872/backupchecksum01.png" alt="" /></p>
<p>Nous créons ensuite une table dans cette base de données, à laquelle nous ajoutons une ligne :</p>
<div class="codecolorer-container tsql vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><div class="tsql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000FF;">USE</span> TEST_CHECKSUM<br />
GO<br />
<br />
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> test_backup_checksum<br />
<span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; id <span style="color: #0000FF;">tinyint</span> <span style="color: #0000FF;">IDENTITY</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , nom <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">16</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #808080;">&#41;</span><br />
GO<br />
<br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">test_backup_checksum</span> <span style="color: #808080;">&#40;</span>nom<span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'test_checksum'</span><span style="color: #808080;">&#41;</span><br />
GO<br />
<br />
<span style="color: #0000FF;">SELECT</span>&nbsp; <span style="color: #808080;">*</span><br />
<span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; dbo.<span style="color: #202020;">test_backup_checksum</span></div></td></tr></tbody></table></div>
<p>Nous forçons ensuite l&rsquo;écriture des pages sur disque, à l&rsquo;aide de l&rsquo;instruction <a href="http://blog.developpez.com/elsuket/p9529/moteur-de-base-de-donnees-sql-server/lazywriter_checkpoint_aamp_ghost_cleanup">CHECKPOINT</a>, puis nous vidons le cache de données, de sorte que la prochaine fois que nous exécuterons une requête sur cette table, ses pages seront lues à partir du disque, à l&rsquo;aide de l&rsquo;instruction DBCC DROPCLEANBUFFERS.</p>
<p><strong>N.B. : </strong>N&rsquo;exécutez <strong>JAMAIS</strong> une instruction non-documentée sur une base de données qui sert une charge de production. Si tel devait être le cas, je ne peux <strong>en aucun cas être tenu pour responsable des conséquences. Vous devez limiter strictement l&rsquo;usage de cette instruction aux environnements de test</strong>.</p>
<div class="codecolorer-container tsql vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="tsql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000FF;">CHECKPOINT</span><br />
GO<br />
<br />
<span style="color: #0000FF;">DBCC</span> DROPCLEANBUFFERS<br />
GO</div></td></tr></tbody></table></div>
<p><img src="http://img12.imageshack.us/img12/5913/iconarrowp.gif" alt="" /> <strong>L&rsquo;instruction DBCC IND, ou la DMF sys.dm_db_database_page_allocations</strong></p>
<p>Encore deux options d&rsquo;exploration de la mécanique interne de SQL Server, qui permettent de lister les pages d&rsquo;une table ou d&rsquo;un index, et qui ne sont pas documentées. Voici le prototype d&rsquo;utilisation de ces deux alternatives :</p>
<div class="codecolorer-container tsql vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br /></div></td><td><div class="tsql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000FF;">DBCC</span> <span style="color: #808080;">IN</span>D <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'TEST_CHECKSUM'</span>, <span style="color: #FF0000;">'dbo.test_backup_checksum'</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #008080;">-- le dernier argument est l'index id</span><br />
GO<br />
<br />
<span style="color: #0000FF;">SELECT</span>&nbsp; allocated_page_file_id<br />
&nbsp; &nbsp; &nbsp; &nbsp; , allocated_page_page_id<br />
&nbsp; &nbsp; &nbsp; &nbsp; , allocated_page_iam_file_id<br />
&nbsp; &nbsp; &nbsp; &nbsp; , allocated_page_iam_page_id<br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #FF00FF;">object_id</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , index_id<br />
&nbsp; &nbsp; &nbsp; &nbsp; , partition_id<br />
&nbsp; &nbsp; &nbsp; &nbsp; , allocation_unit_type_desc <span style="color: #0000FF;">AS</span> iam_chain_type<br />
&nbsp; &nbsp; &nbsp; &nbsp; , page_type_desc<br />
&nbsp; &nbsp; &nbsp; &nbsp; , page_level<br />
&nbsp; &nbsp; &nbsp; &nbsp; , rowset_id<br />
&nbsp; &nbsp; &nbsp; &nbsp; , next_page_file_id<br />
&nbsp; &nbsp; &nbsp; &nbsp; , next_page_page_id <br />
&nbsp; &nbsp; &nbsp; &nbsp; , previous_page_file_id <br />
&nbsp; &nbsp; &nbsp; &nbsp; , previous_page_page_id<br />
<span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; sys.<span style="color: #202020;">dm_db_database_page_allocations</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF00FF;">DB_ID</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'TEST_CHECKSUM'</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #FF00FF;">OBJECT_ID</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'dbo.test_backup_checksum'</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #000;">0</span> <span style="color: #008080;">-- index id : 0 pour un tas, 1 pour un index cluster, plus grand pour un index non-cluster</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">NULL</span> <span style="color: #008080;">-- partition id</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #FF0000;">'DETAILED'</span> <span style="color: #008080;">-- niveau de détail</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">WHERE</span> &nbsp; is_allocated <span style="color: #808080;">=</span> <span style="color: #000;">1</span>;</div></td></tr></tbody></table></div>
<p>On remarque tout de suite que la DMF produit bien plus d&rsquo;informations que l&rsquo;instruction DBCC (nous ne donnons pas ici toutes les colonnes), et que ses arguments sont similaires à la DMF <em>sys.dm_db_index_physical_stats</em>.</p>
<p><img src="http://img689.imageshack.us/img689/1618/backupchecksum02.png" alt="" /></p>
<p>Voyons donc ce que contient la page 272, puisque c&rsquo;est elle qui contient les données (page_type_desc = DATA_PAGE) :</p>
<div class="codecolorer-container tsql vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="tsql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080;">-- Activation de la sortie de la commande DBCC</span><br />
<span style="color: #0000FF;">DBCC</span> TRACEON <span style="color: #808080;">&#40;</span><span style="color: #000;">3604</span><span style="color: #808080;">&#41;</span><br />
GO<br />
<br />
<span style="color: #008080;">-- DBCC PAGE ([database_name_or_id], [file_id], [page_id], [option])</span><br />
<span style="color: #008080;">-- option = 0 montre seulement l'en-tête de la page</span><br />
<span style="color: #0000FF;">DBCC</span> PAGE <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'TEST_CHECKSUM'</span>, <span style="color: #000;">1</span>, <span style="color: #000;">272</span>, <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>;<br />
GO</div></td></tr></tbody></table></div>
<p>Nous obtenons :</p>
<blockquote><p>PAGE: (1:272)</p>
<p>BUFFER:</p>
<p>BUF @0x000000027407BB80</p>
<p>bpage = 0x00000002649F4000          bhash = 0x0000000000000000          bpageno = (1:272)<br />
bdbid = 8                           breferences = 0                     bcputicks = 0<br />
bsampleCount = 0                    bUse1 = 33295                       bstat = 0x9<br />
blog = 0x2121215a                   bnext = 0x0000000000000000          </p>
<p>PAGE HEADER:</p>
<p>Page @0x00000002649F4000</p>
<p>m_pageId = (1:272)                  m_headerVersion = 1                 m_type = 1<br />
m_typeFlagBits = 0x0                m_level = 0                         <strong>m_flagBits = 0x8200</strong><br />
m_objId (AllocUnitId.idObj) = 84    m_indexId (AllocUnitId.idInd) = 256<br />
Metadata: AllocUnitId = 72057594043432960<br />
Metadata: PartitionId = 72057594039042048                                Metadata: IndexId = 0<br />
Metadata: ObjectId = 245575913      m_prevPage = (0:0)                  m_nextPage = (0:0)<br />
pminlen = 5                         m_slotCnt = 1                       m_freeCnt = 8069<br />
m_freeData = 121                    m_reservedCnt = 0                   m_lsn = (30:345:30)<br />
m_xactReserved = 0                  m_xdesId = (0:0)                    m_ghostRecCnt = 0<br />
<strong>m_tornBits = 1209479828</strong>             DB Frag ID = 1</p></blockquote>
<p>Si nous nous en référons à la <a href="http://www.sqlskills.com/blogs/paul/inside-the-storage-engine-anatomy-of-a-page/">documentation donnée par Paul Randal</a>, nous avons bien :</p>
<p>&#8211; <em>m_flagBits = 0x8200</em>, qui indique que l&rsquo;intégrité de la page est vérifiée par somme de contrôle<br />
&#8211; <em>m_tornBits</em> est la somme de contrôle de cette page</p>
<p>Nous allons maintenant mettre la base de données hors-ligne, puis éditer le fichier de données avec un éditeur hexadécimal, de façon à vérifier si SQL Server vérifie bien la somme à la lecture de la page :</p>
<div class="codecolorer-container tsql vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="tsql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">DATABASE</span> <span style="color: #0000FF;">SET</span> OFFL<span style="color: #808080;">IN</span>E<br />
GO</div></td></tr></tbody></table></div>
<p>En ce qui concerne la lecture en hexadécimal, j&rsquo;utilise le logiciel <a href="http://www.chmaas.handshake.de/delphi/freeware/xvi32/xvi32.htm">Hex Editor XVI32</a>. Une fois l&rsquo;outil ouvert en tant qu&rsquo;administrateur, il suffit de chercher la chaîne que nous avons ajouté comme ligne dans la table, en suivant le menu <em>Search</em> / <em>Find</em> :</p>
<p><img src="http://img850.imageshack.us/img850/1565/backupchecksum03.png" alt="" /></p>
<p>Un clic sur le bouton OK nous amène directement à l&rsquo;emplacement, où l&rsquo;on est alors libre de changer la chaîne, où j&rsquo;ai changé le &laquo;&nbsp;k&nbsp;&raquo; en &laquo;&nbsp;d&nbsp;&raquo;, puis sauvé le fichier :</p>
<p><img src="http://img833.imageshack.us/img833/341/backupchecksum04.png" alt="" /></p>
<p>Nous remettons la base de données en ligne :</p>
<div class="codecolorer-container tsql vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="tsql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000FF;">ALTER</span> <span style="color: #0000FF;">DATABASE</span> TEST_CHECKSUM<br />
<span style="color: #0000FF;">SET</span> ONL<span style="color: #808080;">IN</span>E<br />
GO</div></td></tr></tbody></table></div>
<p>Aucun problème. Exécutons un SELECT * FROM dbo.test_backup_checksum. Nous obtenons le message suivant :</p>
<blockquote><p>
Msg 824, Level 24, State 2, Line 1<br />
SQL Server detected a logical consistency-based I/O error: incorrect checksum (expected: 0xf9ec3284; actual: 0xfe6c3284). It occurred during a read of page (1:228) in database ID 7 at offset 0x000000001c8000 in file &lsquo;C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\TEST_CHECKSUM.mdf&rsquo;.  Additional messages in the SQL Server error log or system event log may provide more detail. This is a severe error condition that threatens database integrity and must be corrected immediately. Complete a full database consistency check (DBCC CHECKDB). This error can be caused by many factors; for more information, see SQL Server Books Online.</p></blockquote>
<p>Prenons maintenant une sauvegarde de cette base de données, avec COMPRESSION, mais sans spécifier l&rsquo;option CHECKSUM :</p>
<div class="codecolorer-container tsql vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="tsql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000FF;">BACKUP</span> <span style="color: #0000FF;">DATABASE</span> TEST_CHECKSUM<br />
<span style="color: #0000FF;">TO</span> <span style="color: #0000FF;">DISK</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'F:\SQLServer2012\TEST_CHECKSUM.bak'</span><br />
<span style="color: #0000FF;">WITH</span> COMPRESSION<br />
GO</div></td></tr></tbody></table></div>
<p>Aucun problème : nous obtenons :</p>
<blockquote><p>Processed 280 pages for database &lsquo;TEST_CHECKSUM&rsquo;, file &lsquo;TEST_CHECKSUM&rsquo; on file 1.<br />
Processed 3 pages for database &lsquo;TEST_CHECKSUM&rsquo;, file &lsquo;TEST_CHECKSUM_log&rsquo; on file 1.<br />
BACKUP DATABASE successfully processed 283 pages in 0.468 seconds (4.709 MB/sec).</p></blockquote>
<p>Voyons ce que contient la table d&rsquo;historique des sauvegardes :</p>
<div class="codecolorer-container tsql vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="tsql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000FF;">SELECT</span>&nbsp; <span style="color: #FF00FF;">user_name</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , backup_start_date<br />
&nbsp; &nbsp; &nbsp; &nbsp; , recovery_model<br />
&nbsp; &nbsp; &nbsp; &nbsp; , has_backup_checksums<br />
&nbsp; &nbsp; &nbsp; &nbsp; , is_damaged<br />
<span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; msdb.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">backupset</span><br />
<span style="color: #0000FF;">WHERE</span> &nbsp; database_name <span style="color: #808080;">=</span> <span style="color: #FF0000;">'TEST_CHECKSUM'</span></div></td></tr></tbody></table></div>
<p><img src="http://img195.imageshack.us/img195/6295/backupchecksum05.png" alt="" /></p>
<p>On obtient un résultat similaire avec l&rsquo;instruction RESTORE HEADERONLY (colonnes <em>HasBackupChecksums</em> et <em>IsDamaged</em> toutes deux à zéro) :</p>
<div class="codecolorer-container tsql vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="tsql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000FF;">RESTORE</span> HEADERONLY<br />
<span style="color: #0000FF;">FROM</span> <span style="color: #0000FF;">DISK</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'F:\SQLServer2012\TEST_CHECKSUM.bak'</span></div></td></tr></tbody></table></div>
<p>Le fichier de sauvegarde ne dispose pas de la somme de contrôle, et n&rsquo;est pas marqué comme endommagé. Pourtant nous avons bien corrompu le fichier de données, et nous ne pouvons pas lire la table que nous avons peuplé auparavant !</p>
<p>Voyons ce que produit la même instruction, avec l&rsquo;option CHECKSUM :</p>
<div class="codecolorer-container tsql vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="tsql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000FF;">BACKUP</span> <span style="color: #0000FF;">DATABASE</span> TEST_CHECKSUM<br />
<span style="color: #0000FF;">TO</span> <span style="color: #0000FF;">DISK</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'F:\SQLServer2012\TEST_CHECKSUM_CC.bak'</span><br />
<span style="color: #0000FF;">WITH</span> COMPRESSION, <span style="color: #FF00FF;">CHECKSUM</span><br />
GO</div></td></tr></tbody></table></div>
<blockquote><p>Msg 3043, Level 16, State 1, Line 1<br />
BACKUP &lsquo;TEST_CHECKSUM&rsquo; detected an error on page (1:228) in file &lsquo;C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\TEST_CHECKSUM.mdf&rsquo;.<br />
Msg 3013, Level 16, State 1, Line 1<br />
BACKUP DATABASE is terminating abnormally.</p></blockquote>
<p>Voyons maintenant la même instruction, seulement avec l&rsquo;option CHECKSUM :</p>
<div class="codecolorer-container tsql vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="tsql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000FF;">BACKUP</span> <span style="color: #0000FF;">DATABASE</span> TEST_CHECKSUM<br />
<span style="color: #0000FF;">TO</span> <span style="color: #0000FF;">DISK</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'F:\SQLServer2012\TEST_CHECKSUM_CHK.bak'</span><br />
<span style="color: #0000FF;">WITH</span> <span style="color: #FF00FF;">CHECKSUM</span><br />
GO</div></td></tr></tbody></table></div>
<p>Nous obtenons la même erreur. Si nous interrogeons de nouveau la table d&rsquo;historique des sauvegardes pour cette base de données, nous ne trouvons que le premier backup, dont on sait qu&rsquo;il est corrompu. Pourtant nous n&rsquo;avons (presque) aucun moyen de le savoir, car ni cette table, ni l&rsquo;instruction RESTORE HEADERONLY ne permettent de vérifier que le fichier est bien intègre. On peut cependant vérifier les journaux de SQL Server à l&rsquo;aide de la procédure stockée étendue <a href="http://blog.developpez.com/elsuket/p10279/moteur-de-base-de-donnees-sql-server/gestion_lecture_journaux_sql_server"><em>xp_readerrorlog</em></a>, où l&rsquo;on trouve :</p>
<p><img src="http://img17.imageshack.us/img17/9685/backupchecksum06.png" alt="" /></p>
<p><img src="http://img12.imageshack.us/img12/5913/iconarrowp.gif" alt="" /> <strong>Détecter la corruption le plus tôt possible</strong></p>
<p>Une des premières choses à faire après avoir réalisé une installation fraîche de SQL Server est d&rsquo;ajouter une liste d&rsquo;alertes de l&rsquo;Agent SQL Server, qui enverront un mail dès la détection d&rsquo;un problème de corruption. Ceci nécessite le paramétrage de Database Mail, et la mise en place d&rsquo;un opérateur SQL Agent. Voici le code qui permet d&rsquo;ajouter l&rsquo;alerte :</p>
<div class="codecolorer-container tsql vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br /></div></td><td><div class="tsql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080;">-------------------------------</span><br />
<span style="color: #008080;">-- Nicolas Souquet - 26/05/2013</span><br />
<span style="color: #008080;">-------------------------------</span><br />
<span style="color: #0000FF;">DECLARE</span> @i <span style="color: #0000FF;">smallint</span> <span style="color: #808080;">=</span> <span style="color: #000;">823</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , @alert_name sysname<br />
<br />
<span style="color: #0000FF;">DECLARE</span> @operator_name sysname <span style="color: #808080;">=</span> <span style="color: #FF0000;">'monOperateur'</span><br />
<br />
<span style="color: #0000FF;">WHILE</span> @i <span style="color: #808080;">&amp;</span>lt;<span style="color: #808080;">=</span> <span style="color: #000;">825</span><br />
<span style="color: #0000FF;">BEGIN</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span>&nbsp; @alert_name <span style="color: #808080;">=</span> <span style="color: #808080;">&amp;</span>#039;Message <span style="color: #808080;">&amp;</span>#039; <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>@i <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">3</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">EXEC</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #AF0000;">sp_add_alert</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @name <span style="color: #808080;">=</span> @alert_name<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , @message_id <span style="color: #808080;">=</span> @i<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , @severity <span style="color: #808080;">=</span> <span style="color: #000;">0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , @enabled <span style="color: #808080;">=</span> <span style="color: #000;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , @delay_between_responses <span style="color: #808080;">=</span> <span style="color: #000;">0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , @include_event_description_in <span style="color: #808080;">=</span> <span style="color: #000;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , @job_id <span style="color: #808080;">=</span> N<span style="color: #808080;">&amp;</span>#039;00000000<span style="color: #808080;">-</span>0000<span style="color: #808080;">-</span>0000<span style="color: #808080;">-</span>0000<span style="color: #808080;">-</span>000000000000<span style="color: #808080;">&amp;</span>#039;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">EXEC</span> msdb.<span style="color: #202020;">dbo</span>.<span style="color: #AF0000;">sp_add_notification</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @alert_name <span style="color: #808080;">=</span> @alert_name<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , @operator_name <span style="color: #808080;">=</span> @operator_name<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , @notification_method <span style="color: #808080;">=</span> <span style="color: #000;">1</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @i <span style="color: #808080;">=</span> @i <span style="color: #808080;">+</span> <span style="color: #000;">1</span><br />
<span style="color: #0000FF;">END</span></div></td></tr></tbody></table></div>
<p>L&rsquo;erreur qui nous intéresse est la 824, mais les erreurs 823 et 825 sont importantes aussi. Pour trouver leur signification, il suffit d&rsquo;interroger la vue système <em>sys.messages</em> :</p>
<div class="codecolorer-container tsql vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="tsql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000FF;">SELECT</span>&nbsp; <span style="color: #808080;">*</span><br />
<span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; sys.<span style="color: #202020;">messages</span><br />
<span style="color: #0000FF;">WHERE</span> &nbsp; message_id <span style="color: #808080;">BETWEEN</span> <span style="color: #000;">823</span> <span style="color: #808080;">AND</span> <span style="color: #000;">825</span><br />
<span style="color: #808080;">AND</span> &nbsp; &nbsp; language_id <span style="color: #808080;">=</span> <span style="color: #000;">1036</span> <span style="color: #008080;">-- Français</span></div></td></tr></tbody></table></div>
<p>On peut faire de même avec l&rsquo;erreur n° 3043, qui est celle que l&rsquo;on obtient lors de la sauvegarde.</p>
<p><img src="http://img12.imageshack.us/img12/5913/iconarrowp.gif" alt="" /> <strong>Activer CHECKSUM pour toutes les sauvegardes</strong></p>
<p>Cela devrait aussi faire partie de la checklist d&rsquo;installation de SQL Server, d&rsquo;autant que c&rsquo;est d&rsquo;une simplicité enfantine : il suffit d&rsquo;activer le drapeau de trace n° 3023 :</p>
<div class="codecolorer-container tsql vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="tsql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000FF;">DBCC</span> TRACEON <span style="color: #808080;">&#40;</span><span style="color: #000;">3023</span>, <span style="color: #808080;">-</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span></div></td></tr></tbody></table></div>
<p>Après avoir exécuté cette instruction, si nous ré-exécutons maintenant la commande suivante :</p>
<div class="codecolorer-container tsql vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br /></div></td><td><div class="tsql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000FF;">BACKUP</span> <span style="color: #0000FF;">DATABASE</span> TEST_CHECKSUM<br />
<span style="color: #0000FF;">TO</span> <span style="color: #0000FF;">DISK</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'F:\SQLServer2012\TEST_CHECKSUM.bak'</span><br />
<span style="color: #0000FF;">WITH</span> COMPRESSION<br />
GO</div></td></tr></tbody></table></div>
<p>Nous obtenons l&rsquo;erreur que nous avions obtenue lorsque nous avions ajouté l&rsquo;option CHECKSUM :</p>
<blockquote><p>Msg 3043, Level 16, State 1, Line 1<br />
BACKUP &lsquo;TEST_CHECKSUM&rsquo; detected an error on page (1:228) in file &lsquo;C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\TEST_CHECKSUM.mdf&rsquo;.<br />
Msg 3013, Level 16, State 1, Line 1<br />
BACKUP DATABASE is terminating abnormally.</p></blockquote>
<p>Ceci est très pratique :</p>
<p>&#8211; Si l&rsquo;on prend les sauvegardes de base de données à l&rsquo;aide de <a href="http://blog.developpez.com/elsuket/p11161/agent-sql-server/supporter_plan_de_maintenance">plans de maintenance</a>, il est impossible de spécifier que l&rsquo;on souhaite avoir la somme de contrôle;<br />
&#8211; Si l&rsquo;on code une procédure stockée qui s&rsquo;en charge, cela évite d&rsquo;avoir à l&rsquo;ajouter dans le code.</p>
<p>D&rsquo;autre part si la <a href="http://blog.developpez.com/elsuket/p9040/moteur-de-base-de-donnees-sql-server/compresser_tous_les_backups_sans_modifie_2008">compression des sauvegardes est activée au niveau de l&rsquo;instance</a>, on n&rsquo;a plus qu&rsquo;a se soucier de la sauvegarde elle-même !</p>
<p>Bonnes sauvegardes et intégrité de pages à tous !</p>
<p>ElSüket.</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Une procédure stockée pour connaître l&#8217;état des fichiers du journal des transactions</title>
		<link>https://blog.developpez.com/elsuket/p11777/moteur-de-base-de-donnees-sql-server/une-procedure-stockee-pour-connaitre-letat-des-fichiers-du-journal-des-transactions</link>
		<comments>https://blog.developpez.com/elsuket/p11777/moteur-de-base-de-donnees-sql-server/une-procedure-stockee-pour-connaitre-letat-des-fichiers-du-journal-des-transactions#comments</comments>
		<pubDate>Mon, 11 Feb 2013 05:12:22 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Moteur de base de données SQL Server]]></category>
		<category><![CDATA[Non documenté]]></category>
		<category><![CDATA[DBCC]]></category>
		<category><![CDATA[journal]]></category>
		<category><![CDATA[log]]></category>
		<category><![CDATA[transaction]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/elsuket/?p=199</guid>
		<description><![CDATA[Ha ! le fichier du journal des transactions, l&#8217;option de récupération, son grossissement, &#8230; et bien sûr la fameuse transaction restée ouverte qui a provoqué une explosion de la taille de celui-ci, allant jusqu&#8217;à remplir le volume disque qui l&#8217;héberge &#8230; <a href="https://blog.developpez.com/elsuket/p11777/moteur-de-base-de-donnees-sql-server/une-procedure-stockee-pour-connaitre-letat-des-fichiers-du-journal-des-transactions">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Ha ! le fichier du journal des transactions, l&rsquo;option de récupération, son grossissement, &#8230; et bien sûr la fameuse transaction restée ouverte qui a provoqué une explosion de la taille de celui-ci, allant jusqu&rsquo;à remplir le volume disque qui l&rsquo;héberge : tout autant de paramétrages et d&rsquo;investigations fastidieuses.</p>
<p>Alors comme je n&rsquo;aime pas beaucoup répéter plusieurs fois les mêmes opérations sans avoir un script, ou mieux, une procédure stockée sous la main, j&rsquo;ai créé la suivante, dont voici le script de création :</p>
<div class="codecolorer-container tsql vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br />127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br />137<br />138<br />139<br />140<br />141<br />142<br />143<br />144<br />145<br />146<br />147<br />148<br />149<br />150<br />151<br />152<br />153<br />154<br />155<br />156<br />157<br />158<br />159<br />160<br />161<br />162<br />163<br />164<br />165<br />166<br />167<br />168<br />169<br />170<br />171<br />172<br />173<br />174<br />175<br />176<br />177<br />178<br />179<br />180<br />181<br />182<br />183<br />184<br />185<br />186<br />187<br />188<br />189<br />190<br />191<br />192<br />193<br />194<br />195<br />196<br />197<br />198<br />199<br />200<br />201<br />202<br />203<br />204<br />205<br />206<br />207<br />208<br />209<br />210<br />211<br />212<br />213<br />214<br />215<br /></div></td><td><div class="tsql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0000FF;">USE</span> master<br />
GO<br />
<br />
<span style="color: #008080;">-----------------------------------------------------------------</span><br />
<span style="color: #008080;">-- Nicolas Souquet - 09/02/2013</span><br />
<span style="color: #008080;">-- LA PROCEDURE STOCKEE SUIVANTE EST DONNEE EN TANT QUE TELLE</span><br />
<span style="color: #008080;">-- SON UTILISATION ET SA MODIFICATION EST A VOS RISQUES ET PÉRILS</span><br />
<span style="color: #008080;">-----------------------------------------------------------------</span><br />
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">PROCEDURE</span> <span style="color: #808080;">&#91;</span>dbo<span style="color: #808080;">&#93;</span>.<span style="color: #808080;">&#91;</span>sp__log_space_get<span style="color: #808080;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @_database_name sysname <span style="color: #808080;">=</span> <span style="color: #808080;">NULL</span><br />
<span style="color: #0000FF;">AS</span><br />
<span style="color: #0000FF;">BEGIN</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SET</span> <span style="color: #0000FF;">NOCOUNT</span> <span style="color: #0000FF;">ON</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">DECLARE</span> @<span style="color: #0000FF;">sql</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">256</span><span style="color: #808080;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- Récupération de l'espace disque disponible</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- sur tous les volumes visibles par l'instance SQL Server</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">DECLARE</span> @free_disk_space <span style="color: #0000FF;">TABLE</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; drive_letter <span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , free_space_MB <span style="color: #0000FF;">int</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span>&nbsp; <span style="color: #0000FF;">INTO</span> @free_disk_space<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">EXEC</span>&nbsp; &nbsp; master.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">xp_fixeddrives</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- Récupération de l'occupation des fichiers des journaux de transaction</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- de toutes les bases de données hébergées par l'instance SQL Server</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">DECLARE</span> @dbcc_sqlperf_logspace <span style="color: #0000FF;">TABLE</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; database_name sysname<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , log_size <span style="color: #0000FF;">float</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , log_space_used_pct <span style="color: #0000FF;">decimal</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">5</span>,<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , status <span style="color: #0000FF;">tinyint</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span>&nbsp; <span style="color: #0000FF;">INTO</span> @dbcc_sqlperf_logspace<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">EXEC</span>&nbsp; &nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'DBCC SQLPERF(logspace)'</span><span style="color: #808080;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- Récupération de la liste de tous les fichiers virtuels</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- du fichier du journal des transactions de toutes les bases de données</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- disponibles, hébergées par l'instance SQL Server</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">DECLARE</span> @dbcc_loginfo <span style="color: #0000FF;">TABLE</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recovery_unit_id <span style="color: #0000FF;">tinyint</span> <span style="color: #008080;">-- new column in SQL Server 2012</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , fileid <span style="color: #0000FF;">tinyint</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , file_size <span style="color: #0000FF;">bigint</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , start_offset <span style="color: #0000FF;">bigint</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , f_seq_no <span style="color: #0000FF;">bigint</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , status <span style="color: #0000FF;">tinyint</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , parity <span style="color: #0000FF;">tinyint</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , create_LSN <span style="color: #0000FF;">varbinary</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">max</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- Cas où l'on souhaite réaliser l'audit du fichier du journal des transactions</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- de toutes les bases de données hébergées par l'instance SQL Server</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- Dans ce cas le paramètre d'entrée @_database_name est à NULL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">IF</span> @_database_name <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NULL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">DECLARE</span> @dbcc_loginfo_all_db <span style="color: #0000FF;">TABLE</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; database_name <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , log_file_id <span style="color: #0000FF;">tinyint</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , vlf_count <span style="color: #0000FF;">smallint</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- Récupération de liste de toutes les bases de données disponibles</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- (pas une capture iOFFLINE, SINGLE_USER, ou ... SUSPECT ;))</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span>&nbsp; <span style="color: #0000FF;">INTO</span> @dbcc_loginfo_all_db<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; database_name<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , log_file_id<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , vlf_count<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; D.<span style="color: #202020;">name</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , MF.<span style="color: #FF00FF;">file_id</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #000;">0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sys.<span style="color: #202020;">databases</span> <span style="color: #0000FF;">AS</span> D<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>&nbsp; &nbsp; &nbsp; sys.<span style="color: #202020;">master_files</span> <span style="color: #0000FF;">AS</span> MF<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> D.<span style="color: #202020;">database_id</span> <span style="color: #808080;">=</span> MF.<span style="color: #202020;">database_id</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; D.<span style="color: #202020;">source_database_id</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NULL</span> <span style="color: #008080;">-- ce n'est pas une capture instantanée de base de données</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">AND</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; D.<span style="color: #202020;">state_desc</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'ONLINE'</span> <span style="color: #008080;">-- la base de données est disponible</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">AND</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; D.<span style="color: #202020;">user_access_desc</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'MULTI_USER'</span> <span style="color: #008080;">-- la base de données est à l'écoute de connexions utilisateur</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">AND</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MF.<span style="color: #202020;">type_desc</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'LOG'</span> <span style="color: #008080;">-- le type de fichier est &quot;journal des transactions&quot;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- Pour chaque base de données, récupération de la liste des fichiers virtuels</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- du fichier du journal des transactions pour toutes les bases de données disponibles</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHILE</span> <span style="color: #808080;">EXISTS</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span>&nbsp; <span style="color: #808080;">*</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; @dbcc_loginfo_all_db<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> &nbsp; vlf_count <span style="color: #808080;">=</span> <span style="color: #000;">0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span>&nbsp; <span style="color: #0000FF;">TOP</span> <span style="color: #000;">1</span> @_database_name <span style="color: #808080;">=</span> database_name<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; @dbcc_loginfo_all_db<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> &nbsp; vlf_count <span style="color: #808080;">=</span> <span style="color: #000;">0</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @<span style="color: #0000FF;">sql</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'DBCC LOGINFO ('</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> @_database_name <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">')'</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span>&nbsp; <span style="color: #0000FF;">INTO</span> @dbcc_loginfo<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">EXEC</span>&nbsp; &nbsp; <span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">sql</span><span style="color: #808080;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;<span style="color: #0000FF;">WITH</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CTE <span style="color: #0000FF;">AS</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span>&nbsp; fileid<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">*</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> vlf_count<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; @dbcc_loginfo<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">GROUP</span> &nbsp; <span style="color: #0000FF;">BY</span> fileid<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">UPDATE</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @dbcc_loginfo_all_db<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SET</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vlf_count <span style="color: #808080;">=</span> C.<span style="color: #202020;">vlf_count</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @dbcc_loginfo_all_db <span style="color: #0000FF;">AS</span> DLAD<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>&nbsp; &nbsp; &nbsp; CTE <span style="color: #0000FF;">AS</span> C<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> DLAD.<span style="color: #202020;">log_file_id</span> <span style="color: #808080;">=</span> C.<span style="color: #202020;">fileid</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DLAD.<span style="color: #202020;">database_name</span> <span style="color: #808080;">=</span> @_database_name<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">DELETE</span>&nbsp; <span style="color: #0000FF;">FROM</span> @dbcc_loginfo<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- Résultat final, avec addition de quelques détails</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LS.<span style="color: #202020;">database_name</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , D.<span style="color: #202020;">recovery_model_desc</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , MF.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> logical_name<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , FDS.<span style="color: #202020;">drive_letter</span> <span style="color: #0000FF;">AS</span> transaction_log_file_stored_on<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , FDS.<span style="color: #202020;">free_space_MB</span> <span style="color: #0000FF;">AS</span> volume_free_space_MB<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>LS.<span style="color: #202020;">log_size</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">decimal</span><span style="color: #808080;">&#40;</span><span style="color: #000;">38</span>,<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> transaction_log_file_size_MB<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , LS.<span style="color: #202020;">log_space_used_pct</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span>LS.<span style="color: #202020;">log_space_used_pct</span> <span style="color: #808080;">/</span> <span style="color: #000;">100.0</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">*</span> LS.<span style="color: #202020;">log_size</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">decimal</span><span style="color: #808080;">&#40;</span><span style="color: #000;">38</span>,<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> log_used_size_MB<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #0000FF;">CASE</span> MF.<span style="color: #202020;">is_percent_growth</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHEN</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">THEN</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>MF.<span style="color: #202020;">growth</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">3</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' %'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ELSE</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>MF.<span style="color: #202020;">growth</span> <span style="color: #808080;">/</span> <span style="color: #000;">128</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' MB'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span> <span style="color: #0000FF;">AS</span> growth &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; , D.<span style="color: #202020;">log_reuse_wait_desc</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , DLAD.<span style="color: #202020;">vlf_count</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , MF.<span style="color: #202020;">physical_name</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sys.<span style="color: #202020;">master_files</span> <span style="color: #0000FF;">AS</span> MF<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>&nbsp; &nbsp; &nbsp; @dbcc_sqlperf_logspace <span style="color: #0000FF;">AS</span> LS<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> LS.<span style="color: #202020;">database_name</span> <span style="color: #808080;">=</span> <span style="color: #FF00FF;">DB_NAME</span><span style="color: #808080;">&#40;</span>MF.<span style="color: #202020;">database_id</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>&nbsp; &nbsp; &nbsp; @free_disk_space <span style="color: #0000FF;">AS</span> FDS<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> FDS.<span style="color: #202020;">drive_letter</span> <span style="color: #808080;">=</span> <span style="color: #0000FF;">LEFT</span><span style="color: #808080;">&#40;</span>MF.<span style="color: #202020;">physical_name</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">COLLATE</span> database_default<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>&nbsp; &nbsp; &nbsp; sys.<span style="color: #202020;">databases</span> <span style="color: #0000FF;">AS</span> D<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> D.<span style="color: #202020;">name</span> <span style="color: #808080;">=</span> LS.<span style="color: #202020;">database_name</span> <span style="color: #0000FF;">COLLATE</span> database_default<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>&nbsp; &nbsp; &nbsp; @dbcc_loginfo_all_db <span style="color: #0000FF;">AS</span> DLAD<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> DLAD.<span style="color: #202020;">database_name</span> <span style="color: #808080;">=</span> D.<span style="color: #202020;">name</span> <span style="color: #0000FF;">COLLATE</span> database_default<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">AND</span> DLAD.<span style="color: #202020;">log_file_id</span> <span style="color: #808080;">=</span> MF.<span style="color: #FF00FF;">file_id</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">AND</span> DLAD.<span style="color: #202020;">database_name</span> <span style="color: #808080;">=</span> <span style="color: #FF00FF;">DB_NAME</span><span style="color: #808080;">&#40;</span>MF.<span style="color: #202020;">database_id</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MF.<span style="color: #202020;">type_desc</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'LOG'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">AND</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; D.<span style="color: #202020;">source_database_id</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NULL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">AND</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; D.<span style="color: #202020;">state_desc</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'ONLINE'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">AND</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; D.<span style="color: #202020;">user_access_desc</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'MULTI_USER'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ELSE</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- Si la base de données que l'on souhaite auditer n'existe pas ==&amp;gt; exception</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">IF</span> <span style="color: #FF00FF;">DB_ID</span><span style="color: #808080;">&#40;</span>@_database_name<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NULL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">RAISERROR</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'The database named '</span><span style="color: #FF0000;">'%s'</span><span style="color: #FF0000;">' does not exist'</span>, <span style="color: #000;">16</span>, <span style="color: #000;">1</span>, @_database_name<span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">RETURN</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- récupération de la liste des fichiers virtuels</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- du fichier du journal des transactions de la base de données choisie</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">DECLARE</span> @vlf_count <span style="color: #0000FF;">int</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @<span style="color: #0000FF;">sql</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'DBCC LOGINFO ('</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> @_database_name <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">')'</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">INSERT</span>&nbsp; <span style="color: #0000FF;">INTO</span> @dbcc_loginfo<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">EXEC</span>&nbsp; &nbsp; <span style="color: #808080;">&#40;</span>@<span style="color: #0000FF;">sql</span><span style="color: #808080;">&#41;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- Résultat final, avec addition de quelques détails</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;<span style="color: #0000FF;">WITH</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CTE <span style="color: #0000FF;">AS</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span>&nbsp; fileid<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">*</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> vlf_count<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; @dbcc_loginfo<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">GROUP</span> &nbsp; <span style="color: #0000FF;">BY</span> fileid<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LS.<span style="color: #202020;">database_name</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , D.<span style="color: #202020;">recovery_model_desc</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , MF.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> logical_name<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , FDS.<span style="color: #202020;">drive_letter</span> <span style="color: #0000FF;">AS</span> transaction_log_file_stored_on<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , FDS.<span style="color: #202020;">free_space_MB</span> <span style="color: #0000FF;">AS</span> volume_free_space_MB<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>LS.<span style="color: #202020;">log_size</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">decimal</span><span style="color: #808080;">&#40;</span><span style="color: #000;">38</span>,<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> transaction_log_file_size_MB<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , LS.<span style="color: #202020;">log_space_used_pct</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span>LS.<span style="color: #202020;">log_space_used_pct</span> <span style="color: #808080;">/</span> <span style="color: #000;">100.0</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">*</span> LS.<span style="color: #202020;">log_size</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">decimal</span><span style="color: #808080;">&#40;</span><span style="color: #000;">38</span>,<span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> log_used_size_MB<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #0000FF;">CASE</span> MF.<span style="color: #202020;">is_percent_growth</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHEN</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">THEN</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>MF.<span style="color: #202020;">growth</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">3</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' %'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ELSE</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>MF.<span style="color: #202020;">growth</span> <span style="color: #808080;">/</span> <span style="color: #000;">128</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' MB'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span> <span style="color: #0000FF;">AS</span> growth<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , D.<span style="color: #202020;">log_reuse_wait_desc</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , C.<span style="color: #202020;">vlf_count</span> <span style="color: #0000FF;">AS</span> VLF_count<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , MF.<span style="color: #202020;">physical_name</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sys.<span style="color: #202020;">master_files</span> <span style="color: #0000FF;">AS</span> MF<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>&nbsp; &nbsp; &nbsp; @dbcc_sqlperf_logspace <span style="color: #0000FF;">AS</span> LS<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> LS.<span style="color: #202020;">database_name</span> <span style="color: #808080;">=</span> <span style="color: #FF00FF;">DB_NAME</span><span style="color: #808080;">&#40;</span>MF.<span style="color: #202020;">database_id</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>&nbsp; &nbsp; &nbsp; @free_disk_space <span style="color: #0000FF;">AS</span> FDS<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> FDS.<span style="color: #202020;">drive_letter</span> <span style="color: #808080;">=</span> <span style="color: #0000FF;">LEFT</span><span style="color: #808080;">&#40;</span>MF.<span style="color: #202020;">physical_name</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">COLLATE</span> database_default<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>&nbsp; &nbsp; &nbsp; sys.<span style="color: #202020;">databases</span> <span style="color: #0000FF;">AS</span> D<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> D.<span style="color: #202020;">name</span> <span style="color: #808080;">=</span> LS.<span style="color: #202020;">database_name</span> <span style="color: #0000FF;">COLLATE</span> database_default<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>&nbsp; &nbsp; &nbsp; CTE <span style="color: #0000FF;">AS</span> C<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> C.<span style="color: #202020;">fileid</span> <span style="color: #808080;">=</span> MF.<span style="color: #FF00FF;">file_id</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MF.<span style="color: #202020;">type_desc</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'LOG'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">AND</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LS.<span style="color: #202020;">database_name</span> <span style="color: #808080;">=</span> @_database_name<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">AND</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; D.<span style="color: #202020;">source_database_id</span> <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NULL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">AND</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; D.<span style="color: #202020;">state_desc</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'ONLINE'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">AND</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; D.<span style="color: #202020;">user_access_desc</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'MULTI_USER'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span><br />
<span style="color: #0000FF;">END</span><br />
GO<br />
<br />
<span style="color: #008080;">-- On marque la procédure stockée comme objet système</span><br />
<span style="color: #008080;">-- Dès lors, on pourra l'exécuter dans n'importe quel contexte de base de données</span><br />
<span style="color: #0000FF;">EXEC</span> sp_ms_marksystemobject <span style="color: #FF0000;">'sp__log_space_get'</span></div></td></tr></tbody></table></div>
<p>Elle donne un résultat comme le suivant :</p>
<p><a href="http://blog.developpez.com/elsuket/p11777/moteur-de-base-de-donnees-sql-server/une-procedure-stockee-pour-connaitre-letat-des-fichiers-du-journal-des-transactions/attachment/sp__log_space_get" rel="attachment wp-att-203"><img src="http://blog.developpez.com/elsuket/files/2013/02/sp__log_space_get-300x29.png" alt="sp__log_space_get" width="300" height="29" class="alignnone size-medium wp-image-203" /></a></p>
<p>à savoir :</p>
<p>&#8211; le nom de la base de données<br />
&#8211; l&rsquo;option de récupération de la base de données<br />
&#8211; le nom logique du fichier du journal des transactions (utile pour un DBCC SHRINKFILE après que le journal ait explosé en taille)<br />
&#8211; le volume sur lequel est hébergé le fichier du journal des transactions<br />
&#8211; la quantité d&rsquo;espace disque libre restant sur ce volume<br />
&#8211; la taille actuelle du fichier du journal des transactions<br />
&#8211; le pourcentage d&rsquo;utilisation de ce fichier (taille occupée dans le fichier par rapport à la taille totale du fichier)<br />
&#8211; la taille équivalente à ce pourcentage<br />
&#8211; le paramètre de grossissement de ce fichier<br />
&#8211; ce qui empêche actuellement la troncature de l&rsquo;espace occupé dans ce fichier<br />
&#8211; le chemin complet du fichier</p>
<p>On peut utiliser cette procédure stockée sous SQL Server 2005, 2008 et 2012.<br />
Elle fonctionne également pour les bases de données ayant plusieurs fichiers du journal des transactions</p>
<p>Bonne gestion des fichiers du journal des transactions !</p>
<p>ElSüket <img src="https://blog.developpez.com/elsuket/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley" /></p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
