<?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; servuer lié récursif</title>
	<atom:link href="https://blog.developpez.com/transactivesql/ptag/servuer-lie-recursif/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>
	</channel>
</rss>
