<?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>Transactive SQL - le blog de SQL_EVAN &#187; Boucle sur chaque base</title>
	<atom:link href="https://blog.developpez.com/transactivesql/ptag/boucle-sur-chaque-base/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.developpez.com/transactivesql</link>
	<description>Tout sur SQL Server</description>
	<lastBuildDate>Mon, 23 Sep 2013 15:16:45 +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>SQL Server &#8211; Sp_MSForEachDB une façon simple de requêter toutes les bases de données d&#8217;une instance</title>
		<link>https://blog.developpez.com/transactivesql/p12231/t-sql/sql-server-sp_msforeachdb-une-facon-simple-de-requeter-toutes-les-bases-de-donnees-dune-instance</link>
		<comments>https://blog.developpez.com/transactivesql/p12231/t-sql/sql-server-sp_msforeachdb-une-facon-simple-de-requeter-toutes-les-bases-de-donnees-dune-instance#comments</comments>
		<pubDate>Mon, 16 Sep 2013 15:16:23 +0000</pubDate>
		<dc:creator><![CDATA[SQL_EVAN]]></dc:creator>
				<category><![CDATA[Administration de bases de données]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Boucle sur chaque base]]></category>
		<category><![CDATA[sp_MsForEachDB]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/transactivesql/?p=8</guid>
		<description><![CDATA[De temps en temps il est nécessaire d&#8217;exécuter une requête simple sur toute les bases de données d&#8217;une instance SQL Server. Jusqu&#8217;à il n&#8217;y a pas très longtemps j&#8217;ai toujours écrit des requêtes très complexes qui utilisé des CURSOR et des tables temporaires afin d&#8217;effectuer le travail voulu. Et après j&#8217;ai trouvé la procédure stockée [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>De temps en temps il est nécessaire d&rsquo;exécuter une requête simple sur toute les bases de données d&rsquo;une instance SQL Server.</p>
<p>Jusqu&rsquo;à il n&rsquo;y a pas très longtemps j&rsquo;ai toujours écrit des requêtes très complexes qui utilisé des CURSOR et des tables temporaires afin d&rsquo;effectuer le travail voulu.</p>
<p>Et après j&rsquo;ai trouvé la procédure stockée non-documentée <strong>Sp_MSForEachDB. </strong></p>
<p>Pour expliquer simplement : cette procédure stockée laisse l&rsquo;utilisateur coller un variable ? dans une requête qui prend la place de la base de données et elle exécute la même requête pour chaque base attachée à l&rsquo;instance SQL Server.</p>
<p>Voici une petite requête pour montrer comment ça fonctionne :</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;">EXEC</span> sys<span style="color: #66cc66;">.</span>sp_MSforeachdb <span style="color: #ff0000;">'USE ?;<br />
SELECT DB_ID(), DB_NAME()'</span></div></div>
<p>Vous devriez voir une like de toute les bases de données attachées à votre instance par identifiant et nom.</p>
<p>Vous allez voir que le moteur retourne plusieurs jeux de résultats dans le fenêtre SSMS. C&rsquo;est à cause du fait que sp_MSforeachdb itère sur chaque base. Ce n&rsquo;est pas une transaction unique.</p>
<p>Pour l&rsquo;utilisateur avancé qui aimerait industrialiser l&rsquo;utilité de cette procédure stockée vous pouvez simplement créer une table temporaire qui stocke les résultats et ensuite vous allez pouvoir requête sur cette table à peu plus loin dans le code T-SQL.</p>
<p>Voici un exemple que j&rsquo;ai utilisé pour contrôler le taux de fragmentation de toutes les indexes fragmentés de plus que 5%:</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;">TABLE</span> dbo<span style="color: #66cc66;">.</span>#FragTab<br />
<span style="color: #66cc66;">&#40;</span>DB_Name <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">100</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
<span style="color: #66cc66;">&#91;</span>Schema<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
<span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">TABLE</span><span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
<span style="color: #66cc66;">&#91;</span><span style="color: #993333; font-weight: bold;">INDEX</span><span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
avg_fragmentation_in_percent <span style="color: #993333; font-weight: bold;">FLOAT</span><span style="color: #66cc66;">,</span><br />
REBUILD_Necessary BIT<span style="color: #66cc66;">,</span><br />
REORGANISE_Necessary BIT<span style="color: #66cc66;">&#41;</span><br />
<br />
<span style="color: #993333; font-weight: bold;">EXEC</span> sys<span style="color: #66cc66;">.</span>sp_MSforeachdb<br />
<span style="color: #ff0000;">'<br />
USE ?;<br />
INSERT INTO #FragTab<br />
SELECT<br />
DBs.name as '</span><span style="color: #ff0000;">'DB_Name'</span><span style="color: #ff0000;">',<br />
dbschemas.[name] as '</span><span style="color: #ff0000;">'Schema'</span><span style="color: #ff0000;">',<br />
dbtables.[name] as '</span><span style="color: #ff0000;">'Table'</span><span style="color: #ff0000;">',<br />
dbindexes.[name] as '</span><span style="color: #ff0000;">'Index'</span><span style="color: #ff0000;">',<br />
indexstats.avg_fragmentation_in_percent,<br />
CASE WHEN indexstats.avg_fragmentation_in_percent &amp;gt; 30 THEN 1 ELSE 0 END as REBUILD_Necessary,<br />
CASE WHEN indexstats.avg_fragmentation_in_percent BETWEEN 5 AND 30 THEN 1 ELSE 0 END as REORGANISE_Necessary<br />
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS indexstats<br />
INNER JOIN sys.tables dbtables on dbtables.[object_id] = indexstats.[object_id]<br />
INNER JOIN sys.schemas dbschemas on dbtables.[schema_id] = dbschemas.[schema_id]<br />
INNER JOIN sys.indexes AS dbindexes ON dbindexes.[object_id] = indexstats.[object_id]<br />
AND indexstats.index_id = dbindexes.index_id<br />
LEFT OUTER JOIN sys.databases as DBs ON DBs.database_id = indexstats.database_id<br />
WHERE indexstats.database_id = DB_ID()<br />
AND indexstats.avg_fragmentation_in_percent &amp;gt; 5<br />
AND dbindexes.name IS NOT NULL'</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> #FragTab<br />
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> #FragTab</div></div>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
