<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Mytips Tech Weblog</title>
	<atom:link href="http://mytips.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://mytips.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Fri, 28 Sep 2007 21:17:45 +0000</lastBuildDate>
	<language>fr</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='mytips.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Mytips Tech Weblog</title>
		<link>http://mytips.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://mytips.wordpress.com/osd.xml" title="Mytips Tech Weblog" />
	<atom:link rel='hub' href='http://mytips.wordpress.com/?pushpress=hub'/>
		<item>
		<title>SQL Server 2005 &#8211; Récursivité hiérarchique pour concaténation de chaîne</title>
		<link>http://mytips.wordpress.com/2007/09/28/sql-server-2005-recursivite-hierarchique-pour-concatenation-de-chaine/</link>
		<comments>http://mytips.wordpress.com/2007/09/28/sql-server-2005-recursivite-hierarchique-pour-concatenation-de-chaine/#comments</comments>
		<pubDate>Fri, 28 Sep 2007 21:17:45 +0000</pubDate>
		<dc:creator>mytips</dc:creator>
				<category><![CDATA[BDD]]></category>
		<category><![CDATA[sql server]]></category>
		<category><![CDATA[CTE]]></category>
		<category><![CDATA[hierarchical]]></category>
		<category><![CDATA[recursif]]></category>

		<guid isPermaLink="false">http://mytips.wordpress.com/2007/09/28/sql-server-2005-recursivite-hierarchique-pour-concatenation-de-chaine/</guid>
		<description><![CDATA[Tip SQL Server 2005 Concaténation de chaînes d&#8217;une base de donnée hiérarchique Article du 21 septembre 2007 Récursivité à l&#8217;aide des CTE de SQL Server 2005 Ce billet explique comment faire pour récupérer une chaine de caractère qui est la concaténation de toutes les valeurs d&#8217;un champs dans les différents niveaux d&#8217;une base de donnée [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mytips.wordpress.com&amp;blog=1811357&amp;post=3&amp;subd=mytips&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2>Tip SQL Server 2005</h2>
<h3>Concaténation de chaînes d&#8217;une base de donnée hiérarchique</h3>
<p>Article du 21 septembre 2007</p>
<h4>Récursivité à l&#8217;aide des CTE de SQL Server 2005</h4>
<p>Ce billet explique comment faire pour récupérer une chaine de caractère qui est la concaténation de toutes les valeurs d&#8217;un champs dans les différents niveaux d&#8217;une base de donnée hierarchique (en SQL Server 2005).</p>
<p>Un exemple pour montrer à quel résultat vous arriverez, exemple d&#8217;une base de donnée des états, pays, zones géographique :<br />
Structure de la table :<br />
<code><br />
CREATE TABLE [dbo].[T_LOCATION] (<br />
[ID_LOCATION] [int] IDENTITY (1, 1) NOT NULL ,<br />
[LOCATION_NAME] [varchar] (255) NOT NULL ,<br />
[IDR_PARENT_LOCATION] [int],<br />
CONSTRAINT PK_LOCATION PRIMARY KEY CLUSTERED(ID_LOCATION)<br />
)<br />
</code></p>
<p>Nous avons les enregistrements suivants :</p>
<table style="border-width:1px;" border="0">
<tr>
<th>ID_LOCATION</th>
<th>LOCATION_NAME</th>
<th>IDR_PARENT_LOCATION</th>
</tr>
<tr>
<td>1</td>
<td>Europe</td>
<td>NULL</td>
</tr>
<tr>
<td>2</td>
<td>France</td>
<td>1</td>
</tr>
<tr>
<td>3</td>
<td>Espagne</td>
<td>1</td>
</tr>
<tr>
<td>4</td>
<td>Amérique du nord</td>
<td>NULL</td>
</tr>
<tr>
<td>5</td>
<td>Canada</td>
<td>4</td>
</tr>
<tr>
<td>6</td>
<td>USA</td>
<td>4</td>
</tr>
<tr>
<td>7</td>
<td>Utah</td>
<td>6</td>
</tr>
<tr>
<td>8</td>
<td>Texas</td>
<td>6</td>
</tr>
</table>
<p>Je veux qu&#8217;en appellant ma fonction de concaténation avec comme argument l&#8217;identifiant d&#8217;un lieu, il me renvoit le nom de ce lieu et de ses &#8220;parents&#8221;, concaténés.</p>
<p>Exemple :<br />
<code><br />
Select getLocationHierarchy(8)<br />
Resultat: "Texas USA Amérique du Nord"</code><br />
<code><br />
Select getLocationHierarchy(3)<br />
Resultat: "Espagne Europe"<br />
</code><br />
Pour cela on utilise une nouveauté dans SQL Server 2005, les Common Table Expressions (CTE).</p>
<p>Bon voilà comment j&#8217;ai fait pour avoir le résultat ci-dessus, un exemple valant mieux qu&#8217;un long discours!</p>
<p><code><br />
CREATE FUNCTION  getLocationHierarchy  (@id as INT)<br />
RETURNS VARCHAR(3000) WITH SCHEMABINDING AS<br />
BEGIN<br />
DECLARE @hierarchy VARCHAR(3000)<br />
SET @hierarchy = ''; /* j'aime bien initialiser les variables... */<br />
-- Declaration de la CTE<br />
with temp as<br />
(<br />
-- On récupère le premier enregistrement, celui qui correspond à l'ID passé<br />
SELECT<br />
CAST (LOCATION_NAME as VARCHAR(3000)) AS LOCATION_NAME,IDR_PARENT_LOCATION,ID_LOCATION  FROM dbo.T_LOCATION WHERE ID_LOCATION  = @id<br />
-- On va chercher les parents et on concatène les noms<br />
UNION ALL<br />
SELECT CAST ((t.LOCATION_NAME+ ' ' +t1.LOCATION_NAME) AS VARCHAR(3000)) AS LOCATION_NAME ,t1.IDR_PARENT_LOCATION,t1.ID_LOCATION<br />
FROM dbo.T_LOCATION t1 INNER JOIN TEMP t ON t.IDR_PARENT_LOCATION = t1.ID_LOCATION)<br />
-- récupération de la chaine concaténée<br />
SELECT @hierarchy = LOCATION_NAME FROM temp WHERE IDR_PARENT_LOCATION IS NULL<br />
RETURN (@hierarchy)<br />
END</code></p>
<p>Voilà, si vous avez des commentaires, n&#8217;hésitez pas!</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/mytips.wordpress.com/3/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/mytips.wordpress.com/3/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mytips.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mytips.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mytips.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mytips.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mytips.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mytips.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mytips.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mytips.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mytips.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mytips.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mytips.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mytips.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mytips.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mytips.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mytips.wordpress.com&amp;blog=1811357&amp;post=3&amp;subd=mytips&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://mytips.wordpress.com/2007/09/28/sql-server-2005-recursivite-hierarchique-pour-concatenation-de-chaine/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/0aeb54503c83c5f9b42314f1801ecf3e?s=96&#38;d=identicon" medium="image">
			<media:title type="html">mytips</media:title>
		</media:content>
	</item>
	</channel>
</rss>
