<?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; Administration de bases de données</title>
	<atom:link href="https://blog.developpez.com/transactivesql/pcategory/administration-de-bases-de-donnees/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; Comment appeler une procédure stockée dans une vue ou fonction avec un serveur lié récursif et OPENQUERY</title>
		<link>https://blog.developpez.com/transactivesql/p12236/t-sql/sql-server-comment-appeler-une-procedure-stockee-dans-une-vue-ou-fonction-avec-un-serveur-lie-recursif-et-openquery</link>
		<comments>https://blog.developpez.com/transactivesql/p12236/t-sql/sql-server-comment-appeler-une-procedure-stockee-dans-une-vue-ou-fonction-avec-un-serveur-lie-recursif-et-openquery#comments</comments>
		<pubDate>Tue, 17 Sep 2013 14:32:46 +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[Astuce]]></category>
		<category><![CDATA[servuer lié récursif]]></category>
		<category><![CDATA[Souplesse]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/transactivesql/?p=17</guid>
		<description><![CDATA[Vous le savez peut être que SQL Server ne laisse pas passer les commandes CREATE VIEW basé sur les résultats des procédures stockées. Des fois ceci peut être pénalisant! Imaginons une requête qui boucle sur chaque base de données d&#8217;une instance et qui insère les résultats dans une table temporaire de type #table. CREATE TABLE [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Vous le savez peut être que SQL Server ne laisse pas passer les commandes CREATE VIEW basé sur les résultats des procédures stockées. Des fois ceci peut être pénalisant!</p>
<p>Imaginons une requête qui boucle sur chaque base de données d&rsquo;une instance et qui insère les résultats dans une table temporaire de type #table.</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> #DB_NAMES <span style="color: #66cc66;">&#40;</span>DB_NAMES <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span><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 <span style="color: #ff0000;">'USE ?<br />
INSERT INTO SELECT '</span><span style="color: #ff0000;">'?'</span><span style="color: #ff0000;">''</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> #DB_NAMES<br />
<br />
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> #DB_NAMES</div></div>
<p>Ce code ne peut pas être incorporé dans une vue à cause du fait qu&rsquo;il se base sur une #table. Vous pouvez, cependant créer une procédure stockée avec la requête.</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;">USE</span> <span style="color: #66cc66;">&#91;</span>VOTRE_BASE<span style="color: #66cc66;">&#93;</span><br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<span style="color: #993333; font-weight: bold;">SET</span> ANSI_NULLS OFF<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<span style="color: #993333; font-weight: bold;">SET</span> QUOTED_IDENTIFIER OFF<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">PROCEDURE</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>LOOPBACKTEST<span style="color: #66cc66;">&#93;</span><br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> #DB_NAMES <span style="color: #66cc66;">&#40;</span>DB_NAMES <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span><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 <span style="color: #ff0000;">'USE ?<br />
INSERT INTO SELECT '</span><span style="color: #ff0000;">'?'</span><span style="color: #ff0000;">''</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> #DB_NAMES<br />
<br />
<span style="color: #993333; font-weight: bold;">DROP</span> <span style="color: #993333; font-weight: bold;">TABLE</span> #DB_NAMES</div></div>
<p>Maintenant, qu&rsquo;est-ce qui se passe si on veut requêter sur les résultats de la procédure stockée? C&rsquo;est là le problème. La réponse se trouve dans la clause OPENQUERY. Il s&rsquo;agit d&rsquo;une feinte/workaround mais SQL Server laisse passer des requêtes sur les résultats des requêtes sur des serveurs liés. Mais ceci est prévu pour marcher avec des serveurs distants non? Pas forcément! Vous pouvez créer ce que l&rsquo;on appelle un serveur lié récursif.</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> sp_addlinkedserver @server <span style="color: #66cc66;">=</span> N<span style="color: #ff0000;">'LOOPBACK'</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; @srvproduct <span style="color: #66cc66;">=</span> N<span style="color: #ff0000;">' '</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; @provider <span style="color: #66cc66;">=</span> N<span style="color: #ff0000;">'SQLNCLI'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; @datasrc <span style="color: #66cc66;">=</span> N<span style="color: #ff0000;">'VOTRE NOM D'</span><span style="color: #ff0000;">'INSTANCE SQL SERVER ICI'</span><span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; @catalog <span style="color: #66cc66;">=</span> N<span style="color: #ff0000;">'VOTRE_BASE'</span></div></div>
<p>Une fois terminé vous pouvez requête sur les résultats de votre procédure stockée avec la syntaxe suivant :</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span>&nbsp; <span style="color: #66cc66;">*</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span>&nbsp; &nbsp; OPENQUERY<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>LOOPBACK<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'<br />
EXEC [YOUR_DATABASE].[dbo].[LOOPBACKTEST]'</span><span style="color: #66cc66;">&#41;</span></div></div>
<p>Et vous pouvez même créer une vue qui se base sur la requête pour pouvoir requête souplement sur les résulats.</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">VIEW</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>LOOPBACKTESTVIEW<span style="color: #66cc66;">&#93;</span><br />
<span style="color: #993333; font-weight: bold;">AS</span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span>&nbsp; <span style="color: #66cc66;">*</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span>&nbsp; &nbsp; OPENQUERY<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>LOOPBACK<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'<br />
EXEC [VOTRE_BASE].[dbo].[LOOPBACKTEST]'</span><span style="color: #66cc66;">&#41;</span></div></div>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span>&nbsp; <span style="color: #66cc66;">*</span><br />
dbo<span style="color: #66cc66;">.</span>LOOPBACKTESTVIEW<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> DB_NAMES <span style="color: #993333; font-weight: bold;">LIKE</span> <span style="color: #ff0000;">'VOTRE_%'</span></div></div>
<p>Sachez que ceci est simplement un exemple inutile mais vous pouvez faire ce que vous voulez dans la première requête afin de créer une contrôle introspective par exemple. Tout est possible <img src="https://blog.developpez.com/transactivesql/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley" /></p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Server &#8211; Comment visualiser très facilement l&#8217;espace disque restant à partir de SSMS</title>
		<link>https://blog.developpez.com/transactivesql/p12233/t-sql/sql-server-comment-visualiser-tres-facilement-lespace-disque-restant-a-partir-de-ssms</link>
		<comments>https://blog.developpez.com/transactivesql/p12233/t-sql/sql-server-comment-visualiser-tres-facilement-lespace-disque-restant-a-partir-de-ssms#comments</comments>
		<pubDate>Mon, 16 Sep 2013 15:40:14 +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[efficace]]></category>
		<category><![CDATA[Pratique]]></category>
		<category><![CDATA[procédure stockée]]></category>
		<category><![CDATA[simple]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[xp_fixeddrives]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/transactivesql/?p=14</guid>
		<description><![CDATA[Des fois il est nécessaire de connaitre l&#8217;espace disque d&#8217;un serveur quand on n&#8217;a pas de connexion de bureau à distance sur la machine hôte. On peut imaginer des situations qui nécessitent la mise en places des indexes ou de faire des grosses transactions qui pourraient faire grossir la tempdb sans avoir un accès sur [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Des fois il est nécessaire de connaitre l&rsquo;espace disque d&rsquo;un serveur quand on n&rsquo;a pas de connexion de bureau à distance sur la machine hôte. On peut imaginer des situations qui nécessitent la mise en places des indexes ou de faire des grosses transactions qui pourraient faire grossir la tempdb sans avoir un accès sur le serveur.</p>
<p>Il existe un moyen très simple de le faire en une ligne de code!!</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> master<span style="color: #66cc66;">.</span>sys<span style="color: #66cc66;">.</span>xp_fixeddrives</div></div>
<p>Cette magnifique procédure stockée retourne une liste de tous les disques ainsi que leur espace disque restant.</p>
<p>On pourra même imaginer une mise en place industrialiser d&rsquo;un système qui nécessite une contrôle d&rsquo;espace disque avant de se lancer dans sa transaction. Avec cette procédure stockée il n&rsquo;a pas de limit! En dehors de la taille de votre disque dur <img src="https://blog.developpez.com/transactivesql/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley" /></p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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>
