<?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; T-SQL</title>
	<atom:link href="https://blog.developpez.com/elsuket/pcategory/t-sql/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>Promotion sur les livres sur Microsoft Press Store</title>
		<link>https://blog.developpez.com/elsuket/p13058/t-sql/promotion-sur-les-livres-sur-microsoft-press-store</link>
		<comments>https://blog.developpez.com/elsuket/p13058/t-sql/promotion-sur-les-livres-sur-microsoft-press-store#comments</comments>
		<pubDate>Tue, 28 Jun 2016 22:04:59 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Lecture]]></category>
		<category><![CDATA[SQL général]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/elsuket/?p=1257</guid>
		<description><![CDATA[Le livre T-SQL Fundamentals d&#8217;Itzik Ben-Gan, avec qui on a la sensation de réapprendre le T-SQL à chaque ouvrage, va bientôt arriver dans son troisième opus. Pour l&#8217;occasion, Microsoft offre jusqu&#8217;à 40% de remise avec le code PREORDER. Ceci est &#8230; <a href="https://blog.developpez.com/elsuket/p13058/t-sql/promotion-sur-les-livres-sur-microsoft-press-store">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Le livre T-SQL Fundamentals d&rsquo;Itzik Ben-Gan, avec qui on a la sensation de réapprendre le T-SQL à chaque ouvrage, va bientôt arriver dans son <a href="https://www.microsoftpressstore.com/cart/buy.aspx?isbn=9781509302000&amp;partner=76&amp;cmd=add">troisième opus</a>. Pour l&rsquo;occasion, Microsoft offre jusqu&rsquo;à 40% de remise avec le code PREORDER. Ceci est valable jusqu&rsquo;au 31 Juillet 2016.</p>
<p>Bonne lecture !</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Vérifier la validité d&#8217;un SIRET &#8211; Algorithme de Luhn</title>
		<link>https://blog.developpez.com/elsuket/p13056/snippets/verifier-la-validite-dun-siret-algorithme-de-luhn</link>
		<comments>https://blog.developpez.com/elsuket/p13056/snippets/verifier-la-validite-dun-siret-algorithme-de-luhn#comments</comments>
		<pubDate>Thu, 23 Jun 2016 18:54:56 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Utilitaires]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/elsuket/?p=1233</guid>
		<description><![CDATA[Lors d&#8217;un chargement massif de données et/ou d&#8217;une migration, il est possible que la qualité des données soient altérées. Il faut alors tester leur qualité et valider chaque écart. Parmi ceux-ci, on peut trouver les numéro SIRET, qui permet l&#8217;identification &#8230; <a href="https://blog.developpez.com/elsuket/p13056/snippets/verifier-la-validite-dun-siret-algorithme-de-luhn">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Lors d&rsquo;un chargement massif de données et/ou d&rsquo;une migration, il est possible que la qualité des données soient altérées. Il faut alors tester leur qualité et valider chaque écart. Parmi ceux-ci, on peut trouver les numéro SIRET, qui permet l&rsquo;identification d&rsquo;un établissement ou d&rsquo;une entreprise Française.</p>
<p>Cet identifiant numérique est constitué de 14 chiffres, et est articulé en trois parties :</p>
<ul>
<li>le SIREN</li>
<li>le Numéro Interne de Classement, à quatre chiffres : c&rsquo;est l&rsquo;ordre de création de l’établissement par l&rsquo;entreprise Française</li>
<li>une clé de contrôle, constituée d&rsquo;un seul chiffre</li>
</ul>
<p>Ce dernier est calculé à l&rsquo;aide de l&rsquo;<a href="https://fr.wikipedia.org/wiki/Formule_de_Luhn">algorithme de Luhn</a>. Cet algorithme est aussi utilisé pour valider les numéros de carte bleue, et de nombreux identifiants distribués par les administrations de beaucoup de pays. Nous allons nous en servir pour vérifier la validité de SIRETs en l&rsquo;implémentant spécifiquement pour ce type d&rsquo;identifiant.</p>
<p><span id="more-1233"></span></p>
<p>L&rsquo;algorithme est descriptible comme suit :</p>
<ol>
<li>Pour chaque chiffre dont l&rsquo;index dans la chaîne est impair, on le multiplie par 2; si le résultat est supérieur à 9, on additionne les chiffes qui constituent ce résultat. Ceci revient à multiplier par 2 le chiffre, diviser le résultat par 10, puis ajouter la multiplication par 2 de ce même chiffre modulo 10.</li>
<li>On additionne ensuite les chiffres du résultat un à un, avec les chiffres de l&rsquo;identifiant qui n&rsquo;ont pas été traités, c&rsquo;est à dire ceux dont l&rsquo;index dans la chaîne est pair. Ceci inclut le chiffre de l&rsquo;identifiant qui est clé.</li>
<li>Si le résultat obtenu est un multiple de 10, alors l&rsquo;identifiant est valide. Ceci revient à vérifier que le résultat modulo 10 vaut zéro.</li>
</ol>
<p>La traduction en SQL donne :</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 /></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;">CASE</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHEN</span> laColonneSIRET <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">THEN</span> <span style="color: #808080;">NULL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHEN</span> <span style="color: #FF00FF;">LEN</span><span style="color: #808080;">&#40;</span>laColonneSIRET<span style="color: #808080;">&#41;</span> <span style="color: #808080;">&amp;</span>lt; <span style="color: #000;">14</span> <span style="color: #0000FF;">THEN</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #000;">0</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">bit</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHEN</span> laColonneSIRET <span style="color: #808080;">LIKE&amp;</span>#039;<span style="color: #808080;">%</span><span style="color: #808080;">&#91;</span><span style="color: #808080;">^</span><span style="color: #000;">0</span><span style="color: #808080;">-</span><span style="color: #000;">9</span><span style="color: #808080;">&#93;</span><span style="color: #808080;">%&amp;</span>#039; <span style="color: #0000FF;">THEN</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #000;">0</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">bit</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHEN</span> <span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000;">2</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>laColonneSIRET, <span style="color: #000;">1</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">tinyint</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">10</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">+</span> <span style="color: #000;">2</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>laColonneSIRET, <span style="color: #000;">1</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">tinyint</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">%</span> <span style="color: #000;">10</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">+</span> <span style="color: #000;">2</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>laColonneSIRET, <span style="color: #000;">3</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">tinyint</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">10</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">+</span> <span style="color: #000;">2</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>laColonneSIRET, <span style="color: #000;">3</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">tinyint</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">%</span> <span style="color: #000;">10</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">+</span> <span style="color: #000;">2</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>laColonneSIRET, <span style="color: #000;">5</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">tinyint</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">10</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">+</span> <span style="color: #000;">2</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>laColonneSIRET, <span style="color: #000;">5</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">tinyint</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">%</span> <span style="color: #000;">10</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">+</span> <span style="color: #000;">2</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>laColonneSIRET, <span style="color: #000;">7</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">tinyint</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">10</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">+</span> <span style="color: #000;">2</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>laColonneSIRET, <span style="color: #000;">7</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">tinyint</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">%</span> <span style="color: #000;">10</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">+</span> <span style="color: #000;">2</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>laColonneSIRET, <span style="color: #000;">9</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">tinyint</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">10</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">+</span> <span style="color: #000;">2</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>laColonneSIRET, <span style="color: #000;">9</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">tinyint</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">%</span> <span style="color: #000;">10</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">+</span> <span style="color: #000;">2</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>laColonneSIRET, <span style="color: #000;">11</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">tinyint</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">10</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">+</span> <span style="color: #000;">2</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>laColonneSIRET, <span style="color: #000;">11</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">tinyint</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">%</span> <span style="color: #000;">10</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">+</span> <span style="color: #000;">2</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>laColonneSIRET, <span style="color: #000;">13</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">tinyint</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">10</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">+</span> <span style="color: #000;">2</span> <span style="color: #808080;">*</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>laColonneSIRET, <span style="color: #000;">13</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">tinyint</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">%</span> <span style="color: #000;">10</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">---</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>laColonneSIRET, <span style="color: #000;">2</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">tinyint</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>laColonneSIRET, <span style="color: #000;">4</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">tinyint</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>laColonneSIRET, <span style="color: #000;">6</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">tinyint</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>laColonneSIRET, <span style="color: #000;">8</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">tinyint</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>laColonneSIRET, <span style="color: #000;">10</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">tinyint</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>laColonneSIRET, <span style="color: #000;">12</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">tinyint</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SUBSTRING</span><span style="color: #808080;">&#40;</span>laColonneSIRET, <span style="color: #000;">14</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">tinyint</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span> <span style="color: #808080;">%</span> <span style="color: #000;">10</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span> <span style="color: #0000FF;">THEN</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">bit</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #000;">0</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">bit</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">END</span> <span style="color: #0000FF;">AS</span> is_valid_SIRET</div></td></tr></tbody></table></div>
<p>On peut l&#039;utiliser directement dans une vue, comme dans le pseudo code suivant :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">CREATE VIEW maVue<br />
AS<br />
SELECT&nbsp; desColonnes<br />
&nbsp; &nbsp; &nbsp; &nbsp; , CASE<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN laColonneSIRET IS NULL THEN NULL<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN LEN(laColonneSIRET) &amp;lt; 14 THEN CAST(0 AS bit)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN laColonneSIRET LIKE&amp;#039;%[^0-9]%&amp;#039; THEN CAST(0 AS bit)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN (<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2 * CAST(SUBSTRING(laColonneSIRET, 1, 1) AS tinyint) / 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(laColonneSIRET, 1, 1) AS tinyint) % 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(laColonneSIRET, 3, 1) AS tinyint) / 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(laColonneSIRET, 3, 1) AS tinyint) % 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(laColonneSIRET, 5, 1) AS tinyint) / 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(laColonneSIRET, 5, 1) AS tinyint) % 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(laColonneSIRET, 7, 1) AS tinyint) / 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(laColonneSIRET, 7, 1) AS tinyint) % 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(laColonneSIRET, 9, 1) AS tinyint) / 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(laColonneSIRET, 9, 1) AS tinyint) % 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(laColonneSIRET, 11, 1) AS tinyint) / 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(laColonneSIRET, 11, 1) AS tinyint) % 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(laColonneSIRET, 13, 1) AS tinyint) / 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(laColonneSIRET, 13, 1) AS tinyint) % 10<br />
&nbsp; &nbsp; &nbsp; &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; + CAST(SUBSTRING(laColonneSIRET, 2, 1) AS tinyint)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + CAST(SUBSTRING(laColonneSIRET, 4, 1) AS tinyint)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + CAST(SUBSTRING(laColonneSIRET, 6, 1) AS tinyint)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + CAST(SUBSTRING(laColonneSIRET, 8, 1) AS tinyint)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + CAST(SUBSTRING(laColonneSIRET, 10, 1) AS tinyint)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + CAST(SUBSTRING(laColonneSIRET, 12, 1) AS tinyint)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + CAST(SUBSTRING(laColonneSIRET, 14, 1) AS tinyint)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ) % 10 = 0 THEN CAST(1 AS bit) ELSE CAST(0 AS bit)<br />
&nbsp; &nbsp; &nbsp; &nbsp; END AS is_valid_SIRET<br />
FROM&nbsp; &nbsp; dbo.maTable</div></td></tr></tbody></table></div>
<p>Si l&#039;on souhaite l&#039;utiliser en colonne calculée, on peut utiliser ce CASE directement dans la définition de ladite colonne, ou bien définir une fonction scalaire comme suit :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">CREATE FUNCTION fn_scl_is_valid_SIRET<br />
&nbsp; &nbsp; &nbsp; &nbsp; (<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@_siret char(14)<br />
&nbsp; &nbsp; &nbsp; &nbsp; )<br />
RETURNS bit<br />
WITH SCHEMABINDING<br />
AS<br />
BEGIN<br />
&nbsp; &nbsp; &nbsp; &nbsp; DECLARE @ok bit<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; SELECT&nbsp; @ok = CASE<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN @_siret IS NULL THEN NULL<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN LEN(@_siret) &amp;lt; 14 THEN CAST(0 AS bit)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN @_siret LIKE&amp;#039;%[^0-9]%&amp;#039; THEN CAST(0 AS bit)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN (<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2 * CAST(SUBSTRING(@_siret, 1, 1) AS tinyint) / 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@_siret, 1, 1) AS tinyint) % 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@_siret, 3, 1) AS tinyint) / 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@_siret, 3, 1) AS tinyint) % 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@_siret, 5, 1) AS tinyint) / 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@_siret, 5, 1) AS tinyint) % 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@_siret, 7, 1) AS tinyint) / 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@_siret, 7, 1) AS tinyint) % 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@_siret, 9, 1) AS tinyint) / 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@_siret, 9, 1) AS tinyint) % 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@_siret, 11, 1) AS tinyint) / 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@_siret, 11, 1) AS tinyint) % 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@_siret, 13, 1) AS tinyint) / 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@_siret, 13, 1) AS tinyint) % 10<br />
&nbsp; &nbsp; &nbsp; &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; + CAST(SUBSTRING(@_siret, 2, 1) AS tinyint)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + CAST(SUBSTRING(@_siret, 4, 1) AS tinyint)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + CAST(SUBSTRING(@_siret, 6, 1) AS tinyint)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + CAST(SUBSTRING(@_siret, 8, 1) AS tinyint)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + CAST(SUBSTRING(@_siret, 10, 1) AS tinyint)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + CAST(SUBSTRING(@_siret, 12, 1) AS tinyint)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + CAST(SUBSTRING(@_siret, 14, 1) AS tinyint)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ) % 10 = 0 THEN CAST(1 AS bit) ELSE CAST(0 AS bit)<br />
&nbsp; &nbsp; &nbsp; &nbsp; END<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; RETURN @ok<br />
END</div></td></tr></tbody></table></div>
<p>Et en ce qui concerne la création de la colonne calculée, on peut l&#039;ajouter à l&#039;aide du pseudo-T-SQL suivant :</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ALTER TABLE maTable<br />
ADD is_valid_SIRET AS(dbo.fn_scl_is_valid_SIRET(laColonneSIRET))</div></td></tr></tbody></table></div>
<p>Enfin, si l&#039;on souhaite exécuter cette vérification au sein d&#039;une requête, on peut s&#039;en remettre à une fonction de table en ligne. Ce type de fonction n&#039;étant appelé qu&#039;une seule fois, il s&#039;avérera plus performant à l&#039;exécution sur un grand nombre de lignes que l&#039;appel à une fonction scalaire : ces dernières sont appelées autant de fois qu&#039;il y a de lignes de résultat. Ce qui donne :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">CREATE FUNCTION fn_it_is_valid_SIRET<br />
&nbsp; &nbsp; &nbsp; &nbsp; (<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @siret char(14)<br />
&nbsp; &nbsp; &nbsp; &nbsp; )<br />
RETURNS TABLE<br />
WITH SCHEMABINDING<br />
AS<br />
RETURN&nbsp; <br />
(<br />
&nbsp; &nbsp; &nbsp; &nbsp; SELECT&nbsp; CASE<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN LEN(COALESCE(@siret,&amp;#039;&amp;#039;)) &amp;lt; 14 THEN CAST(0 AS bit)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN @siret LIKE&amp;#039;%[^0-9]%&amp;#039; THEN CAST(0 AS bit)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN @siret IS NULL THEN CAST(0 AS bit)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN (<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2 * CAST(SUBSTRING(@siret, 1, 1) AS tinyint) / 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@siret, 1, 1) AS tinyint) % 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@siret, 3, 1) AS tinyint) / 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@siret, 3, 1) AS tinyint) % 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@siret, 5, 1) AS tinyint) / 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@siret, 5, 1) AS tinyint) % 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@siret, 7, 1) AS tinyint) / 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@siret, 7, 1) AS tinyint) % 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@siret, 9, 1) AS tinyint) / 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@siret, 9, 1) AS tinyint) % 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@siret, 11, 1) AS tinyint) / 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@siret, 11, 1) AS tinyint) % 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@siret, 13, 1) AS tinyint) / 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + 2 * CAST(SUBSTRING(@siret, 13, 1) AS tinyint) % 10<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; + CAST(SUBSTRING(@siret, 2, 1) AS tinyint)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + CAST(SUBSTRING(@siret, 4, 1) AS tinyint)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + CAST(SUBSTRING(@siret, 6, 1) AS tinyint)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + CAST(SUBSTRING(@siret, 8, 1) AS tinyint)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + CAST(SUBSTRING(@siret, 10, 1) AS tinyint)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + CAST(SUBSTRING(@siret, 12, 1) AS tinyint)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + CAST(SUBSTRING(@siret, 14, 1) AS tinyint)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ) % 10 = 0 THEN CAST(1 AS bit) ELSE CAST(0 AS bit)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; END AS is_valid_SIRET<br />
)</div></td></tr></tbody></table></div>
<p>On peut alors l&#039;utiliser à l&#039;aide de l&#039;opérateur APPLY, comme dans l&#039;exemple suivant :</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">SELECT&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; S.*<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , F.is_valid_SIRET<br />
FROM&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dbo.maTableAvecUneColonneSIRET AS S<br />
CROSS APPLY &nbsp; &nbsp; dbo.fn_it_is_valid_SIRET(S.laColonneSIRET) AS F</div></td></tr></tbody></table></div>
<p>Bonne validation de SIRETs !</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sauvegardes de bases de données et envoi d&#8217;email avec SQL Server Express</title>
		<link>https://blog.developpez.com/elsuket/p12612/snippets/sauvegardes-de-bases-de-donnees-et-envoi-demail-avec-sql-server-express</link>
		<comments>https://blog.developpez.com/elsuket/p12612/snippets/sauvegardes-de-bases-de-donnees-et-envoi-demail-avec-sql-server-express#comments</comments>
		<pubDate>Sun, 11 May 2014 10:01:01 +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[T-SQL]]></category>
		<category><![CDATA[Utilitaires]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[BACKUP]]></category>
		<category><![CDATA[sauvegarde]]></category>
		<category><![CDATA[SQL Server Express]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/elsuket/?p=1006</guid>
		<description><![CDATA[Windows Server Update Services (WSUS) est une application qui permet de gérer et distribuer les patch que Microsoft publie pour ses produits. Elle est supportée par SQL Server Express (with Adanced Services), et de ce fait : On ne dispose &#8230; <a href="https://blog.developpez.com/elsuket/p12612/snippets/sauvegardes-de-bases-de-donnees-et-envoi-demail-avec-sql-server-express">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p><em>Windows Server Update Services</em> (WSUS) est une application qui permet de gérer et distribuer les patch que Microsoft publie pour ses produits. Elle est supportée par SQL Server Express (with Adanced Services), et de ce fait :</p>
<ul>
<li>On ne dispose pas de l&rsquo;Agent SQL Server</li>
<li>On ne peut pas utiliser des plans de maintenance pour gérer les sauvegardes</li>
<li>On ne peut pas envoyer d&rsquo;emails</li>
<li>La compression des fichiers de backup, introduite avec SQL Server 2008, n&rsquo;est pas prise en charge</li>
</ul>
<p>Si donc on souhaite planifier les sauvegardes de base de données, et envoyer un email en cas d&rsquo;échec, on peut :</p>
<ul>
<li>Remplacer un job de l&rsquo;Agent SQL Server par une tâche du Planificateur de Tâches de Windows</li>
<li>Écrire une procédure stockée d&rsquo;assembly pour envoyer des emails</li>
</ul>
<p>Cet article détaille les étapes à suivre pour ce faire.<br />
<span id="more-1006"></span></p>
<p><span style="font-weight:bold;font-family:Verdana;font-size:18px">Une procédure stockée d&rsquo;assembly pour envoyer des emails</span></p>
<p>Si l&rsquo;Agent SQL Server n&rsquo;est pas disponible, l&rsquo;intégration CLR l&rsquo;est. Ainsi en quelques lignes de code C#, on peut envoyer des e-mails de façon assez similaire à ce qu&rsquo;on peut faire sous les éditions non-Express de SQL Server avec la procédure stockée système <em>msdb.dbo.sp_send_dbmail</em>.</p>
<p>Pour créer une procédure stockée d&rsquo;assembly sous Visual Studio 2012 ou ultérieur, il faut tout d&rsquo;abord installer <em>SQL Server Data Tools</em>. Pour ce faire, vous pouvez lire <a href="http://blog.developpez.com/elsuket/p12610/utilitaires/ssdt-bi-et-ssdt-ou-lun-ou-lautre-et-aussi-les-deux" title="SSDT-BI et SSDT : ou l’un, ou l’autre, et aussi les deux !">ce billet</a>. Notez qu&rsquo;il n&rsquo;est pas nécessaire d&rsquo;avoir Visual Studio déjà installé.</p>
<p>Une fois Visual Studio démarré, il suffit de créer un nouveau projet à partir de la page de démarrage, ou bien de suivre <em>File</em> &gt; <em>New</em> &gt; <em>Project</em>, et de choisir un projet SQL Server :</p>
<p><img src="http://i.imgur.com/3Doxf4q.png" alt="" /></p>
<p>Il faut faire attention à la version du framework .NET. Par exemple, si l&rsquo;on souhaite que l&rsquo;assembly soit exécutable par SQL Server 2008, il faut spécifier la version 3.5. Une fois que l&rsquo;on a validé le tout par OK, nous pouvons ajouter une procédure stockée :</p>
<p><img src="http://i.imgur.com/YbADCND.png" alt="" /></p>
<p>Comme je préfère C# à VB.NET, j&rsquo;ai choisi une procédure stockée C#, mais si vous préférez VB.NET, rien ne s&rsquo;y oppose. Notons que par défaut, le langage de la procédure stockée n&rsquo;est pas pré-sélectionné.</p>
<p><img src="http://i.imgur.com/Fb6O7pN.png" alt="" /></p>
<p>Une fois que l&rsquo;on a validé le tout par un clic sur <em>Add</em>, on peut écrire :</p>
<div class="codecolorer-container csharp 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 /></div></td><td><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Net</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Net.Mail</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Data</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Data.SqlClient</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Data.SqlTypes</span><span style="color: #008000;">;</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">Microsoft.SqlServer.Server</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">partial</span> <span style="color: #6666cc; font-weight: bold;">class</span> StoredProcedures<br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#91;</span>Microsoft<span style="color: #008000;">.</span><span style="color: #0000FF;">SqlServer</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Server</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SqlProcedure</span><span style="color: #008000;">&#93;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> spasb_mail_send <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> sender, <span style="color: #6666cc; font-weight: bold;">string</span> recipients, <span style="color: #6666cc; font-weight: bold;">string</span> subject, <span style="color: #6666cc; font-weight: bold;">string</span> body<span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; SmtpClient mailServer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SmtpClient<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;mail.myCompany.com&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; MailMessage mail <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MailMessage<span style="color: #008000;">&#40;</span>sender, recipients, subject, body<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mail<span style="color: #008000;">.</span><span style="color: #0000FF;">IsBodyHtml</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; mailServer<span style="color: #008000;">.</span><span style="color: #0000FF;">Send</span><span style="color: #008000;">&#40;</span>mail<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></td></tr></tbody></table></div>
<p>Ce qui, même pour l&rsquo;ultra-débutant C# que je suis, est loin d&rsquo;être complexe. Avant de builder le projet, il nous faut configurer l&rsquo;assembly pour lui permettre d&rsquo;accéder à des ressources externes, ce qui se fait après un clic-droit sur le projet, et choisi <em>Properties</em> (ou pour les aficionados du raccourci clavier, ALT + Entrée). Dans la page <em>SQLCLR</em>, nous commutons donc la liste déroulante <em>Permission Level</em> sur EXTERNAL_ACCESS. De la même façon, dans la page <em>SQLCLR Build</em>, nous commutons la liste déroulante <em>Configuration</em> sur <em>Release</em>, ce qui permet un léger gain de performances. Ce n&rsquo;est pas très important dans notre cas, mais si l&rsquo;assembly est plus complexe, cela peut le devenir.</p>
<p><img src="http://i.imgur.com/TW1gYM4.png" alt="" /></p>
<p>Après un clic sur <em>Build</em> &gt; <em>Build Solution</em>, nous pouvons aller chercher le fichier dll dans le dossier de la solution. Dans cet exemple, il s&rsquo;agit de <em>D:\SQLServer\Assemblies\SQLServerExpressDatabaseMail\SQLServerExpressDatabaseMail\bin\Debug</em>, et le fichier est nommé <em>SQLServerExpressDatabaseMail.dll</em>.</p>
<p><img src="http://i.imgur.com/vjePpel.png" alt="" /></p>
<p>De retour dans <em>SQL Server Management Studio</em>, une fois connecté à l&rsquo;instance SQL Server Express, il est tout d&rsquo;abord nécessaire d&rsquo;activer la prise en charge de la CLR par cette dernière. Cela se fait sans redémarrage de l&rsquo;instance, en quelques instruction :</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;">EXEC</span> <span style="color: #AF0000;">sp_configure</span> <span style="color: #FF0000;">'clr enabled'</span>, <span style="color: #000;">1</span><br />
GO<br />
<span style="color: #0000FF;">RECONFIGURE</span><br />
GO</div></td></tr></tbody></table></div>
<p>Il est maintenant requis de déclarer la base de données dans laquelle nous allons enregistrer la procédure stockée d&rsquo;assembly comme &laquo;&nbsp;digne de confiance&nbsp;&raquo;. Ceci nous permet d&rsquo;emprunter l&rsquo;identité de SQL Server pour exécuter cette procédure qui va accéder à des ressources en dehors de la base de données : en l’occurrence, un serveur de mails (mais ce peut être le système de fichiers, un webservice, &#8230;). Là encore, une simple commande suffit :</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> DBA<br />
<span style="color: #0000FF;">SET</span> TRUSTW<span style="color: #808080;">OR</span>THY <span style="color: #0000FF;">ON</span><br />
GO</div></td></tr></tbody></table></div>
<p>Nous devons maintenant enregistrer l&rsquo;assembly dans la base de données, ce que l&rsquo;on peut faire une fois que l&rsquo;on a copié l&rsquo;assembly dans un dossier de notre choix, sur le serveur hébergeant l&rsquo;instance SQL Server Express / WSUS. Dans cet exemple, nous utilisons le dossier C:\SQLServer\.</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;">USE</span> DBA<br />
GO<br />
<br />
<span style="color: #0000FF;">CREATE</span> ASSEMBLY SQLServerExpressDatabaseMail<br />
<span style="color: #0000FF;">FROM</span> <span style="color: #FF0000;">'C:\SQLServer\SQLServerExpressDatabaseMail.dll'</span><br />
<span style="color: #0000FF;">WITH</span> PERMISSION_SET <span style="color: #808080;">=</span> EXTERNAL_ACCESS<br />
GO</div></td></tr></tbody></table></div>
<p>On peut vérifier l&rsquo;enregistrement de l&rsquo;assembly en interrogeant la vue système <em>sys.assemblies</em>. En ce point, on n&rsquo;a plus besoin du fichier dll : le binaire de celle-ci est maintenant enregistré dans la base de données (de nom DBA dans cet exemple).<br />
Créons maintenant la procédure stockée :</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;">CREATE</span> <span style="color: #0000FF;">PROCEDURE</span> dbo.<span style="color: #202020;">spasb_mail_send</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @_sender <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">256</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , @_recipients <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">256</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , @_subject <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">256</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , @_body <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4000</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">EXTERNAL</span> NAME SQLServerExpressDatabaseMail.<span style="color: #202020;">StoredProcedures</span>.<span style="color: #202020;">spasb_mail_send</span><br />
GO</div></td></tr></tbody></table></div>
<p>Et testons enfin :</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;">EXEC</span> dbo.<span style="color: #202020;">spasb_mail_send</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF0000;">'it@myCompany.com'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #FF0000;">'me@myCompany.com'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #FF0000;">'Test Mail from Assembly Stored Procedure'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #FF0000;">'Hey Hey !'</span></div></td></tr></tbody></table></div>
<p><span style="font-weight:bold;font-family:Verdana;font-size:18px">Une procédure stockée pour gérer les sauvegardes</span></p>
<p>Voici une procédure stockée générique qui permet de sauvegarder une base de données en particulier, ou toutes les bases de données hébergées par une instance SQL Server. Dans le second cas, les sauvegardes du fichier du journal des transactions n&rsquo;est pas supportée. En revanche, on peut exécuter cette procédure stockée sur toutes les versions de SQL Server, à partir de 2005. Enfin, le cryptage des sauvegardes, introduit avec SQL Server 2014 n&rsquo;est pas supporté non plus. Cela exige que quelques prérequis soient en place, et ceux-là sortent du cadre de cet article.</p>
<p>La procédure stockée d&rsquo;assembly est référencée dans le bloc CATCH final : pour toute erreur, on envoie un email.</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 /></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;">PROCEDURE</span> dbo.<span style="color: #202020;">databaseBackup_manage</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; @_backup_type <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">12</span><span style="color: #808080;">&#41;</span><br />
&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> <span style="color: #808080;">=</span> <span style="color: #808080;">NULL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , @_backup_server_network_path <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 />
&nbsp; &nbsp; &nbsp; &nbsp; , @_print_only <span style="color: #0000FF;">bit</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</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;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4000</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , @err_msg <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">2048</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , @compression <span style="color: #0000FF;">bit</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , @checkum <span style="color: #0000FF;">bit</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , @mail_subject <span style="color: #0000FF;">nvarchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">4000</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF00FF;">@@SERVERNAME</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">' Backups'</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- SQL Server 2005 ne supporte pas l'affectation directe de valeurs*</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- à des variables lors de la déclaration</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span>&nbsp; @compression <span style="color: #808080;">=</span> <span style="color: #000;">0</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span> TRY<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- Gestion de la compression des sauvegardes</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">IF</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: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- SQL Server 2008 Enterprise et Developer</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: #FF00FF;">SERVERPROPERTY</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'ProductVersion'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">32</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">LIKE</span> <span style="color: #FF0000;">'10.0%'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">AND</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;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SERVERPROPERTY</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Edition'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">64</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">LIKE</span> <span style="color: #FF0000;">'Enterprise%'</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;">OR</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SERVERPROPERTY</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Edition'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">64</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">LIKE</span> <span style="color: #FF0000;">'Developer%'</span><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: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">OR</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: #008080;">-- SQL Server 2008 R2 Enterprise, Standard et Developer</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- Et toute version après celle-ci</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;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SERVERPROPERTY</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'ProductVersion'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">32</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">LIKE</span> <span style="color: #FF0000;">'10.50%'</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;">OR</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">REPLACE</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">LEFT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SERVERPROPERTY</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'ProductVersion'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">32</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">'.'</span>, <span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">tinyint</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">&amp;</span>gt; <span style="color: #000;">10</span><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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">AND</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;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SERVERPROPERTY</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Edition'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">64</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">LIKE</span> <span style="color: #FF0000;">'Enterprise%'</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;">OR</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SERVERPROPERTY</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Edition'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">64</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">LIKE</span> <span style="color: #FF0000;">'Standard%'</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;">OR</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">SERVERPROPERTY</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Edition'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">64</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">LIKE</span> <span style="color: #FF0000;">'Developer%'</span><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: #808080;">&#41;</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;">SET</span> @compression <span style="color: #808080;">=</span> <span style="color: #000;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- Vérifie que le chemin réseau commence par un double anti-slash</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">IF</span> @_backup_server_network_path <span style="color: #808080;">NOT</span> <span style="color: #808080;">LIKE</span> <span style="color: #FF0000;">'\\%'</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;">SET</span> @err_msg <span style="color: #808080;">=</span> <span style="color: #FF0000;">'The network path '</span> <span style="color: #808080;">+</span> @_backup_server_network_path <span style="color: #808080;">+</span> <span style="color: #FF0000;">' is not valid.'</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>@err_msg, <span style="color: #000;">16</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- Ajoute l'anti-slash à la fin du chemin réseau, le cas échéant</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> @_backup_server_network_path <span style="color: #808080;">=</span> @_backup_server_network_path <span style="color: #808080;">+</span> <span style="color: #0000FF;">CASE</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHEN</span> <span style="color: #FF00FF;">CHARINDEX</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'\'</span>, <span style="color: #FF00FF;">REVERSE</span><span style="color: #808080;">&#40;</span>@_backup_server_network_path<span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">&amp;</span>gt; <span style="color: #000;">1</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'\'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">''</span><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: #0000FF;">IF</span> @_database_name <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NOT</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: #008080;">--------------------------------------------------</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- Sauvegarde d'une base de données en particulier</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">--------------------------------------------------</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">IF</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">EXISTS</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; <span style="color: #808080;">*</span><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; sys.<span style="color: #202020;">databases</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> &nbsp; name <span style="color: #808080;">=</span> @_database_name<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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @err_msg <span style="color: #808080;">=</span> <span style="color: #FF0000;">'La base de données '</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;">' n'</span><span style="color: #FF0000;">'existe pas.'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">RAISERROR</span><span style="color: #808080;">&#40;</span>@err_msg, <span style="color: #000;">16</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">IF</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">EXISTS</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; <span style="color: #808080;">*</span><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; sys.<span style="color: #202020;">databases</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> &nbsp; name <span style="color: #808080;">=</span> @_database_name<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">AND</span> &nbsp; &nbsp; state_desc <span style="color: #808080;">=</span> <span style="color: #FF0000;">'ONLINE'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">AND</span> &nbsp; &nbsp; user_access_desc <span style="color: #808080;">=</span> <span style="color: #FF0000;">'MULTI_USER'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">AND</span> &nbsp; &nbsp; source_database_id <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NULL</span> <span style="color: #008080;">-- La base de données n'est pas un snapshot</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">AND</span> &nbsp; &nbsp; database_id &nbsp;<span style="color: #000;">2</span> <span style="color: #008080;">-- La base de données n'est pas TempDB</span><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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @err_msg <span style="color: #808080;">=</span> <span style="color: #FF0000;">'La base de données '</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;">' n'</span><span style="color: #FF0000;">'est pas disponible ou est une capture intantanée.'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">RAISERROR</span><span style="color: #808080;">&#40;</span>@err_msg, <span style="color: #000;">16</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- Vérifie le type de backup</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">IF</span> @_backup_type <span style="color: #808080;">NOT</span> <span style="color: #808080;">IN</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'FULL'</span>, <span style="color: #FF0000;">'DIFFERENTIAL'</span>, <span style="color: #FF0000;">'LOG'</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @err_msg <span style="color: #808080;">=</span> <span style="color: #FF0000;">'The backup type '</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> @_backup_type <span style="color: #808080;">+</span> <span style="color: #FF0000;">''</span><span style="color: #FF0000;">' is invalid.'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">RAISERROR</span><span style="color: #808080;">&#40;</span>@err_msg, <span style="color: #000;">16</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</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;">'BACKUP '</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">CASE</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> @_backup_type <span style="color: #808080;">IN</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'FULL'</span>, <span style="color: #FF0000;">'DIFFERENTIAL'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'DATABASE '</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> @_backup_type <span style="color: #808080;">=</span> <span style="color: #FF0000;">'LOG'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'LOG '</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: #808080;">+</span> @_database_name<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">+</span> <span style="color: #FF0000;">' TO DISK = '</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> @_backup_server_network_path <span style="color: #808080;">+</span> <span style="color: #FF00FF;">REPLACE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">@@SERVERNAME</span>, <span style="color: #FF0000;">'\'</span>, <span style="color: #FF0000;">'_'</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</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: #808080;">+</span> @_backup_type <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; <span style="color: #808080;">+</span> <span style="color: #FF00FF;">REPLACE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">REPLACE</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CONVERT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">19</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>, <span style="color: #000;">120</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">':'</span>, <span style="color: #FF0000;">'-'</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">' '</span>, <span style="color: #FF0000;">'_'</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'.bak'</span><span style="color: #FF0000;">''</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">+</span> <span style="color: #FF0000;">' WITH '</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">CASE</span> @_backup_type<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: #FF0000;">'DIFFERENTIAL'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'DIFFERENTIAL, '</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: #FF0000;">'FULL'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'INIT, '</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: #FF0000;">''</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: #808080;">+</span> <span style="color: #0000FF;">CASE</span> @compression <span style="color: #0000FF;">WHEN</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'COMPRESSION, '</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">''</span> <span style="color: #0000FF;">END</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">+</span> <span style="color: #FF0000;">'CHECKSUM'</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">IF</span> @_print_only <span style="color: #808080;">=</span> <span style="color: #000;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">PRINT</span> @<span style="color: #0000FF;">sql</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ELSE</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span> TRY<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;">EXEC</span> <span style="color: #AF0000;">sp_executesql</span> @<span style="color: #0000FF;">sql</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span> TRY<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span> CATCH<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;">SET</span> @err_msg <span style="color: #808080;">=</span> @_database_name <span style="color: #808080;">+</span> <span style="color: #FF0000;">' | '</span> <span style="color: #808080;">+</span> ERR<span style="color: #808080;">OR</span>_MESSAGE<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</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;">RAISERROR</span><span style="color: #808080;">&#40;</span>@err_msg, <span style="color: #000;">16</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span> CATCH<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</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; <span style="color: #0000FF;">ELSE</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: #008080;">--------------------------------------------------------</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- Sauvegarde de toutes les bases de données disponibles</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">--------------------------------------------------------</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">IF</span> @_backup_type <span style="color: #808080;">=</span> <span style="color: #FF0000;">'LOG'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SET</span> @err_msg <span style="color: #808080;">=</span> <span style="color: #FF0000;">'On ne prend pas une sauvegarde du fichier du journal des transactions de toutes les bases de données.'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">RAISERROR</span><span style="color: #808080;">&#40;</span>@err_msg, <span style="color: #000;">16</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">DECLARE</span> @database_list <span style="color: #0000FF;">TABLE</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; 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; <span style="color: #808080;">&#41;</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> @database_list<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; database_name<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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span>&nbsp; <span style="color: #FF00FF;">REPLACE</span><span style="color: #808080;">&#40;</span>name, <span style="color: #FF0000;">' '</span>, <span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span> <span style="color: #008080;">-- Certaines bases de données ne respectent pas la convention de nommage SQL ANSI.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; sys.<span style="color: #202020;">databases</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> &nbsp; source_database_id <span style="color: #0000FF;">IS</span> <span style="color: #808080;">NULL</span> <span style="color: #008080;">-- La base de données n'est pas un snapshot</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">AND</span> &nbsp; &nbsp; state_desc <span style="color: #808080;">=</span> <span style="color: #FF0000;">'ONLINE'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">AND</span> &nbsp; &nbsp; user_access_desc <span style="color: #808080;">=</span> <span style="color: #FF0000;">'MULTI_USER'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">AND</span> &nbsp; &nbsp; database_id &nbsp;<span style="color: #000;">2</span> <span style="color: #008080;">-- On exclut TempDB</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; &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; <span style="color: #808080;">*</span><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; @database_list<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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</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; <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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; @database_list<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &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;">'BACKUP DATABASE '</span> <span style="color: #808080;">+</span> @_database_name<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;">+</span> <span style="color: #FF0000;">' TO DISK = '</span><span style="color: #FF0000;">''</span> <span style="color: #808080;">+</span> @_backup_server_network_path <span style="color: #808080;">+</span> <span style="color: #FF00FF;">REPLACE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">@@SERVERNAME</span>, <span style="color: #FF0000;">'\'</span>, <span style="color: #FF0000;">'_'</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</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: #808080;">+</span> @_backup_type <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: #808080;">+</span> <span style="color: #FF00FF;">REPLACE</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">REPLACE</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">CONVERT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">19</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>, <span style="color: #000;">120</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">':'</span>, <span style="color: #FF0000;">'-'</span><span style="color: #808080;">&#41;</span>, <span style="color: #FF0000;">' '</span>, <span style="color: #FF0000;">'_'</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'.bak'</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: #808080;">+</span> <span style="color: #FF0000;">' WITH '</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">CASE</span> @_backup_type<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;">WHEN</span> <span style="color: #FF0000;">'DIFFERENTIAL'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'DIFFERENTIAL, '</span><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;">WHEN</span> <span style="color: #FF0000;">'FULL'</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'INIT, '</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;">END</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">CASE</span> @compression <span style="color: #0000FF;">WHEN</span> <span style="color: #000;">1</span> <span style="color: #0000FF;">THEN</span> <span style="color: #FF0000;">'COMPRESSION, '</span> <span style="color: #0000FF;">ELSE</span> <span style="color: #FF0000;">''</span> <span style="color: #0000FF;">END</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;">+</span> <span style="color: #FF0000;">'CHECKSUM'</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">IF</span> @_print_only <span style="color: #808080;">=</span> <span style="color: #000;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">PRINT</span> @<span style="color: #0000FF;">sql</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ELSE</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span> TRY<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;">EXEC</span> <span style="color: #AF0000;">sp_executesql</span> @<span style="color: #0000FF;">sql</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;">END</span> TRY<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;">BEGIN</span> CATCH<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;">SET</span> @err_msg <span style="color: #808080;">=</span> @_database_name <span style="color: #808080;">+</span> <span style="color: #FF0000;">' | '</span> <span style="color: #808080;">+</span> ERR<span style="color: #808080;">OR</span>_MESSAGE<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><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;">RAISERROR</span><span style="color: #808080;">&#40;</span>@err_msg, <span style="color: #000;">16</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</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;">END</span> CATCH<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&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; <span style="color: #0000FF;">DELETE</span>&nbsp; <span style="color: #0000FF;">FROM</span> @database_list<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> &nbsp; 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;">END</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span> TRY<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">BEGIN</span> CATCH<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080;">-- Envoie un email en cas d'erreur</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">EXEC</span> dbo.<span style="color: #202020;">spasb_mail_send</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @_sender <span style="color: #808080;">=</span> <span style="color: #FF0000;">'it@myCompany.com'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , @_recipients <span style="color: #808080;">=</span> <span style="color: #FF0000;">'it@myCompany.com'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , @_subject <span style="color: #808080;">=</span> @mail_subject<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , @_body <span style="color: #808080;">=</span> @err_msg<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">END</span> CATCH<br />
<span style="color: #0000FF;">END</span></div></td></tr></tbody></table></div>
<p><span style="font-weight:bold;font-family:Verdana;font-size:18px">Remplacer l&rsquo;Agent SQL Server par le Planificateur de Tâches de Windows</span></p>
<p>Nous voilà à l&rsquo;étape finale : prendre des sauvegardes de base de données régulièrement. Pour ce faire, on peut tout à fait utiliser le <em>Planificateur de Tâches de Windows</em> : en effet, on peut appeler l&rsquo;utilitaire en ligne de commandes <em>SQLCMD</em> (la <a href="http://technet.microsoft.com/fr-fr/library/ms162773.aspx">documentation</a>, l&rsquo;<a href="http://technet.microsoft.com/fr-fr/library/ms180944.aspx">utilisation</a>, et un <a href="http://technet.microsoft.com/fr-fr/library/ms170207%28v=sql.105%29.aspx">exemple</a>), qui permet d&rsquo;interagir avec une instance SQL Server. Cet outil permet de passer en paramètre un script, d&rsquo;exécuter une requête, mais surtout d&rsquo;enregistrer la sortie dans un fichier. Cette dernière fonctionnalité facilite la tâche d&rsquo;investigation sur l&rsquo;échec d&rsquo;exécution de code.</p>
<p>Une fois l&rsquo;application <em>Planificateur de Tâches</em> démarrée, on créé une tâche assez simplement :</p>
<p><img src="http://i.imgur.com/ZSHAIHP.png" alt="" /></p>
<p>Une fois passé la première page ou nous nommons la tâche, un clic sur <em>Next</em> nous amène à la planification. Pour <em>Windows Server Update Services</em>, on peut estimer qu&rsquo;une sauvegarde complète par jour est suffisante. Évidemment, cela varie avec la criticité de l&rsquo;application. Nous spécifions donc une exécution quotidienne :</p>
<p><img src="http://i.imgur.com/QdAUdsn.png" alt="" /></p>
<p>Nous planifions l&rsquo;exécution à minuit et une minute :</p>
<p><img src="http://i.imgur.com/JXOESBU.png" alt="" /></p>
<p>Et nous choisissons de démarrer une application :</p>
<p><img src="http://i.imgur.com/ImwrMln.png" alt="" /></p>
<p>Dans la saisie texte <em>Program / Script</em>, nous spécifions l&rsquo;invite de commandes. En ce qui concerne les arguments, nous écrivons :</p>
<ul>
<li>Pour SQL Server 2008 :<br />
<code class="codecolorer dos default"><span class="dos">&quot;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\SQLCMD.EXE&quot;</span></code></li>
<li>Pour SQL Server 2012 :<br />
<code class="codecolorer dos default"><span class="dos">&quot;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\SQLCMD.EXE&quot;</span></code></li>
<li>Pour SQL Server 2014 :<br />
<code class="codecolorer dos default"><span class="dos">&quot;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\SQLCMD.EXE&quot;</span></code></li>
</ul>
<p>En ce qui concerne les arguments, nous passons :</p>
<p><code class="codecolorer dos default"><span class="dos">sqlcmd -E -S WSUS -i &quot;C:\SQLServer\WSUSBackup.sql&quot; -o &quot;C:\SQLServerBackup\FullBackupLog.txt&quot;</span></code></p>
<p>Attention, la casse des options est importante.</p>
<ul>
<li>-E pour spécifier une connexion approuvée</li>
<li>-S : l&rsquo;instance SQL Server à laquelle on souhaite se connecter</li>
<li>-i : le chemin et le nom du fichier de script T-SQL</li>
<li>-o : le fichier qui capturera les libellés des messages d&rsquo;information, d&rsquo;avertissement ou d&rsquo;erreurs, le cas échéant</li>
</ul>
<p>Ce qui donne :</p>
<p><img src="http://i.imgur.com/6I12ozL.png" alt="" /></p>
<p>Un clic sur <em>Next</em> nous offre une fenêtre de résumé, et on peut valider le tout. Si l&rsquo;on souhaite ajouter des planifications ou modifier d&rsquo;autres options pour cette tâche, il suffit de cocher la case <em>Open the Properties dialog for this task when I click Finish</em>.</p>
<p>Nous créons ensuite un fichier <em>WSUSBackup.sql</em> dans un répertoire <em>C:\SQLServer\</em> sur la machine qui héberge l&rsquo;application WSUS. Ce fichier appelle la procédure stockée <em>DBA.dbo.databaseBackup_manage</em> comme suit :</p>
<p><code class="codecolorer text default"><span class="text">EXEC DBA.dbo.databaseBackup_manage 'FULL', NULL, '\\monPartageDeBackup\', 0</span></code></p>
<p>Et voilà nos sauvegardes de base de données en place.</p>
<p><span style="font-weight:bold;font-family:Verdana;font-size:18px">Utiliser PowerShell pour supprimer les fichiers de sauvegardes trop anciens</span></p>
<p>Dans la liste des actions de la tâche que nous venons de créer, nous pouvons en ajouter une qui aura :</p>
<ul>
<li>Pour programme : <code class="codecolorer text default"><span class="text">powershell</span></code></li>
<li>Pour arguments : <code class="codecolorer text default"><span class="text">-file &quot;C:\SQLServer\DeleteOldBackupFiles.ps1&quot;</span></code></li>
</ul>
<p>Le fichier <em>DeleteOldBackupFiles.ps1</em> contient la commande suivante, qui supprime tous les fichiers créés il y a plus de 7 jours, y compris dans les sous-dossiers du partage de backup (-Recurse) :</p>
<p><code class="codecolorer powershell default"><span class="powershell"><span style="color: #008080; font-weight: bold;">Get-ChildItem</span> <span style="color: #800000;">'\\monPartageDeBackup\'</span> <span style="color: #008080; font-style: italic;">-Recurse</span> <span style="color: pink;">|</span> <span style="color: #0000FF;">Where</span> <span style="color: #000000;">&#123;</span><span style="color: #000080;">$_</span>.CreationTime <span style="color: #FF0000;">-lt</span> <span style="color: #000000;">&#40;</span><span style="color: #008080; font-weight: bold;">Get-Date</span><span style="color: #000000;">&#41;</span>.AddDays<span style="color: #000000;">&#40;</span><span style="color: pink;">-</span><span style="color: #804000;">7</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#125;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Remove-Item</span> <span style="color: #008080; font-style: italic;">-Force</span></span></code></p>
<p>Par défaut, PowerShell ne permet pas l&rsquo;exécution de scripts. Pour l&rsquo;autoriser, il faut démarrer l&rsquo;interface PowerShell, et exécuter <code class="codecolorer powershell default"><span class="powershell"><span style="color: #008080; font-weight: bold;">Set-ExecutionPolicy</span> RemoteSigned</span></code> (en accord avec votre Administrateur Systèmes !).</p>
<p>Retenons également que les statistiques de colonne et d&rsquo;index de la base de données supportant WSUS ne sont pas maintenues. Ce sera probablement suffisant pour générer une lenteur après quelques mois d&rsquo;exploitation, dont votre Administrateur Systèmes vous demandera l&rsquo;origine <img src="https://blog.developpez.com/elsuket/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley" /></p>
<p>ElSüket.</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Comment passer d’une liste de valeurs séparées par des virgules à une table, et inversement ?</title>
		<link>https://blog.developpez.com/elsuket/p12576/t-sql/csv_scalaire_et_inversement</link>
		<comments>https://blog.developpez.com/elsuket/p12576/t-sql/csv_scalaire_et_inversement#comments</comments>
		<pubDate>Mon, 14 Apr 2014 04:47:40 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[concaténer]]></category>
		<category><![CDATA[CSV]]></category>
		<category><![CDATA[SQLXML]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[XQuery]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/elsuket/?p=987</guid>
		<description><![CDATA[La réponse est très courte : avec les fonctionnalités SQLXML et XQuery intégrées à SQL Server. Il est parfois utile de pouvoir générer une liste de valeurs d&#8217;une colonne, séparées par des virgules ou un autre symbole imprimable. Bien que &#8230; <a href="https://blog.developpez.com/elsuket/p12576/t-sql/csv_scalaire_et_inversement">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>La réponse est très courte : avec les fonctionnalités SQLXML et XQuery intégrées à SQL Server.<br />
<span id="more-987"></span><br />
Il est parfois utile de pouvoir générer une liste de valeurs d&rsquo;une colonne, séparées par des virgules ou un autre symbole imprimable. Bien que cela serve souvent des buts de présentation, qui n&rsquo;ont donc rien à faire avec SQL, il se trouve des cas où l&rsquo;on n&rsquo;a pas le choix. C&rsquo;est par exemple le cas avec SQL Server Reporting Services (SSRS) : en effet, cet outil efficace de création assistée de rapports permet de choisir plusieurs valeurs pour un paramètre. SSRS les transmet ensuite sous la forme d&rsquo;une chaîne de caractères, dont les valeurs sont séparées par des virgules. Lorsqu&rsquo;on requête les données avec un tel paramètre, il faut dépouiller la chaîne pour en obtenir les valeurs scalaires, intelligibles par le moteur de bases de données.</p>
<p>Voyons un premier exemple très simple de génération de liste de valeurs séparées par des virgules :</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 /></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;">TABLE</span> dbo.<span style="color: #202020;">liste_GUID</span><br />
<span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; valeur_scalaire <span style="color: #0000FF;">uniqueidentifier</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;">liste_GUID</span> <span style="color: #808080;">&#40;</span>valeur_scalaire<span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'30815903-5911-418B-BB56-CE0CF4981142'</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'029FE2E3-E57D-4C4D-A514-2448149560F9'</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'F6A73DFA-C79A-40B5-B12F-55DA66C5A158'</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'D896FB47-B5B8-4E8F-BFA1-62A65F66CB8B'</span><span style="color: #808080;">&#41;</span><br />
GO<br />
<br />
<span style="color: #0000FF;">DECLARE</span> @s <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">max</span><span style="color: #808080;">&#41;</span>;<br />
<br />
<span style="color: #0000FF;">WITH</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CUMUL <span style="color: #808080;">&#40;</span>csv<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>valeur_scalaire <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">36</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> &nbsp;<span style="color: #FF0000;">','</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> &nbsp; dbo.<span style="color: #202020;">liste_GUID</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FOR</span> &nbsp; &nbsp;XML <span style="color: #0000FF;">PATH</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">SELECT</span>&nbsp; @s <span style="color: #808080;">=</span> csv<br />
<span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; CUMUL<br />
<br />
<span style="color: #0000FF;">SELECT</span> @s</div></td></tr></tbody></table></div>
<p>Notons qu&rsquo;avec cet exemple, nous avons une virgule en toute fin de chaîne. On peut l’éliminer simplement en écrivant :</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 /></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;">DECLARE</span> @s <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">max</span><span style="color: #808080;">&#41;</span>;<br />
<br />
<span style="color: #0000FF;">WITH</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CUMUL <span style="color: #808080;">&#40;</span>csv<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>valeur_scalaire <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">char</span><span style="color: #808080;">&#40;</span><span style="color: #000;">36</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> &nbsp;<span style="color: #FF0000;">','</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span> &nbsp; dbo.<span style="color: #202020;">liste_GUID</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FOR</span> &nbsp; &nbsp;XML <span style="color: #0000FF;">PATH</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">SELECT</span>&nbsp; @s <span style="color: #808080;">=</span> <span style="color: #0000FF;">LEFT</span><span style="color: #808080;">&#40;</span>csv, <span style="color: #FF00FF;">LEN</span><span style="color: #808080;">&#40;</span>csv<span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; CUMUL<br />
<br />
<span style="color: #0000FF;">SELECT</span> @s</div></td></tr></tbody></table></div>
<p>Si l&rsquo;on a besoin de réaliser cela pour un ensemble (i.e. une colonne), on utilisera l&rsquo;opérateur APPLY, ou une sous requête corrélée.<br />
Pour l&rsquo;exemple, considérons les tables suivantes :</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 /></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;">TABLE</span> Contact<br />
<span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; IDContact <span style="color: #0000FF;">int</span> <span style="color: #0000FF;">IDENTITY</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">CONSTRAINT</span> PK_Contact <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , NomContact <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;">NOT</span> <span style="color: #808080;">NULL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">CONSTRAINT</span> UQ_Contact__NomContact <span style="color: #0000FF;">UNIQUE</span><br />
<span style="color: #808080;">&#41;</span><br />
GO<br />
&nbsp;<br />
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> Club<br />
<span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; IDClub <span style="color: #0000FF;">int</span> <span style="color: #0000FF;">IDENTITY</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">CONSTRAINT</span> PK_Club <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , NomClub <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;">NOT</span> <span style="color: #808080;">NULL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">CONSTRAINT</span> UQ_Club__NomClub <span style="color: #0000FF;">UNIQUE</span><br />
<span style="color: #808080;">&#41;</span><br />
GO<br />
&nbsp;<br />
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> Membre<br />
<span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; IDMembre <span style="color: #0000FF;">int</span> <span style="color: #0000FF;">IDENTITY</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">CONSTRAINT</span> PK_Membre <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , IDContact <span style="color: #0000FF;">int</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">CONSTRAINT</span> FK_Membre_IDContact <span style="color: #0000FF;">FOREIGN</span> <span style="color: #0000FF;">KEY</span><span style="color: #808080;">&#40;</span>IDContact<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">REFERENCES</span> Contact<span style="color: #808080;">&#40;</span>IDContact<span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , IDClub <span style="color: #0000FF;">int</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">CONSTRAINT</span> FK_Membre_IDClub <span style="color: #0000FF;">FOREIGN</span> <span style="color: #0000FF;">KEY</span><span style="color: #808080;">&#40;</span>IDClub<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">REFERENCES</span> Club<span style="color: #808080;">&#40;</span>IDClub<span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #0000FF;">CONSTRAINT</span> UQ_Membre_IDContact_IDClub <span style="color: #0000FF;">UNIQUE</span> <span style="color: #808080;">&#40;</span>IDContact, IDClub<span style="color: #808080;">&#41;</span><br />
<span style="color: #808080;">&#41;</span><br />
GO<br />
&nbsp;<br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Contact</span> <span style="color: #808080;">&#40;</span>NomContact<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'BipBip'</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Contact</span> <span style="color: #808080;">&#40;</span>NomContact<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Coyotte'</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Contact</span> <span style="color: #808080;">&#40;</span>NomContact<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Titi'</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Contact</span> <span style="color: #808080;">&#40;</span>NomContact<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Sylvestre'</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Contact</span> <span style="color: #808080;">&#40;</span>NomContact<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Bugs Bunny'</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Contact</span> <span style="color: #808080;">&#40;</span>NomContact<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Daffy Duck'</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Contact</span> <span style="color: #808080;">&#40;</span>NomContact<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Sam Le Pirate'</span><span style="color: #808080;">&#41;</span><br />
GO<br />
&nbsp;<br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Club</span> <span style="color: #808080;">&#40;</span>NomClub<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'SQLServer'</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Club</span> <span style="color: #808080;">&#40;</span>NomClub<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Oracle'</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Club</span> <span style="color: #808080;">&#40;</span>NomClub<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'PostGreSQL'</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Club</span> <span style="color: #808080;">&#40;</span>NomClub<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'MySQL'</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Club</span> <span style="color: #808080;">&#40;</span>NomClub<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'FireBird'</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Club</span> <span style="color: #808080;">&#40;</span>NomClub<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Sybase'</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Club</span> <span style="color: #808080;">&#40;</span>NomClub<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'DB2'</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Club</span> <span style="color: #808080;">&#40;</span>NomClub<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'SQLite'</span><span style="color: #808080;">&#41;</span><br />
GO<br />
&nbsp;<br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Membre</span> <span style="color: #808080;">&#40;</span>IDContact, IDClub<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Membre</span> <span style="color: #808080;">&#40;</span>IDContact, IDClub<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>, <span style="color: #000;">2</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Membre</span> <span style="color: #808080;">&#40;</span>IDContact, IDClub<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>, <span style="color: #000;">3</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Membre</span> <span style="color: #808080;">&#40;</span>IDContact, IDClub<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">2</span>, <span style="color: #000;">4</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Membre</span> <span style="color: #808080;">&#40;</span>IDContact, IDClub<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">2</span>, <span style="color: #000;">5</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Membre</span> <span style="color: #808080;">&#40;</span>IDContact, IDClub<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">3</span>, <span style="color: #000;">6</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Membre</span> <span style="color: #808080;">&#40;</span>IDContact, IDClub<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">4</span>, <span style="color: #000;">7</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Membre</span> <span style="color: #808080;">&#40;</span>IDContact, IDClub<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">4</span>, <span style="color: #000;">8</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Membre</span> <span style="color: #808080;">&#40;</span>IDContact, IDClub<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">5</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Membre</span> <span style="color: #808080;">&#40;</span>IDContact, IDClub<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">5</span>, <span style="color: #000;">3</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Membre</span> <span style="color: #808080;">&#40;</span>IDContact, IDClub<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">6</span>, <span style="color: #000;">5</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Membre</span> <span style="color: #808080;">&#40;</span>IDContact, IDClub<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">6</span>, <span style="color: #000;">7</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Membre</span> <span style="color: #808080;">&#40;</span>IDContact, IDClub<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">7</span>, <span style="color: #000;">8</span><span style="color: #808080;">&#41;</span><br />
GO</div></td></tr></tbody></table></div>
<p>On obtient alors la liste des clubs desquels un contact est membre avec l&rsquo;une des requêtes suivantes :</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 /></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;">-- Avec l'opérateur APPLY</span><br />
<span style="color: #0000FF;">SELECT</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CT.<span style="color: #202020;">NomContact</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #0000FF;">LEFT</span><span style="color: #808080;">&#40;</span>LC.<span style="color: #202020;">liste_club</span>, <span style="color: #FF00FF;">LEN</span><span style="color: #808080;">&#40;</span>LC.<span style="color: #202020;">liste_club</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> liste_club<br />
<span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dbo.<span style="color: #202020;">Contact</span> <span style="color: #0000FF;">AS</span> CT<br />
<span style="color: #808080;">CROSS</span> APPLY &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; &nbsp; &nbsp; &nbsp; &nbsp; CL.<span style="color: #202020;">NomClub</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">', '</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; dbo.<span style="color: #202020;">Club</span> <span style="color: #0000FF;">AS</span> CL<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; dbo.<span style="color: #202020;">Membre</span> <span style="color: #0000FF;">AS</span> MB<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> MB.<span style="color: #202020;">IDClub</span> <span style="color: #808080;">=</span> CL.<span style="color: #202020;">IDClub</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; CT.<span style="color: #202020;">IDContact</span> <span style="color: #808080;">=</span> MB.<span style="color: #202020;">IDContact</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FOR</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XML <span style="color: #0000FF;">PATH</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> LC<span style="color: #808080;">&#40;</span>liste_club<span style="color: #808080;">&#41;</span>;<br />
<br />
<span style="color: #008080;">-- Avec une sous-requête corrélée</span><br />
<span style="color: #008080;">-- Pour éliminer la virgule en fin de chaîne, on utilise ici une expression de table commune (CTE)</span><br />
;<span style="color: #0000FF;">WITH</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; CTE <span style="color: #0000FF;">AS</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span>&nbsp; CT.<span style="color: #202020;">NomContact</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; &nbsp; &nbsp; &nbsp; &nbsp; CL.<span style="color: #202020;">NomClub</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; <span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dbo.<span style="color: #202020;">Club</span> <span style="color: #0000FF;">AS</span> CL<br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; dbo.<span style="color: #202020;">Membre</span> <span style="color: #0000FF;">AS</span> MB<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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> MB.<span style="color: #202020;">IDClub</span> <span style="color: #808080;">=</span> CL.<span style="color: #202020;">IDClub</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CT.<span style="color: #202020;">IDContact</span> <span style="color: #808080;">=</span> MB.<span style="color: #202020;">IDContact</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FOR</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XML <span style="color: #0000FF;">PATH</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> liste_club<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; dbo.<span style="color: #202020;">Contact</span> <span style="color: #0000FF;">AS</span> CT<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">SELECT</span>&nbsp; NomContact<br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #0000FF;">LEFT</span><span style="color: #808080;">&#40;</span>liste_club, <span style="color: #FF00FF;">LEN</span><span style="color: #808080;">&#40;</span>liste_club<span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> liste_club<br />
<span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; CTE;<br />
<br />
<span style="color: #008080;">-- Mais on peut se passer de la CTE en utilisant une table dérivée :</span><br />
<span style="color: #0000FF;">SELECT</span>&nbsp; LC.<span style="color: #202020;">NomContact</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #0000FF;">LEFT</span><span style="color: #808080;">&#40;</span>LC.<span style="color: #202020;">liste_club</span>, <span style="color: #FF00FF;">LEN</span><span style="color: #808080;">&#40;</span>LC.<span style="color: #202020;">liste_club</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> liste_club<br />
<span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; <span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span>&nbsp; CT.<span style="color: #202020;">NomContact</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; &nbsp; &nbsp; &nbsp; &nbsp; CL.<span style="color: #202020;">NomClub</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; <span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dbo.<span style="color: #202020;">Club</span> <span style="color: #0000FF;">AS</span> CL<br />
&nbsp; &nbsp; &nbsp; &nbsp; &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; dbo.<span style="color: #202020;">Membre</span> <span style="color: #0000FF;">AS</span> MB<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; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> MB.<span style="color: #202020;">IDClub</span> <span style="color: #808080;">=</span> CL.<span style="color: #202020;">IDClub</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">WHERE</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CT.<span style="color: #202020;">IDContact</span> <span style="color: #808080;">=</span> MB.<span style="color: #202020;">IDContact</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FOR</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XML <span style="color: #0000FF;">PATH</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">''</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> liste_club<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; dbo.<span style="color: #202020;">Contact</span> <span style="color: #0000FF;">AS</span> CT<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> LC<span style="color: #808080;">&#40;</span>NomContact, liste_club<span style="color: #808080;">&#41;</span></div></td></tr></tbody></table></div>
<p>Maintenant, supposons que nous avons un rapport SSRS qui appelle une procédure stockée, et que l&rsquo;un des paramètres du rapport permet un choix multiple de valeurs. Il nous est donc nécessaire de transformer cette liste de valeurs en une table d&rsquo;une seule colonne :</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;">DECLARE</span> @s <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">max</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'30815903-5911-418B-BB56-CE0CF4981142,029FE2E3-E57D-4C4D-A514-2448149560F9,F6A73DFA-C79A-40B5-B12F-55DA66C5A158,D896FB47-B5B8-4E8F-BFA1-62A65F66CB8B'</span><br />
<br />
<span style="color: #008080;">-- On ajoute la balise v en tête et en queue de chaîne,</span><br />
<span style="color: #008080;">-- et on remplace les virgules par le doublet</span><br />
<span style="color: #0000FF;">DECLARE</span> @x xml &nbsp;<span style="color: #808080;">=</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'&lt;v&gt;'</span><span style="color: #808080;">+</span> <span style="color: #FF00FF;">REPLACE</span><span style="color: #808080;">&#40;</span>@s, <span style="color: #FF0000;">','</span> ,<span style="color: #FF0000;">'&lt;/v&gt;&lt;v&gt;'</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> <span style="color: #FF0000;">'&lt;/v&gt;'</span> <span style="color: #0000FF;">AS</span> &nbsp;xml<span style="color: #808080;">&#41;</span><br />
<br />
<span style="color: #008080;">-- On dépouille le document XML créé en utilisant la fonction nodes()</span><br />
<span style="color: #008080;">-- du langage XQuery, intégré à SQL Server depuis sa version 2005</span><br />
<span style="color: #0000FF;">SELECT</span> vs.<span style="color: #0000FF;">value</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'.'</span>, <span style="color: #FF0000;">'uniqueidentifier'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> valeur_scalaire<br />
<span style="color: #0000FF;">FROM</span> &nbsp; @x.<span style="color: #202020;">nodes</span><span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'/v'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> x<span style="color: #808080;">&#40;</span>vs<span style="color: #808080;">&#41;</span></div></td></tr></tbody></table></div>
<p>L&rsquo;utilisation de FOR XML PATH est de très loin la méthode qui permet de générer une liste de valeurs de façon performante. Ceci n&rsquo;en fait pas une raison pour l&rsquo;utiliser à tors et à travers, mais seulement lorsque cela est nécessaire.</p>
<p>ElSüket.</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>La clause VALUES n&#8217;est-elle réservée qu&#8217;à l&#8217;ordre INSERT ?</title>
		<link>https://blog.developpez.com/elsuket/p11988/t-sql/la-clause-values-nest-elle-reservee-qua-lordre-insert</link>
		<comments>https://blog.developpez.com/elsuket/p11988/t-sql/la-clause-values-nest-elle-reservee-qua-lordre-insert#comments</comments>
		<pubDate>Sat, 25 May 2013 12:21:25 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[DELETE]]></category>
		<category><![CDATA[INSERT]]></category>
		<category><![CDATA[MAX]]></category>
		<category><![CDATA[MERGE]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[UPDATE]]></category>
		<category><![CDATA[VALUES]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/elsuket/?p=376</guid>
		<description><![CDATA[Si je pose la question, c&#8217;est que la réponse est &#8230; non, et ce depuis SQL Server 2008 ! En effet l&#8217;utilisation la plus commune de la clause VALUES se fait lors de la définition d&#8217;un ordre INSERT, et permettait &#8230; <a href="https://blog.developpez.com/elsuket/p11988/t-sql/la-clause-values-nest-elle-reservee-qua-lordre-insert">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Si je pose la question, c&rsquo;est que la réponse est &#8230; non, et ce depuis SQL Server 2008 !</p>
<p>En effet l&rsquo;utilisation la plus commune de la clause VALUES se fait lors de la définition d&rsquo;un ordre INSERT, et permettait jusqu&rsquo;à SQL Server 2005 inclus d&rsquo;insérer seulement une ligne dans une table.</p>
<p>Cela a changé lors de la sortie de SQL Server 2008, qui permet de réaliser un INSERT d&rsquo;au plus 1000 lignes dont les valeurs sont codées &laquo;&nbsp;en dur&nbsp;&raquo; dans un script. Voyons les autres utilisations possible de cette clause &#8230;<br />
<span id="more-376"></span></p>
<p>En fait, cette clause est utilisable pour toute instruction DML, pour peu qu&rsquo;on la précise dans la clause FROM d&rsquo;une instruction SELECT, INSERT, UPDATE ou DELETE, et dans la clause USING de l&rsquo;instruction MERGE.</p>
<p>Voici quelques exemples simples d&rsquo;utilisation, qui éveilleront votre imagination dès que vous retournerez à vos claviers.<br />
Commençons par appréhender la clause avec une requête triviale :</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; <span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #808080;">-</span><span style="color: #000;">15</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #000;">2</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #000;">3</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> V<span style="color: #808080;">&#40;</span>i<span style="color: #808080;">&#41;</span></div></td></tr></tbody></table></div>
<p>Ce qui nous retourne le résultat suivant :</p>
<p><img src="http://img835.imageshack.us/img835/6108/values01.png" alt="" /></p>
<p>Si nous regardons le plan d&rsquo;exécution à l&rsquo;aide de <a href="http://blog.developpez.com/elsuket/p10724/utilitaires/sql_sentry_plan_explorer">SQL Sentry Plan Explorer</a>, on voit que l&rsquo;estimation de cardinalité se fait proprement :</p>
<p><img src="http://img51.imageshack.us/img51/5177/values02.png" alt="" /></p>
<p>Ceci évite d&rsquo;écrire une requête avec de nombreux UNION ALL, comme suit :</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: #0000FF;">SELECT</span>&nbsp; <span style="color: #808080;">*</span><br />
<span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; <span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">-</span><span style="color: #000;">15</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span> <span style="color: #0000FF;">SELECT</span> <span style="color: #000;">0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span> <span style="color: #0000FF;">SELECT</span> <span style="color: #000;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span> <span style="color: #0000FF;">SELECT</span> <span style="color: #000;">2</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span> <span style="color: #0000FF;">SELECT</span> <span style="color: #000;">3</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> V<span style="color: #808080;">&#40;</span>i<span style="color: #808080;">&#41;</span></div></td></tr></tbody></table></div>
<p>Même si l&rsquo;estimation de cardinalité se fait de la même manière.</p>
<p>On peut donc spécifier un INSERT de plusieurs lignes à l&rsquo;aide de la clause VALUES :</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;">DECLARE</span> @pays_capitale <span style="color: #0000FF;">TABLE</span><br />
<span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; pays <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">15</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , capitale <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">32</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span><br />
<span style="color: #808080;">&#41;</span><br />
<br />
<span style="color: #0000FF;">INSERT</span>&nbsp; <span style="color: #0000FF;">INTO</span> @pays_capitale<br />
<span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; pays<br />
&nbsp; &nbsp; &nbsp; &nbsp; , capitale<br />
<span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">VALUES</span>&nbsp; <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'France'</span>, <span style="color: #FF0000;">'Paris'</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Thaïlande'</span>, <span style="color: #FF0000;">'Bangkok'</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Bouthan'</span>, <span style="color: #FF0000;">'Thimphou'</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Vanuatu'</span>, <span style="color: #FF0000;">'Port-Vila'</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Singapour'</span>, <span style="color: #FF0000;">'Singapour'</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Viêt Nam'</span>, <span style="color: #FF0000;">'Hanoï'</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Cambodge'</span>, <span style="color: #FF0000;">'Phnom Penh'</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Laos'</span>, <span style="color: #FF0000;">'Vientiane'</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Malaisie'</span>, <span style="color: #FF0000;">'Kuala Lumpur'</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Brunei'</span>, <span style="color: #FF0000;">'Bandar Seri Begawan'</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Philippines'</span>, <span style="color: #FF0000;">'Manille'</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Indonésie'</span>, <span style="color: #FF0000;">'Jakarta'</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Birmanie'</span>, <span style="color: #FF0000;">'Naypyidaw'</span><span style="color: #808080;">&#41;</span><br />
<br />
<span style="color: #0000FF;">SELECT</span>&nbsp; <span style="color: #808080;">*</span><br />
<span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; @pays_capitale</div></td></tr></tbody></table></div>
<p>Nous obtenons le résultat suivant :</p>
<p><img src="http://img713.imageshack.us/img713/5703/values03.png" alt="" /></p>
<p>La démonstration des exemples suivants, nous utilisons le jeu de données défini par le lot de requêtes ci-dessous :</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 /></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;">TABLE</span> capteur<br />
<span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; capteur_id <span style="color: #0000FF;">tinyint</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">IDENTITY</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">0</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">CONSTRAINT</span> PK_capteur <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , capteur_nom <span style="color: #0000FF;">varchar</span><span style="color: #808080;">&#40;</span><span style="color: #000;">15</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; <span style="color: #0000FF;">CONSTRAINT</span> UQ_capteur <span style="color: #0000FF;">UNIQUE</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , capteur_date_ajout <span style="color: #0000FF;">datetime</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">CONSTRAINT</span> DF_capteur__capteur_date_ajout <span style="color: #0000FF;">DEFAULT</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #808080;">&#41;</span><br />
GO<br />
<br />
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> mesure<br />
<span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; mesure_id <span style="color: #0000FF;">int</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">IDENTITY</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">-</span><span style="color: #000;">2147483648</span>, <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">CONSTRAINT</span> PK_mesure <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , capteur_id <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; <span style="color: #0000FF;">CONSTRAINT</span> FK_mesure__capteur_id<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FOREIGN</span> <span style="color: #0000FF;">KEY</span> <span style="color: #808080;">&#40;</span>capteur_id<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">REFERENCES</span> dbo.<span style="color: #202020;">capteur</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , mesure_date_time <span style="color: #0000FF;">datetime</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">CONSTRAINT</span> DF_mesure_mesure_date_time <span style="color: #0000FF;">DEFAULT</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , valeur <span style="color: #0000FF;">tinyint</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</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;">capteur</span><br />
<span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; capteur_nom<br />
<span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Capteur 1'</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Capteur 2'</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Capteur 3'</span><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;">mesure</span><br />
<span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; capteur_id<br />
&nbsp; &nbsp; &nbsp; &nbsp; , valeur<br />
<span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">SELECT</span>&nbsp; <span style="color: #FF00FF;">ABS</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">CHECKSUM</span><span style="color: #808080;">&#40;</span>NEWID<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">%</span> <span style="color: #000;">3</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #FF00FF;">ABS</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">CHECKSUM</span><span style="color: #808080;">&#40;</span>NEWID<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">%</span> <span style="color: #000;">256</span><br />
<span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; master.<span style="color: #202020;">dbo</span>.<span style="color: #202020;">spt_values</span><br />
<span style="color: #0000FF;">WHERE</span> &nbsp; type <span style="color: #808080;">=</span> <span style="color: #FF0000;">'P'</span><br />
GO <span style="color: #000;">50</span></div></td></tr></tbody></table></div>
<p>La vue master.dbo.spt_values retourne 2048 lignes qui ont pour type &laquo;&nbsp;P&nbsp;&raquo; : la table <em>dbo.mesure</em> contient donc 2048 * 50 = 102.400 lignes.</p>
<p>Étant donné que la clause se comporte de la même façon qu&rsquo;une table dérivée, on peut tout à fait l&rsquo;utiliser pour réaliser une jointure.<br />
Essayons de classer les mesures par fourchettes :</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 /></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; &nbsp; &nbsp; &nbsp; &nbsp; BUCKET.<span style="color: #202020;">low_bound</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , BUCKET.<span style="color: #202020;">high_bound</span><br />
&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> occurences<br />
<span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dbo.<span style="color: #202020;">mesure</span> <span style="color: #0000FF;">AS</span> M<br />
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>&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;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">0</span>, <span style="color: #000;">5</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #000;">5</span>, <span style="color: #000;">10</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #000;">10</span>, <span style="color: #000;">15</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #000;">15</span>, <span style="color: #000;">20</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #000;">20</span>, <span style="color: #000;">30</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">&#40;</span><span style="color: #000;">30</span>, <span style="color: #000;">40</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #000;">40</span>, <span style="color: #000;">50</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #000;">50</span>, <span style="color: #000;">100</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #000;">100</span>, <span style="color: #000;">200</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">&#40;</span><span style="color: #000;">200</span>, <span style="color: #000;">1000</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> BUCKET<span style="color: #808080;">&#40;</span>low_bound, high_bound<span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> M.<span style="color: #202020;">valeur</span> <span style="color: #808080;">&amp;</span>gt;<span style="color: #808080;">=</span> BUCKET.<span style="color: #202020;">low_bound</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">AND</span> M.<span style="color: #202020;">valeur</span> <span style="color: #808080;">&amp;</span>lt; BUCKET.<span style="color: #202020;">high_bound</span><br />
<span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span>&nbsp; &nbsp; &nbsp; &nbsp; BUCKET.<span style="color: #202020;">low_bound</span>, BUCKET.<span style="color: #202020;">high_bound</span><br />
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span>&nbsp; &nbsp; &nbsp; &nbsp; BUCKET.<span style="color: #202020;">low_bound</span>, BUCKET.<span style="color: #202020;">high_bound</span></div></td></tr></tbody></table></div>
<p>Ce qui nous retourne :</p>
<p><img src="http://img845.imageshack.us/img845/7702/values04.png" alt="" /></p>
<p>Voyons le cas d&rsquo;un UPDATE :</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 /></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;">WITH</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; BUCKET <span style="color: #808080;">&#40;</span>low_bound, high_bound<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#40;</span><br />
&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; <span style="color: #0000FF;">FROM</span>&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;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #000;">0</span>, <span style="color: #000;">5</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #000;">5</span>, <span style="color: #000;">10</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">&#40;</span><span style="color: #000;">10</span>, <span style="color: #000;">15</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #000;">15</span>, <span style="color: #000;">20</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #000;">20</span>, <span style="color: #000;">30</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> V<span style="color: #808080;">&#40;</span>low_bound, high_bound<span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">UPDATE</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dbo.<span style="color: #202020;">mesure</span><br />
<span style="color: #0000FF;">SET</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; valeur <span style="color: #808080;">*=</span> <span style="color: #000;">2</span> <span style="color: #008080;">-- multiplie par deux la valeur mesurée</span><br />
<span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dbo.<span style="color: #202020;">mesure</span> <span style="color: #0000FF;">AS</span> M<br />
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>&nbsp; &nbsp; &nbsp; BUCKET <span style="color: #0000FF;">AS</span> B<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> B.<span style="color: #202020;">low_bound</span> &nbsp;M.<span style="color: #202020;">valeur</span></div></td></tr></tbody></table></div>
<p>Notez que rien ici n&rsquo;oblige à la construction d&rsquo;une expression de table commune : on peut tout à fait spécifier le constructeur de valeurs de table dans la clause INNER JOIN, comme nous l&rsquo;avons fait avec la requête précédente.</p>
<p>Si nous ré-exécutons la requête de classement par fourchettes, nous obtenons maintenant :</p>
<p><img src="http://img69.imageshack.us/img69/5861/values05.png" alt="" /></p>
<p>l&rsquo;instruction DELETE se décrit de manière similaire :</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: #0000FF;">DELETE</span> <span style="color: #0000FF;">FROM</span> &nbsp; &nbsp; dbo.<span style="color: #202020;">mesure</span><br />
<span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dbo.<span style="color: #202020;">mesure</span> <span style="color: #0000FF;">AS</span> M<br />
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>&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;">VALUES</span> &nbsp;<span style="color: #808080;">&#40;</span><span style="color: #000;">30</span>, <span style="color: #000;">40</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #000;">40</span>, <span style="color: #000;">50</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #000;">50</span>, <span style="color: #000;">100</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">&#40;</span><span style="color: #000;">100</span>, <span style="color: #000;">200</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #000;">200</span>, <span style="color: #000;">1000</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> BUCKET <span style="color: #808080;">&#40;</span>low_bound, high_bound<span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> M.<span style="color: #202020;">valeur</span> <span style="color: #808080;">&amp;</span>gt;<span style="color: #808080;">=</span> BUCKET.<span style="color: #202020;">low_bound</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">AND</span> M.<span style="color: #202020;">valeur</span> <span style="color: #808080;">&amp;</span>lt; BUCKET.<span style="color: #202020;">high_bound</span></div></td></tr></tbody></table></div>
<p>Après ré-exécution de la requête de distribution des valeurs par plages, on voit que la table des mesures contient maintenant :</p>
<p><img src="http://img90.imageshack.us/img90/1578/values06.png" alt="" /></p>
<p>Terminons par l&rsquo;ordre MERGE, avec lequel nous allons ajouter des capteurs, ou marquer ceux qui existent déjà à la date courante :</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 /></div></td><td><div class="tsql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">MERGE &nbsp; dbo.<span style="color: #202020;">capteur</span> <span style="color: #0000FF;">AS</span> TGT<br />
<span style="color: #0000FF;">USING</span> &nbsp; <span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Capteur 1'</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Capteur 2'</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Capteur 3'</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Capteur 4'</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Capteur 5'</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'Capteur 6'</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> SRC <span style="color: #808080;">&#40;</span>capteur_nom<span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> TGT.<span style="color: #202020;">capteur_nom</span> <span style="color: #808080;">=</span> SRC.<span style="color: #202020;">capteur_nom</span><br />
<span style="color: #0000FF;">WHEN</span>&nbsp; &nbsp; MATCHED <span style="color: #0000FF;">THEN</span> <span style="color: #0000FF;">UPDATE</span> <span style="color: #0000FF;">SET</span> capteur_date_ajout <span style="color: #808080;">=</span> <span style="color: #FF00FF;">GETDATE</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">WHEN</span>&nbsp; &nbsp; <span style="color: #808080;">NOT</span> MATCHED <span style="color: #0000FF;">THEN</span> <span style="color: #0000FF;">INSERT</span> <span style="color: #808080;">&#40;</span>capteur_nom<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">VALUES</span> <span style="color: #808080;">&#40;</span>SRC.<span style="color: #202020;">capteur_nom</span><span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">OUTPUT</span>&nbsp; $ACTION, DELETED.<span style="color: #202020;">capteur_nom</span>, <span style="color: #808080;">IN</span>SERTED.<span style="color: #202020;">capteur_nom</span>;<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;">capteur</span></div></td></tr></tbody></table></div>
<p><img src="http://img855.imageshack.us/img855/6277/values07.png" alt="" /></p>
<p>On voit bien les capteurs 4, 5 et 6 ajoutés, et les capteurs 1, 2 et 3, dont les lignes existaient déjà, modifiés. Tous les capteurs portent la même date d&rsquo;ajout, transaction oblige.</p>
<p>Enfin on peut calculer le maximum d&rsquo;un tuple de valeurs sans avoir à écrire une fonction pour ce faire. Soit la variable de type TABLE 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 />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<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;">DECLARE</span> @series <span style="color: #0000FF;">TABLE</span><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; , a <span style="color: #0000FF;">tinyint</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , b <span style="color: #0000FF;">tinyint</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , c <span style="color: #0000FF;">tinyint</span><br />
<span style="color: #808080;">&#41;</span><br />
<br />
<span style="color: #0000FF;">INSERT</span>&nbsp; <span style="color: #0000FF;">INTO</span> @series<br />
<span style="color: #0000FF;">VALUES</span>&nbsp; <span style="color: #808080;">&#40;</span><span style="color: #000;">1</span>, <span style="color: #000;">2</span>, <span style="color: #000;">3</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">&#40;</span><span style="color: #000;">4</span>, <span style="color: #000;">5</span>, <span style="color: #000;">6</span><span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">&#40;</span><span style="color: #000;">7</span>, <span style="color: #000;">8</span>, <span style="color: #000;">9</span><span style="color: #808080;">&#41;</span></div></td></tr></tbody></table></div>
<p>Nous souhaitons obtenir la plus grande valeur des trois colonnes a, b et c, pour chaque ligne. On peut exprimer ce besoin comme suit :</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: #0000FF;">SELECT</span>&nbsp; id<br />
&nbsp; &nbsp; &nbsp; &nbsp; , <span style="color: #808080;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">SELECT</span>&nbsp; <span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#40;</span>n<span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">FROM</span>&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;">VALUES</span> <span style="color: #808080;">&#40;</span>a<span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span>b<span style="color: #808080;">&#41;</span>, <span style="color: #808080;">&#40;</span>c<span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> V<span style="color: #808080;">&#40;</span>n<span style="color: #808080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080;">&#41;</span><br />
<span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; @series</div></td></tr></tbody></table></div>
<p>Ce qui amène le résultat suivant :</p>
<p><img src="http://img15.imageshack.us/img15/2505/values08.png" alt="" /></p>
<p>Amusez-vous avec VALUES !</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Connaître les caractéristiques d&#8217;une trace SQL Profiler</title>
		<link>https://blog.developpez.com/elsuket/p9016/moteur-de-base-de-donnees-sql-server/connaitre_les_caracteristiques_d_une_tra</link>
		<comments>https://blog.developpez.com/elsuket/p9016/moteur-de-base-de-donnees-sql-server/connaitre_les_caracteristiques_d_une_tra#comments</comments>
		<pubDate>Wed, 16 Jun 2010 06:27:27 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Moteur de base de données SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Utilitaires]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Sur un serveur à la configuration sous-dimensionnée que je surveille de temps en temps, j&#8217;ai trouvé une trace SQL Profiler en cours d&#8217;exécution. J&#8217;ai voulu en savoir plus sur les caractéristiques de cette trace. Voici la requête : 123456789101112131415------------------------------- -- &#8230; <a href="https://blog.developpez.com/elsuket/p9016/moteur-de-base-de-donnees-sql-server/connaitre_les_caracteristiques_d_une_tra">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Sur un serveur à la configuration sous-dimensionnée que je surveille de temps en temps, j&rsquo;ai trouvé une trace SQL Profiler en cours d&rsquo;exécution.<br />
J&rsquo;ai voulu en savoir plus sur les caractéristiques de cette trace.<br />
Voici la requête :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">------------------------------- <br />
-- Nicolas SOUQUET - 16/06/2010 <br />
------------------------------- <br />
SELECT &nbsp; &nbsp;C.name <br />
&nbsp; &nbsp; , FI.value <br />
&nbsp; &nbsp; , T.start_time <br />
&nbsp; &nbsp; , T.last_event_time <br />
&nbsp; &nbsp; , T.event_count <br />
&nbsp; &nbsp; , S.login_name <br />
&nbsp; &nbsp; , S.session_id <br />
&nbsp; &nbsp; , 'EXEC sp_trace_setstatus ' + CAST(T.id AS varchar(10)) + ', 0' AS stop_trace_stmt <br />
FROM &nbsp; &nbsp;sys.traces AS T <br />
CROSS APPLY &nbsp;sys.fn_trace_getfilterinfo (T.id) AS FI <br />
INNER JOIN &nbsp;sys.trace_columns AS C ON FI.columnid = C.trace_column_id <br />
INNER JOIN &nbsp;sys.dm_exec_sessions AS S ON S.session_id = T.reader_spid</div></td></tr></tbody></table></div>
<p>On sait tout de suite quel filtres ont été utilisés à l&rsquo;aide des deux premières colonnes.<br />
La dernière colonne contient l&rsquo;instruction qui permet d&rsquo;arrêter la trace : le développeur a donc toujours ses résultats.</p>
<p>Pour savoir comment créer une trace côté serveur, c&rsquo;est par <a href="http://elsuket.developpez.com/tutoriels/sqlserveur/fichier-trace-cote-serveur/">ici</a></p>
<p>Bon traçage !</p>
<p>ElSüket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Puissance de la clause IN</title>
		<link>https://blog.developpez.com/elsuket/p9093/t-sql/puissance_de_la_clause_in</link>
		<comments>https://blog.developpez.com/elsuket/p9093/t-sql/puissance_de_la_clause_in#comments</comments>
		<pubDate>Wed, 07 Jul 2010 04:33:26 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[La clause IN permet de filtrer une requête sur un ensemble de valeurs, soit en les listant &#171;&#160;en dur&#160;&#187;, soit pas une sous-requête. Si ce n&#8217;est pas un opérateur que j&#8217;affectionne particulièrement puisqu&#8217;il n&#8217;est pas SARGable, en voici une utilisation &#8230; <a href="https://blog.developpez.com/elsuket/p9093/t-sql/puissance_de_la_clause_in">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>La clause IN permet de filtrer une requête sur un ensemble de valeurs, soit en les listant &laquo;&nbsp;en dur&nbsp;&raquo;, soit pas une sous-requête.</p>
<p>Si ce n&rsquo;est pas un opérateur que j&rsquo;affectionne particulièrement puisqu&rsquo;il n&rsquo;est pas <a href="http://blog.developpez.com/elsuket/p6886/moteur-de-base-de-donnees-sql-server/sql-searchable-arguments-ou-s-args/">SARGable</a>, en voici une utilisation assez puissante :</p>
<p><span id="more-10"></span></p>
<p>Créons donc la petite table suivante :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">CREATE TABLE uneTable &nbsp;<br />
( <br />
&nbsp; id TINYINT IDENTITY <br />
&nbsp; , uneColonne TINYINT <br />
&nbsp; , uneAutreColonne TINYINT <br />
&nbsp; , uneNouvelleColonne TINYINT <br />
) <br />
&nbsp;<br />
INSERT INTO uneTable VALUES (1, 2, 100) <br />
INSERT INTO uneTable VALUES (2, 100, 1) <br />
INSERT INTO uneTable VALUES (100, 1, 2)</div></td></tr></tbody></table></div>
<p>Vous devinez simplement le résultat de la requête suivante :</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">SELECT &nbsp;id <br />
FROM &nbsp;dbo.uneTable <br />
WHERE &nbsp;uneColonne IN (1, 2)</div></td></tr></tbody></table></div>
<p>qui retourne :</p>
<p>id<br />
&#8212;<br />
1<br />
2</p>
<p>Mais quid ce celle-ci ? <img src="https://blog.developpez.com/elsuket/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /></p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">SELECT &nbsp;id <br />
FROM &nbsp;dbo.uneTable <br />
WHERE &nbsp;1 IN (uneAutreColonne, uneNouvelleColonne)</div></td></tr></tbody></table></div>
<p>id<br />
&#8212;<br />
2<br />
3</p>
<p>ElSüket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Attention aux types de données money et smallmoney !</title>
		<link>https://blog.developpez.com/elsuket/p11175/moteur-de-base-de-donnees-sql-server/attention_money_smallmoney</link>
		<comments>https://blog.developpez.com/elsuket/p11175/moteur-de-base-de-donnees-sql-server/attention_money_smallmoney#comments</comments>
		<pubDate>Wed, 25 Jul 2012 18:23:18 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Moteur de base de données SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[En effet, ces deux types de données produisent des erreurs de calculs. Voici un petit test pour révéler cela : 12345678910111213141516171819202122DECLARE @mon1 smallmoney, &#160; &#160; &#160; &#160;@mon2 smallmoney, &#160; &#160; &#160; &#160;@mon3 smallmoney, &#160; &#160; &#160; &#160;@mon4 smallmoney, &#160; &#160; &#8230; <a href="https://blog.developpez.com/elsuket/p11175/moteur-de-base-de-donnees-sql-server/attention_money_smallmoney">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>En effet, ces deux types de données produisent des erreurs de calculs.<br />
Voici un petit test pour révéler cela :</p>
<p><span id="more-12"></span></p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">DECLARE @mon1 smallmoney, <br />
&nbsp; &nbsp; &nbsp; &nbsp;@mon2 smallmoney, <br />
&nbsp; &nbsp; &nbsp; &nbsp;@mon3 smallmoney, <br />
&nbsp; &nbsp; &nbsp; &nbsp;@mon4 smallmoney, <br />
&nbsp; &nbsp; &nbsp; &nbsp;@num1 decimal(19,4), <br />
&nbsp; &nbsp; &nbsp; &nbsp;@num2 decimal(19,4), <br />
&nbsp; &nbsp; &nbsp; &nbsp;@num3 decimal(19,4), <br />
&nbsp; &nbsp; &nbsp; &nbsp;@num4 decimal(19,4) <br />
&nbsp;<br />
SELECT @mon1 = 100 <br />
&nbsp; &nbsp; &nbsp; &nbsp;, @mon2 = 339 <br />
&nbsp; &nbsp; &nbsp; &nbsp;, @mon3 = 10000 <br />
&nbsp; &nbsp; &nbsp; &nbsp;--- <br />
&nbsp; &nbsp; &nbsp; &nbsp;, @num1 = 100 <br />
&nbsp; &nbsp; &nbsp; &nbsp;, @num2 = 339 <br />
&nbsp; &nbsp; &nbsp; &nbsp;, @num3 = 10000 <br />
&nbsp;<br />
SELECT @mon4 = @mon1 / @mon2 * @mon3 <br />
&nbsp; &nbsp; &nbsp; &nbsp;, @num4 = @num1 / @num2 * @num3 <br />
&nbsp;<br />
SELECT @mon4 AS money_result <br />
&nbsp; &nbsp; &nbsp; &nbsp;, @num4 AS numeric_result</div></td></tr></tbody></table></div>
<p><img src="http://blog.developpez.com/media/money_error.PNG" width="325" height="507" alt="" /></p>
<p>Attention donc au <a href="http://blog.developpez.com/elsuket/p8233/sql-general/du-choix-des-types-de-donnees/">choix des types de données</a> !</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Tout savoir sur SEQUENCE avec SQL Server 2012</title>
		<link>https://blog.developpez.com/elsuket/p11008/t-sql/sql_server_2012_sequence</link>
		<comments>https://blog.developpez.com/elsuket/p11008/t-sql/sql_server_2012_sequence#comments</comments>
		<pubDate>Sun, 03 Jun 2012 11:03:40 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Implémentation de SQL dans SQL Server longtemps attendue, voici enfin SEQUENCE, qui permet d&#8217;obtenir un nombre augmenté d&#8217;un certain incrément à chaque appel, que l&#8217;on peut partager entre les tables et les utilisateurs d&#8217;une base de données, et bien d&#8217;autre &#8230; <a href="https://blog.developpez.com/elsuket/p11008/t-sql/sql_server_2012_sequence">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Implémentation de SQL dans SQL Server longtemps attendue, voici enfin SEQUENCE, qui permet d&rsquo;obtenir un nombre augmenté d&rsquo;un certain incrément à chaque appel, que l&rsquo;on peut partager entre les tables et les utilisateurs d&rsquo;une base de données, et bien d&rsquo;autre possibilités encore &#8230;<br />
<span id="more-168"></span><br />
<strong>Attention : cet article comporte 6 pages <img src="https://blog.developpez.com/elsuket/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /></strong></p>
<p>On notera tout d&rsquo;abord qu&rsquo;un objet de type SEQUENCE n&rsquo;est utilisable qu&rsquo;avec des types numériques précis : tinyint, smallint, int, bigint, decimal et numéric, les deux derniers avec une échelle à zéro.</p>
<p>Commençons donc par créer une SEQUENCE de la façon la plus simple qui soit :</p>
<p><code class="codecolorer text default"><span class="text">CREATE SEQUENCE dbo.maSequence</span></code></p>
<p><img src="http://blog.developpez.com/media/sequence_01.PNG" width="289" height="157" alt="" /></p>
<p>C&rsquo;est aussi simple que cela, mais nous allons voir par la suite qu&rsquo;on peut aller un peu plus loin.</p>
<p>Maintenant, comment obtenir la &laquo;&nbsp;première&nbsp;&raquo; valeur de la SEQUENCE dbo.maSequence ?<br />
Là encore, c&rsquo;est relativement simple :</p>
<p><code class="codecolorer text default"><span class="text">SELECT NEXT VALUE FOR dbo.maSequence</span></code></p>
<p><img src="http://blog.developpez.com/media/sequence_02.PNG" width="340" height="204" alt="" /></p>
<p>Donc si l&rsquo;on crée un objet SEQUENCE sans aucune option, le type par défaut est <em>bigint</em>, et la valeur initiale attribuée à cette SEQUENCE est la plus petite valeur que peut stocker le type de données <em>bigint</em>.</p>
<p>Est-ce le cas avec les autres types de données ?</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 /></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;">SEQUENCE</span> dbo.<span style="color: #202020;">ma_sequence_smallint</span><br />
<span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">smallint</span><br />
GO<br />
<br />
<span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">NEXT</span> <span style="color: #0000FF;">VALUE</span> <span style="color: #0000FF;">FOR</span> dbo.<span style="color: #202020;">ma_sequence_smallint</span> <span style="color: #0000FF;">AS</span> next_value_ma_sequence_smallint<br />
GO<br />
<br />
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">SEQUENCE</span> dbo.<span style="color: #202020;">ma_sequence_tinyint</span><br />
<span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">tinyint</span><br />
GO<br />
<br />
<span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">NEXT</span> <span style="color: #0000FF;">VALUE</span> <span style="color: #0000FF;">FOR</span> dbo.<span style="color: #202020;">ma_sequence_tinyint</span> <span style="color: #0000FF;">AS</span> next_value_ma_sequence_tinyint<br />
GO</div></td></tr></tbody></table></div>
<p>
<img src="http://blog.developpez.com/media/sequence_03.PNG" width="657" height="422" alt="" /></p>
<p>Rien de surprenant par rapport au comportement par défaut.<br />
En revanche le fait que la SEQUENCE ne commence pas à 1 comme avec les colonnes de type entier auxquelles on confère la propriété d&rsquo;auto-incrémentation à l&rsquo;aide de la clause IDENTITY est appréciable.</p>
<p>Avant de continuer à nous éclairer sur SEQUENCE, voyons comment connaître les propriétés d&rsquo;un tel objet : la vue système <em>sys.sequences</em> est là pour cela :</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 /></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; &nbsp; &nbsp; &nbsp; &nbsp; S.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> sequence_name<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , T.<span style="color: #202020;">name</span> <span style="color: #0000FF;">AS</span> sequence_data_type_name<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , S.<span style="color: #202020;">start_value</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , S.<span style="color: #202020;">increment</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , S.<span style="color: #202020;">minimum_value</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , S.<span style="color: #202020;">maximum_value</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , S.<span style="color: #202020;">is_cycling</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , S.<span style="color: #202020;">cache_size</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , S.<span style="color: #202020;">current_value</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; , S.<span style="color: #202020;">is_exhausted</span><br />
<span style="color: #0000FF;">FROM</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sys.<span style="color: #202020;">sequences</span> <span style="color: #0000FF;">AS</span> S<br />
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span>&nbsp; &nbsp; &nbsp; sys.<span style="color: #202020;">types</span> <span style="color: #0000FF;">AS</span> T<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">ON</span> S.<span style="color: #202020;">system_type_id</span> <span style="color: #808080;">=</span> T.<span style="color: #202020;">system_type_id</span></div></td></tr></tbody></table></div>
<p>Ce qui retourne :</p>
<p><img src="http://blog.developpez.com/media/sequence_04.PNG" width="1126" height="376" alt="" /></p>
<p>Je passerai sur la description des colonnes jusqu&rsquo;à <em>maximum_value</em>, et sur <em>current_value</em> puisque leur nom est suffisamment explicite.</p>
<p>La colonne <em>is_cycling</em> nous montre qu&rsquo;il est possible de faire boucler la SEQUENCE, c&rsquo;est à dire de la faire revenir à sa valeur de départ.<br />
La colonne <em>cache_size</em> indique le nombre de valeurs qui est mise en cache. Nous verrons la notion de cache de SEQUENCE par la suite.<br />
Enfin la colonne <em>is_exhausted</em> indique s&rsquo;il est encore possible de générer de nouveaux nombres à partir de celle-ci.</p>
<p>On retrouve les objets SEQUENCE dans l&rsquo;Explorateur d&rsquo;Objets, avec les fonctionnalités accessibles par clic-droit que l&rsquo;on connaît :</p>
<p><img src="http://blog.developpez.com/media/sequence_05.PNG" width="336" height="520" alt="" /></p>
<p>Après un clic sur <em>Propriétés</em> :</p>
<p><img src="http://blog.developpez.com/media/sequence_06.png" width="457" height="759" alt="" /></p>
<p>On obtient la fenêtre suivante :</p>
<p><img src="http://blog.developpez.com/media/sequence_07.png" width="756" height="681" alt="" /></p>
<p>On peut aussi créer une SEQUENCE décrémentielle, simplement en spécifiant un incrément négatif :</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 /></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;">SEQUENCE</span> uneSequenceDecrementielle<br />
<span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">int</span><br />
<span style="color: #0000FF;">START</span> <span style="color: #0000FF;">WITH</span> <span style="color: #000;">0</span><br />
<span style="color: #808080;">IN</span>CREMENT <span style="color: #0000FF;">BY</span> <span style="color: #808080;">-</span><span style="color: #000;">5</span><br />
GO<br />
<br />
<span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">NEXT</span> <span style="color: #0000FF;">VALUE</span> <span style="color: #0000FF;">FOR</span> uneSequenceDecrementielle<br />
<span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">NEXT</span> <span style="color: #0000FF;">VALUE</span> <span style="color: #0000FF;">FOR</span> uneSequenceDecrementielle<br />
<span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">NEXT</span> <span style="color: #0000FF;">VALUE</span> <span style="color: #0000FF;">FOR</span> uneSequenceDecrementielle<br />
<span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">NEXT</span> <span style="color: #0000FF;">VALUE</span> <span style="color: #0000FF;">FOR</span> uneSequenceDecrementielle<br />
<span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">NEXT</span> <span style="color: #0000FF;">VALUE</span> <span style="color: #0000FF;">FOR</span> uneSequenceDecrementielle</div></td></tr></tbody></table></div>
<p><img src="http://blog.developpez.com/media/sequence_08.png" /></p>
<p>On peut également changer l&rsquo;incrément en cours de route à l&rsquo;aide d&rsquo;une instruction ALTER SEQUENCE :</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;">ALTER</span> <span style="color: #0000FF;">SEQUENCE</span> dbo.<span style="color: #202020;">uneSequenceDecrementielle</span><br />
<span style="color: #808080;">IN</span>CREMENT <span style="color: #0000FF;">BY</span> <span style="color: #808080;">-</span><span style="color: #000;">2</span><br />
GO<br />
<br />
<span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">NEXT</span> <span style="color: #0000FF;">VALUE</span> <span style="color: #0000FF;">FOR</span> uneSequenceDecrementielle</div></td></tr></tbody></table></div>
<p>
<img src="http://blog.developpez.com/media/sequence_09.png" /></p>
<p>Il est également possible, comme le suggère la colonne <em>maximum_value</em> de la vue <em>sys.sequences</em>, mettre une valeur maximale à cette séquence.<br />
Il faut bien sûr dans ce cas que la valeur maximale de la séquence prenne en compte la valeur de départ, sinon celle-ci ne serait pas générable :</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;">SEQUENCE</span> dbo.<span style="color: #202020;">uneSequenceDecrementielle</span><br />
MAXVALUE <span style="color: #808080;">-</span><span style="color: #000;">30</span><br />
GO</div></td></tr></tbody></table></div>
<p>
<img src="http://blog.developpez.com/media/sequence_10.png" /></p>
<p>Ici comme la séquence est décrémentielle, SQL Server rejette l&rsquo;instruction ALTER SEQUENCE puisque nous avions spécifié que la SEQUENCE démarre à zéro.</p>
<p>Si nous changeons par MINVALUE :</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;">SEQUENCE</span> dbo.<span style="color: #202020;">uneSequenceDecrementielle</span><br />
MAXVALUE <span style="color: #808080;">-</span><span style="color: #000;">30</span><br />
GO</div></td></tr></tbody></table></div>
<p>
<img src="http://blog.developpez.com/media/sequence_11.png" /></p>
<p>Voici un message d&rsquo;avertissement intéressant : nous n&rsquo;avons pas spécifié de valeur pour la taille du cache de la SEQUENCE, mais SQL Server en a mis une par défaut &#8230;<br />
J&rsquo;ai tenté de savoir quelle est la taille du cache par défaut, et comment SQL Server stocke celle-ci en utilisant une connexion d&rsquo;administrateur dédiée (DAC), mais il semble que SQL Server réalise cela dans des tables internes.<br />
Si quelqu&rsquo;un découvre cela, merci de le poster en commentaire <img src="https://blog.developpez.com/elsuket/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /></p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Les nouvelles fonctions T-SQL de SQL Server 2012 &#8211; Episode 1/8 &#8211; PARSE() &#8211;</title>
		<link>https://blog.developpez.com/elsuket/p10950/t-sql/sql_server_2012_nouvelles_fonctions_pars</link>
		<comments>https://blog.developpez.com/elsuket/p10950/t-sql/sql_server_2012_nouvelles_fonctions_pars#comments</comments>
		<pubDate>Mon, 16 Apr 2012 12:18:57 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Nouvelles fonctions T-SQL]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Je vous propose de découvrir les 14 nouvelles fonctions que SQL Server 2012 introduit, en dédiant un billet soit à chacune d&#8217;entre-elles, ou bien à un groupe d&#8217;entre-elles. Commençons avec la fonction PARSE() &#8230; La syntaxe d&#8217;utilisation de cette fonction &#8230; <a href="https://blog.developpez.com/elsuket/p10950/t-sql/sql_server_2012_nouvelles_fonctions_pars">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Je vous propose de découvrir les 14 nouvelles fonctions que SQL Server 2012 introduit, en dédiant un billet soit à chacune d&rsquo;entre-elles, ou bien à un groupe d&rsquo;entre-elles.</p>
<p>Commençons avec la fonction PARSE() &#8230;<br />
<span id="more-165"></span><br />
La syntaxe d&rsquo;utilisation de cette fonction est la suivante :</p>
<p><code class="codecolorer text default"><span class="text">PARSE('&lt;uneValeur&gt;' AS &lt;data_type&gt; [USING '&lt;culture&gt;'])</span></code></p>
<p>La nouveauté c&rsquo;est la possibilité de formater une valeur suivant une culture, et ce n&rsquo;est pas moins de 33 cultures qui sont disponibles !<br />
De façon générale, cette fonction est une extension de ce que l&rsquo;on peut faire avec CONVERT().<br />
Rappelons dans les deux cas qu&rsquo;il s&rsquo;agit de fonctions de formatage de données.</p>
<p>On notera que :</p>
<p>&#8211; <em>data_type</em> est limité aux types suivants : bigint, int, smallint, tinyint, decimal, numeric, float, real, smallmoney, money, date, time, datetime, smalldatetime, datetime2 et datetimeoffset.</p>
<p>&#8211; On ne peut pas utiliser PARSE() pour exécuter une requête sur un serveur distant, car celle-ci dépend du .NET Framework 3.5 SP1 , et que rien ne garantit qu&rsquo;il est présent sur la machine distante.</p>
<p>Faisons quelques essais !</p>
<p><code class="codecolorer text default"><span class="text">SELECT PARSE ('100.000' AS int)</span></code></p>
<p><img src="http://blog.developpez.com/media/parse_01.PNG" width="469" height="153" alt="" /></p>
<p>Alors qu&rsquo;un <code class="codecolorer text default"><span class="text">SELECT CONVERT(int, '100.000')</span></code> :</p>
<p><img src="http://blog.developpez.com/media/parse_02.png" width="625" height="167" alt="" /></p>
<p>Essayons un <code class="codecolorer text default"><span class="text">SELECT PARSE('Lundi 16 Avril 2012' AS datetime USING 'fr-FR')</span></code></p>
<p><img src="http://blog.developpez.com/media/parse_03.PNG" width="528" height="154" alt="" /></p>
<p>Voyons au format Anglo-Saxon : <code class="codecolorer text default"><span class="text">SELECT PARSE('Monday 16 April 2012' AS datetime USING 'en-US')</span></code></p>
<p><img src="http://blog.developpez.com/media/parse_04.PNG" width="527" height="168" alt="" /></p>
<p>Quid du format que l&rsquo;on nous a donné à l&rsquo;école ? <code class="codecolorer text default"><span class="text">SELECT PARSE('Monday, April 16 2012' AS datetime USING 'en-US')</span></code></p>
<p><img src="http://blog.developpez.com/media/parse_05.png" width="529" height="162" alt="" /></p>
<p>En revanche <code class="codecolorer text default"><span class="text">SELECT PARSE('Monday, April 16th 2012' AS datetime USING 'en-US')</span></code></p>
<p><img src="http://blog.developpez.com/media/parse_06.PNG" width="751" height="155" alt="" /></p>
<p>Essayons <code class="codecolorer text default"><span class="text">SELECT PARSE('April, 16 2012' AS datetime USING 'en-US')</span></code></p>
<p><img src="http://blog.developpez.com/media/parse_07.png" width="517" height="164" alt="" /></p>
<p>Voyons avec une date du calendrier Grégo-Thaï : en effet, le calendrier Grégo-Thaï à 543 ans d&rsquo;avance sur le calendrier Grégorien :<br />
<code class="codecolorer text default"><span class="text">SELECT PARSE(N'16 &amp;#3648;&amp;#3617;&amp;#3625;&amp;#3634;&amp;#3618;&amp;#3609; 2555' AS datetime USING 'Th-TH')</span></code></p>
<p><img src="http://blog.developpez.com/media/parse_08.PNG" width="516" height="158" alt="" /></p>
<p>Et avec des variables ?</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">DECLARE @date_string char(20) = 'Lundi 16 Avril 2012' <br />
&nbsp; , @culture char(5) = 'fr-FR' <br />
&nbsp;<br />
SELECT PARSE(@date_string AS datetime USING @culture)</div></td></tr></tbody></table></div>
<p><img src="http://blog.developpez.com/media/parse_09.PNG" width="524" height="216" alt="" /></p>
<p>Et dans une table :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">CREATE TABLE test_PARSE <br />
( <br />
&nbsp; date_string nvarchar(20) <br />
&nbsp; , culture char(5) <br />
&nbsp; , formatted_date AS (PARSE(date_string AS datetime USING culture)) <br />
) <br />
GO <br />
&nbsp;<br />
INSERT &nbsp;INTO dbo.test_PARSE <br />
VALUES &nbsp;(N'Lundi 16 Avril 2012', 'fr-FR'), (N'16 &amp;#3648;&amp;#3617;&amp;#3625;&amp;#3634;&amp;#3618;&amp;#3609; 2555', 'th-TH') <br />
GO <br />
&nbsp;<br />
SELECT &nbsp;* <br />
FROM &nbsp;dbo.test_PARSE</div></td></tr></tbody></table></div>
<p><img src="http://blog.developpez.com/media/parse_10.PNG" width="597" height="376" alt="" /></p>
<p>Vous vous demandez s&rsquo;il sera possible de retourner une valeur décimale avec le symbole de la devise suivant une culture ? la réponse est oui : avec la fonction FORMAT(), que nous verrons au prochain épisode <img src="https://blog.developpez.com/elsuket/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley" /></p>
<p>Bon formatage de données !</p>
<p>ElSüket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>Une fonction de dépouillement d&#8217;une chaîne de valeurs séparées par des virgules</title>
		<link>https://blog.developpez.com/elsuket/p10599/t-sql/fonction_depouillement_chaine_csv</link>
		<comments>https://blog.developpez.com/elsuket/p10599/t-sql/fonction_depouillement_chaine_csv#comments</comments>
		<pubDate>Mon, 19 Dec 2011 15:37:42 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Je vous propose ici une fonction en pur T-SQL qui permet de retourner les valeurs que l&#8217;on trouve entre les virgules d&#8217;une chaîne en contenant un plus ou moins grand nombre. Celle-ci repose sur une table de nombres, ce qui &#8230; <a href="https://blog.developpez.com/elsuket/p10599/t-sql/fonction_depouillement_chaine_csv">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Je vous propose ici une fonction en pur T-SQL qui permet de retourner les valeurs que l&rsquo;on trouve entre les virgules d&rsquo;une chaîne en contenant un plus ou moins grand nombre.</p>
<p>Celle-ci repose sur une table de nombres, ce qui permet d&rsquo;éviter de boucler par un WHILE, qui s&rsquo;avère être contre-performant, SQL n&rsquo;étant pas un langage itératif.<br />
<span id="more-11"></span><br />
Il existe pour réaliser le même but plusieurs alternatives, la première et la plus évidente étant de créer une fonction de type table qui exécute une boucle par un WHILE en parcourant la chaîne à la recherche des valeurs entre les virgules.</p>
<p>Cela fonctionne très bien pour un faible nombre de valeurs, mais lorsque celui-ci grossit, et les applications, web tout particulièrement, qui sont à l&rsquo;origine de ce &laquo;&nbsp;grossissement&nbsp;&raquo;, le permettent facilement.</p>
<p>Étant donné que SQL n&rsquo;est pas non plus un langage très performant sur les opérations de chaîne de caractère, on peut tout à fait se tourner vers une fonction d&rsquo;assembly CLR.<br />
L&rsquo;avantage d&rsquo;une telle fonction est qu&rsquo;elle s&rsquo;exécute très rapidement.</p>
<p>Malheureusement en ouvrant une base de données à l&rsquo;utilisation CLR, on ouvre aussi une porte aux problèmes de sécurité, et peut-être de performance de SQL Server, qui doit instancier la CLR &#8230;</p>
<p>Je devais donc écrire une fonction en pur T-SQL qui fasse la même chose, avec une vitesse comparable.<br />
Mes tests ont révélé que la fonction T-SQL est plus rapide qu&rsquo;une fonction CLR, peut-être parce qu&rsquo;un module CLR ne peut être exécuté que par un seul thread sous SQL Server &#8230;</p>
<p>Voici donc la table de nombres, très simple :</p>
<div class="codecolorer-container text 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 />17<br />18<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">CREATE TABLE dbo.nombre <br />
( <br />
&nbsp; nombre int NOT NULL CONSTRAINT PK_Nombres PRIMARY KEY CLUSTERED <br />
) <br />
GO <br />
&nbsp;<br />
SET NOCOUNT ON <br />
GO <br />
&nbsp;<br />
DECLARE &nbsp;@max int = 300000 <br />
&nbsp; , @i int = 1 <br />
&nbsp;<br />
WHILE @i &lt;= @max <br />
BEGIN <br />
&nbsp; INSERT dbo.nombre SELECT @i <br />
&nbsp; SET @i += 1 <br />
END <br />
GO</div></td></tr></tbody></table></div>
<p>La variable @max représente en fait le nombre de caractère maximal que la fonction va prendre en entrée<br />
J&rsquo;ai mis 300.000 car je suis sûr avec un tel nombre de couvrir tous les cas qui se présentent en production.<br />
Mais rien ne nous empêche de mettre plus &#8230; ou moins <img src="https://blog.developpez.com/elsuket/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /></p>
<p>Et voici la fonction :</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">------------------------------- <br />
-- Nicolas Souquet - 19/12/2011 <br />
------------------------------- <br />
CREATE FUNCTION dbo.SplitStringToInt <br />
&nbsp; ( <br />
&nbsp; &nbsp; @string AS varchar(max) <br />
&nbsp; &nbsp; , @sep AS char(1) <br />
&nbsp; ) <br />
RETURNS TABLE <br />
AS <br />
&nbsp; RETURN <br />
&nbsp; &nbsp; SELECT &nbsp;CAST(SUBSTRING(@string, nombre, CHARINDEX(@sep, @string + @sep, nombre) - nombre) AS int) AS element <br />
&nbsp; &nbsp; FROM &nbsp;dbo.nombre <br />
&nbsp; &nbsp; WHERE &nbsp;nombre &lt;= LEN(@string) + 1 <br />
&nbsp; &nbsp; AND &nbsp;SUBSTRING(@sep + @string, nombre, 1) = @sep <br />
GO</div></td></tr></tbody></table></div>
<p>Bon dépouillement !</p>
<p>ElSüket <img src="https://blog.developpez.com/elsuket/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /></p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>[Snippets] Générer le script de création d&#8217;une table existante</title>
		<link>https://blog.developpez.com/elsuket/p7134/snippets/generer_script_creation_table_existante</link>
		<comments>https://blog.developpez.com/elsuket/p7134/snippets/generer_script_creation_table_existante#comments</comments>
		<pubDate>Wed, 28 Jan 2009 21:02:18 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Voici une petite procédure stockée qui permet d&#8217;imprimer le code permettant de créer une table existante. Il ne permet pas encore de récupérer les commandes de création des indexes attachées à la table &#8230; 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130--------------------------------- -- Nicolas SOUQUET - 28/01/2009 &#8230; <a href="https://blog.developpez.com/elsuket/p7134/snippets/generer_script_creation_table_existante">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Voici une petite procédure stockée qui permet d&rsquo;imprimer le code permettant de créer une table existante.<br />
Il ne permet pas encore de récupérer les commandes de création des indexes attachées à la table &#8230;</p>
<p><span id="more-38"></span></p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 28/01/2009 - <br />
--------------------------------- <br />
CREATE PROCEDURE uspScripteTable <br />
&nbsp; @nomTable SYSNAME, <br />
&nbsp; @copie BIT = 0, <br />
&nbsp; @execute BIT = 0 <br />
AS <br />
BEGIN <br />
&nbsp; SET NOCOUNT ON <br />
&nbsp; <br />
&nbsp; DECLARE @script VARCHAR(MAX) <br />
&nbsp;<br />
&nbsp; SELECT &nbsp; &nbsp;@script = <br />
&nbsp; &nbsp; &nbsp; ISNULL(@script, '') + CHAR(9) + <br />
&nbsp; &nbsp; &nbsp; COLONNES.name + <br />
&nbsp; &nbsp; &nbsp; CASE <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN COMPCOL.object_id IS NOT NULL THEN '' <br />
&nbsp; &nbsp; &nbsp; &nbsp; ELSE ' ' + TYPES.name <br />
&nbsp; &nbsp; &nbsp; &nbsp; END + '' + <br />
&nbsp; &nbsp; &nbsp; CASE TYPES.name <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 'float' THEN '(' + CAST(COLONNES.precision AS VARCHAR) + ')' <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 'decimal' THEN '(' + CAST(COLONNES.precision AS VARCHAR) + ')' <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 'numeric' THEN '(' + CAST(COLONNES.precision AS VARCHAR) + ', ' + CAST(COLONNES.scale AS VARCHAR) + ')' <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 'binary' THEN '(' + CAST(COLONNES.max_length AS VARCHAR) + ')' <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 'varbinary' THEN '(' + CASE COLONNES.max_length WHEN -1 THEN 'max' ELSE CAST(COLONNES.max_length AS VARCHAR) END + ')' <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 'char' THEN '(' + CAST(COLONNES.max_length AS VARCHAR) + ')' <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 'varchar' THEN '(' + CASE COLONNES.max_length WHEN -1 THEN 'max' ELSE CAST(COLONNES.max_length AS VARCHAR) END + ')' <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 'nchar' THEN '(' + CAST(COLONNES.max_length AS VARCHAR) + ')' <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 'nvarchar' THEN '(' + &nbsp;CASE COLONNES.max_length WHEN -1 THEN 'max' ELSE CAST(COLONNES.max_length AS VARCHAR) END + ')' <br />
&nbsp; &nbsp; &nbsp; &nbsp; ELSE '' <br />
&nbsp; &nbsp; &nbsp; END + <br />
&nbsp; &nbsp; &nbsp; CASE COLONNES.is_identity <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 0 THEN '' <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 1 THEN ' IDENTITY(' + CAST(IDENT_SEED(TABLES.name) AS VARCHAR) + ', ' + CAST(IDENT_INCR(TABLES.name) AS VARCHAR) + ')' <br />
&nbsp; &nbsp; &nbsp; END + <br />
&nbsp; &nbsp; &nbsp; CASE COLONNES.is_nullable <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 0 THEN ' NOT NULL' <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 1 THEN ' NULL' <br />
&nbsp; &nbsp; &nbsp; END + <br />
&nbsp; &nbsp; &nbsp; CASE <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN FK_COLONNES.constraint_object_id IS NULL THEN '' <br />
&nbsp; &nbsp; &nbsp; &nbsp; ELSE ' CONSTRAINT FK_' + TABLES.name + <br />
&nbsp; &nbsp; &nbsp; &nbsp; CASE @copie <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN 1 THEN '_COPIE' <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ELSE '' <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; END <br />
&nbsp; &nbsp; &nbsp; &nbsp; + '_' + COLONNES.name + ' FOREIGN KEY (' + COLONNES.name + ') REFERENCES ' + SCHEMA_FK.name + '.' + TAB_FK.name + '(' + COL_FK.name + ')' <br />
&nbsp; &nbsp; &nbsp; END + <br />
&nbsp; &nbsp; &nbsp; CASE <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN DF.object_id IS NULL THEN '' <br />
&nbsp; &nbsp; &nbsp; &nbsp; ELSE ' CONSTRAINT DF_' + TABLES.name + <br />
&nbsp; &nbsp; &nbsp; &nbsp; CASE @copie <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN 1 THEN '_COPIE' <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ELSE '' <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; END + '_' + COLONNES.name + ' DEFAULT ' + REPLACE(REPLACE(DF.definition, '(', ''), ')', '') <br />
&nbsp; &nbsp; &nbsp; END + <br />
&nbsp; &nbsp; &nbsp; CASE <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN CHK.object_id IS NULL THEN '' <br />
&nbsp; &nbsp; &nbsp; &nbsp; ELSE ' CONSTRAINT CHK_' + TABLES.name + <br />
&nbsp; &nbsp; &nbsp; &nbsp; CASE @copie <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN 1 THEN '_COPIE' <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ELSE '' <br />
&nbsp; &nbsp; &nbsp; &nbsp; END + '_' + COLONNES.name + ' CHECK (' + REPLACE(REPLACE(REPLACE(REPLACE(CHK.definition, '(', ''), ')', ''), '[', ''), ']', '') + ')' <br />
&nbsp; &nbsp; &nbsp; END + <br />
&nbsp; &nbsp; &nbsp; CASE <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN COMPCOL.object_id IS NULL THEN '' <br />
&nbsp; &nbsp; &nbsp; &nbsp; ELSE ' AS (' + REPLACE(REPLACE(REPLACE(REPLACE(COMPCOL.definition, '(', ''), ')', ''), '[', ''), ']', '') + ')' <br />
&nbsp; &nbsp; &nbsp; &nbsp; END + <br />
&nbsp; &nbsp; &nbsp; CASE <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN ISNULL(INDEXES.is_primary_key, 0) = 0 THEN '' <br />
&nbsp; &nbsp; &nbsp; &nbsp; ELSE ' CONSTRAINT PK_' + TABLES.name + <br />
&nbsp; &nbsp; &nbsp; &nbsp; CASE @copie <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN 1 THEN '_COPIE' <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ELSE '' <br />
&nbsp; &nbsp; &nbsp; &nbsp; END + ' PRIMARY KEY' <br />
&nbsp; &nbsp; &nbsp; END + <br />
&nbsp; &nbsp; &nbsp; CASE <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN ISNULL(INDEXES.is_unique_constraint, 0) = 0 THEN '' <br />
&nbsp; &nbsp; &nbsp; &nbsp; ELSE ' CONSTRAINT UQ_' + TABLES.name + '_' + COLONNES.name + ' UNIQUE' <br />
&nbsp; &nbsp; &nbsp; END + ',' + CHAR(10) <br />
&nbsp; FROM &nbsp; &nbsp;sys.tables AS TABLES -- Tables <br />
&nbsp; INNER JOIN &nbsp;sys.columns AS COLONNES <br />
&nbsp; &nbsp; &nbsp; &nbsp; ON TABLES.object_id = COLONNES.object_id -- Colonnes <br />
&nbsp; INNER JOIN &nbsp;sys.types AS TYPES <br />
&nbsp; &nbsp; &nbsp; &nbsp; ON COLONNES.user_type_id = TYPES.user_type_id -- types <br />
&nbsp; LEFT JOIN &nbsp;sys.index_columns COL_INDEX -- indexes <br />
&nbsp; &nbsp; &nbsp; &nbsp; ON COL_INDEX.object_id = COLONNES.object_id <br />
&nbsp; &nbsp; &nbsp; &nbsp; AND COL_INDEX.column_id = COLONNES.column_id <br />
&nbsp; LEFT JOIN &nbsp;sys.indexes AS INDEXES <br />
&nbsp; &nbsp; &nbsp; &nbsp; ON INDEXES.object_id = COL_INDEX.object_id <br />
&nbsp; &nbsp; &nbsp; &nbsp; AND INDEXES.index_id = COL_INDEX.index_id <br />
&nbsp; LEFT JOIN &nbsp;sys.default_constraints AS DF -- contraintes de valeur par défaut <br />
&nbsp; &nbsp; &nbsp; &nbsp; ON DF.parent_object_id = TABLES.object_id <br />
&nbsp; &nbsp; &nbsp; &nbsp; AND DF.parent_column_id = COLONNES.column_id <br />
&nbsp; LEFT JOIN &nbsp;sys.check_constraints AS CHK -- contraintes CHECK <br />
&nbsp; &nbsp; &nbsp; &nbsp; ON CHK.parent_object_id = TABLES.object_id <br />
&nbsp; &nbsp; &nbsp; &nbsp; AND CHK.parent_column_id = COLONNES.column_id <br />
&nbsp; LEFT JOIN &nbsp;sys.computed_columns AS COMPCOL -- colonnes calculées <br />
&nbsp; &nbsp; &nbsp; &nbsp; ON COMPCOL.column_id = COLONNES.column_id <br />
&nbsp; &nbsp; &nbsp; &nbsp; AND COMPCOL.object_id = TABLES.object_id <br />
&nbsp; LEFT JOIN &nbsp;sys.foreign_key_columns AS FK_COLONNES <br />
&nbsp; &nbsp; &nbsp; &nbsp; ON FK_COLONNES.parent_object_id = TABLES.object_id <br />
&nbsp; &nbsp; &nbsp; &nbsp; AND FK_COLONNES.parent_column_id = COLONNES.column_id <br />
&nbsp; LEFT JOIN &nbsp;sys.columns AS COL_FK <br />
&nbsp; &nbsp; &nbsp; &nbsp; ON FK_COLONNES.referenced_object_id = COL_FK.object_id <br />
&nbsp; &nbsp; &nbsp; &nbsp; AND FK_COLONNES.referenced_column_id = COL_FK.column_id <br />
&nbsp; LEFT JOIN &nbsp;sys.tables AS TAB_FK <br />
&nbsp; &nbsp; &nbsp; &nbsp; ON COL_FK.object_id = TAB_FK.object_id <br />
&nbsp; LEFT JOIN &nbsp;sys.schemas AS SCHEMA_FK <br />
&nbsp; &nbsp; &nbsp; &nbsp; ON SCHEMA_FK.schema_id = TAB_FK.schema_id <br />
&nbsp; WHERE &nbsp; &nbsp;TABLES.name = @nomTable <br />
&nbsp;<br />
&nbsp; SELECT &nbsp;@script = 'CREATE TABLE ' + S.name + '.' + <br />
&nbsp; &nbsp; CASE @copie <br />
&nbsp; &nbsp; &nbsp; WHEN 1 THEN T.name + '_COPIE' <br />
&nbsp; &nbsp; &nbsp; ELSE T.name <br />
&nbsp; &nbsp; END + CHAR(10) + <br />
&nbsp; &nbsp; '(' + CHAR(10) + SUBSTRING(@script, 1, LEN(@script) - 2) + CHAR(10) + ')' <br />
&nbsp; FROM &nbsp;sys.schemas AS S <br />
&nbsp; JOIN &nbsp;sys.tables AS T ON S.schema_id = T.schema_id <br />
&nbsp; WHERE &nbsp;T.name = @nomTable <br />
&nbsp;<br />
&nbsp; PRINT @script <br />
&nbsp; <br />
&nbsp; IF @execute = 1 <br />
&nbsp; BEGIN <br />
&nbsp; &nbsp; EXEC (@script) <br />
&nbsp; END <br />
END</div></td></tr></tbody></table></div>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>En SQL, NULL n&#8217;est pas une valeur !</title>
		<link>https://blog.developpez.com/elsuket/p9971/t-sql/en_sql_null_n_est_pas_une_valeur</link>
		<comments>https://blog.developpez.com/elsuket/p9971/t-sql/en_sql_null_n_est_pas_une_valeur#comments</comments>
		<pubDate>Fri, 13 May 2011 12:54:58 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[SQL général]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Je vois encore énormément d&#8217;erreurs à ce sujet, chez les développeurs avec qui je travaille et j&#8217;ai travaillé, mais aussi sur le forum. Voici donc la démonstration que NULL en SQL n&#8217;est pas une valeur &#8230; C&#8217;est très simple : &#8230; <a href="https://blog.developpez.com/elsuket/p9971/t-sql/en_sql_null_n_est_pas_une_valeur">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Je vois encore énormément d&rsquo;erreurs à ce sujet, chez les développeurs avec qui je travaille et j&rsquo;ai travaillé, mais aussi sur le <a href="http://www.developpez.net/forums/f49/bases-donnees/ms-sql-server/">forum</a>.</p>
<p>Voici donc la démonstration que NULL en SQL n&rsquo;est pas une valeur &#8230;</p>
<p><span id="more-155"></span><br />
C&rsquo;est très simple : exécutez la requête suivante :</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">IF NULL = NULL <br />
&nbsp; PRINT 'OK' <br />
ELSE PRINT 'KO'</div></td></tr></tbody></table></div>
<p>Vous pensez que l&rsquo;on va obtenir <em>OK</em> &#8230; Donc vous pensez que NULL est une valeur et c&rsquo;est faux : on obtiendra <em>KO</em> !<br />
Vous pouvez faire l&rsquo;expérience avec n&rsquo;importe quel autre opérateur arithmétique (<code class="codecolorer text default"><span class="text">&gt;, &gt;=, &lt;, &lt;=, &lt;&gt;, !=</span></code>) : vous obtiendrez toujours <em>KO</em></p>
<p>Ceci prouve que NULL n&rsquo;est pas une valeur : en SQL, <strong>c&rsquo;est un marqueur qui signifie l&rsquo;absence de valeur</strong>.<br />
C&rsquo;est d&rsquo;ailleurs la raison pour laquelle en SQL, on écrit IS NULL dans un prédicat (WHERE ou JOIN), et non pas = NULL.<br />
En revanche, dans un langage comme Java ou C#, null == null est effectivement vrai.</p>
<p>En effet, beaucoup de langages évaluent les expressions logiques suivant deux valeurs : <em>true</em> ou <em>false</em>.<br />
Mais en SQL, un prédicat et évalué comme étant TRUE, FALSE <strong>ou UNKNOWN</strong>.</p>
<p>De la même façon, toute autre opération (NULL [operateur_arithmétique] [valeur]), peu importe le type de [valeur], retourne toujours NULL :</p>
<p>&#8211; NULL + 4<br />
&#8211; NULL + &lsquo;uneChaine&rsquo;<br />
&#8211; REPLACE(uneChaineNULL, &lsquo;qqch&rsquo;, &lsquo;_qqch&rsquo;)</p>
<p>Enfin, NULL est traité différemment par les différents opérateurs que l&rsquo;implémentation SQL Server de SQL propose :</p>
<p>&#8211; Les filtres de requête (ON d&rsquo;une jointure, WHERE (et AND qui suivent) et HAVING) évalueront NULL comme ne vérifiant pas le prédicat<br />
&#8211; Une contrainte CHECK évaluera NULL comme vérifiant le prédicat<br />
&#8211; Une contrainte UNIQUE prend NULL comme une valeur, ce qui est totalement faux<br />
&#8211; Une opération avec les opérateurs d&rsquo;ensembles UNION, EXCEPT et INTERSECT traite NULL comme une valeur, ce qui est également faux<br />
&#8211; GROUP BY groupe tous les NULL dans un groupe distinct<br />
&#8211; ORDER BY trie le NULL ensemble<br />
&#8211; La fonction COUNT() tient compte de NULL<br />
&#8211; Les <a href="http://msdn.microsoft.com/fr-fr/library/ms173454.aspx">autres fonctions d’agrégation</a> n&rsquo;en tiennent pas compte</p>
<p>Attention à NULL <img src="https://blog.developpez.com/elsuket/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley" /></p>
<p>ElSüket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Trouver le premier et le dernier jour de la semaine, du mois, du trimestre, et autre calculs</title>
		<link>https://blog.developpez.com/elsuket/p9840/snippets/trouver_le_premier_et_le_dernier_jour_de</link>
		<comments>https://blog.developpez.com/elsuket/p9840/snippets/trouver_le_premier_et_le_dernier_jour_de#comments</comments>
		<pubDate>Tue, 29 Mar 2011 15:04:38 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Utilitaires]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Beaucoup de participants au forum SQL Server se compliquent la vie avec des calculs bizarres, à trouver le premier ou dernier jour d&#8217;un index temporel, comme celui d&#8217;une semaine, d&#8217;un mois, ou d&#8217;un trimestre. Ces calculs se font souvent à &#8230; <a href="https://blog.developpez.com/elsuket/p9840/snippets/trouver_le_premier_et_le_dernier_jour_de">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Beaucoup de participants au forum SQL Server se compliquent la vie avec des calculs bizarres, à trouver le premier ou dernier jour d&rsquo;un index temporel, comme celui d&rsquo;une semaine, d&rsquo;un mois, ou d&rsquo;un trimestre.</p>
<p>Ces calculs se font souvent à base d&rsquo;extractions de sous-chaînes après avoir converti une date en varchar(n), et on aboutit donc à des choses abominables.</p>
<p>Voici donc une série de calculs de valeurs, uniquement à base de fonctions de date &#8230;<br />
Oui, les dates se manipulent avec des <a href="http://msdn.microsoft.com/fr-fr/library/ms186724.aspx#DateandTimeFunctions">fonctions de date</a>, et non pas avec des <a href="http://msdn.microsoft.com/fr-fr/library/ms181984.aspx">fonctions de chaîne</a> !</p>
<p>J&rsquo;en profite au passage pour rappeler mon billet sur le <a href="http://blog.developpez.com/elsuket/p8233/sql-general/du-choix-des-types-de-donnees/">choix des types de données</a> &#8230;</p>
<p><span id="more-65"></span></p>
<p>La majorité des valeurs calculées ci-dessous repose sur la différence en semaines, mois, trimestres, &#8230;, entre la date courante et le 1er Janvier 1900.<br />
En effet, on peut introduire directement cette date par un zéro dans les fonctions de date, que SQL Server caste automatiquement, et qui donne cette date.</p>
<div class="codecolorer-container sql 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 /></div></td><td><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">------------------------------- </span><br />
<span style="color: #808080; font-style: italic;">-- Nicolas Souquet - 29/03/2011 </span><br />
<span style="color: #808080; font-style: italic;">------------------------------- </span><br />
<span style="color: #993333; font-weight: bold;">SELECT</span> &nbsp;<span style="color: #808080; font-style: italic;">--*** Semaine </span><br />
&nbsp; DATEADD<span style="color: #66cc66;">&#40;</span>week<span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span>week<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> premier_jour_de_la_semaine_precedente <br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span>week<span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span>week<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> premier_jour_de_la_semaine_courante <br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span>week<span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span>week<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> premier_jour_de_la_semaine_prochaine <br />
&nbsp; <span style="color: #808080; font-style: italic;">--- </span><br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span>week<span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span>week<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> dernier_jour_de_la_semaine_precedente <br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span>week<span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span>week<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> dernier_jour_de_la_semaine_courante <br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span>week<span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span>week<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> dernier_jour_de_la_semaine_prochaine <br />
&nbsp; <span style="color: #808080; font-style: italic;">--*** Mois </span><br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MONTH</span><span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MONTH</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> premier_jour_du_mois_precedent <br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MONTH</span><span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MONTH</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> premier_jour_du_mois_courant &nbsp;<br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MONTH</span><span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MONTH</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> premier_jour_du_mois_prochain <br />
&nbsp; <span style="color: #808080; font-style: italic;">--- </span><br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MONTH</span><span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MONTH</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> dernier_jour_du_mois_precedent <br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MONTH</span><span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MONTH</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> dernier_jour_du_mois_courant &nbsp;<br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MONTH</span><span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MONTH</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> dernier_jour_du_mois_prochain <br />
&nbsp; <span style="color: #808080; font-style: italic;">--*** Trimestre </span><br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span>quarter<span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span>quarter<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> premier_jour_du_trimestre_precedent <br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span>quarter<span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span>quarter<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> permier_jour_du_trimestre_courant <br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span>quarter<span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span>quarter<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> premier_jour_du_trimestre_prochain <br />
&nbsp; <span style="color: #808080; font-style: italic;">--- </span><br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span>quarter<span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span>quarter<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> dernier_jour_du_trimestre_precedent <br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span>quarter<span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span>quarter<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> dernier_jour_du_trimestre_courant <br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span>quarter<span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span>quarter<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> dernier_jour_du_trimestre_prochain <br />
&nbsp; <span style="color: #808080; font-style: italic;">--*** Semestre </span><br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MONTH</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>DATEDIFF<span style="color: #66cc66;">&#40;</span>quarter<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> premier_jour_du_semestre_precedent <br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MONTH</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span>DATEDIFF<span style="color: #66cc66;">&#40;</span>quarter<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> premier_jour_du_semestre_courant <br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MONTH</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>DATEDIFF<span style="color: #66cc66;">&#40;</span>quarter<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> premier_jour_du_semestre_prochain <br />
&nbsp; <span style="color: #808080; font-style: italic;">--- </span><br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MONTH</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span>DATEDIFF<span style="color: #66cc66;">&#40;</span>quarter<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> dernier_jour_du_semestre_precedent <br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MONTH</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>DATEDIFF<span style="color: #66cc66;">&#40;</span>quarter<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> dernier_jour_du_semestre_courant <br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">MONTH</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>DATEDIFF<span style="color: #66cc66;">&#40;</span>quarter<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">12</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> dernier_jour_du_semestre_prochain <br />
&nbsp; <span style="color: #808080; font-style: italic;">--*** Annee </span><br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">YEAR</span> <span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">YEAR</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> premier_jour_annee_precedente <br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">YEAR</span> <span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">YEAR</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> premier_jour_annee_courante <br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">YEAR</span> <span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">YEAR</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> premier_jour_annee_suivante <br />
&nbsp; <span style="color: #808080; font-style: italic;">--- </span><br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">YEAR</span> <span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">YEAR</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> dernier_jour_annee_precedente <br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">YEAR</span> <span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">YEAR</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> dernier_jour_annee_courante <br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">YEAR</span> <span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">YEAR</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> dernier_jour_annee_suivante <br />
&nbsp; <span style="color: #808080; font-style: italic;">--*** Siècle </span><br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">YEAR</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #993333; font-weight: bold;">YEAR</span><span style="color: #66cc66;">&#40;</span>GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> % <span style="color: #cc66cc;">100</span><span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">YEAR</span> <span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">YEAR</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> premier_jour_du_siecle_courant <br />
&nbsp; <span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">YEAR</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #993333; font-weight: bold;">YEAR</span><span style="color: #66cc66;">&#40;</span>GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> % <span style="color: #cc66cc;">100</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">100</span><span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> DATEADD<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">YEAR</span> <span style="color: #66cc66;">,</span> DATEDIFF<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">YEAR</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> GETDATE<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> dernier_jour_du_siecle_courant</div></td></tr></tbody></table></div>
<p>J&rsquo;ai eu un peu la flemme sur le siècle, et j&rsquo;ai trouvé comme prétexte pour m&rsquo;arrêter que cela ne doit pas présenter grand intérêt <img src="https://blog.developpez.com/elsuket/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley" /></p>
<p>Bon calculs sur les dates !</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>11</slash:comments>
		</item>
		<item>
		<title>Voir l&#8217;historique d&#8217;exécution des jobs par une requête</title>
		<link>https://blog.developpez.com/elsuket/p7895/snippets/voir_l_historique_d_execution_des_jobs_p</link>
		<comments>https://blog.developpez.com/elsuket/p7895/snippets/voir_l_historique_d_execution_des_jobs_p#comments</comments>
		<pubDate>Fri, 24 Jul 2009 06:00:00 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Agent SQL Server]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Si la visionneuse du journal est pratique pour voir l&#8217;historique des exécutions d&#8217;un job, comment peut-on voir s&#8217;il existe des jobs dont l&#8217;exécution a échoué (ou réussi) pour pouvoir l&#8217;encapsuler dans une application ou un rapport SSRS par exemple ? &#8230; <a href="https://blog.developpez.com/elsuket/p7895/snippets/voir_l_historique_d_execution_des_jobs_p">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Si la visionneuse du journal est pratique pour voir l&rsquo;historique des exécutions d&rsquo;un job, comment peut-on voir s&rsquo;il existe des jobs dont l&rsquo;exécution a échoué (ou réussi) pour pouvoir l&rsquo;encapsuler dans une application ou un rapport SSRS par exemple ?</p>
<p><span id="more-14"></span></p>
<p>Les tables système de la base de données MSDB nous permettent de spécifier une requête nous permettant de retrouver tous les informations dont nous avons besoin à propos des jobs :</p>
<p>&#8211; msdb.dbo.sysjobs stocke la liste de tous les jobs présents sur l&rsquo;instance, qu&rsquo;ils soient activés ou non<br />
&#8211; msdb.dbo.sysjobhistory stocke la liste de toutes les exécutions des jobs et de leurs étapes<br />
&#8211; msdb.dbo.sysjobsteps stocke la description de toutes les étapes de tous les jobs de l&rsquo;instance</p>
<p>Voici donc, par exemple, comment retrouver tous les échecs pour le job nommé &lsquo;TEST&rsquo;, avec leur date :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 24/07/2009 - <br />
--------------------------------- <br />
SELECT nomJob, <br />
&nbsp; &nbsp; CAST(STUFF(STUFF(runDateTime, 12, 0, ':'), 15, 0, ':') AS DATETIME) AS DateEchecExecJob <br />
FROM <br />
( <br />
&nbsp; SELECT J.name AS nomJob, <br />
&nbsp; &nbsp; &nbsp; CAST(H.run_date AS CHAR(8)) + ' ' + <br />
&nbsp; &nbsp; &nbsp; CASE LEN(CAST(H.run_time AS VARCHAR(6))) <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 5 THEN '0' + CAST(H.run_time AS CHAR(5)) <br />
&nbsp; &nbsp; &nbsp; &nbsp; ELSE CAST(H.run_time AS CHAR(6)) <br />
&nbsp; &nbsp; &nbsp; END AS runDateTime <br />
&nbsp; FROM msdb.dbo.sysjobs J <br />
&nbsp; JOIN msdb.dbo.sysjobhistory H <br />
&nbsp; &nbsp; ON J.job_id = H.job_id <br />
&nbsp; WHERE J.name = 'TEST' <br />
&nbsp; AND J.enabled = 1 -- Le job est activé <br />
&nbsp; AND H.run_status = 0 -- 0 : Echec | 1 : Réussite <br />
&nbsp; AND H.step_id = 0 -- quelle que soit l'étape du job <br />
) AS JOB_FAILED_EXEC (nomJob, runDateTime)</div></td></tr></tbody></table></div>
<p>Et si l&rsquo;on veut être relativement complet :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 24/07/2009 - <br />
--------------------------------- <br />
SELECT &nbsp;nomJob <br />
&nbsp; , nomEtapeJob <br />
&nbsp; , numeroEtapeJob <br />
&nbsp; , msgEtape <br />
&nbsp; , CAST(STUFF(STUFF(dateExecutionEtape, 12, 0, ':'), 15, 0, ':') AS DATETIME) AS dateExecutionEtape <br />
&nbsp; , dureeExecutionEtape <br />
&nbsp; , statutExecutionEtape <br />
FROM &nbsp;<br />
( &nbsp;<br />
&nbsp; SELECT &nbsp; &nbsp;J.name AS nomJob <br />
&nbsp; &nbsp; &nbsp; , S.step_name AS nomEtapeJob <br />
&nbsp; &nbsp; &nbsp; , S.step_id AS numeroEtapeJob <br />
&nbsp; &nbsp; &nbsp; , ISNULL('Erreur : ' + M.description, H.message) AS msgEtape <br />
&nbsp; &nbsp; &nbsp; , CAST(H.run_date AS CHAR(8)) + ' ' + &nbsp;<br />
&nbsp; &nbsp; &nbsp; CASE LEN(CAST(H.run_time AS VARCHAR(6))) <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 1 THEN '00000' + CAST(H.run_time AS CHAR(1)) &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 2 THEN '0000' + CAST(H.run_time AS CHAR(2)) &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 3 THEN '000' + CAST(H.run_time AS CHAR(3)) &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 4 THEN '00' + CAST(H.run_time AS CHAR(4)) &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 5 THEN '0' + CAST(H.run_time AS CHAR(5)) &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; ELSE CAST(H.run_time AS CHAR(6)) &nbsp;<br />
&nbsp; &nbsp; &nbsp; END AS dateExecutionEtape <br />
&nbsp; &nbsp; &nbsp; , CASE LEN(CAST(H.run_duration AS VARCHAR(10))) &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 1 THEN '00:00:0' + CAST(H.run_duration AS CHAR(1)) &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 2 THEN '00:00:' + CAST(H.run_duration AS CHAR(2)) &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 3 THEN '00:0' + STUFF(CAST(H.run_duration AS CHAR(3)), 2, 0, ':') &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 4 THEN '00:' + STUFF(CAST(H.run_duration AS CHAR(4)), 3, 0, ':') &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 5 THEN '0' + STUFF(STUFF(CAST(H.run_duration AS CHAR(5)), 2, 0, ':'), 5, 0, ':') &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 6 THEN STUFF(STUFF(CAST(H.run_duration AS CHAR(6)), 3, 0, ':'), 6, 0, ':') &nbsp;<br />
&nbsp; &nbsp; &nbsp; END AS dureeExecutionEtape <br />
&nbsp; &nbsp; &nbsp; , CASE H.run_status &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 0 THEN 'FAILED' <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 1 THEN 'SUCCESS' <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 2 THEN 'RETRY' <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 3 THEN 'CANCELED' <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN 4 THEN 'RUNNING' -- Pas fiable <br />
&nbsp; &nbsp; &nbsp; END AS statutExecutionEtape <br />
&nbsp; FROM &nbsp; &nbsp;msdb.dbo.sysjobs AS J &nbsp; <br />
&nbsp; INNER JOIN &nbsp;msdb.dbo.sysjobsteps AS S &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; ON J.job_id = S.job_id &nbsp;<br />
&nbsp; INNER JOIN &nbsp;msdb.dbo.sysjobhistory AS H &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; ON S.job_id = H.job_id &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; AND S.step_id = H.step_id &nbsp;<br />
&nbsp; LEFT JOIN &nbsp;sys.sysmessages AS M &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; ON H.sql_message_id = M.error &nbsp;<br />
) AS JOB_CHARACTERISTICS (nomJob, nomEtapeJob, numeroEtapeJob, msgEtape, dateExecutionEtape, dureeExecutionEtape, statutExecutionEtape)</div></td></tr></tbody></table></div>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Quelle différences y-a-t-il entre TRUNCATE et DELETE ?</title>
		<link>https://blog.developpez.com/elsuket/p8929/moteur-de-base-de-donnees-sql-server/quelle_differences_y_a_t_il_entre_trunca</link>
		<comments>https://blog.developpez.com/elsuket/p8929/moteur-de-base-de-donnees-sql-server/quelle_differences_y_a_t_il_entre_trunca#comments</comments>
		<pubDate>Fri, 21 May 2010 06:03:41 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Moteur de base de données SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[TRUNCATE et DELETE peuvent avoir le même effet sur une table si le DELETE n&#8217;a pas de clause WHERE. Mais est-ce bien la même chose qui se passe au niveau des données ? En réalité, ces deux instructions ont bien &#8230; <a href="https://blog.developpez.com/elsuket/p8929/moteur-de-base-de-donnees-sql-server/quelle_differences_y_a_t_il_entre_trunca">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>TRUNCATE et DELETE peuvent avoir le même effet sur une table si le DELETE n&rsquo;a pas de clause WHERE.<br />
Mais est-ce bien la même chose qui se passe au niveau des données ?</p>
<p><span id="more-9"></span></p>
<p>En réalité, ces deux instructions ont bien des éléments qui les différencient :</p>
<p>=> TRUNCATE est une instruction DDL, alors que DELETE est une instruction DML.<br />
Conséquence directe : TRUNCATE ne peut pas avoir de clause WHERE.</p>
<p>=> TRUNCATE ne peut pas être exécuté :
<ol>
<li>sur une table disposant de contraintes d&rsquo;intégrité (clé primaire référencée par au moins une table,  et clés étrangères)</li>
<li>sur une table qui est répliquée</li>
<li>dans une base de données répliquée par envoi de journaux</li>
</ol>
<p>DELETE peut l&rsquo;être si les contraintes d&rsquo;intégrité restent respectées.</p>
<p>=> TRUNCATE ne déclenche pas un trigger sur DELETE, ce qui est le cas pour DELETE.<br />
Donc l&rsquo;utilisation de TRUNCATE peut causer des problèmes de consistance des données, car si l&rsquo;on a supprimé les contraintes d&rsquo;intégrité pour pouvoir exécuter un TRUNCATE, le traitement du trigger sur DELETE ne sera pas exécuté non plus.</p>
<p>=> TRUNCATE enregistre dans le fichier du journal des transactions les pages de données désallouées, et supprime les pointeurs des index.<br />
C&rsquo;est ce qui fait que TRUNCATE est plus rapide à s&rsquo;exécuter que DELETE : TRUNCATE utilise très peu de verrous par rapport à DELETE, qui nécessite dont plus de ressources disque.<br />
En effet, DELETE enregistre dans le fichier du journal des transactions toutes les données de chaque ligne affectée par l&rsquo;instruction.<br />
En realité donc, TRUNCATE n&rsquo;efface pas les données contenues dans les pages, mais marque celles-ci comme réutilisables.<br />
DELETE en revanche supprime physiquement les données.</p>
<p>=> En conséquence, les données supprimées par un DELETE sont restaurables; les données &laquo;&nbsp;supprimées&nbsp;&raquo; par un TRUNCATE ne le sont pas.</p>
<p>=> TRUNCATE réinitialise la valeur d&rsquo;une colonne de type entier avec la propriété d&rsquo;auto-incrémentation (IDENTITY). DELETE laisse cette valeur inchangée.</p>
<p>Et bonne suppression de données <img src="https://blog.developpez.com/elsuket/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley" /></p>
<p>ElSüket.</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Lister quelques propriétés des logins</title>
		<link>https://blog.developpez.com/elsuket/p8617/t-sql/lister_quelques_proprietes_des_logins</link>
		<comments>https://blog.developpez.com/elsuket/p8617/t-sql/lister_quelques_proprietes_des_logins#comments</comments>
		<pubDate>Thu, 11 Feb 2010 03:01:37 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Sécurité]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Voici une petite requête qui retourne quelques propriétés pour tous les logins d&#8217;un instance SQL Server, avec la prochaine date d&#8217;expiration des mots de passe &#8230; 123456789101112131415161718192021222324252627------------------------------- -- 22/01/2010 - Nicolas SOUQUET ------------------------------- WITH &#160; LOGIN_PROPERTIES AS &#160; ( &#160; &#8230; <a href="https://blog.developpez.com/elsuket/p8617/t-sql/lister_quelques_proprietes_des_logins">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Voici une petite requête qui retourne quelques propriétés pour tous les logins d&rsquo;un instance SQL Server, avec la prochaine date d&rsquo;expiration des mots de passe &#8230;<br />
<span id="more-147"></span></p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">------------------------------- <br />
-- 22/01/2010 - Nicolas SOUQUET <br />
------------------------------- <br />
WITH <br />
&nbsp; LOGIN_PROPERTIES AS <br />
&nbsp; ( <br />
&nbsp; &nbsp; SELECT SP.name <br />
&nbsp; &nbsp; &nbsp; , SP.type_desc <br />
&nbsp; &nbsp; &nbsp; , SP.is_disabled <br />
&nbsp; &nbsp; &nbsp; , SP.create_date <br />
&nbsp; &nbsp; &nbsp; , SP.modify_date <br />
&nbsp; &nbsp; &nbsp; , SP.default_database_name AS default_db <br />
&nbsp; &nbsp; &nbsp; , SP.default_language_name AS default_lang <br />
&nbsp; &nbsp; &nbsp; , CAST(LOGINPROPERTY(SP.name, 'PasswordLAstSetTime') AS DATETIME) AS password_last_set_time <br />
&nbsp; &nbsp; &nbsp; , CAST(LOGINPROPERTY(SP.name, 'DaysUntilExpiration') AS DATETIME) AS days_until_expiration <br />
&nbsp; &nbsp; FROM sys.server_principals AS SP <br />
&nbsp; ) <br />
SELECT name <br />
&nbsp; , type_desc <br />
&nbsp; , is_disabled <br />
&nbsp; , create_date <br />
&nbsp; , modify_date <br />
&nbsp; , default_db <br />
&nbsp; , default_lang <br />
&nbsp; , password_last_set_time <br />
&nbsp; , DATEADD(day, -DATEDIFF(day, password_last_set_time, GETDATE()), GETDATE()) + days_until_expiration AS password_expiry_date <br />
FROM LOGIN_PROPERTIES</div></td></tr></tbody></table></div>
<p>Bonne sécurisation !</p>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Vérifier la validité d&#8217;une adresse e-mail</title>
		<link>https://blog.developpez.com/elsuket/p8303/snippets/verifier_la_validite_d_une_adresse_e_mai</link>
		<comments>https://blog.developpez.com/elsuket/p8303/snippets/verifier_la_validite_d_une_adresse_e_mai#comments</comments>
		<pubDate>Mon, 09 Nov 2009 22:13:45 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Voici un petit snippet pour vérifier la validité d&#8217;une adresse mail, histoire d&#8217;assurer un minimum d&#8217;intégrité à l&#8217;aide d&#8217;une contrainte de domaine vérifiée par cette fonction : 123456789101112131415161718192021222324------------------------------- -- Nicolas SOUQUET - 09/11/2009 ------------------------------- CREATE FUNCTION Fn_CheckMail &#160; (@_mail VARCHAR(320)) &#8230; <a href="https://blog.developpez.com/elsuket/p8303/snippets/verifier_la_validite_d_une_adresse_e_mai">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Voici un petit snippet pour vérifier la validité d&rsquo;une adresse mail, histoire d&rsquo;assurer un minimum d&rsquo;intégrité à l&rsquo;aide d&rsquo;une contrainte de domaine vérifiée par cette fonction :<br />
<span id="more-64"></span></p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">------------------------------- <br />
-- Nicolas SOUQUET - 09/11/2009 <br />
------------------------------- <br />
CREATE FUNCTION Fn_CheckMail <br />
&nbsp; (@_mail VARCHAR(320)) <br />
&nbsp; RETURNS BIT <br />
&nbsp; WITH SCHEMABINDING <br />
AS <br />
BEGIN <br />
&nbsp; RETURN CASE <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN CHARINDEX(' ', LTRIM(RTRIM(@_mail))) = 0 -- Pas d'espaces <br />
&nbsp; &nbsp; &nbsp; &nbsp; AND LEFT(LTRIM(@_mail), 1) &nbsp;'@' &nbsp;-- '@' ne peut pas être le premier caractère de l'adresse <br />
&nbsp; &nbsp; &nbsp; &nbsp; AND RIGHT(RTRIM(@_mail), 1) &nbsp;'.' -- '.' ne peut pas être le dernier caractère de l'adresse <br />
&nbsp; &nbsp; &nbsp; &nbsp; AND LEN(LTRIM(RTRIM(@_mail))) - LEN(REPLACE(LTRIM(RTRIM(@_mail)), '@', '')) = 1 -- Un seul '@' <br />
&nbsp; &nbsp; &nbsp; &nbsp; AND CHARINDEX('.', @_mail, CHARINDEX('@', @_mail)) - CHARINDEX('@', @_mail) &amp;gt; 1 -- Il doit y avoir un '.' après l'@ &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; AND CHARINDEX('.', REVERSE(LTRIM(RTRIM(@_mail)))) &amp;gt;= 3 -- le nom de domaine doit se terminer avec au moins 2 caractères <br />
&nbsp; &nbsp; &nbsp; &nbsp; AND CHARINDEX('.@', @_mail) = 0 -- pas de '.@' <br />
&nbsp; &nbsp; &nbsp; &nbsp; AND CHARINDEX('..', @_mail) = 0 -- pas de '..' <br />
&nbsp; &nbsp; &nbsp; &nbsp; AND LEN(LEFT(@_mail, CHARINDEX('@', @_mail) - 1)) &amp;lt;= 64 -- la partie locale ne peut excéder 64 caractères <br />
&nbsp; &nbsp; &nbsp; &nbsp; AND LEN(RIGHT(@_mail, LEN(@_mail) - CHARINDEX(&amp;#039;@&amp;#039;, @_mail))) &amp;lt;= 250 -- la partie nom de domaine ne peut excéder 250 caractères <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; THEN CAST(1 AS BIT) <br />
&nbsp; &nbsp; &nbsp; &nbsp; ELSE CAST(0 AS BIT) <br />
&nbsp; &nbsp; &nbsp; END <br />
END</div></td></tr></tbody></table></div>
<p>Et voici comment ajouter la contrainte à la table-exemple suivante :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">CREATE TABLE TbClient <br />
( <br />
&nbsp; IDClient INT NOT NULL CONSTRAINT PK_TbClient PRIMARY KEY, <br />
&nbsp; NumeroClient INT NOT NULL CONSTRAINT UQ_TbClient_NumeroClient UNIQUE, <br />
&nbsp; NomClient VARCHAR(32) NOT NULL, <br />
&nbsp; PrenomClient VARCHAR(32) NOT NULL, <br />
&nbsp; mailClient VARCHAR(320) NOT NULL <br />
) <br />
&nbsp;<br />
ALTER TABLE dbo.TbClient <br />
ADD CONSTRAINT CHK_TbClient_mailClient CHECK (dbo.Fn_CheckMail(mailClient) = 1)</div></td></tr></tbody></table></div>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Utiliser le type TABLE avec SQL Server 2008</title>
		<link>https://blog.developpez.com/elsuket/p8272/t-sql/utiliser_le_type_table_avec_sql_server_2008</link>
		<comments>https://blog.developpez.com/elsuket/p8272/t-sql/utiliser_le_type_table_avec_sql_server_2008#comments</comments>
		<pubDate>Tue, 03 Nov 2009 17:15:50 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Il est parfois nécessaire de créer un curseur pour exécuter une procédure stockée sur chacune des lignes du curseur, en passant en paramètre les valeurs des colonnes de la requête qui spécifie le curseur. SQL Server 2008 a introduit le &#8230; <a href="https://blog.developpez.com/elsuket/p8272/t-sql/utiliser_le_type_table_avec_sql_server_2008">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Il est parfois nécessaire de créer un curseur pour exécuter une procédure stockée sur chacune des lignes du curseur, en passant en paramètre les valeurs des colonnes de la requête qui spécifie le curseur.<br />
SQL Server 2008 a introduit le type TABLE, qui permet de spécifier par un type des variables de type TABLE.<br />
Cette solution est une alternative moins pire que le <a href="http://blog.developpez.com/elsuket/p7445/moteur-de-base-de-donnees-sql-server/comparaison-des-temps-d-execution-entre/">curseur</a> : voyons comment l&rsquo;utiliser (avec modération !)<br />
<span id="more-8"></span><br />
=> <strong>Créons tout d&rsquo;abord un type qui structurera les variables de type TABLE que nous allons utiliser :<br />
</strong></p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">CREATE TYPE dbo.TyTbNombres AS TABLE <br />
( <br />
&nbsp; unNombre TINYINT <br />
)</div></td></tr></tbody></table></div>
<p>=> <strong>Créons une procédure stockée acceptant en entrée un paramètre de type TABLE</strong></p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">CREATE PROCEDURE PsTest <br />
&nbsp; @uneTable dbo.TyTbNombres READONLY <br />
AS <br />
BEGIN <br />
&nbsp; SELECT unNombre <br />
&nbsp; FROM @uneTable <br />
END</div></td></tr></tbody></table></div>
<p>=> <strong>Dans le code de la procédure stockée appelante :</strong></p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">DECLARE @maTable dbo.TyTbNombres <br />
INSERT INTO @maTable VALUES (1), (2), (3) <br />
&nbsp;<br />
EXEC dbo.PsTest @maTable</div></td></tr></tbody></table></div>
<p>Il est ensuite simple, dans la procédure stockée appelée, en l&rsquo;occurence <em>PsTest</em>, de spécifier une jointure sur la variable de type table &#8230;</p>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lister tous les types de données utilisés pour les colonnes des tables d&#8217;une base de données sous SQL Server 2005 et 2008</title>
		<link>https://blog.developpez.com/elsuket/p8259/snippets/lister_tous_les_types_de_donnees_utilise_2008</link>
		<comments>https://blog.developpez.com/elsuket/p8259/snippets/lister_tous_les_types_de_donnees_utilise_2008#comments</comments>
		<pubDate>Fri, 30 Oct 2009 17:45:50 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Voici une petite requête qui permet de lister tous les types de données utilisés pour les colonnes des tables d&#8217;une base de données &#8230; Si on ne souhaite connaître que les types de données : 12345678910------------------------------- -- Nicolas SOUQUET - &#8230; <a href="https://blog.developpez.com/elsuket/p8259/snippets/lister_tous_les_types_de_donnees_utilise_2008">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Voici une petite requête qui permet de lister tous les types de données utilisés  pour les colonnes des tables d&rsquo;une base de données &#8230;<br />
<span id="more-61"></span></p>
<p>Si on ne souhaite connaître que les types de données :</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">------------------------------- <br />
-- Nicolas SOUQUET - 30/10/2009 <br />
------------------------------- <br />
SELECT DISTINCT TY.name AS nomType <br />
FROM sys.TABLES AS T <br />
JOIN sys.COLUMNS AS C <br />
&nbsp; ON T.object_id = C.object_id <br />
JOIN sys.types AS TY <br />
&nbsp; ON C.user_type_id = TY.user_type_id <br />
ORDER BY TY.name</div></td></tr></tbody></table></div>
<p>On peut aussi simplement connaître le nombre d&rsquo;utilisations de tous les types utilisés :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">------------------------------- <br />
-- Nicolas SOUQUET - 30/10/2009 <br />
------------------------------- <br />
SELECT TY.name AS nomType, <br />
&nbsp; &nbsp; COUNT(*) AS nbUtilisationsType <br />
FROM sys.tables AS T <br />
JOIN sys.columns AS C <br />
&nbsp; ON T.object_id = C.object_id <br />
JOIN sys.types AS TY <br />
&nbsp; ON C.user_type_id = TY.user_type_id <br />
GROUP BY TY.name <br />
ORDER BY TY.name</div></td></tr></tbody></table></div>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Créer des captures instantanées de bases de données</title>
		<link>https://blog.developpez.com/elsuket/p7454/moteur-de-base-de-donnees-sql-server/creer_des_captures_instantanees_de_bases</link>
		<comments>https://blog.developpez.com/elsuket/p7454/moteur-de-base-de-donnees-sql-server/creer_des_captures_instantanees_de_bases#comments</comments>
		<pubDate>Mon, 06 Apr 2009 22:06:03 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Moteur de base de données SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Même en phase de développement, il est important de sauvegarder une base de données. On peut estimer qu&#8217;il n&#8217;est pas nécessaire de mettre en place une stratégie de sauvegarde, et, comme une erreur arrive vite lors des tests unitaires, il &#8230; <a href="https://blog.developpez.com/elsuket/p7454/moteur-de-base-de-donnees-sql-server/creer_des_captures_instantanees_de_bases">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Même en phase de développement, il est important de sauvegarder une base de données.<br />
On peut estimer qu&rsquo;il n&rsquo;est pas nécessaire de mettre en place une stratégie de sauvegarde, et, comme une erreur arrive vite lors des tests unitaires, il faut que l&rsquo;on puisse replacer la base de données avec le jeu de données qui a été altéré par un test, sans perdre le travail de plusieurs développeurs.<br />
Si les captures instantanées de bases de données ne permettent pas de protéger complètement l&rsquo;intégrité d&rsquo;une base de données, elles sont très utiles si l&rsquo;on veut se prémunir des erreurs de l&rsquo;utilisateur pour &laquo;&nbsp;restaurer&nbsp;&raquo; les valeurs d&rsquo;une table ou récupérer une procédure stockée &#8230;<br />
<span id="more-75"></span><br />
Tout d&rsquo;abord, qu&rsquo;est-ce qu&rsquo;une capture instantanée de base de données ?<br />
C&rsquo;est une vue statique, en lecture seule, d&rsquo;une base de données source.<br />
Dès lors les possibilités offertes par cette fonctionnalité dépassent ce qui est décrit en introduction, et on peut tout à fait s&rsquo;en servir pour des statistiques, d&rsquo;autant que tout comme une base de données en ligne, on peut en supprimer les objets.</p>
<p>La commande T-SQL qui permet de créer une capture instantanée de base de données est simple :</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">CREATE DATABASE maBD_Snapshot <br />
ON (NAME = nomDesFichiersLogiquesDeDonnees, FILENAME = 'C:\maBD_Snapshot.ext' <br />
AS SNAPSHOT OF maBD</div></td></tr></tbody></table></div>
<p>Après la création d&rsquo;une capture instantanée de base de données, vous noterez en lisant de contenu de la table système sys.databases que la colonne source_database_id est valuée seulement pour celle-ci.<br />
Vous verrez également sous Management Studio apparaître la node &laquo;&nbsp;Captures instantanées de base de données&nbsp;&raquo; sous la node &laquo;&nbsp;Bases de données&nbsp;&raquo;. En la déployant, vous trouverez la capture que vous venez de réaliser.</p>
<p>Ainsi, pour restaurer les valeurs d&rsquo;une table de la base de données en ligne avec les valeurs contenues avant une erreur, il suffit d&rsquo;écrire :</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 06/04/2009 - <br />
--------------------------------- <br />
UPDATE maBD.monSchema.maTable <br />
SET mesColonnes = SNAP.mesColonnes <br />
FROM maBD.monSchema.maTable AS CIBLE <br />
JOIN maBDSnapshot.monSchema.maTable AS SNAP ON CIBLE.PK_ID = SNAP ON.PK_ID</div></td></tr></tbody></table></div>
<p>Et pour restaurer la base de données à partir d&rsquo;une de ses captures instantanées :</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">USE master &nbsp; <br />
RESTORE DATABASE maBD <br />
FROM DATABASE_SNAPSHOT = 'maBDSnaphot'</div></td></tr></tbody></table></div>
<p><strong>Attention :</strong> il est nécessaire que la capture instantanée de données que l&rsquo;on souhaite restaurer soit la <strong>seule</strong> capture instantanée de la base de données à restaurer. Dans le cas contraire, on obtiendra l&rsquo;erreur suivante, qui n&rsquo;en laisse rien voir :</p>
<blockquote><p>Msg 3137, Level 16, State 4, Line 1<br />
Impossible de restaurer la base de données. Le nom primaire ou le nom de la capture instantanée a été spécifié de façon incorrecte, toutes les autres captures instantanées ont été supprimées, ou il manque des fichiers.<br />
Msg 3013, Level 16, State 1, Line 1<br />
RESTORE DATABASE s&rsquo;est terminé anormalement.</p></blockquote>
<p>Il devient ensuite simple de créer un job qui se chargera de créer régulièrement une capture instantanée de votre base de données. La commande T-SQL de ce job pourrait être :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 06/04/2009 - <br />
--------------------------------- <br />
-- Création d'une nouvelle capture instantanée de base de données <br />
DECLARE @nomSnapshot SYSNAME <br />
SELECT @nomSnapshot = 'maBD_' + REPLACE(REPLACE(CONVERT(CHAR(10), GETDATE(), 103) + '_' + CONVERT(CHAR(8), GETDATE(), 108), '/', '_'), ':', '_') <br />
&nbsp;<br />
DECLARE @SQL VARCHAR(256) <br />
SET @SQL = 'CREATE DATABASE ' + @nomSnapshot <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + ' ON (NAME = nomDesFichiersLogiquesDeDonneesDeMaBD, FILENAME = ''C:\' + @nomSnapshot + '.snap'')' <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + ' AS SNAPSHOT OF maBD' <br />
&nbsp;<br />
EXEC (@SQL) <br />
&nbsp;<br />
-- Suppression des anciennes captures instantanées de base de données <br />
DECLARE curSnapshots CURSOR FOR <br />
&nbsp; &nbsp; SELECT name <br />
&nbsp; &nbsp; FROM sys.databases <br />
&nbsp; &nbsp; WHERE source_database_id IS NOT NULL <br />
&nbsp; &nbsp; AND name &lt;&gt; @nomSnapshot <br />
FOR READ ONLY <br />
&nbsp;<br />
OPEN curSnapshots <br />
FETCH NEXT FROM curSnapshots INTO @nomSnapshot <br />
WHILE @@FETCH_STATUS = 0 <br />
BEGIN <br />
&nbsp; &nbsp; SET @SQL = 'DROP DATABASE ' + @nomSnapshot <br />
&nbsp; &nbsp; EXEC (@SQL) <br />
&nbsp; &nbsp; FETCH NEXT FROM curSnapshots INTO @nomSnapshot <br />
END <br />
&nbsp;<br />
DEALLOCATE curSnapshots</div></td></tr></tbody></table></div>
<p>Il vous suffit ensuite de planifier le job pour voir celui-ci s&rsquo;exécuter à une fréquence qui vous garantisse un minimum de perte de données.</p>
<p>Enfin, s&rsquo;il est clair qu&rsquo;une capture de base de données ne vaut pas une bonne stratégie de sauvegarde de base de données, on peut restaurer une base de données à partir d&rsquo;une capture de celle-ci :</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">RESTORE DATABASE maBD <br />
FROM DATABASE_SNAPSHOT = maBD_Snapshot</div></td></tr></tbody></table></div>
<p>Voici un bout de code à placer dans un job pour créer des captures instantanées de toutes les bases de données sauf les bases de données système :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 06/04/2009 - <br />
--------------------------------- <br />
DECLARE @SQL VARCHAR(256) <br />
&nbsp;<br />
--------------------------------------------------------------------- <br />
-- Suppression des anciennes captures instantanées de base de données &nbsp;<br />
--------------------------------------------------------------------- <br />
DECLARE curSnapshots CURSOR FOR &nbsp;<br />
&nbsp; SELECT name <br />
&nbsp; FROM sys.databases &nbsp;<br />
&nbsp; WHERE source_database_id IS NOT NULL <br />
&nbsp; AND create_date &lt; CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME) <br />
FOR READ ONLY &nbsp;<br />
&nbsp;<br />
DECLARE @nomSnapshot SYSNAME <br />
&nbsp;<br />
OPEN curSnapshots &nbsp;<br />
FETCH NEXT FROM curSnapshots INTO @nomSnapshot &nbsp;<br />
WHILE @@FETCH_STATUS = 0 &nbsp;<br />
BEGIN &nbsp;<br />
&nbsp; SET @SQL = 'DROP DATABASE ' + @nomSnapshot &nbsp;<br />
&nbsp; EXEC (@SQL) <br />
&nbsp; FETCH NEXT FROM curSnapshots INTO @nomSnapshot &nbsp;<br />
END <br />
&nbsp;<br />
DEALLOCATE curSnapshots <br />
&nbsp;<br />
-------------------------------------------- <br />
-- Captures instantanées de bases de données <br />
-------------------------------------------- <br />
DECLARE curBD CURSOR FOR <br />
&nbsp; SELECT name <br />
&nbsp; FROM sys.databases <br />
&nbsp; WHERE source_database_id IS NULL <br />
&nbsp; AND name NOT IN ('master', 'tempdb', 'model', 'msdb', 'ReportServer', 'ReportServerTempDB') <br />
&nbsp; AND state_desc = 'ONLINE' <br />
FOR READ ONLY <br />
&nbsp;<br />
DECLARE @NomBD SYSNAME, <br />
&nbsp; &nbsp; @ListeFichiers VARCHAR(255) <br />
&nbsp; &nbsp; <br />
OPEN curBD <br />
FETCH NEXT FROM curBD INTO @NomBD <br />
WHILE @@FETCH_STATUS = 0 <br />
BEGIN <br />
&nbsp; SET @ListeFichiers = NULL <br />
&nbsp;<br />
&nbsp; SELECT @ListeFichiers = ISNULL(@ListeFichiers, '') + <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '(NAME = ' + MF.name + ', FILENAME = ''C:\' + MF.name + '.snap'')' + ', ' <br />
&nbsp; FROM sys.master_files AS MF <br />
&nbsp; JOIN sys.databases AS D ON D.database_id = MF.database_id <br />
&nbsp; WHERE D.name = @NomBD <br />
&nbsp; AND MF.type = 0 -- fichiers de données <br />
&nbsp; <br />
&nbsp; SELECT @ListeFichiers = SUBSTRING(@ListeFichiers, 1, LEN(@ListeFichiers) - 1) <br />
&nbsp; <br />
&nbsp; SET @SQL = 'CREATE DATABASE ' + @NomBD + '_' + REPLACE(REPLACE(CONVERT(CHAR(10), GETDATE(), 103) + '_' + CONVERT(CHAR(8), GETDATE(), 108), '/', '_'), ':', '_') <br />
&nbsp; &nbsp; &nbsp; &nbsp; + ' ON ' + @ListeFichiers + <br />
&nbsp; &nbsp; &nbsp; &nbsp; + ' AS SNAPSHOT OF ' + @NomBD <br />
&nbsp;<br />
&nbsp; EXEC (@SQL) <br />
&nbsp; FETCH NEXT FROM curBD INTO @NomBD <br />
END <br />
&nbsp;<br />
DEALLOCATE curBD</div></td></tr></tbody></table></div>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>[Snippets] Manipuler un fichier texte d&#8217;une colonne comme une table d&#8217;une colonne</title>
		<link>https://blog.developpez.com/elsuket/p7218/snippets/snippets_manipuler_un_fichier_texte_d_un</link>
		<comments>https://blog.developpez.com/elsuket/p7218/snippets/snippets_manipuler_un_fichier_texte_d_un#comments</comments>
		<pubDate>Thu, 12 Feb 2009 18:56:20 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Voyons comment on peut rendre abstraite la notion de fichier en T-SQL en combinant les commandes OPENROWSET et les vénérables CTE &#8230; 123456789101112131415161718192021222324--------------------------------- -- Nicolas SOUQUET - 12/02/2009 - --------------------------------- WITH &#160; CTE_IMPORT AS &#160; ( &#160; &#160; SELECT BulkColumn &#8230; <a href="https://blog.developpez.com/elsuket/p7218/snippets/snippets_manipuler_un_fichier_texte_d_un">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Voyons comment on peut rendre abstraite la notion de fichier en T-SQL en combinant les commandes OPENROWSET et les vénérables CTE &#8230;</p>
<p><span id="more-39"></span></p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 12/02/2009 - <br />
--------------------------------- <br />
WITH <br />
&nbsp; CTE_IMPORT AS <br />
&nbsp; ( <br />
&nbsp; &nbsp; SELECT BulkColumn <br />
&nbsp; &nbsp; FROM OPENROWSET(BULK 'C:\monFichier.txt', SINGLE_CLOB) TMP <br />
&nbsp; ), <br />
&nbsp; CTE_INDICES_LIGNES (Deb, Fin) AS &nbsp;<br />
&nbsp; ( &nbsp;<br />
&nbsp; &nbsp; &nbsp; SELECT 1 Deb, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CHARINDEX(CHAR(10), CTE_IMPORT.BulkColumn + CHAR(10)) Fin <br />
&nbsp; &nbsp; &nbsp; FROM CTE_IMPORT <br />
&nbsp; &nbsp; UNION ALL &nbsp;<br />
&nbsp; &nbsp; &nbsp; SELECT CAST(CTE_INDICES_LIGNES.Fin + 1 AS INT), <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CHARINDEX(CHAR(10), CTE_IMPORT.BulkColumn + CHAR(10), CTE_INDICES_LIGNES.Fin + 1) <br />
&nbsp; &nbsp; &nbsp; FROM CTE_IMPORT, CTE_INDICES_LIGNES <br />
&nbsp; &nbsp; &nbsp; WHERE CHARINDEX(CHAR(10), CTE_IMPORT.BulkColumn + CHAR(10), CTE_INDICES_LIGNES.Fin + 1 ) &gt; 0 &nbsp;<br />
&nbsp; ) <br />
SELECT SUBSTRING(BulkColumn, CTE_INDICES_LIGNES.Deb, CTE_INDICES_LIGNES.Fin - CTE_INDICES_LIGNES.Deb) AS Colonne <br />
FROM CTE_INDICES_LIGNES <br />
CROSS JOIN CTE_IMPORT <br />
WHERE CTE_INDICES_LIGNES.Deb &lt;&gt; CTE_INDICES_LIGNES.Fin</div></td></tr></tbody></table></div>
<p>Comme on peut s&rsquo;y attendre, c&rsquo;est particulièrement contre-performant :<br />
en activant SET STATISTICS IO puis SET STATISTICS TIME, on obtient successivement, pour un fichier contenant 50 lignes:</p>
<blockquote><p>Table &lsquo;Worktable&rsquo;. Nombre d&rsquo;analyses 3, lectures logiques 810<br />
Temps UC = 40 ms, temps écoulé = 101 ms.</p></blockquote>
<p>Ce snippet s&rsquo;avère donc utile pour faire un import d&rsquo;un faible de nombre de lignes sans passer par une table de base de données, mais il est à éviter pour faire des import massifs.<br />
L&rsquo;utilisation de la CTE permet de plus de réaliser des jointures &#8230;</p>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Les dates et le type DATETIME sous SQL Server</title>
		<link>https://blog.developpez.com/elsuket/p7805/t-sql/les_dates_et_le_type_datetime_sous_sql_s</link>
		<comments>https://blog.developpez.com/elsuket/p7805/t-sql/les_dates_et_le_type_datetime_sous_sql_s#comments</comments>
		<pubDate>Sat, 27 Jun 2009 06:32:18 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Sujet maintes fois abordé pour un problème de format, la gestion des dates sous SQL Server est pourtant très simple si l&#8217;on se sert, comme d&#8217;habitude, des standards &#8230; Très souvent on demande comment faire pour afficher une date sous &#8230; <a href="https://blog.developpez.com/elsuket/p7805/t-sql/les_dates_et_le_type_datetime_sous_sql_s">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Sujet maintes fois abordé pour un problème de format, la gestion des dates sous SQL Server est pourtant très simple si l&rsquo;on se sert, comme d&rsquo;habitude, des standards &#8230;</p>
<p><span id="more-5"></span></p>
<p>Très souvent on demande comment faire pour afficher une date sous tel ou tel format, ou encore pourquoi en stockant une date sous tel format, <em>Enterprise Manager</em> ou <em>SQL Server Management Studio</em> les affichent sous la forme AAAA-MM-JJ hh:mm:ss.</p>
<p>Il faut d&rsquo;abord savoir comment sont stockées les données de type DATETIME : deux entiers, chacun de 4 octets :<br />
&#8211; un entier pour la date elle-même<br />
&#8211; un entier pour les heures/minutes/secondes/millisecondes.</p>
<p>Ceci explique pourquoi, lorsqu&rsquo;on écrit :</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">SELECT maDate , maDate - 1 AS hier <br />
FROM dbo.Table</div></td></tr></tbody></table></div>
<p>les valeurs contenues dans la colonne &laquo;&nbsp;hier&nbsp;&raquo; sont plus vieilles d&rsquo;exactement un jour par rapport à la colonne maDate.</p>
<p><strong>Passons maintenant aux divers format proposés par la fonction CONVERT() : cette fonction permet de choisir un style d&rsquo;affichage, mais en aucun cas il ne spécifie une date.</strong></p>
<p>C&rsquo;est pourquoi on peut utiliser la fonction CONVERT() pour obtenir une date à partir d&rsquo;une chaîne de caractères, et inversement, <strong>en spécifiant le style de la chaîne qui représente une date</strong></p>
<p>=><code class="codecolorer text default"><span class="text">SELECT CONVERT(DATETIME, '26/06/2006', 103)</span></code></p>
<p>affiche 2006-06-26 00:00:00.000  sous <em>SQL Server Management Studio</em> ou <em>Enterprise Manager</em>, <strong>mais là encore ce n&rsquo;est qu&rsquo;une représentation de la date.</strong></p>
<p>Nous avons du spécifier que le format de la chaîne à convertir est le 103, c&rsquo;est à dire le format français, avec des slashes pour séparer les parties d&rsquo;une date, et une année sur 4 chiffres (sinon c&rsquo;est le style &laquo;&nbsp;3&nbsp;&raquo;, pour spécifier une année sur deux chiffres).</p>
<p>Inversement :</p>
<p>=><code class="codecolorer text default"><span class="text">SELECT CONVERT(CHAR(10), GETDATE(), 103)</span></code></p>
<p>La fonction GETDATE() retourne la date courante en utilisant le type de données DATETIME (donc deux entiers de 4 octets).<br />
L&rsquo;exécution de cette dernière requête retourne 26/06/2009.</p>
<p><strong>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
&#8212; Normalisation &#8212;<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</strong><br />
Supposons que nous avons totalement normalisé tous les échanges de données entre nos applications et la base de données.<br />
Nous utilisons donc le format ISO, c&rsquo;est à dire AAAAMMJJ, que SQL Server sait transformer au type DATETIME, directement avec la fonction CAST, qui permet de s&rsquo;affranchir de tout format de date :</p>
<p>=><code class="codecolorer text default"><span class="text">SELECT CAST('20090626' AS DATETIME)</span></code></p>
<p>affiche bien 2009-06-26 00:00:00.000 sous <em>SQL Server Management Studio</em> ou <em>Enterprise Manager</em></p>
<p>De même si nous voulons en plus préciser l&rsquo;heure :</p>
<p>=><code class="codecolorer text default"><span class="text">SELECT CAST('20090626 22:14:03.257' AS DATETIME)</span></code></p>
<p>affiche 2009-06-26 22:14:03.257 sous <em>SQL Server Management Studio</em> ou <em>Enterprise Manager</em></p>
<p>Dès lors dans le cas d&rsquo;une application internationalisée, l&rsquo;application peut communiquer des dates au format ISO, et les afficher suivant le format de la nationalité de l&rsquo;utilisateur.</p>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Suivre les sauvegardes de base de données avec MSDB</title>
		<link>https://blog.developpez.com/elsuket/p7807/snippets/suivre_les_sauvegardes_de_base_de_donnee</link>
		<comments>https://blog.developpez.com/elsuket/p7807/snippets/suivre_les_sauvegardes_de_base_de_donnee#comments</comments>
		<pubDate>Sat, 27 Jun 2009 07:17:56 +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[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Comment suivre l&#8217;évolution des sauvegardes de base de données avec une requête sur la base de données système MSDB ? L&#8217;exécution de la requête suivante montre, pour chaque sauvegarde de base de données : &#8211; l&#8217;heure de début, &#8211; l&#8217;heure &#8230; <a href="https://blog.developpez.com/elsuket/p7807/snippets/suivre_les_sauvegardes_de_base_de_donnee">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Comment suivre l&rsquo;évolution des sauvegardes de base de données avec une requête sur la base de données système MSDB ?</p>
<p><span id="more-6"></span></p>
<p>L&rsquo;exécution de la requête suivante montre, pour chaque sauvegarde de base de données :</p>
<p>&#8211; l&rsquo;heure de début,<br />
&#8211; l&rsquo;heure de fin,<br />
&#8211; la durée,<br />
&#8211; la taille,<br />
&#8211; l&rsquo;emplacement du fichier de sauvegarde,<br />
&#8211; quelques autres caractéristiques de la sauvegarde &#8230;</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 27/06/2009 - <br />
--------------------------------- <br />
SELECT database_name, <br />
&nbsp; &nbsp; physical_drive, <br />
&nbsp; &nbsp; physical_name, <br />
&nbsp; &nbsp; physical_device_name, <br />
&nbsp; &nbsp; logical_name, <br />
&nbsp; &nbsp; CAST(SUM(file_size) / 1048576 AS NUMERIC (10, 2)) AS Taille, <br />
&nbsp; &nbsp; name, <br />
&nbsp; &nbsp; user_name, <br />
&nbsp; &nbsp; database_creation_date, <br />
&nbsp; &nbsp; backup_start_date, <br />
&nbsp; &nbsp; backup_finish_date, <br />
&nbsp; &nbsp; Duree, <br />
&nbsp; &nbsp; Type, <br />
&nbsp; &nbsp; server_name, <br />
&nbsp; &nbsp; machine_name, <br />
&nbsp; &nbsp; recovery_model, <br />
&nbsp; &nbsp; is_snapshot, <br />
&nbsp; &nbsp; is_damaged, <br />
&nbsp; &nbsp; has_incomplete_metadata <br />
FROM <br />
( <br />
&nbsp; SELECT F.physical_drive, <br />
&nbsp; &nbsp; F.physical_name, <br />
&nbsp; &nbsp; F.logical_name, <br />
&nbsp; &nbsp; F.file_size, <br />
&nbsp; &nbsp; S.name, <br />
&nbsp; &nbsp; S.user_name, <br />
&nbsp; &nbsp; S.database_creation_date, <br />
&nbsp; &nbsp; S.backup_start_date, <br />
&nbsp; &nbsp; S.backup_finish_date, <br />
&nbsp; &nbsp; CASE LEN(CAST(DATEDIFF(second, S.backup_start_date, S.backup_finish_date) / 3600 AS VARCHAR(4))) <br />
&nbsp; &nbsp; &nbsp; WHEN 1 THEN '0' + CAST(DATEDIFF(second, S.backup_start_date, S.backup_finish_date) / 3600 AS VARCHAR(4)) <br />
&nbsp; &nbsp; &nbsp; ELSE CAST(DATEDIFF(second, S.backup_start_date, S.backup_finish_date) / 3600 AS VARCHAR(4)) <br />
&nbsp; &nbsp; END + ':' + <br />
&nbsp; &nbsp; CASE LEN(CAST(DATEDIFF(second, S.backup_start_date, S.backup_finish_date) / 60 % 60 AS VARCHAR(2))) <br />
&nbsp; &nbsp; &nbsp; WHEN 1 THEN '0' + CAST(DATEDIFF(second, S.backup_start_date, S.backup_finish_date) / 60 % 60 AS VARCHAR(2)) <br />
&nbsp; &nbsp; &nbsp; ELSE CAST(DATEDIFF(second, S.backup_start_date, S.backup_finish_date) / 60 % 60 AS VARCHAR(2)) <br />
&nbsp; &nbsp; END + ':' + <br />
&nbsp; &nbsp; CASE LEN(CAST(DATEDIFF(second, S.backup_start_date, S.backup_finish_date) % 60 AS VARCHAR(2))) <br />
&nbsp; &nbsp; &nbsp; WHEN 1 THEN '0' + CAST(DATEDIFF(second, S.backup_start_date, S.backup_finish_date) % 60 AS VARCHAR(2)) <br />
&nbsp; &nbsp; &nbsp; ELSE CAST(DATEDIFF(second, S.backup_start_date, S.backup_finish_date) % 60 AS VARCHAR(2)) <br />
&nbsp; &nbsp; END Duree, <br />
&nbsp; &nbsp; CASE S.type <br />
&nbsp; &nbsp; &nbsp; WHEN 'D' THEN 'Base de données - Complet' <br />
&nbsp; &nbsp; &nbsp; WHEN 'I' THEN 'Base de données - Différentiel' <br />
&nbsp; &nbsp; &nbsp; WHEN 'L' THEN 'Journal de transactions' <br />
&nbsp; &nbsp; &nbsp; WHEN 'F' THEN 'Fichier ou groupe de fichiers' <br />
&nbsp; &nbsp; &nbsp; WHEN 'G' THEN 'Fichier - Différentiel' <br />
&nbsp; &nbsp; &nbsp; WHEN 'P' THEN 'Partiel' <br />
&nbsp; &nbsp; &nbsp; WHEN 'Q' THEN 'Partiel - Différentiel' <br />
&nbsp; &nbsp; END Type, <br />
&nbsp; &nbsp; S.database_name, <br />
&nbsp; &nbsp; S.server_name, <br />
&nbsp; &nbsp; S.machine_name, <br />
&nbsp; &nbsp; S.recovery_model, <br />
&nbsp; &nbsp; S.is_snapshot, <br />
&nbsp; &nbsp; S.is_damaged, <br />
&nbsp; &nbsp; S.has_incomplete_metadata, <br />
&nbsp; &nbsp; FAM.physical_device_name <br />
&nbsp; FROM msdb.dbo.backupfile AS F &nbsp;<br />
&nbsp; JOIN msdb.dbo.backupset AS S &nbsp;<br />
&nbsp; &nbsp; ON F.backup_set_id = S.backup_set_id <br />
&nbsp; JOIN msdb.dbo.backupmediafamily AS FAM <br />
&nbsp; &nbsp; ON FAM.media_set_id = S.media_set_id <br />
&nbsp; WHERE S.backup_start_date BETWEEN CAST(FLOOR(CAST(GETDATE()- 2 AS FLOAT)) AS DATETIME) AND GETDATE() <br />
) AS SUB <br />
GROUP BY physical_drive, <br />
&nbsp; &nbsp; physical_name, <br />
&nbsp; &nbsp; physical_device_name, <br />
&nbsp; &nbsp; logical_name, <br />
&nbsp; &nbsp; name, <br />
&nbsp; &nbsp; user_name, <br />
&nbsp; &nbsp; database_creation_date, <br />
&nbsp; &nbsp; backup_start_date, <br />
&nbsp; &nbsp; backup_finish_date, <br />
&nbsp; &nbsp; Duree, <br />
&nbsp; &nbsp; Type, <br />
&nbsp; &nbsp; database_name, <br />
&nbsp; &nbsp; server_name, &nbsp;<br />
&nbsp; &nbsp; machine_name, &nbsp;<br />
&nbsp; &nbsp; recovery_model, &nbsp;<br />
&nbsp; &nbsp; is_snapshot, &nbsp;<br />
&nbsp; &nbsp; is_damaged, &nbsp;<br />
&nbsp; &nbsp; has_incomplete_metadata <br />
ORDER BY database_name, backup_start_date</div></td></tr></tbody></table></div>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Découper une chaîne en sous-chaînes de longueur fixe</title>
		<link>https://blog.developpez.com/elsuket/p7818/snippets/decouper_une_chaine_en_sous_chaines_de_l</link>
		<comments>https://blog.developpez.com/elsuket/p7818/snippets/decouper_une_chaine_en_sous_chaines_de_l#comments</comments>
		<pubDate>Wed, 01 Jul 2009 06:12:23 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Certains me désignent sur le forum SQL Server comme l&#8217;homme qui murmure à l&#8217;oreille des CTE. En voici donc une nouvelle ! 1234567891011121314151617181920--------------------------------- -- Nicolas SOUQUET - 01/07/2009 - --------------------------------- DECLARE @toto VARCHAR(50) = 'ABCDEFGHIJKLM' SELECT LEN(@toto) &#160; ;WITH &#160; &#8230; <a href="https://blog.developpez.com/elsuket/p7818/snippets/decouper_une_chaine_en_sous_chaines_de_l">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Certains me désignent sur le forum SQL Server comme l&rsquo;homme qui murmure à l&rsquo;oreille des CTE.<br />
En voici donc une nouvelle !</p>
<p><span id="more-49"></span></p>
<div class="codecolorer-container text 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 />17<br />18<br />19<br />20<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 01/07/2009 - <br />
--------------------------------- <br />
DECLARE @toto VARCHAR(50) = 'ABCDEFGHIJKLM' <br />
SELECT LEN(@toto) <br />
&nbsp;<br />
;WITH <br />
&nbsp; CTE AS <br />
&nbsp; ( <br />
&nbsp; &nbsp; &nbsp; SELECT SUBSTRING(@toto, 1, 4) AS sousChaine, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 AS nFinSousChaine <br />
&nbsp; &nbsp; UNION ALL <br />
&nbsp; &nbsp; &nbsp; SELECT SUBSTRING(@toto, nFinSousChaine + 4, 4) AS sousChaine, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nFinSousChaine + 4 AS nFinSousChaine <br />
&nbsp; &nbsp; &nbsp; FROM CTE <br />
&nbsp; &nbsp; &nbsp; WHERE nFinSousChaine &lt;= LEN(@toto) <br />
&nbsp; ) <br />
SELECT sousChaine <br />
FROM CTE <br />
WHERE nFinSousChaine &lt;= LEN(@toto)</div></td></tr></tbody></table></div>
<p>Retourne :</p>
<blockquote><p>sousChaine<br />
&#8212;&#8212;&#8212;-<br />
ABCD<br />
EFGH<br />
IJKL<br />
M</p></blockquote>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Consulter l&#8217;historique des restaurations de bases de données</title>
		<link>https://blog.developpez.com/elsuket/p7914/snippets/consulter_l_historique_des_restaurations</link>
		<comments>https://blog.developpez.com/elsuket/p7914/snippets/consulter_l_historique_des_restaurations#comments</comments>
		<pubDate>Thu, 30 Jul 2009 17:09:21 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Voici une petite requête qui permet de voir toutes les restaurations de bases de données d&#8217;une instance &#8230; 12345678910111213141516171819--------------------------------- -- Nicolas SOUQUET - 30/07/2009 - --------------------------------- SELECT H.restore_date, &#160; &#160; H.destination_database_name, &#160; &#160; H.user_name, &#160; &#160; CASE restore_type &#160; &#160; &#8230; <a href="https://blog.developpez.com/elsuket/p7914/snippets/consulter_l_historique_des_restaurations">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Voici une petite requête qui permet de voir toutes les restaurations de bases de données d&rsquo;une instance &#8230;</p>
<p><span id="more-54"></span></p>
<div class="codecolorer-container text 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 />17<br />18<br />19<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 30/07/2009 - <br />
--------------------------------- <br />
SELECT H.restore_date, <br />
&nbsp; &nbsp; H.destination_database_name, <br />
&nbsp; &nbsp; H.user_name, <br />
&nbsp; &nbsp; CASE restore_type <br />
&nbsp; &nbsp; &nbsp; WHEN 'D' THEN 'Base de données' <br />
&nbsp; &nbsp; &nbsp; WHEN 'F' THEN 'Fichier' <br />
&nbsp; &nbsp; &nbsp; WHEN 'G' THEN 'Groupe de fichiers' <br />
&nbsp; &nbsp; &nbsp; WHEN 'I' THEN 'Différentiel' <br />
&nbsp; &nbsp; &nbsp; WHEN 'L' THEN 'Fichier du journal des transactions' <br />
&nbsp; &nbsp; &nbsp; WHEN 'V' THEN 'Vérification' <br />
&nbsp; &nbsp; &nbsp; WHEN 'R' THEN 'Par capture instantanée' <br />
&nbsp; &nbsp; END AS restore_type, <br />
&nbsp; &nbsp; F.destination_phys_name <br />
FROM msdb.dbo.restorehistory AS H <br />
JOIN msdb.dbo.restorefile AS F <br />
&nbsp; ON H.restore_history_id &nbsp;= F.restore_history_id</div></td></tr></tbody></table></div>
<p>Facile à placer dans un rapport SSRS <img src="https://blog.developpez.com/elsuket/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley" /></p>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Différence entre LOGIN et USER, entre connexion et utilisateur</title>
		<link>https://blog.developpez.com/elsuket/p7848/t-sql/difference_entre_login_et_user_entre_con</link>
		<comments>https://blog.developpez.com/elsuket/p7848/t-sql/difference_entre_login_et_user_entre_con#comments</comments>
		<pubDate>Mon, 06 Jul 2009 20:25:01 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Sécurité]]></category>
		<category><![CDATA[SQL Server Management Studio]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Quelle est la différence entre une connexion, ou LOGIN, et un utilisateur de base de données ? Une connexion, ou login, permet d&#8217;accéder à une instance de SQL Server, mais c&#8217;est tout. Une instance de SQL Server pouvant héberger plusieurs &#8230; <a href="https://blog.developpez.com/elsuket/p7848/t-sql/difference_entre_login_et_user_entre_con">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Quelle est la différence entre une connexion, ou LOGIN, et un utilisateur de base de données ?</p>
<p><span id="more-145"></span></p>
<p><strong>Une connexion, ou login,</strong> permet d&rsquo;accéder à une instance de SQL Server, mais c&rsquo;est tout.<br />
Une instance de SQL Server pouvant héberger plusieurs bases de données utilisateur, nous ne souhaitons peut-être pas que tous les utilisateurs aient accès à toutes les bases de données d&rsquo;une instance, et de la même façon.<br />
On peut aussi créer une connexion à un utilisateur pour qu&rsquo;il puisse seulement effectuer des sauvegardes de base de données</p>
<p><strong>A toute connexion|login, peut correspondre un utilisateur de base de données.</strong><br />
L&rsquo;utilisateur de base de données est alors &laquo;&nbsp;mappé&nbsp;&raquo; sur une connexion, qui le donne le droit de naviguer dans 0 à autant de bases de données que l&rsquo;instance en héberge.<br />
Un administrateur ou les personnes à qui l&rsquo;administrateur en a délégué le droit peuvent accorder des droits d&rsquo;accès et des privilèges.</p>
<p><strong>Un droit d&rsquo;accès</strong> permet à un utilisateur de naviguer dans une base de données suivant des privilèges que l&rsquo;administrateur lui a octroyés.<br />
<strong>Un privilège</strong> permet de limiter l&rsquo;accès aux données, par exemple en refusant le droit de SELECT sur la colonne salaire de la table des employés aux utilisateurs employés.</p>
<p>On peut encore gérer plus globalement au niveau de la base de données ou de l&rsquo;instance les accès aux données en créant des rôles : on affecte ainsi au rôle des droits d&rsquo;accès et des privilèges, et lors de l&rsquo;arrivée d&rsquo;un nouvel utilisateur, il nous suffit de l&rsquo;ajouter au rôle de base de données.</p>
<p><strong>Pour créer une connexion :</strong></p>
<p><code class="codecolorer text default"><span class="text">CREATE LOGIN monLogin WITH PASSWORD = 'monMotDePasse'</span></code></p>
<p><strong>Pour permettre la navigation dans la base de données :</strong></p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">USE maBD <br />
GO <br />
CREATE USER monUtilisateur FOR LOGIN monLogin</div></td></tr></tbody></table></div>
<p><strong>On peut créer de plusieurs façon un rôle :</strong></p>
<p>=> Soit par &laquo;&nbsp;recopie&nbsp;&raquo; des drois d&rsquo;un utilisateur :</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">USE maBD; <br />
CREATE ROLE monRole AUTHORIZATION unUtilsateur; <br />
GO</div></td></tr></tbody></table></div>
<p>=> Soit par recopie d&rsquo;un rôle de base de données existant :</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">USE maBD; <br />
CREATE ROLE monRole AUTHORIZATION db_securityadmin; <br />
GO</div></td></tr></tbody></table></div>
<p>Pour gérer les privilèges, on peut voir la documentation des instructions <a href="http://msdn.microsoft.com/fr-fr/library/ms187965%28SQL.90%29.aspx">GRANT</a>, <a href="http://msdn.microsoft.com/fr-fr/library/ms187728.aspx">REVOKE</a> et <a href="http://msdn.microsoft.com/fr-fr/library/ms188338.aspx">DENY</a></p>
<p><strong>On peut également réaliser tout cela à l&rsquo;aide de <em>SQL Server Management Studio :</em></strong></p>
<p>&#8211; Pour la création de la connexion, ouvrez la node &laquo;&nbsp;Sécurité&nbsp;&raquo; de votre instance, puis faites un clic-droit sur la node &laquo;&nbsp;Connexions&nbsp;&raquo;, et choisissez &laquo;&nbsp;Nouvelle connexion&nbsp;&raquo;</p>
<p><img src="http://blog.developpez.com/media/login.PNG" width="390" height="274" alt="" /></p>
<p><img src="http://blog.developpez.com/media/login2.PNG" width="704" height="632" alt="" /></p>
<p>&#8211; Pour la création de l&rsquo;utilisateur, après avoir ouvert la node possédant le nom de la base de données en question, ouvrez la node &laquo;&nbsp;Sécurité&nbsp;&raquo;, puis cliquez-droit sur la node &laquo;&nbsp;Utilisateurs&nbsp;&raquo; en choisissant &laquo;&nbsp;Nouvel utilisateur&nbsp;&raquo;</p>
<p><img src="http://blog.developpez.com/media/user.PNG" width="389" height="518" alt="" /></p>
<p><img src="http://blog.developpez.com/media/user2.PNG" width="704" height="771" alt="" /></p>
<p>Un peu de lecture par <a href="http://sqlpro.developpez.com/cours/sqlaz/dcl/">ici</a> et <a href="http://sqlpro.developpez.com/cours/sqlserver/securisation/">ici</a>, par SQLPro <img src="https://blog.developpez.com/elsuket/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley" /></p>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Suivre les plans de maintenance</title>
		<link>https://blog.developpez.com/elsuket/p7865/moteur-de-base-de-donnees-sql-server/suivre_les_plans_de_maintenance</link>
		<comments>https://blog.developpez.com/elsuket/p7865/moteur-de-base-de-donnees-sql-server/suivre_les_plans_de_maintenance#comments</comments>
		<pubDate>Thu, 09 Jul 2009 20:15:56 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Agent SQL Server]]></category>
		<category><![CDATA[Moteur de base de données SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Comment savoir si un plan de maintenance s&#8217;est correctement exécuté ? Comment savoir combien de temps a duré son exécution ? Une requête simple dans la base de données système MSDB nous donne la réponse &#8230; Valable sous SQL Server &#8230; <a href="https://blog.developpez.com/elsuket/p7865/moteur-de-base-de-donnees-sql-server/suivre_les_plans_de_maintenance">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Comment savoir si un plan de maintenance s&rsquo;est correctement exécuté ?<br />
Comment savoir combien de temps a duré son exécution ?</p>
<p>Une requête simple dans la base de données système MSDB nous donne la réponse &#8230;<br />
Valable sous SQL Server 2000 et ultérieur <img src="https://blog.developpez.com/elsuket/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley" /></p>
<p><span id="more-13"></span></p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 09/07/2009 - &nbsp;<br />
--------------------------------- <br />
SELECT plan_name, <br />
&nbsp; &nbsp; database_name, <br />
&nbsp; &nbsp; activity, <br />
&nbsp; &nbsp; start_time, <br />
&nbsp; &nbsp; end_time, <br />
&nbsp; &nbsp; CASE LEN(CAST(duration / 3600 AS VARCHAR(4))) <br />
&nbsp; &nbsp; &nbsp; WHEN 1 THEN '0' + CAST(duration / 3600 AS VARCHAR(4)) <br />
&nbsp; &nbsp; &nbsp; ELSE CAST(duration / 3600 AS VARCHAR(4)) <br />
&nbsp; &nbsp; END + ':' + <br />
&nbsp; &nbsp; CASE LEN(CAST(duration / 60 % 60 AS VARCHAR(2))) <br />
&nbsp; &nbsp; &nbsp; WHEN 1 THEN '0' + CAST(duration / 60 % 60 AS VARCHAR(2)) <br />
&nbsp; &nbsp; &nbsp; ELSE CAST(duration / 60 % 60 AS VARCHAR(2)) <br />
&nbsp; &nbsp; END <br />
&nbsp; &nbsp; + ':' + &nbsp;<br />
&nbsp; &nbsp; CASE LEN(CAST(duration % 60 AS VARCHAR(2))) <br />
&nbsp; &nbsp; &nbsp; WHEN 1 THEN '0' + CAST(duration % 60 AS VARCHAR(2)) <br />
&nbsp; &nbsp; &nbsp; ELSE CAST(duration % 60 AS VARCHAR(2)) <br />
&nbsp; &nbsp; END Durée <br />
FROM msdb.dbo.sysdbmaintplan_history <br />
WHERE succeeded = 0 -- 0 : échec | 1 : réussite <br />
-- AND start_time BETWEEN '20090907' AND '20090908' <br />
ORDER BY database_name, start_time</div></td></tr></tbody></table></div>
<p>Il est dès lors facile de placer cette requête dans un DataSet sous Reporting Services ou dans une application WinForms pour pouvoir s&rsquo;afficher l&rsquo;état des plans de maintenance qui doivent avoir lieu sur une instance de SQL Server <img src="https://blog.developpez.com/elsuket/wp-includes/images/smilies/icon_wink.gif" alt=";)" class="wp-smiley" /></p>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Créer une table de dates</title>
		<link>https://blog.developpez.com/elsuket/p7872/snippets/creer_une_table_de_dates</link>
		<comments>https://blog.developpez.com/elsuket/p7872/snippets/creer_une_table_de_dates#comments</comments>
		<pubDate>Fri, 10 Jul 2009 18:23:08 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Pourquoi ne pas créer une table de dates pour se faciliter les recherches dans des tables suivant la colonne de type date qu&#8217;elles contiennent ? On peut vous demander par exemple de rechercher la quantité d&#8217;eau consommée par un parc &#8230; <a href="https://blog.developpez.com/elsuket/p7872/snippets/creer_une_table_de_dates">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Pourquoi ne pas créer une table de dates pour se faciliter les recherches dans des tables suivant la colonne de type date qu&rsquo;elles contiennent ?<br />
On peut vous demander par exemple de rechercher la quantité d&rsquo;eau consommée par un parc de machines pour la deuxième semaine de chaque mois d&rsquo;une année.<br />
Imaginez un peu la complexité de la requête à écrire &#8230;<br />
Voyons comment on peut résoudre cette demande très facilement en créant une table de dates &#8230;</p>
<p><span id="more-52"></span></p>
<p>Voici donc notre table :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 10/07/2009 - <br />
--------------------------------- <br />
CREATE TABLE TbDates <br />
( <br />
&nbsp; date DATETIME NOT NULL CONSTRAINT PK_TbDates_date PRIMARY KEY, <br />
&nbsp; annee AS (YEAR(date)) PERSISTED NOT NULL, <br />
&nbsp; mois AS(MONTH(date)) PERSISTED NOT NULL, <br />
&nbsp; semaineDansAnnee AS(DATEPART(week, date)) PERSISTED NOT NULL, <br />
&nbsp; semaineDansMois AS(DATEPART(week, date) - DATEPART(week, DATEADD(day, -DATEPART(day, date) + 1, date)) + 1) &nbsp;PERSISTED NOT NULL, <br />
&nbsp; jourDansAnnee AS (DATEPART(dayofyear, date)) PERSISTED NOT NULL, <br />
&nbsp; jourDansMois AS(DAY(date)) PERSISTED NOT NULL, <br />
&nbsp; jourDansSemaine TINYINT NULL CONSTRAINT CHK_TbDates_jourDansSemaine CHECK(jourDansSemaine BETWEEN 0 AND 7), <br />
) <br />
GO</div></td></tr></tbody></table></div>
<p>Comme toutes les colonnes sont calculées à l&rsquo;exception de <em>jourDansSemaine </em> (parce que la fonction <em>DATEPART(weekday, date)</em> n&rsquo;est pas déterministe, elle ne peut pas être utilisée comme spécification d&rsquo;une colonne calculée), nous n&rsquo;aurons donc qu&rsquo;à insérer les dates dans la colonne <em>date</em>.</p>
<p>Comment allons-nous valuer la colonne <em>jourDansSemaine </em> ? Avec le trigger suivant :</p>
<div class="codecolorer-container text 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 />17<br />18<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------------------------------------------------------- <br />
-- Nicolas SOUQUET - 10/07/2009 - Déclencheur qui calcule le jour de la semaine - <br />
-- &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pour la table dbo.TbDates &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - <br />
--------------------------------------------------------------------------------- <br />
CREATE TRIGGER TR_A_I_TbDates <br />
&nbsp; ON dbo.TbDates <br />
&nbsp; AFTER INSERT <br />
AS <br />
BEGIN <br />
&nbsp; SET NOCOUNT ON <br />
&nbsp;<br />
&nbsp; UPDATE dbo.TbDates <br />
&nbsp; SET jourDansSemaine = DATEPART(weekday, D.date) <br />
&nbsp; FROM dbo.TbDates AS D <br />
&nbsp; JOIN INSERTED AS I <br />
&nbsp; &nbsp; ON D.date = I.date <br />
END <br />
GO</div></td></tr></tbody></table></div>
<p>Et pour stocker les dates, il suffira d&rsquo;exécuter la procédure stockée suivante :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">----------------------------------------------------------------------------------------- <br />
-- Nicolas SOUQUET - 10/07/2009 - Procédure de génération de tous les jours d'une année - <br />
----------------------------------------------------------------------------------------- <br />
ALTER PROCEDURE Ps_GenereDatesAnnee <br />
&nbsp; @annee SMALLINT <br />
AS <br />
BEGIN <br />
&nbsp; SET NOCOUNT ON <br />
&nbsp;<br />
&nbsp; DECLARE @dateDeb DATETIME, <br />
&nbsp; &nbsp; &nbsp; @dateFin DATETIME <br />
&nbsp;<br />
&nbsp; SELECT @dateDeb = CAST(CAST(@annee AS VARCHAR) + '0101' AS DATETIME), <br />
&nbsp; &nbsp; &nbsp; @dateFin = CAST(CAST(@annee AS VARCHAR) + '1231' AS DATETIME) <br />
&nbsp; <br />
&nbsp; ;WITH <br />
&nbsp; &nbsp; CTE_DATES AS <br />
&nbsp; &nbsp; ( <br />
&nbsp; &nbsp; &nbsp; &nbsp; SELECT @dateDeb AS Date <br />
&nbsp; &nbsp; &nbsp; UNION ALL <br />
&nbsp; &nbsp; &nbsp; &nbsp; SELECT Date + 1 <br />
&nbsp; &nbsp; &nbsp; &nbsp; FROM CTE_DATES <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHERE Date &lt;= @dateFin <br />
&nbsp; &nbsp; ) <br />
&nbsp; SELECT Date <br />
&nbsp; FROM CTE_DATES <br />
&nbsp; WHERE Date &lt;= @dateFin <br />
&nbsp; OPTION (MAXRECURSION 366) <br />
END</div></td></tr></tbody></table></div>
<p>Ainsi pour stocker tous les jours de l&rsquo;année 2009, il suffira d&rsquo;exécuter :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 10/07/2009 - &nbsp;<br />
--------------------------------- <br />
ALTER TABLE dbo.TbDates <br />
ALTER COLUMN jourDansSemaine TINYINT NULL <br />
GO <br />
INSERT INTO dbo.TbDates (date) &nbsp;<br />
EXEC dbo.Ps_GenereDatesAnnee 2009 <br />
GO <br />
ALTER TABLE dbo.TbDates <br />
ALTER COLUMN jourDansSemaine TINYINT NOT NULL <br />
GO</div></td></tr></tbody></table></div>
<p>Pour voir ce que contient la table après cette insertion :</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 10/07/2009 - &nbsp;<br />
--------------------------------- <br />
SELECT date, annee, mois, semaineDansAnnee, semaineDansMois, jourDansAnnee, jourDansMois, jourDansSemaine <br />
FROM dbo.TbDates</div></td></tr></tbody></table></div>
<p>Finalement, pour rechercher la quantité d&rsquo;eau consommée par un parc de machines pour la deuxième semaine de chaque mois de l&rsquo;année 2009, nous devrons écrire :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 10/07/2009 - &nbsp;<br />
--------------------------------- <br />
SELECT SUM(T.conso) AS consoDeuxiemeSemaine2009 <br />
FROM dbo.maTable AS T <br />
JOIN dbo.TbDates AS D <br />
&nbsp; ON T.colonneDate = D.date <br />
WHERE D.semaineDansLeMois = 2 <br />
AND D.annee = 2009</div></td></tr></tbody></table></div>
<p>Une autre exemple : rechercher la consommation moyenne d&rsquo;eau par semaine dans le mois, pour toute l&rsquo;année 2009 :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 10/07/2009 - &nbsp;<br />
--------------------------------- <br />
SELECT semaineDansLeMois, AVG(consoSemaine) <br />
FROM <br />
( <br />
&nbsp; SELECT D.semaineDansLeMois, SUM(T.conso) AS consoSemaine <br />
&nbsp; FROM dbo.maTable AS T &nbsp;<br />
&nbsp; JOIN dbo.TbDates AS D &nbsp;<br />
&nbsp; &nbsp; ON T.colonneDate = D.date &nbsp;<br />
&nbsp; WHERE D.annee = 2009 <br />
&nbsp; GROUP BY D.semaineDansLeMois <br />
) AS SUB (semaineDansLeMois, consoSemaine) <br />
GROUP BY semaineDansLeMois</div></td></tr></tbody></table></div>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Détecter un caractère unicode dans une chaîne de caractères</title>
		<link>https://blog.developpez.com/elsuket/p7886/snippets/detecter_un_caractere_unicode_dans_une_c</link>
		<comments>https://blog.developpez.com/elsuket/p7886/snippets/detecter_un_caractere_unicode_dans_une_c#comments</comments>
		<pubDate>Fri, 17 Jul 2009 16:30:46 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Voici une petite fonction qui permet de vérifier la présence d&#8217;un caractère qui n&#8217;est pas supporté par la norme ASCII dans une chaîne de caractères &#8230; 12345678910111213141516171819202122232425262728293031--------------------------------- -- Nicolas SOUQUET - 17/07/2009 - --------------------------------- CREATE FUNCTION Fn_CheckUnicode (@chaine NVARCHAR(MAX)) &#160; &#8230; <a href="https://blog.developpez.com/elsuket/p7886/snippets/detecter_un_caractere_unicode_dans_une_c">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Voici une petite fonction qui permet de vérifier la présence d&rsquo;un caractère qui n&rsquo;est pas supporté par la norme ASCII dans une chaîne de caractères &#8230;</p>
<p><span id="more-53"></span></p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 17/07/2009 - <br />
--------------------------------- <br />
CREATE FUNCTION Fn_CheckUnicode (@chaine NVARCHAR(MAX)) <br />
&nbsp; RETURNS BIT <br />
&nbsp; WITH SCHEMABINDING <br />
AS <br />
BEGIN <br />
&nbsp; DECLARE @i INT, <br />
&nbsp; &nbsp; &nbsp; @lgr INT, <br />
&nbsp; &nbsp; &nbsp; @caractere NCHAR(1), <br />
&nbsp; &nbsp; &nbsp; @contientCaractereUnicode BIT <br />
&nbsp;<br />
&nbsp; SELECT @i = 1, <br />
&nbsp; &nbsp; &nbsp; @lgr = LEN(@chaine), <br />
&nbsp; &nbsp; &nbsp; @contientCaractereUnicode = 0 <br />
&nbsp;<br />
&nbsp; WHILE @i &lt;= @lgr AND @contientCaractereUnicode = 0 <br />
&nbsp; BEGIN <br />
&nbsp; &nbsp; SELECT @caractere = SUBSTRING(@chaine, @i, 1) <br />
&nbsp;<br />
&nbsp; &nbsp; IF ASCII(@caractere) &lt;&gt; UNICODE(@caractere) <br />
&nbsp; &nbsp; BEGIN <br />
&nbsp; &nbsp; &nbsp; SET @contientCaractereUnicode = 1 <br />
&nbsp; &nbsp; END <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; SET @i = @i + 1 <br />
&nbsp; END <br />
&nbsp;<br />
&nbsp; RETURN @contientCaractereUnicode <br />
END</div></td></tr></tbody></table></div>
<p>Et un exemple d&rsquo;utilisation :</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 17/07/2009 - <br />
--------------------------------- <br />
SELECT dbo.Fn_CheckUnicode(N'toto')</div></td></tr></tbody></table></div>
<p>retourne 0</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 17/07/2009 - <br />
--------------------------------- <br />
SELECT dbo.Fn_CheckUnicode(N'&amp;#3626;t&amp;#3623;&amp;#3633;o&amp;#3626;t&amp;#3604;&amp;#3637;o')</div></td></tr></tbody></table></div>
<p>retourne 1.</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ajouter des contraintes à des tables déjà existantes</title>
		<link>https://blog.developpez.com/elsuket/p7900/moteur-de-base-de-donnees-sql-server/indexation/ajouter_des_contraintes_a_des_tables_dej</link>
		<comments>https://blog.developpez.com/elsuket/p7900/moteur-de-base-de-donnees-sql-server/indexation/ajouter_des_contraintes_a_des_tables_dej#comments</comments>
		<pubDate>Sat, 25 Jul 2009 14:07:42 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Indexation]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Comment peut-on ajouter des contraintes de tous types à une table qui existe déjà, mais dont on a modifié la structure ou tout simplement oublié d&#8217;ajouter la contrainte en même temps que la colonne ? Considérons le DDL des tables &#8230; <a href="https://blog.developpez.com/elsuket/p7900/moteur-de-base-de-donnees-sql-server/indexation/ajouter_des_contraintes_a_des_tables_dej">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Comment peut-on ajouter des contraintes de tous types à une table qui existe déjà, mais dont on a modifié la structure ou tout simplement oublié d&rsquo;ajouter la contrainte en même temps que la colonne ?</p>
<p><span id="more-7"></span></p>
<p>Considérons le DDL des tables suivantes :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 25/07/2009 - <br />
--------------------------------- <br />
CREATE TABLE TbPays <br />
( <br />
&nbsp; IDPays INT IDENTITY, <br />
&nbsp; nomPays VARCHAR(30), <br />
&nbsp; prefixeTelephonique VARCHAR(4), <br />
&nbsp; longueurNumeroTelephone TINYINT <br />
) <br />
&nbsp;<br />
CREATE TABLE TbClient <br />
( <br />
&nbsp; IDClient INT IDENTITY, <br />
&nbsp; nomClient VARCHAR(20), <br />
&nbsp; IDPays INT, <br />
&nbsp; numeroTelephoneClient VARCHAR(36) <br />
) <br />
&nbsp;<br />
CREATE TABLE TbAbonnement &nbsp;<br />
( <br />
&nbsp; IDAbonnement INT IDENTITY, <br />
&nbsp; referenceAbonnement CHAR(10), <br />
&nbsp; nomAbonnement VARCHAR(30), <br />
&nbsp; descriptionAbonnemement VARCHAR(255) <br />
) <br />
&nbsp;<br />
CREATE TABLE TbAbonnementClient <br />
( <br />
&nbsp; IDAbonnementClient INT IDENTITY, <br />
&nbsp; IDClient INT, <br />
&nbsp; IDAbonnement INT, <br />
&nbsp; dateAbonnementClient DATETIME <br />
)</div></td></tr></tbody></table></div>
<p>Manifestement il manque toutes les contraintes  :</p>
<p>&#8211; de valuation obligatoire (NOT NULL),<br />
&#8211; de clé primaire,<br />
&#8211; de clé étrangère,<br />
&#8211; d&rsquo;unicité,<br />
&#8211; de valeur par défaut<br />
&#8211; de domaine (CHECK)</p>
<p><strong>Le squelette d&rsquo;ajout de contraintes est le suivant :</strong></p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 25/07/2009 - <br />
---------------------------------</div></td></tr></tbody></table></div>
<p>=> contraintes de valuation obligatoire</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ALTER TABLE maTable <br />
ALTER COLUMN maColonne memeType NOT NULL</div></td></tr></tbody></table></div>
<p>=> contraintes de clé primaire</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ALTER TABLE maTable <br />
ADD CONSTRAINT PK_maTable <br />
&nbsp; PRIMARY KEY (colonnesConstituantLaCléPrimaire)</div></td></tr></tbody></table></div>
<p>=> contraintes de clé étrangère</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ALTER TABLE maTable <br />
ADD CONSTRAINT FK_maTable_colonneDeCleEtrangere <br />
&nbsp; FOREIGN KEY(colonneDeCleEtrangere) <br />
&nbsp; REFERENCES tableContenantLaClePrimaireAReferencer(colonneDeClePrimaire)</div></td></tr></tbody></table></div>
<p>=> contraintes d&rsquo;unicité</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ALTER TABLE maTable <br />
ADD CONSTRAINT UQ_maTable_colonnesConstituantLeTupleQuiDoitEtreUnique <br />
&nbsp; UNIQUE (colonnesConstituantLeTupleQuiDoitEtreUnique)</div></td></tr></tbody></table></div>
<p>=> contraintes de valeur par défaut :</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ALTER TABLE maTable <br />
ADD CONSTRAINT DF_maTable_maColonne <br />
&nbsp; DEFAULT (uneValeur) FOR (maColonne)</div></td></tr></tbody></table></div>
<p>=> contraintes de domaine (CHECK)<br />
ALTER TABLE maTable<br />
ADD CONSTRAINT CHK_maTable_maColonne<br />
	CHECK(maColonne [formuleDeVerification]</p>
<p><strong>Définissons les dans cet ordre :</strong></p>
<p>=> <strong>Contraintes de valuation obligatoire</strong> </p>
<p>Une clé primaire ne pouvant logiquement pas être NULL, nous devons définir toute les colonnes qui vont servir de clé primaire aux tables comme ne pouvant pas être NULL.<br />
Nous devons également obliger la valuation des colonnes qui sont des clés naturelles, et celles qui seront clé étrangère :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 25/07/2009 - <br />
--------------------------------- <br />
-- TbPays <br />
ALTER TABLE dbo.TbPays <br />
ALTER COLUMN IDPays INT NOT NULL <br />
&nbsp;<br />
ALTER TABLE dbo.TbPays <br />
ALTER COLUMN nomPays VARCHAR(30) NOT NULL <br />
&nbsp;<br />
ALTER TABLE dbo.TbPays <br />
ALTER COLUMN prefixeTelephonique VARCHAR(4) NOT NULL <br />
&nbsp;<br />
ALTER TABLE dbo.TbPays <br />
ALTER COLUMN longueurNumeroTelephone TINYINT NOT NULL <br />
&nbsp;<br />
-- TbClient <br />
ALTER TABLE dbo.TbClient <br />
ALTER COLUMN IDClient INT NOT NULL <br />
&nbsp;<br />
ALTER TABLE dbo.TbClient <br />
ALTER COLUMN nomClient VARCHAR(20) NOT NULL <br />
&nbsp;<br />
ALTER TABLE dbo.TbClient <br />
ALTER COLUMN IDPays INT NOT NULL <br />
&nbsp;<br />
ALTER TABLE dbo.TbClient <br />
ALTER COLUMN numeroTelephoneClient VARCHAR(36) NOT NULL <br />
&nbsp;<br />
-- TbAbonnement <br />
ALTER TABLE dbo.TbAbonnement <br />
ALTER COLUMN IDAbonnement INT NOT NULL <br />
&nbsp;<br />
ALTER TABLE dbo.TbAbonnement <br />
ALTER COLUMN referenceAbonnement NOT NULL <br />
&nbsp;<br />
ALTER TABLE dbo.TbAbonnement <br />
ALTER COLUMN nomAbonnement VARCHAR(30) NOT NULL <br />
&nbsp;<br />
-- TbAbonnementClient <br />
ALTER TABLE dbo.TbAbonnementClient <br />
ALTER COLUMN IDAbonnementClient INT NOT NULL <br />
&nbsp;<br />
ALTER TABLE dbo.TbAbonnementClient <br />
ALTER COLUMN IDClient INT NOT NULL <br />
&nbsp;<br />
ALTER TABLE dbo.TbAbonnementClient <br />
ALTER COLUMN IDAbonnement INT NOT NULL <br />
&nbsp;<br />
ALTER TABLE dbo.TbAbonnementClient <br />
ALTER COLUMN dateAbonnementClient DATETIME NOT NULL</div></td></tr></tbody></table></div>
<p>=> <strong>Contraintes de clé primaire</strong></p>
<div class="codecolorer-container text 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 />17<br />18<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 25/07/2009 - <br />
--------------------------------- <br />
-- TbPays <br />
ALTER TABLE dbo.TbPays <br />
ADD CONSTRAINT PK_TbPays_IDPays PRIMARY KEY (IDPays) <br />
&nbsp;<br />
-- TbClient <br />
ALTER TABLE dbo.TbClient <br />
ADD CONSTRAINT PK_TbClient_IDClient PRIMARY KEY(IDClient) <br />
&nbsp;<br />
-- TbAbonnement <br />
ALTER TABLE dbo.TbAbonnement <br />
ADD CONSTRAINT PK_TbAbonnement_IDAbonnement PRIMARY KEY (IDAbonnement) <br />
&nbsp;<br />
-- TbAbonnementClient <br />
ALTER TABLE dbo.TbAbonnementClient <br />
ADD CONSTRAINT PK_TbAbonnementClient_IDAbonnementClient PRIMARY KEY (IDAbonnementClient)</div></td></tr></tbody></table></div>
<p>=> <strong>Contraintes de clé étrangère</strong></p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 25/07/2009 - <br />
--------------------------------- <br />
-- TbClient <br />
ALTER TABLE dbo.TbClient <br />
ADD CONSTRAINT FK_TbClient_IDPays <br />
&nbsp; FOREIGN KEY (IDPays) REFERENCES dbo.TbPays(IDPays) <br />
&nbsp;<br />
-- TbAbonnementClient <br />
ALTER TABLE dbo.TbAbonnementClient <br />
ADD CONSTRAINT FK_TbAbonnementClient_IDClient <br />
&nbsp; FOREIGN KEY (IDClient) REFERENCES dbo.TbClient(IDClient), <br />
&nbsp; ---- <br />
&nbsp; CONSTRAINT FK_TbAbonnementClient_IDAbonnement <br />
&nbsp; FOREIGN KEY (IDAbonnement) REFERENCES dbo.TbAbonnement(IDAbonnement)</div></td></tr></tbody></table></div>
<p>=> <strong>Contraintes d&rsquo;unicité</strong></p>
<p>Elles permettent de s&rsquo;assurer qu&rsquo;aucune valeur ne sera pas insérée en double dans des colonnes qui ne constituent pas la clé primaire.<br />
Bien qu&rsquo;une clé primaire permette également d&rsquo;assurer l&rsquo;unicité, on ne peut bien sûr en définir qu&rsquo;une seule par table, pour qu&rsquo;il soit possible de la référencer par des clés étrangères dans d&rsquo;autres tables.<br />
Au contraire, on peut définir plusieurs contraintes d&rsquo;unicité par table.</p>
<p>Similairement, des indexes sont créés implicitement lors de la création d&rsquo;une clé primaire ou d&rsquo;une contrainte d&rsquo;unicité.<br />
Mais seul l&rsquo;index de clé primaire est toujours cluster : l&rsquo;index sous-jacent à une contrainte d&rsquo;unicité est cluster si la table n&rsquo;a pas de clé primaire définie, et non-cluster à l&rsquo;inverse.<br />
Comme pour une clé primaire, il est nécessaire de mettre la colonne qui a la sélectivité la plus élevée en premier dans la définition de la contrainte d&rsquo;unicité, car cet ordre sert dans la définition de l&rsquo;index.<br />
Comme les recherches à travers les indexes se font suivant la valeur de la première clé, l&rsquo;index sera d&rsquo;autant plus efficace que la valeur de sa première clé sera sélective.</p>
<div class="codecolorer-container text 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 />17<br />18<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 25/07/2009 - <br />
--------------------------------- <br />
-- TbClient <br />
ALTER TABLE dbo.TbClient <br />
ADD CONSTRAINT UQ_TbClient_nomClient UNIQUE (nomClient), <br />
&nbsp; CONSTRAINT UQ_TbClient_numeroTelephoneClient UNIQUE (numeroTelephoneClient) <br />
&nbsp;<br />
-- TbAbonnement <br />
ALTER TABLE dbo.TbAbonnement <br />
ADD CONSTRAINT UQ_TbAbonnement_nomAbonnement UNIQUE (nomAbonnement) <br />
&nbsp;<br />
ALTER TABLE dbo.TbAbonnement <br />
ADD CONSTRAINT UQ_TbAbonnement_referenceAbonnement UNIQUE (referenceAbonnement) <br />
&nbsp;<br />
-- TbAbonnementClient <br />
ALTER TABLE dbo.TbAbonnementClient <br />
ADD CONSTRAINT UQ_TbAbonnementClient_IDClient_IDAbonnement UNIQUE(IDClient, IDAbonnement)</div></td></tr></tbody></table></div>
<p>=> <strong>Contraintes de valeur par défaut :</strong></p>
<p>Il peut être intéressant de définir une valeur par défaut pour l&rsquo;insertion (mais pas à l&rsquo;UPDATE) d&rsquo;une nouvelle ligne à une table si la colonne est omise dans la spécification de l&rsquo;instruction INSERT :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 25/07/2009 - <br />
--------------------------------- <br />
ALTER TABLE dbo.TbAbonnementClient <br />
ADD CONSTRAINT DF_TbAbonnementClient_dateAbonnementClient <br />
&nbsp; DEFAULT (GETDATE()) FOR dateAbonnementClient</div></td></tr></tbody></table></div>
<p>Ainsi si nous écrivons :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 25/07/2009 - <br />
--------------------------------- <br />
INSERT INTO dbo.TbAbonnementClient <br />
( <br />
&nbsp; IDClient, <br />
&nbsp; IDAbonnement <br />
) <br />
VALUES <br />
( <br />
&nbsp; @IDClient, <br />
&nbsp; @IDAbonnement <br />
)</div></td></tr></tbody></table></div>
<p>la colonne <em>dateAbonnementClient</em> sera valuée à la date actuelle du système.</p>
<p>=> <strong>Contraintes de domaine</strong></p>
<p>Elles permettent de limiter ou de vérifier les valeurs que peuvent contenir une colonne.<br />
Elles nécessitent une formule de vérification, qui peut être spécifiée directement lors de la définition de la contrainte, ou bien à travers une fonction définie par l&rsquo;utilisateur.<br />
L&rsquo;utilisation d&rsquo;une fonction pour la définition d&rsquo;une contrainte de domaine permet bien souvent de ne pas avoir recours à un déclencheur pour vérifier la validité des données.</p>
<p>Ici, nous souhaitons vérifier que les numéros de téléphone que nous stockons dans la base de données contiennent le préfixe du pays du client, et que la longueur des numéros de téléphone est correcte.<br />
Pour cela nous créons la fonction suivante :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 25/07/2009 - <br />
--------------------------------- <br />
CREATE FUNCTION Fn_IsNumTelClient_Correct <br />
&nbsp; ( <br />
&nbsp; &nbsp; @IDPays INT, <br />
&nbsp; &nbsp; @numeroTelephoneClient VARCHAR(36) <br />
&nbsp; ) <br />
RETURNS BIT <br />
WITH SCHEMABINDING <br />
AS <br />
BEGIN <br />
&nbsp; RETURN <br />
&nbsp; ( <br />
&nbsp; &nbsp; SELECT CASE <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN @numeroTelephoneClient LIKE prefixeTelephonique + '%' <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AND LEN(@numeroTelephoneClient) = longueurNumeroTelephone <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;THEN CAST(1 AS BIT) <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ELSE CAST(0 AS BIT) <br />
&nbsp; &nbsp; &nbsp; &nbsp; END <br />
&nbsp; &nbsp; FROM dbo.TbPays <br />
&nbsp; &nbsp; WHERE IDPays = @IDPays <br />
&nbsp; ) <br />
END</div></td></tr></tbody></table></div>
<p>La contrainte de domaine sera alors définie comme suit :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 25/07/2009 - <br />
--------------------------------- <br />
ALTER TABLE dbo.TbAbonnementClient <br />
ADD CONSTRAINT CHK_TbAbonnementClient_numeroTelephoneClient <br />
&nbsp; CHECK(dbo.Fn_IsNumTelClient_Correct(IDPays, numeroTelephoneClient) = 1)</div></td></tr></tbody></table></div>
<p>Nous souhaitons également vérifier que les valeurs de la colonne <em>referenceAbonnement</em> de la table <em>TbAbonnement</em> aient le format XXX-nnn-nn.<br />
Là nous pouvons définir celui-ci directement  dans la spécification de la contrainte :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 25/07/2009 - <br />
--------------------------------- <br />
ALTER TABLE dbo.TbAbonnement <br />
ADD CONSTRAINT CHK_TbAbonnement_referenceAbonnement <br />
&nbsp; CHECK(referenceAbonnement LIKE '[A-Z][A-Z][A-Z]-[0-9][0-9][0-9]-[0-9][0-9]')</div></td></tr></tbody></table></div>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Une fonction d&#8217;extraction des nombres contenus dans une chaîne de caractères : les fonctions ISNUMERIC() et ASCII()</title>
		<link>https://blog.developpez.com/elsuket/p7726/snippets/une_fonction_d_extraction_des_nombres_co</link>
		<comments>https://blog.developpez.com/elsuket/p7726/snippets/une_fonction_d_extraction_des_nombres_co#comments</comments>
		<pubDate>Tue, 09 Jun 2009 22:16:05 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Voici une petite fonction qui vous permettra d&#8217;extraire les nombres contenus dans une chaîne de caractères, et de les retourner comme un entier&#8230; 1234567891011121314151617181920212223242526272829303132333435363738--------------------------------- -- Nicolas SOUQUET - 09/06/2009 - --------------------------------- CREATE FUNCTION dbo.FnTrouveChiffres(@string VARCHAR(1024)) &#160; RETURNS INT &#160; WITH &#8230; <a href="https://blog.developpez.com/elsuket/p7726/snippets/une_fonction_d_extraction_des_nombres_co">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Voici une petite fonction qui vous permettra d&rsquo;extraire les nombres contenus dans une chaîne de caractères, et de les retourner comme un entier&#8230;</p>
<p><span id="more-45"></span></p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 09/06/2009 - <br />
--------------------------------- <br />
CREATE FUNCTION dbo.FnTrouveChiffres(@string VARCHAR(1024)) <br />
&nbsp; RETURNS INT <br />
&nbsp; WITH SCHEMABINDING <br />
AS <br />
BEGIN <br />
&nbsp; DECLARE @result VARCHAR(1024) <br />
&nbsp;<br />
&nbsp; IF ISNUMERIC(@string) = 1 <br />
&nbsp; BEGIN <br />
&nbsp; &nbsp; SET @result = @string <br />
&nbsp; END <br />
&nbsp; ELSE <br />
&nbsp; BEGIN <br />
&nbsp; &nbsp; DECLARE @i INT, <br />
&nbsp; &nbsp; &nbsp; &nbsp; @Lgr INT, <br />
&nbsp; &nbsp; &nbsp; &nbsp; @char CHAR(1) <br />
&nbsp;<br />
&nbsp; &nbsp; SELECT @i = 1, <br />
&nbsp; &nbsp; &nbsp; &nbsp; @Lgr = LEN(@string), <br />
&nbsp; &nbsp; &nbsp; &nbsp; @result = '' <br />
&nbsp;<br />
&nbsp; &nbsp; WHILE @i &lt;= @Lgr <br />
&nbsp; &nbsp; BEGIN <br />
&nbsp; &nbsp; &nbsp; SELECT @char = SUBSTRING(@string, @i, 1) <br />
&nbsp;<br />
&nbsp; &nbsp; &nbsp; IF ISNUMERIC(@char) = 1 <br />
&nbsp; &nbsp; &nbsp; BEGIN <br />
&nbsp; &nbsp; &nbsp; &nbsp; SET @result = @result + @char <br />
&nbsp; &nbsp; &nbsp; END <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; SET @i = @i + 1 <br />
&nbsp; &nbsp; END <br />
&nbsp; END <br />
&nbsp; RETURN CAST(@result AS INT) <br />
END</div></td></tr></tbody></table></div>
<p>Vous pouvez utiliser cette fonction si vous êtes certain que votre chaîne ne contiendra jamais les symboles arithmétiques &laquo;&nbsp;+&nbsp;&raquo; et &laquo;&nbsp;-&laquo;&nbsp;.<br />
En effet, la fonction ISNUMERIC() retourne 1 (vrai) pour ces deux symboles.</p>
<p>Il existe une autre solution avec la fonction ASCII() :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 09/06/2009 - <br />
--------------------------------- <br />
ALTER FUNCTION [dbo].[FnTrouveChiffres](@string VARCHAR(1024)) <br />
&nbsp; RETURNS INT <br />
&nbsp; WITH SCHEMABINDING <br />
AS <br />
BEGIN <br />
&nbsp; DECLARE @result VARCHAR(1024) <br />
&nbsp;<br />
&nbsp; DECLARE @i INT, <br />
&nbsp; &nbsp; &nbsp; @Lgr INT, <br />
&nbsp; &nbsp; &nbsp; @char CHAR(1) <br />
&nbsp;<br />
&nbsp; SELECT @i = 1, <br />
&nbsp; &nbsp; &nbsp; @Lgr = LEN(@string), <br />
&nbsp; &nbsp; &nbsp; @result = '' <br />
&nbsp;<br />
&nbsp; WHILE @i &lt;= @Lgr <br />
&nbsp; BEGIN <br />
&nbsp; &nbsp; SELECT @char = SUBSTRING(@string, @i, 1) <br />
&nbsp;<br />
&nbsp; &nbsp; IF ASCII(@char) BETWEEN 48 AND 57 <br />
&nbsp; &nbsp; BEGIN <br />
&nbsp; &nbsp; &nbsp; SET @result = @result + @char <br />
&nbsp; &nbsp; END <br />
&nbsp; <br />
&nbsp; &nbsp; SET @i = @i + 1 <br />
&nbsp; END <br />
&nbsp;<br />
&nbsp; RETURN CAST(@result AS INT) <br />
END</div></td></tr></tbody></table></div>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Stocker simplement tout types de fichiers dans une table en utilisant le type de données VARBINARY et les BLOBs</title>
		<link>https://blog.developpez.com/elsuket/p7799/snippets/stocker_simplement_tout_types_de_fichier</link>
		<comments>https://blog.developpez.com/elsuket/p7799/snippets/stocker_simplement_tout_types_de_fichier#comments</comments>
		<pubDate>Thu, 25 Jun 2009 23:26:28 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Voici comment copier le contenu d&#8217;un fichier contenu sur un disque dur pour le stocker dans une table, à l&#8217;aide de la commande OPENROWSET et de son option BULK &#8230; Supposons que nous avons la table suivante, dans laquelle nous &#8230; <a href="https://blog.developpez.com/elsuket/p7799/snippets/stocker_simplement_tout_types_de_fichier">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Voici comment copier le contenu d&rsquo;un fichier contenu sur un disque dur pour le stocker dans une table, à l&rsquo;aide de la commande OPENROWSET et de son option BULK &#8230;</p>
<p><span id="more-47"></span></p>
<p>Supposons que nous avons la table suivante, dans laquelle nous stockons des icônes :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 26/06/2009 - <br />
--------------------------------- <br />
CREATE TABLE dbo.TbIcone <br />
( <br />
&nbsp; IDIcone INT IDENTITY CONSTRAINT PK_TbIcone PRIMARY KEY, <br />
&nbsp; nomIcone VARCHAR(255) NOT NULL, <br />
&nbsp; icone VARBINARY(MAX) <br />
)</div></td></tr></tbody></table></div>
<p>Et voici la procédure stockée qui va permettre de stocker des icônes stockées sur un des disques dur, simplement en passant son UNC en paramètre :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 26/06/2009 - <br />
--------------------------------- <br />
CREATE PROCEDURE PsStockeIcone <br />
&nbsp; @cheminDuFichier VARCHAR(255) <br />
AS <br />
BEGIN <br />
&nbsp; SET NOCOUNT ON <br />
&nbsp;<br />
&nbsp; DECLARE @SQL NVARCHAR(256), <br />
&nbsp; &nbsp; &nbsp; @ParamDefinition NVARCHAR(64), <br />
&nbsp; &nbsp; &nbsp; @nomDeFichier VARCHAR(64) <br />
&nbsp;<br />
&nbsp; SELECT @SQL = N'SELECT @fluxOUT = BulkColumn FROM OPENROWSET(BULK N''' + @cheminDuFichier + ''', SINGLE_BLOB) AS TMP', <br />
&nbsp; &nbsp; &nbsp; @ParamDefinition = N'@fluxOUT VARBINARY(MAX) OUTPUT', <br />
&nbsp; &nbsp; &nbsp; @nomDeFichier = RIGHT(@cheminDuFichier, CHARINDEX('\', REVERSE(@cheminDuFichier)) - 1) <br />
&nbsp;<br />
&nbsp; -- Récupère le fichier sous la forme d'une valeur binaire <br />
&nbsp; DECLARE @fluxFichier VARBINARY(MAX) <br />
&nbsp;<br />
&nbsp; EXEC sp_executesql <br />
&nbsp; &nbsp; &nbsp; @SQL, <br />
&nbsp; &nbsp; &nbsp; @ParamDefinition, <br />
&nbsp; &nbsp; &nbsp; @fluxFichier OUTPUT <br />
&nbsp;<br />
&nbsp; -- Stocke le flux dans la table <br />
&nbsp; INSERT INTO dbo.TbIcone <br />
&nbsp; ( <br />
&nbsp; &nbsp; nomIcone, <br />
&nbsp; &nbsp; icone <br />
&nbsp; ) <br />
&nbsp; VALUES <br />
&nbsp; ( <br />
&nbsp; &nbsp; @nomDeFichier, <br />
&nbsp; &nbsp; @fluxFichier <br />
&nbsp; ) <br />
END</div></td></tr></tbody></table></div>
<p>Je souhaite stocker l&rsquo;image suivante (C:\SQLServer2008Dark.jpg) :</p>
<p><img src="http://blog.developpez.com/media/SQLServer2008Dark_01.jpg" width="185" height="48" alt="" /></p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 26/06/2009 - <br />
--------------------------------- <br />
EXEC dbo.PsStockeIcone 'C:\SQLServer2008Dark.jpg' <br />
GO <br />
&nbsp;<br />
SELECT IDIcone, nomIcone, icone <br />
FROM dbo.TbIcone</div></td></tr></tbody></table></div>
<p><img src="http://blog.developpez.com/media/icone_01.PNG" width="536" height="204" alt="" /></p>
<p>Notons que cette procédure fonctionne dès SQL Server 2005.</p>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exporter le code de vues, procédures stockées, triggers, fonctions SQL vers un fichier</title>
		<link>https://blog.developpez.com/elsuket/p7798/snippets/exporter_le_code_de_vues_procedures_stoc</link>
		<comments>https://blog.developpez.com/elsuket/p7798/snippets/exporter_le_code_de_vues_procedures_stoc#comments</comments>
		<pubDate>Thu, 25 Jun 2009 21:59:43 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Utilitaires]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Pour effectuer une revue de code ou pour gérer des versions de code en phase de développement, il peut être intéressant d&#8217;exporter les codes des modules SQL vers un fichier. Voyons comment réaliser cela, à l&#8217;aide de l&#8217;utilitaire en ligne &#8230; <a href="https://blog.developpez.com/elsuket/p7798/snippets/exporter_le_code_de_vues_procedures_stoc">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Pour effectuer une revue de code ou pour gérer des versions de code en phase de développement, il peut être intéressant d&rsquo;exporter les codes des modules SQL vers un fichier.<br />
Voyons comment réaliser cela, à l&rsquo;aide de l&rsquo;utilitaire en ligne de commande BCP &#8230;</p>
<p><span id="more-46"></span></p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">-------------------------------------------------------------------------------------- <br />
-- Nicolas SOUQUET - 25/06/2009 - Exporter le code de modules dans un fichier unique - <br />
-------------------------------------------------------------------------------------- <br />
SELECT SQL.definition + CHAR(10) + CHAR(13) + 'GO' AS CodeModule <br />
INTO ##MODULES <br />
FROM sys.objects AS OBJETS <br />
JOIN sys.sql_modules AS SQL ON SQL.object_id = OBJETS.object_id <br />
WHERE OBJETS.type IN ('P', 'V', 'TR', 'FN', 'IF', 'TF') <br />
AND OBJETS.name NOT LIKE 'dt%' AND OBJETS.name NOT LIKE 'sp%' <br />
ORDER BY OBJETS.object_id <br />
&nbsp;<br />
DECLARE @szBCP VARCHAR(256); <br />
SET @szBCP = 'bcp &quot;SELECT CodeModule FROM ##MODULES&quot; queryout C:\ModulesSQL.sql -T -c'; <br />
EXEC master.dbo.xp_cmdshell @szBCP; <br />
&nbsp;<br />
DROP TABLE ##MODULES; <br />
&nbsp;<br />
---------------------------------------------------------------------------------------------------- <br />
-- Nicolas SOUQUET - 25/06/2009 - Exporter le code de plusieurs modules avec un fichier par module - <br />
---------------------------------------------------------------------------------------------------- <br />
--&gt;&gt;&gt;&gt; A EXECUTER DANS LE CONTEXTE DE LA BD DEPUIS LAQUELLE ON SOUHAITE RÉALISER L'EXPORT <br />
SET NOCOUNT ON <br />
&nbsp;<br />
DECLARE CUR_MODULES CURSOR FOR <br />
&nbsp; SELECT name <br />
&nbsp; FROM sys.objects <br />
&nbsp; WHERE type IN ('P', 'V', 'TR', 'FN', 'IF', 'TF') <br />
&nbsp; -- AND name LIKE '%blablabla%' <br />
FOR READ ONLY <br />
&nbsp;<br />
DECLARE @szNomModule SYSNAME <br />
OPEN CUR_MODULES <br />
FETCH NEXT FROM CUR_MODULES INTO @szNomModule <br />
WHILE @@FETCH_STATUS = 0 <br />
BEGIN <br />
&nbsp; SELECT definition <br />
&nbsp; INTO ##MODULE_TEXT <br />
&nbsp; FROM sys.sql_modules <br />
&nbsp; WHERE OBJECT_NAME(object_id) = @szNomModule <br />
&nbsp;<br />
&nbsp; DECLARE @szBCP VARCHAR(512); <br />
&nbsp; SET @szBCP = 'bcp &quot;SELECT definition FROM ##PROC_TEXT&quot; queryout C:\' + @szNomModule + '.sql -T -c'; <br />
&nbsp; EXEC master.dbo.xp_cmdshell @szBCP, NO_OUTPUT <br />
&nbsp;<br />
&nbsp; DROP TABLE ##MODULE_TEXT; <br />
&nbsp; FETCH NEXT FROM CUR_MODULES INTO @szNomModule <br />
END <br />
DEALLOCATE CUR_MODULES</div></td></tr></tbody></table></div>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rechercher une chaîne de caractère dans le code des modules SQL sous SQL Server 2005 et 2008</title>
		<link>https://blog.developpez.com/elsuket/p7612/snippets/rechercher_une_chaine_de_caractere_dans_2005</link>
		<comments>https://blog.developpez.com/elsuket/p7612/snippets/rechercher_une_chaine_de_caractere_dans_2005#comments</comments>
		<pubDate>Mon, 18 May 2009 17:11:35 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Il arrive parfois de vouloir chercher un bout de code dans toutes les procédures stockées d&#8217;une base de données, comme par exemple le nom d&#8217;une colonne. Voyons comment réaliser cela avec une simple requête &#8230; 1234567891011121314151617181920212223242526--------------------------------- -- Nicolas SOUQUET - &#8230; <a href="https://blog.developpez.com/elsuket/p7612/snippets/rechercher_une_chaine_de_caractere_dans_2005">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Il arrive parfois de vouloir chercher un bout de code dans toutes les procédures stockées d&rsquo;une base de données, comme par exemple le nom d&rsquo;une colonne.</p>
<p>Voyons comment réaliser cela avec une simple requête &#8230;</p>
<p><span id="more-43"></span></p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 18/05/2009 - <br />
--------------------------------- <br />
/* <br />
Valable pour les objets dont le type est : <br />
- Procédure stockée SQL (P) <br />
- Procédure de filtre de réplication (RF) <br />
- Vues (V) <br />
- Déclencheur DML SQL (TR) <br />
- Fonction scalaire (FN) <br />
- Fonction table incluse SQL (IF) <br />
- Fonction table SQL (TF) <br />
- Règle (R) <br />
*/ <br />
SELECT O.type_desc, <br />
&nbsp; &nbsp; O.name, <br />
&nbsp; &nbsp; O.create_date, <br />
&nbsp; &nbsp; O.modify_date, <br />
&nbsp; &nbsp; M.Definition <br />
FROM sys.objects O <br />
JOIN sys.sql_modules M ON M.object_id = O.object_id <br />
WHERE O.name NOT LIKE 'dt%' <br />
AND O.name NOT LIKE 'sys%' <br />
--AND O.type = 'P' -- Filtre sur le type d'objet <br />
AND M.Definition LIKE '%codeARechercher%' <br />
ORDER BY O.Name</div></td></tr></tbody></table></div>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Une seule instruction DROP pour supprimer plusieurs objets de base de données de même type</title>
		<link>https://blog.developpez.com/elsuket/p7721/snippets/une_seule_instruction_drop_pour_supprime</link>
		<comments>https://blog.developpez.com/elsuket/p7721/snippets/une_seule_instruction_drop_pour_supprime#comments</comments>
		<pubDate>Mon, 08 Jun 2009 22:05:00 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Voici comment, dans la même instruction, supprimer plusieurs procédures stockées ou tables &#8230; Supposons que nous voulons supprimer quatre tables dont les noms sont : &#8211; Client &#8211; Client_Commande &#8211; Client_Livraison &#8211; Livraison On peut écrire : 1234567891011--------------------------------- -- Nicolas &#8230; <a href="https://blog.developpez.com/elsuket/p7721/snippets/une_seule_instruction_drop_pour_supprime">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Voici comment, dans la même instruction, supprimer plusieurs procédures stockées ou tables &#8230;</p>
<p><span id="more-4"></span></p>
<p>Supposons que nous voulons supprimer quatre tables dont les noms sont :</p>
<p>&#8211; Client<br />
&#8211; Client_Commande<br />
&#8211; Client_Livraison<br />
&#8211; Livraison</p>
<p>On peut écrire :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 08/06/2009 - <br />
--------------------------------- <br />
DROP TABLE dbo.Client <br />
GO <br />
DROP TABLE dbo.Client_Commande <br />
GO <br />
DROP TABLE dbo.Client_Livraison <br />
GO <br />
DROP TABLE dbo.Livraison <br />
GO</div></td></tr></tbody></table></div>
<p>Et dès lors si l&rsquo;on veut supprimer un jeu de procédures stockées correspondant à certains critères, on se voir obligé d&rsquo;écrire :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 08/06/2009 - <br />
--------------------------------- <br />
DECLARE curProcedures CURSOR FOR <br />
&nbsp; SELECT name <br />
&nbsp; FROM sys.procedures <br />
&nbsp; WHERE name LIKE '%client%' <br />
&nbsp; OR name LIKE '%livraison%' <br />
FOR READ ONLY <br />
&nbsp;<br />
DECLARE @nomProc SYSNAME, <br />
&nbsp; &nbsp; @SQL VARCHAR(128) <br />
&nbsp;<br />
OPEN curProcedures <br />
FETCH NEXT FROM curProcedures INTO @nomProc <br />
WHILE @@FETCH_STATUS = 0 <br />
BEGIN <br />
&nbsp; SET @SQL = 'DROP PROCEDURE dbo.' + @nomProc <br />
&nbsp; <br />
&nbsp; PRINT @nomProc &nbsp;<br />
&nbsp; EXEC (@SQL) <br />
&nbsp;<br />
&nbsp; FETCH NEXT FROM curProcedures INTO @nomProc <br />
END <br />
&nbsp;<br />
DEALLOCATE curProcedures</div></td></tr></tbody></table></div>
<p>Mais la syntaxe suivante est aussi valable :</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 08/06/2009 - <br />
--------------------------------- <br />
DROP TABLE dbo.Client, dbo.Client_Commande, dbo.Client_Livraison, dbo.Livraison</div></td></tr></tbody></table></div>
<p>Et on peut alors écrire :</p>
<div class="codecolorer-container text 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 />17<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 08/06/2009 - <br />
--------------------------------- <br />
DECLARE @SQL NVARCHAR(2048), <br />
&nbsp; &nbsp; @separateur NCHAR(6) <br />
&nbsp;<br />
SET @separateur = N', dbo.' <br />
&nbsp;<br />
SELECT @SQL = ISNULL(@SQL, '') + name + @separateur <br />
FROM sys.procedures <br />
WHERE name LIKE '%client%' <br />
OR name LIKE '%livraison%' <br />
&nbsp;<br />
SELECT @SQL = 'DROP PROCEDURE ' + LEFT(@SQL, LEN(@SQL) - LEN(@separateur)) <br />
&nbsp;<br />
PRINT @SQL <br />
EXEC (@SQL)</div></td></tr></tbody></table></div>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Concaténer les valeurs d&#8217;une colonne dans une variable</title>
		<link>https://blog.developpez.com/elsuket/p7460/snippets/concatener_les_valeurs_d_une_colonne_dan</link>
		<comments>https://blog.developpez.com/elsuket/p7460/snippets/concatener_les_valeurs_d_une_colonne_dan#comments</comments>
		<pubDate>Tue, 07 Apr 2009 21:11:22 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Il est souvent demandé s&#8217;il est possible de concaténer les valeurs d&#8217;une colonne dans une variable, éventuellement en les séparant avec un symbole de ponctuation. Voici comment faire, sans curseur ni expression de table commune &#8230; Supposons la table suivante &#8230; <a href="https://blog.developpez.com/elsuket/p7460/snippets/concatener_les_valeurs_d_une_colonne_dan">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Il est souvent demandé s&rsquo;il est possible de concaténer les valeurs d&rsquo;une colonne dans une variable, éventuellement en les séparant avec un symbole de ponctuation.<br />
Voici comment faire, sans curseur ni expression de table commune &#8230;<br />
<span id="more-40"></span><br />
Supposons la table suivante :</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">CREATE TABLE TABLE_TEST <br />
( <br />
&nbsp; Lettre CHAR(1) <br />
) <br />
GO</div></td></tr></tbody></table></div>
<p>Et le jeu de données suivant :</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">INSERT INTO dbo.TABLE_TEST (Lettre) VALUES ('A') <br />
INSERT INTO dbo.TABLE_TEST (Lettre) VALUES ('B') <br />
INSERT INTO dbo.TABLE_TEST (Lettre) VALUES ('C') <br />
INSERT INTO dbo.TABLE_TEST (Lettre) VALUES ('D') <br />
GO</div></td></tr></tbody></table></div>
<p>Si l&rsquo;on souhaite concaténer les valeurs de la colonne <em>Lettre</em> dans une variable de type chaîne de caractère, avec par exemple une virgule comme séparateur, il suffit d&rsquo;écrire :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 07/04/2009 - <br />
--------------------------------- <br />
DECLARE @concat VARCHAR(128) <br />
SELECT @concat = ISNULL(@concat, '') + Lettre + ',' <br />
FROM dbo.TABLE_TEST</div></td></tr></tbody></table></div>
<p>Et l&rsquo;on obtient ainsi :</p>
<blockquote><p>A, B, C, D,</p></blockquote>
<p>Pour aller plus <a href="http://blog.developpez.com/elsuket/p7311/t-sql/aggreger-des-donnees-non-numeriques-d-un/">loin</a>, mais cette fois avec une <a href="http://blog.developpez.com/elsuket/p6541/snippets/snippets-utilite-des-cte-recursives/">expression de table commune</a></p>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Comparaison des temps d&#8217;exécution entre un curseur et une requête SQL</title>
		<link>https://blog.developpez.com/elsuket/p7445/moteur-de-base-de-donnees-sql-server/comparaison_des_temps_d_execution_entre</link>
		<comments>https://blog.developpez.com/elsuket/p7445/moteur-de-base-de-donnees-sql-server/comparaison_des_temps_d_execution_entre#comments</comments>
		<pubDate>Sun, 05 Apr 2009 14:40:48 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Moteur de base de données SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Je vois souvent sur le forum SQL Server l&#8217;utilisation de curseurs alors qu&#8217;une vraie requête SQL pourrait le remplacer. Outre le fait que les curseurs datent de COBOL (1956 &#8230;), et que leur spécification en COBOL et T-SQL est similaire &#8230; <a href="https://blog.developpez.com/elsuket/p7445/moteur-de-base-de-donnees-sql-server/comparaison_des_temps_d_execution_entre">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Je vois souvent sur le forum <a href="http://www.developpez.net/forums/f49/bases-donnees/ms-sql-server/">SQL Server</a> l&rsquo;utilisation de curseurs alors qu&rsquo;une vraie requête SQL pourrait le remplacer.<br />
Outre le fait que les curseurs datent de COBOL (1956 &#8230;), et que leur spécification en COBOL et T-SQL est similaire à s&rsquo;y méprendre, comparons donc les temps d&rsquo;exécution entre un curseur et une requête sur un traitement très simple &#8230;</p>
<p><span id="more-74"></span></p>
<p>Ce test a été réalisé sur mon ordinateur personnel, dont voici la configuration matérielle :</p>
<p>&#8211; Windows XP SP3<br />
&#8211; 2 Go de RAM<br />
&#8211; CPU Intel Core 2 Duo à 2.13 GHz</p>
<p>Voici les requêtes :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 05/04/2009 - <br />
--------------------------------- <br />
SET NOCOUNT ON <br />
GO <br />
&nbsp;<br />
CREATE TABLE BASE_DATA <br />
( <br />
&nbsp; ID INT IDENTITY CONSTRAINT PK_BASE_DATA PRIMARY KEY, <br />
&nbsp; Nombre INT CONSTRAINT DF_BASE_DATA_Nombre DEFAULT 0 <br />
) <br />
GO <br />
&nbsp;<br />
INSERT INTO dbo.BASE_DATA VALUES (DEFAULT) <br />
GO 10</div></td></tr></tbody></table></div>
<p>Le contenu de la table BASE_DATA après ces deux requêtes est le suivant :</p>
<blockquote><pre>ID          Nombre
----------- -----------
1           0
2           0
3           0
4           0
5           0
6           0
7           0
8           0
9           0
10          0</pre>
</blockquote>
<p>Voici la requête avec le curseur :</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 05/04/2009 - <br />
--------------------------------- <br />
DECLARE @dateDeb DATETIME <br />
DECLARE @dateFin DATETIME <br />
DECLARE @ID INT <br />
&nbsp;<br />
SET @dateDeb = GETDATE() <br />
&nbsp;<br />
DECLARE cur CURSOR FOR <br />
&nbsp; SELECT ID <br />
&nbsp; FROM dbo.BASE_DATA <br />
FOR UPDATE OF Nombre <br />
&nbsp;<br />
OPEN cur <br />
FETCH NEXT FROM cur INTO @ID <br />
WHILE @@FETCH_STATUS = 0 <br />
BEGIN <br />
&nbsp; UPDATE dbo.BASE_DATA <br />
&nbsp; SET Nombre = 10 * @ID <br />
&nbsp; WHERE ID = @ID <br />
&nbsp;<br />
&nbsp; FETCH NEXT FROM cur INTO @ID <br />
END <br />
SET @dateFin = GETDATE() <br />
DEALLOCATE cur <br />
&nbsp;<br />
SELECT DATEDIFF(millisecond, @dateDeb, @dateFin)</div></td></tr></tbody></table></div>
<p>Et la requête avec UPDATE :</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">UPDATE BASE_DATA <br />
SET Nombre = 10 * ID</div></td></tr></tbody></table></div>
<p>Entre chaque exécution, j&rsquo;ai augmenté le nombre de lignes suivant les puissances de 10 :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 05/04/2009 - <br />
--------------------------------- <br />
INSERT INTO dbo.BASE_DATA VALUES (DEFAULT) <br />
GO 90 <br />
INSERT INTO dbo.BASE_DATA VALUES (DEFAULT) <br />
GO 900 <br />
INSERT INTO dbo.BASE_DATA VALUES (DEFAULT) <br />
GO 9000 <br />
INSERT INTO dbo.BASE_DATA VALUES (DEFAULT) <br />
GO 90000 <br />
INSERT INTO dbo.BASE_DATA VALUES (DEFAULT) <br />
GO 900000</div></td></tr></tbody></table></div>
<p>Entre le relevé de temps d&rsquo;exécutions pour le curseur et pour la requête, j&rsquo;ai exécuté :</p>
<p><code class="codecolorer text default"><span class="text">TRUNCATE TABLE dbo.BASE_DATA</span></code> </p>
<p>Et voici le résultat :</p>
<p><img src="http://blog.developpez.com/media/cur_01.PNG" width="1071" height="737" alt="" /></p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Aggréger des données non numériques d&#8217;une entité par une liste de valeurs</title>
		<link>https://blog.developpez.com/elsuket/p7311/snippets/aggreger_des_donnees_non_numeriques_d_un</link>
		<comments>https://blog.developpez.com/elsuket/p7311/snippets/aggreger_des_donnees_non_numeriques_d_un#comments</comments>
		<pubDate>Wed, 04 Mar 2009 22:26:03 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Il est souvent demandé sur le forum comment on peut créer une liste de valeurs stockées dans une colonne en une liste. Cela est en effet souvent demandé par le client pour des rapports, mais il n&#8217;est pas évident de &#8230; <a href="https://blog.developpez.com/elsuket/p7311/snippets/aggreger_des_donnees_non_numeriques_d_un">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Il est souvent demandé sur le <a href="http://www.developpez.net/forums/f49/bases-donnees/ms-sql-server/">forum</a> comment on peut créer une liste de valeurs stockées dans une colonne en une liste.<br />
Cela est en effet souvent demandé par le client pour des rapports, mais il n&rsquo;est pas évident de prime abord de fournir un tel résultat, puisqu&rsquo;il est anti-relationnel !</p>
<p>Prenons pour exemple des adhérents à plusieurs clubs :</p>
<pre>
NomContact	NomClub
--------------- ------------
BipBip		SQLServer
BipBip		Oracle
BipBip		PostGreSQL
Coyotte		MySQL
Coyotte		FireBird
Titi		Sybase
Sylvestre	DB2
Sylvestre	SQLite
Bugs Bunny	SQLServer
Bugs Bunny	PostGreSQL
Daffy Duck	FireBird
Daffy Duck	DB2
Sam Le Pirate	SQLite
</pre>
<p>Le résultat recherché se présente sous la forme :</p>
<pre>
NomContact	ListeClub
--------------- -----------------------------
Titi		Sybase
Sam Le Pirate	SQLite
Daffy Duck	FireBird, DB2
Bugs Bunny	SQLServer, PostGreSQL
Sylvestre	DB2, SQLite
Coyotte		MySQL, FireBird
BipBip		SQLServer, Oracle, PostGreSQL
</pre>
<p><span id="more-3"></span></p>
<p>Représentons physiquement nos clubs et leurs adhérents :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 04/03/2009 - &nbsp;<br />
--------------------------------- <br />
CREATE TABLE Contact <br />
( <br />
&nbsp; IDContact INT IDENTITY CONSTRAINT PK_Contact PRIMARY KEY, <br />
&nbsp; NomContact VARCHAR(20) NOT NULL <br />
) <br />
GO <br />
&nbsp;<br />
CREATE TABLE Club <br />
( <br />
&nbsp; IDClub INT IDENTITY CONSTRAINT PK_Club PRIMARY KEY, <br />
&nbsp; NomClub VARCHAR(20) NOT NULL <br />
) <br />
GO <br />
&nbsp;<br />
CREATE TABLE Membre <br />
( <br />
&nbsp; IDMembre INT IDENTITY CONSTRAINT PK_Membre PRIMARY KEY, <br />
&nbsp; IDContact INT NOT NULL CONSTRAINT FK_Membre_IDContact FOREIGN KEY(IDContact) REFERENCES Contact(IDContact), <br />
&nbsp; IDClub INT NOT NULL CONSTRAINT FK_Membre_IDClub FOREIGN KEY(IDClub) REFERENCES Club(IDClub), <br />
&nbsp; CONSTRAINT UQ_Membre_IDContact_IDClub UNIQUE (IDContact, IDClub) <br />
) <br />
GO <br />
&nbsp;<br />
INSERT INTO dbo.Contact (NomContact) VALUES('BipBip') <br />
INSERT INTO dbo.Contact (NomContact) VALUES('Coyotte') <br />
INSERT INTO dbo.Contact (NomContact) VALUES('Titi') <br />
INSERT INTO dbo.Contact (NomContact) VALUES('Sylvestre') <br />
INSERT INTO dbo.Contact (NomContact) VALUES('Bugs Bunny') <br />
INSERT INTO dbo.Contact (NomContact) VALUES('Daffy Duck') <br />
INSERT INTO dbo.Contact (NomContact) VALUES('Sam Le Pirate') <br />
GO <br />
&nbsp;<br />
INSERT INTO dbo.Club (NomClub) VALUES ('SQLServer') <br />
INSERT INTO dbo.Club (NomClub) VALUES ('Oracle') <br />
INSERT INTO dbo.Club (NomClub) VALUES ('PostGreSQL') <br />
INSERT INTO dbo.Club (NomClub) VALUES ('MySQL') <br />
INSERT INTO dbo.Club (NomClub) VALUES ('FireBird') <br />
INSERT INTO dbo.Club (NomClub) VALUES ('Sybase') <br />
INSERT INTO dbo.Club (NomClub) VALUES ('DB2') <br />
INSERT INTO dbo.Club (NomClub) VALUES ('SQLite') <br />
GO <br />
&nbsp;<br />
INSERT INTO dbo.Membre (IDContact, IDClub) VALUES (1, 1) <br />
INSERT INTO dbo.Membre (IDContact, IDClub) VALUES (1, 2) <br />
INSERT INTO dbo.Membre (IDContact, IDClub) VALUES (1, 3) <br />
INSERT INTO dbo.Membre (IDContact, IDClub) VALUES (2, 4) <br />
INSERT INTO dbo.Membre (IDContact, IDClub) VALUES (2, 5) <br />
INSERT INTO dbo.Membre (IDContact, IDClub) VALUES (3, 6) <br />
INSERT INTO dbo.Membre (IDContact, IDClub) VALUES (4, 7) <br />
INSERT INTO dbo.Membre (IDContact, IDClub) VALUES (4, 8) <br />
INSERT INTO dbo.Membre (IDContact, IDClub) VALUES (5, 1) <br />
INSERT INTO dbo.Membre (IDContact, IDClub) VALUES (5, 3) <br />
INSERT INTO dbo.Membre (IDContact, IDClub) VALUES (6, 5) <br />
INSERT INTO dbo.Membre (IDContact, IDClub) VALUES (6, 7) <br />
INSERT INTO dbo.Membre (IDContact, IDClub) VALUES (7, 8) <br />
GO</div></td></tr></tbody></table></div>
<p>La requête qui permet d&rsquo;obtenir tous les clubs auxquels participent tous nos contacts est alors simple :</p>
<div class="codecolorer-container text 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="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">SELECT CONTACT.NomContact, <br />
&nbsp; &nbsp; CLUB.NomClub <br />
FROM dbo.Contact CONTACT <br />
JOIN dbo.Membre MEMBRE ON MBR.IDContact = CONTACT.IDContact <br />
JOIN dbo.Club CLUB ON CLUB.IDClub = MEMBRE.IDClub</div></td></tr></tbody></table></div>
<p>En revanche il est plus complexe d&rsquo;agréger toutes ces valeurs pour en fournir une liste par contact &#8230;<br />
Voyons comment on peut faire pour un seul membre :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">DECLARE @nomContact VARCHAR(20), @listeClubs VARCHAR(1024) <br />
&nbsp;<br />
SELECT @nomContact = 'BipBip' <br />
&nbsp;<br />
SELECT @listeClubs = ISNULL(@listeClubs, '') + CLUB.NomClub + ', ' <br />
FROM dbo.Contact CONTACT <br />
JOIN dbo.Membre MEMBRE ON MEMBRE.IDContact = CONTACT.IDContact <br />
JOIN dbo.Club CLUB ON CLUB.IDClub = MEMBRE.IDClub <br />
WHERE CONTACT.NomContact = @nomContact <br />
&nbsp;<br />
SELECT @nomContact AS Contact, <br />
&nbsp; &nbsp; LEFT(@listeClubs, LEN(@listeClubs) - 1) AS ListeClubs</div></td></tr></tbody></table></div>
<p>Nous permet d&rsquo;obtenir :</p>
<pre>
Contact	ListeClubs
------- -----------------------------
BipBip	SQLServer, Oracle, PostGreSQL
</pre>
<p>Dès lors pour exécuter cette même requête pour tous les contacts, nous allons avoir (encore une fois diront certains) à la puissance des expressions de table commune récursives, introduites avec SQL Server 2005 :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 04/03/2009 - &nbsp;<br />
--------------------------------- <br />
WITH <br />
&nbsp; -- Extrait les informations pour chaque contact suivant la requête &quot;normale&quot; <br />
&nbsp; -- en attribuant à chaque tuple un indice arbitraire <br />
&nbsp; CTE_LISTE_BRUTE(IDContact, NomContact, NomClub, Indice) AS <br />
&nbsp; ( <br />
&nbsp; &nbsp; SELECT CONTACT.IDContact, <br />
&nbsp; &nbsp; &nbsp; &nbsp; CONTACT.NomContact, <br />
&nbsp; &nbsp; &nbsp; &nbsp; CLUB.NomClub, <br />
&nbsp; &nbsp; &nbsp; &nbsp; ROW_NUMBER() OVER(PARTITION BY CONTACT.IDContact ORDER BY CONTACT.IDContact) AS Indice <br />
&nbsp; &nbsp; FROM dbo.Contact CONTACT <br />
&nbsp; &nbsp; JOIN dbo.Membre MEMBRE ON MEMBRE.IDContact = CONTACT.IDContact <br />
&nbsp; &nbsp; JOIN dbo.Club CLUB ON CLUB.IDClub = MEMBRE.IDClub <br />
&nbsp; ), <br />
&nbsp; -- Aggrège les informations pour chaque membre d'un club <br />
&nbsp; CTE_LISTE_CLUB(IDContact, NomContact, ListeClub, Compteur) AS <br />
&nbsp; ( <br />
&nbsp; &nbsp; &nbsp; -- Recherche tous les contacts qui sont membres d'au moins un club <br />
&nbsp; &nbsp; &nbsp; SELECT IDContact, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NomContact, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CAST(NomClub AS VARCHAR(1024)) AS ListeClub, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 AS Compteur <br />
&nbsp; &nbsp; &nbsp; FROM CTE_LISTE_BRUTE <br />
&nbsp; &nbsp; &nbsp; WHERE Indice = 1 <br />
&nbsp; &nbsp; UNION ALL <br />
&nbsp; &nbsp; &nbsp; -- Recherche les autres clubs adhérés par le même membre, pour chaque membre <br />
&nbsp; &nbsp; &nbsp; SELECT CTE_LISTE_CLUB.IDContact, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CTE_LISTE_CLUB.NomContact, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CAST(CTE_LISTE_CLUB.ListeClub + ', ' + CTE_LISTE_BRUTE.NomClub AS VARCHAR(1024)) AS ListeClub, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CTE_LISTE_CLUB.Compteur + 1 <br />
&nbsp; &nbsp; &nbsp; FROM CTE_LISTE_CLUB <br />
&nbsp; &nbsp; &nbsp; JOIN CTE_LISTE_BRUTE ON CTE_LISTE_CLUB.IDContact = CTE_LISTE_BRUTE.IDContact <br />
&nbsp; &nbsp; &nbsp; WHERE Indice = Compteur + 1 <br />
&nbsp; ), <br />
&nbsp; -- Recherche le nombre total d'adhésions pour chaque membre <br />
&nbsp; CTE_MAX(IDContact, maxCompteur) AS <br />
&nbsp; ( <br />
&nbsp; &nbsp; SELECT IDContact, <br />
&nbsp; &nbsp; &nbsp; &nbsp; MAX(Compteur) AS maxCompteur <br />
&nbsp; &nbsp; FROM CTE_LISTE_CLUB <br />
&nbsp; &nbsp; GROUP BY IDContact <br />
&nbsp; ) <br />
SELECT CTE_LISTE_CLUB.NomContact, <br />
&nbsp; &nbsp; CTE_LISTE_CLUB.ListeClub <br />
FROM CTE_LISTE_CLUB <br />
JOIN CTE_MAX <br />
&nbsp; ON CTE_LISTE_CLUB.Compteur = CTE_MAX.maxCompteur <br />
&nbsp; AND CTE_LISTE_CLUB.IDContact = CTE_MAX.IDContact</div></td></tr></tbody></table></div>
<p>Nous obtenons ainsi :</p>
<pre>
NomContact	ListeClub
--------------- -----------------------------
Titi		Sybase
Sam Le Pirate	SQLite
Daffy Duck	FireBird, DB2
Bugs Bunny	SQLServer, PostGreSQL
Sylvestre	DB2, SQLite
Coyotte		MySQL, FireBird
BipBip		SQLServer, Oracle, PostGreSQL
</pre>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ce qu&#8217;il n&#8217;est pas possible d&#8217;utiliser dans les fonctions T-SQL, et pourquoi</title>
		<link>https://blog.developpez.com/elsuket/p7249/t-sql/ce_qu_il_est_interdit_d_utiliser_dans_le</link>
		<comments>https://blog.developpez.com/elsuket/p7249/t-sql/ce_qu_il_est_interdit_d_utiliser_dans_le#comments</comments>
		<pubDate>Tue, 17 Feb 2009 22:23:06 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Voici la liste des instructions qu&#8217;il n&#8217;est pas possible d&#8217;utiliser dans un fonction définie par l&#8217;utilisateur (UDF) sous SQL Server Rappelons d&#8217;abord ce qu&#8217;est une fonction: &#8211; En mathématiques, une fonction, aussi appelée application, est la donnée de deux ensembles &#8230; <a href="https://blog.developpez.com/elsuket/p7249/t-sql/ce_qu_il_est_interdit_d_utiliser_dans_le">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Voici la liste des instructions qu&rsquo;il n&rsquo;est pas possible d&rsquo;utiliser dans un fonction définie par l&rsquo;utilisateur (UDF) sous SQL Server</p>
<p><span id="more-2"></span></p>
<p><strong>Rappelons d&rsquo;abord ce qu&rsquo;est une fonction:</strong></p>
<p>&#8211; <em>En mathématiques</em>, une fonction, aussi appelée application, est la donnée de deux ensembles : un ensemble de départ et un ensemble d&rsquo;arrivée, et une relation associant chaque élément de l&rsquo;ensemble de départ à un et un seul élément de l&rsquo;ensemble d&rsquo;arrivée.<br />
&#8211; <em>En informatique</em>, une fonction est un sous-programme qui exécute un tâche spécifique, et qui est relativement indépendante du code qui l&rsquo;entoure</p>
<p>De ces deux définitions, on peut déduire qu&rsquo;une fonction :</p>
<p>&#8211; doit effectuer seulement un calcul,<br />
&#8211; ne doit pas dépendre du contexte de base de données</p>
<p><strong>C&rsquo;est en outre pour ces raisons que sous SQL Server, il n&rsquo;est pas possible, dans une UDF :</strong></p>
<p>&#8211; d&rsquo;appeler une procédure stockée, sauf si c&rsquo;est une procédure stockée étendue<br />
&#8211; d&rsquo;utiliser la structure de contrôle TRY &#8230; CATCH<br />
&#8211; d&rsquo;utiliser @@ERROR<br />
&#8211; d&rsquo;utiliser RAISERROR<br />
&#8211; de retourner plusieurs ensembles de données<br />
&#8211; d&rsquo;utiliser des fonctions non déterministes comme GETDATE()<br />
&#8211; d&rsquo;utiliser des tables temporaires (# et ##)<br />
&#8211; d&rsquo;utiliser du code SQL dynamique<br />
&#8211; d&rsquo;utiliser une commande SET<br />
&#8211; d&rsquo;utiliser un quelconque ordre de modification de données (INSERT, UPDATE, DELETE), sauf sur les variables table locales à la fonction<br />
&#8211; d&rsquo;encapsuler tout ou partie du code de la fonction dans une transaction (BEGIN | COMMIT | ROLLBACK TRANSACTION) (<strong>Merci SQLPro</strong> ;))</p>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>[T-SQL] Les jointures</title>
		<link>https://blog.developpez.com/elsuket/p7101/t-sql/t_sql_les_jointures</link>
		<comments>https://blog.developpez.com/elsuket/p7101/t-sql/t_sql_les_jointures#comments</comments>
		<pubDate>Wed, 21 Jan 2009 19:08:31 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Lors de mes participations à ce forum, je vois parfois des jointures entre les tables qui sont spécifiées à travers la clause WHERE, ou encore quelques participants demander le fonctionnement des jointures. Voyons ce qu&#8217;il en est dans le T-SQL &#8230; <a href="https://blog.developpez.com/elsuket/p7101/t-sql/t_sql_les_jointures">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Lors de mes participations à ce forum, je vois parfois des jointures entre les tables qui sont spécifiées à travers la clause WHERE, ou encore quelques participants demander le fonctionnement des jointures.<br />
Voyons ce qu&rsquo;il en est dans le T-SQL de SQL Server &#8230;</p>
<p><span id="more-1"></span></p>
<p>Les jointures constituent le mécanisme le plus puissant du langage SQL.<br />
Il est donc dommage de s&rsquo;en passer en utilisant par exemple l&rsquo;opérateur IN pour obtenir le même résultat.</p>
<p>Les jointures permettent de récupérer les données de plusieurs tables en les liant logiquement suivant les relations qui existent entre elles (clés primaires, clés étrangères).</p>
<p>Voyons en détail tous les opérateurs de jointures implémentés dans le T-SQL de SQL Server :</p>
<p>&#8211; Une <strong>équi-jointure</strong>, spécifiée en T-SQL par [INNER] JOIN, vous retournera les lignes pour lesquelles chaque clé des deux tables sur laquelle vous effectuez la jointure existe dans les deux tables.<br />
Les clés n&rsquo;existant pas dans l&rsquo;une ou dans l&rsquo;autre table sont exclues.</p>
<p>&#8211; Une <strong>demi-jointure</strong>, spécifiée en T-SQL par LEFT|RIGHT [OUTER] JOIN, vous retournera toutes les lignes de la table à gauche|droite de l&rsquo;opérateur de jointure, y compris celles qui n&rsquo;ont pas de correspondance dans la table à droite|gauche de l&rsquo;opérateur de jointure.</p>
<p>&#8211; Une <strong>jointure externe complète</strong>, spécifiée en T-SQL par FULL [OUTER] JOIN vous retournera tous les lignes des deux tables, y compris celles qui n&rsquo;ont pas de correspondance dans la table à gauche ou (inclusif) à droite de l&rsquo;opérateur de jointure.</p>
<p>&#8211; Une <strong>jointure croisée</strong>, spécifiée en T-SQL par CROSS JOIN, effectue le produit cartésien de deux tables.</p>
<p>Un démonstration valant mieux que mille lignes, voyons ce qu&rsquo;il en est en T-SQL:</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">CREATE TABLE A <br />
( <br />
&nbsp; A TINYINT <br />
) <br />
GO <br />
&nbsp;<br />
CREATE TABLE B <br />
( <br />
&nbsp; B TINYINT <br />
) <br />
GO <br />
&nbsp;<br />
INSERT INTO A VALUES (1) <br />
INSERT INTO A VALUES (2) <br />
INSERT INTO A VALUES (3) <br />
INSERT INTO A VALUES (4) <br />
INSERT INTO A VALUES (5) <br />
GO <br />
&nbsp;<br />
INSERT INTO B VALUES (1) <br />
INSERT INTO B VALUES (2) <br />
INSERT INTO B VALUES (3) <br />
GO <br />
&nbsp;<br />
-- Equi-jointure <br />
SELECT A.A, B.B <br />
FROM A <br />
INNER JOIN B ON B.B = A.A <br />
GO <br />
&nbsp;<br />
-- Demi-jointure gauche OU jointure externe gauche <br />
SELECT A.A, B.B <br />
FROM A <br />
LEFT OUTER JOIN B ON B.B = A.A <br />
GO <br />
&nbsp;<br />
INSERT INTO B VALUES (6) <br />
GO <br />
&nbsp;<br />
-- Demi-jointure droite OU jointure externe droite <br />
SELECT A.A, B.B <br />
FROM A <br />
RIGHT OUTER JOIN B ON B.B = A.A <br />
GO <br />
-- Jointure externe complète <br />
SELECT A.A, B.B <br />
FROM A <br />
FULL OUTER JOIN B ON B.B = A.A <br />
&nbsp;<br />
-- Jointure croisée (produit cartésien) <br />
SELECT A.A, B.B <br />
FROM A <br />
CROSS JOIN B</div></td></tr></tbody></table></div>
<p>Les mots-clé <strong>INNER</strong> et <strong>OUTER</strong> sont facultatifs (mais pas <strong>CROSS</strong> !)</p>
<p>Pourquoi ne faut-il pas spécifier les jointures dans une clause WHERE ?<br />
Deux raisons à cela :</p>
<p>&#8211; Comme tout SGBDR, SQL Server possède un optimiseur de requête, qui recherche les mots-clé dans le code que vous lui soumettez, pour établir un plan de requête.<br />
Dans une clause WHERE, il s&rsquo;attend à trouver des contraintes (WHERE maColonne = uneValeur), et après un JOIN, une jointure.<br />
Bien sûr, il a été programmé suffisamment intelligemment pour détecter les jointures dans les WHERE, mais dans certains cas très particuliers, il ne les trouve pas, ce qui peut rendre l&rsquo;exécution de la requête peu rapide.</p>
<p>&#8211; Par pur souci de lisibilité de votre code : le seul mot-clé JOIN permet de séparer les jointures des contraintes de votre requête.</p>
<p>On réservera aussi l&rsquo;opérateur IN à renseigner une liste de valeurs.</p>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gérer des couleurs en base de données</title>
		<link>https://blog.developpez.com/elsuket/p7831/snippets/title_139</link>
		<comments>https://blog.developpez.com/elsuket/p7831/snippets/title_139#comments</comments>
		<pubDate>Fri, 03 Jul 2009 13:27:20 +0000</pubDate>
		<dc:creator><![CDATA[elsuket]]></dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Il peut vite devenir difficile de gérer de façon uniforme les couleurs pour plusieurs applications accédant à une base de données : &#8211; s&#8217;il s&#8217;agit d&#8217;une application web, les couleurs s&#8217;affichent sur 6 caractères alphanumériques de la base hexadécimale, &#8211; &#8230; <a href="https://blog.developpez.com/elsuket/p7831/snippets/title_139">Lire la suite <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Il peut vite devenir difficile de gérer de façon uniforme les couleurs pour plusieurs applications accédant à une base de données :</p>
<p>&#8211; s&rsquo;il s&rsquo;agit d&rsquo;une application web, les couleurs s&rsquo;affichent sur 6 caractères alphanumériques de la base hexadécimale,<br />
&#8211; s&rsquo;il s&rsquo;agit d&rsquo;une application lourde, elle peut interpréter les couleurs comme un entier qui est la somme RVB, ou bien sous forme hexadécimale.</p>
<p>Voyons comment gérer cela simplement &#8230;</p>
<p><span id="more-51"></span></p>
<p>Voici une table qui fera tous les calculs automatiquement à partir des trois composantes RVB :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 03/07/2009 - &nbsp;<br />
--------------------------------- <br />
CREATE TABLE TbCouleur <br />
( <br />
&nbsp; IDCouleur INT NOT NULL IDENTITY CONSTRAINT PK_TbCouleur PRIMARY KEY, <br />
&nbsp; nomCouleur VARCHAR(10) NOT NULL CONSTRAINT UQ_TbCouleur_nomCouleur UNIQUE, <br />
&nbsp; composanteRouge TINYINT, <br />
&nbsp; composanteVert TINYINT, <br />
&nbsp; composanteBleu TINYINT, <br />
&nbsp; Decimale AS (composanteRouge + composanteVert * 256 + composanteBleu * 256 * 256) PERSISTED NOT NULL, <br />
&nbsp; Hexadecimale AS (CAST(composanteRouge + composanteVert * 256 + composanteBleu * 256 * 256 AS VARBINARY(3))) PERSISTED NOT NULL, <br />
&nbsp; HTML AS (dbo.Fn_RVB_To_HTML(composanteRouge, composanteVert, composanteBleu)) &nbsp;PERSISTED NOT NULL, <br />
&nbsp; CONSTRAINT UQ_TbCouleur_composanteRouge_composanteVert_composanteBleu UNIQUE (composanteRouge, composanteVert, composanteBleu) <br />
)</div></td></tr></tbody></table></div>
<p>L&rsquo;utilisation de colonnes calculées (persistées, mais ce n&rsquo;est pas obligatoire !) pour faire effectuer tous les calculs au moteur de base de données rend très simple toute saisie de nouvelles couleurs.</p>
<p>Pour calculer le codage HTML, nous utilisons la fonction suivante, dont le code est un peu barbare, mais efficace :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 03/07/2009 - &nbsp;<br />
--------------------------------- <br />
CREATE FUNCTION Fn_RVB_To_HTML <br />
&nbsp; ( <br />
&nbsp; &nbsp; @rouge TINYINT, <br />
&nbsp; &nbsp; @vert TINYINT, <br />
&nbsp; &nbsp; @bleu TINYINT <br />
&nbsp; ) <br />
&nbsp; RETURNS CHAR(6) <br />
&nbsp; WITH SCHEMABINDING <br />
AS <br />
BEGIN <br />
&nbsp; RETURN <br />
&nbsp; ( <br />
&nbsp; &nbsp; CASE @rouge / 16 <br />
&nbsp; &nbsp; &nbsp; WHEN 0 THEN '0' <br />
&nbsp; &nbsp; &nbsp; WHEN 1 THEN '1' <br />
&nbsp; &nbsp; &nbsp; WHEN 2 THEN '2' <br />
&nbsp; &nbsp; &nbsp; WHEN 3 THEN '3' <br />
&nbsp; &nbsp; &nbsp; WHEN 4 THEN '4' <br />
&nbsp; &nbsp; &nbsp; WHEN 5 THEN '5' <br />
&nbsp; &nbsp; &nbsp; WHEN 6 THEN '6' <br />
&nbsp; &nbsp; &nbsp; WHEN 7 THEN '7' <br />
&nbsp; &nbsp; &nbsp; WHEN 8 THEN '8' <br />
&nbsp; &nbsp; &nbsp; WHEN 9 THEN '9' <br />
&nbsp; &nbsp; &nbsp; WHEN 10 THEN 'A' <br />
&nbsp; &nbsp; &nbsp; WHEN 11 THEN 'B' <br />
&nbsp; &nbsp; &nbsp; WHEN 12 THEN 'C' <br />
&nbsp; &nbsp; &nbsp; WHEN 13 THEN 'D' <br />
&nbsp; &nbsp; &nbsp; WHEN 14 THEN 'E' <br />
&nbsp; &nbsp; &nbsp; WHEN 15 THEN 'F' <br />
&nbsp; &nbsp; END + <br />
&nbsp; &nbsp; CASE @rouge % 16 <br />
&nbsp; &nbsp; &nbsp; WHEN 0 THEN '0' <br />
&nbsp; &nbsp; &nbsp; WHEN 1 THEN '1' <br />
&nbsp; &nbsp; &nbsp; WHEN 2 THEN '2' <br />
&nbsp; &nbsp; &nbsp; WHEN 3 THEN '3' <br />
&nbsp; &nbsp; &nbsp; WHEN 4 THEN '4' <br />
&nbsp; &nbsp; &nbsp; WHEN 5 THEN '5' <br />
&nbsp; &nbsp; &nbsp; WHEN 6 THEN '6' <br />
&nbsp; &nbsp; &nbsp; WHEN 7 THEN '7' <br />
&nbsp; &nbsp; &nbsp; WHEN 8 THEN '8' <br />
&nbsp; &nbsp; &nbsp; WHEN 9 THEN '9' <br />
&nbsp; &nbsp; &nbsp; WHEN 10 THEN 'A' <br />
&nbsp; &nbsp; &nbsp; WHEN 11 THEN 'B' <br />
&nbsp; &nbsp; &nbsp; WHEN 12 THEN 'C' <br />
&nbsp; &nbsp; &nbsp; WHEN 13 THEN 'D' <br />
&nbsp; &nbsp; &nbsp; WHEN 14 THEN 'E' <br />
&nbsp; &nbsp; &nbsp; WHEN 15 THEN 'F' <br />
&nbsp; &nbsp; END + <br />
&nbsp; &nbsp; CASE @vert / 16 <br />
&nbsp; &nbsp; &nbsp; WHEN 0 THEN '0' <br />
&nbsp; &nbsp; &nbsp; WHEN 1 THEN '1' <br />
&nbsp; &nbsp; &nbsp; WHEN 2 THEN '2' <br />
&nbsp; &nbsp; &nbsp; WHEN 3 THEN '3' <br />
&nbsp; &nbsp; &nbsp; WHEN 4 THEN '4' <br />
&nbsp; &nbsp; &nbsp; WHEN 5 THEN '5' <br />
&nbsp; &nbsp; &nbsp; WHEN 6 THEN '6' <br />
&nbsp; &nbsp; &nbsp; WHEN 7 THEN '7' <br />
&nbsp; &nbsp; &nbsp; WHEN 8 THEN '8' <br />
&nbsp; &nbsp; &nbsp; WHEN 9 THEN '9' <br />
&nbsp; &nbsp; &nbsp; WHEN 10 THEN 'A' <br />
&nbsp; &nbsp; &nbsp; WHEN 11 THEN 'B' <br />
&nbsp; &nbsp; &nbsp; WHEN 12 THEN 'C' <br />
&nbsp; &nbsp; &nbsp; WHEN 13 THEN 'D' <br />
&nbsp; &nbsp; &nbsp; WHEN 14 THEN 'E' <br />
&nbsp; &nbsp; &nbsp; WHEN 15 THEN 'F' <br />
&nbsp; &nbsp; END + <br />
&nbsp; &nbsp; CASE @vert % 16 <br />
&nbsp; &nbsp; &nbsp; WHEN 0 THEN '0' <br />
&nbsp; &nbsp; &nbsp; WHEN 1 THEN '1' <br />
&nbsp; &nbsp; &nbsp; WHEN 2 THEN '2' <br />
&nbsp; &nbsp; &nbsp; WHEN 3 THEN '3' <br />
&nbsp; &nbsp; &nbsp; WHEN 4 THEN '4' <br />
&nbsp; &nbsp; &nbsp; WHEN 5 THEN '5' <br />
&nbsp; &nbsp; &nbsp; WHEN 6 THEN '6' <br />
&nbsp; &nbsp; &nbsp; WHEN 7 THEN '7' <br />
&nbsp; &nbsp; &nbsp; WHEN 8 THEN '8' <br />
&nbsp; &nbsp; &nbsp; WHEN 9 THEN '9' <br />
&nbsp; &nbsp; &nbsp; WHEN 10 THEN 'A' <br />
&nbsp; &nbsp; &nbsp; WHEN 11 THEN 'B' <br />
&nbsp; &nbsp; &nbsp; WHEN 12 THEN 'C' <br />
&nbsp; &nbsp; &nbsp; WHEN 13 THEN 'D' <br />
&nbsp; &nbsp; &nbsp; WHEN 14 THEN 'E' <br />
&nbsp; &nbsp; &nbsp; WHEN 15 THEN 'F' <br />
&nbsp; &nbsp; END + <br />
&nbsp; &nbsp; CASE @bleu / 16 <br />
&nbsp; &nbsp; &nbsp; WHEN 0 THEN '0' <br />
&nbsp; &nbsp; &nbsp; WHEN 1 THEN '1' <br />
&nbsp; &nbsp; &nbsp; WHEN 2 THEN '2' <br />
&nbsp; &nbsp; &nbsp; WHEN 3 THEN '3' <br />
&nbsp; &nbsp; &nbsp; WHEN 4 THEN '4' <br />
&nbsp; &nbsp; &nbsp; WHEN 5 THEN '5' <br />
&nbsp; &nbsp; &nbsp; WHEN 6 THEN '6' <br />
&nbsp; &nbsp; &nbsp; WHEN 7 THEN '7' <br />
&nbsp; &nbsp; &nbsp; WHEN 8 THEN '8' <br />
&nbsp; &nbsp; &nbsp; WHEN 9 THEN '9' <br />
&nbsp; &nbsp; &nbsp; WHEN 10 THEN 'A' <br />
&nbsp; &nbsp; &nbsp; WHEN 11 THEN 'B' <br />
&nbsp; &nbsp; &nbsp; WHEN 12 THEN 'C' <br />
&nbsp; &nbsp; &nbsp; WHEN 13 THEN 'D' <br />
&nbsp; &nbsp; &nbsp; WHEN 14 THEN 'E' <br />
&nbsp; &nbsp; &nbsp; WHEN 15 THEN 'F' <br />
&nbsp; &nbsp; END + <br />
&nbsp; &nbsp; CASE @bleu % 16 <br />
&nbsp; &nbsp; &nbsp; WHEN 0 THEN '0' <br />
&nbsp; &nbsp; &nbsp; WHEN 1 THEN '1' <br />
&nbsp; &nbsp; &nbsp; WHEN 2 THEN '2' <br />
&nbsp; &nbsp; &nbsp; WHEN 3 THEN '3' <br />
&nbsp; &nbsp; &nbsp; WHEN 4 THEN '4' <br />
&nbsp; &nbsp; &nbsp; WHEN 5 THEN '5' <br />
&nbsp; &nbsp; &nbsp; WHEN 6 THEN '6' <br />
&nbsp; &nbsp; &nbsp; WHEN 7 THEN '7' <br />
&nbsp; &nbsp; &nbsp; WHEN 8 THEN '8' <br />
&nbsp; &nbsp; &nbsp; WHEN 9 THEN '9' <br />
&nbsp; &nbsp; &nbsp; WHEN 10 THEN 'A' <br />
&nbsp; &nbsp; &nbsp; WHEN 11 THEN 'B' <br />
&nbsp; &nbsp; &nbsp; WHEN 12 THEN 'C' <br />
&nbsp; &nbsp; &nbsp; WHEN 13 THEN 'D' <br />
&nbsp; &nbsp; &nbsp; WHEN 14 THEN 'E' <br />
&nbsp; &nbsp; &nbsp; WHEN 15 THEN 'F' <br />
&nbsp; &nbsp; END <br />
&nbsp; ) <br />
END</div></td></tr></tbody></table></div>
<p>Ainsi l&rsquo;ajout d&rsquo;une nouvelle couleur dans la table se fait simplement :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 03/07/2009 - &nbsp;<br />
--------------------------------- <br />
INSERT INTO dbo.TbCouleur(nomCouleur, composanteRouge, composanteVert, composanteBleu) VALUES ('Blanc', 255, 255, 255) <br />
INSERT INTO dbo.TbCouleur(nomCouleur, composanteRouge, composanteVert, composanteBleu) VALUES ('Noir', 0, 0, 0) <br />
INSERT INTO dbo.TbCouleur(nomCouleur, composanteRouge, composanteVert, composanteBleu) VALUES ('Gris', 190, 190, 190) <br />
INSERT INTO dbo.TbCouleur(nomCouleur, composanteRouge, composanteVert, composanteBleu) VALUES ('Rouge', 255, 0, 0) <br />
INSERT INTO dbo.TbCouleur(nomCouleur, composanteRouge, composanteVert, composanteBleu) VALUES ('Vert', 0, 255, 0) <br />
INSERT INTO dbo.TbCouleur(nomCouleur, composanteRouge, composanteVert, composanteBleu) VALUES ('Bleau', 0, 0, 255) <br />
INSERT INTO dbo.TbCouleur(nomCouleur, composanteRouge, composanteVert, composanteBleu) VALUES ('Jaune', 255, 255, 0) <br />
INSERT INTO dbo.TbCouleur(nomCouleur, composanteRouge, composanteVert, composanteBleu) VALUES ('Orange', 255, 165, 0) <br />
INSERT INTO dbo.TbCouleur(nomCouleur, composanteRouge, composanteVert, composanteBleu) VALUES ('Rose', 255, 192, 203) <br />
INSERT INTO dbo.TbCouleur(nomCouleur, composanteRouge, composanteVert, composanteBleu) VALUES ('Violet', 238, 130, 238)</div></td></tr></tbody></table></div>
<p>ou encore à l&rsquo;aide de la procédure stockée suivante :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 03/07/2009 - &nbsp;<br />
--------------------------------- <br />
CREATE PROCEDURE Ps_Couleur_Ajouter <br />
&nbsp; @nomCouleur VARCHAR(10), <br />
&nbsp; @composanteRouge TINYINT, <br />
&nbsp; @composanteVert TINYINT, <br />
&nbsp; @composanteBleu TINYINT <br />
AS <br />
BEGIN <br />
&nbsp; INSERT INTO dbo.TbCouleur <br />
&nbsp; ( <br />
&nbsp; &nbsp; nomCouleur, <br />
&nbsp; &nbsp; composanteRouge, <br />
&nbsp; &nbsp; composanteVert, <br />
&nbsp; &nbsp; composanteBleu <br />
&nbsp; ) <br />
&nbsp; VALUES <br />
&nbsp; ( <br />
&nbsp; &nbsp; @nomCouleur, <br />
&nbsp; &nbsp; @composanteRouge, <br />
&nbsp; &nbsp; @composanteVert, <br />
&nbsp; &nbsp; @composanteBleu <br />
&nbsp; ) <br />
END</div></td></tr></tbody></table></div>
<p>Et pour ajouter les couleurs :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 03/07/2009 - &nbsp;<br />
--------------------------------- <br />
EXEC dbo.Ps_Couleur_Ajouter 'Blanc', 255, 255, 255 <br />
EXEC dbo.Ps_Couleur_Ajouter 'Noir', 0, 0, 0 <br />
EXEC dbo.Ps_Couleur_Ajouter 'Gris', 190, 190, 190 <br />
EXEC dbo.Ps_Couleur_Ajouter 'Rouge', 255, 0, 0 <br />
EXEC dbo.Ps_Couleur_Ajouter 'Vert', 0, 255, 0 <br />
EXEC dbo.Ps_Couleur_Ajouter 'Bleau', 0, 0, 255 <br />
EXEC dbo.Ps_Couleur_Ajouter 'Jaune', 255, 255, 0 <br />
EXEC dbo.Ps_Couleur_Ajouter 'Orange', 255, 165, 0 <br />
EXEC dbo.Ps_Couleur_Ajouter 'Rose', 255, 192, 203 <br />
EXEC dbo.Ps_Couleur_Ajouter 'Violet', 238, 130, 238</div></td></tr></tbody></table></div>
<p>Et le résultat est le suivant :</p>
<p><img src="http://blog.developpez.com/media/couleurs.PNG" width="652" height="301" alt="" /></p>
<p>Les trois fonctions suivantes permettent d&rsquo;obtenir la couleur souhaitée dans la représentation qui conviendra suivant l&rsquo;application :</p>
<div class="codecolorer-container text 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 /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">----------------------------------------------------- <br />
-- Nicolas SOUQUET - 03/07/2009 - Fn_Couleur_GetRVB - <br />
----------------------------------------------------- <br />
CREATE FUNCTION Fn_Couleur_GetRVB <br />
&nbsp; (@nomCouleur VARCHAR(10)) <br />
RETURNS TABLE <br />
WITH SCHEMABINDING <br />
AS <br />
RETURN <br />
( <br />
&nbsp; SELECT composanteRouge AS R, <br />
&nbsp; &nbsp; &nbsp; composanteVert AS V, <br />
&nbsp; &nbsp; &nbsp; composanteBleu AS B <br />
&nbsp; FROM dbo.TbCouleur <br />
&nbsp; WHERE nomCouleur = @nomCouleur <br />
) <br />
&nbsp;<br />
------------------------------------------------------ <br />
-- Nicolas SOUQUET - 03/07/2009 - Fn_Couleur_GetHTML - <br />
------------------------------------------------------ <br />
CREATE FUNCTION Fn_Couleur_GetHTML <br />
&nbsp; (@nomCouleur VARCHAR(10)) <br />
&nbsp; RETURNS CHAR(6) <br />
&nbsp; WITH SCHEMABINDING <br />
AS <br />
BEGIN <br />
&nbsp; RETURN <br />
&nbsp; ( <br />
&nbsp; &nbsp; SELECT HTML <br />
&nbsp; &nbsp; FROM dbo.TbCouleur <br />
&nbsp; &nbsp; WHERE nomCouleur = @nomCouleur <br />
&nbsp; ) <br />
END <br />
&nbsp;<br />
---------------------------------------------------------- <br />
-- Nicolas SOUQUET - 03/07/2009 - Fn_Couleur_GetDecimale - <br />
---------------------------------------------------------- <br />
CREATE FUNCTION Fn_Couleur_GetDecimale <br />
&nbsp; (@nomCouleur VARCHAR(10)) <br />
&nbsp; RETURNS INT <br />
&nbsp; WITH SCHEMABINDING <br />
AS <br />
BEGIN <br />
&nbsp; RETURN <br />
&nbsp; ( <br />
&nbsp; &nbsp; SELECT Decimale <br />
&nbsp; &nbsp; FROM dbo.TbCouleur <br />
&nbsp; &nbsp; WHERE nomCouleur = @nomCouleur <br />
&nbsp; ) <br />
END <br />
&nbsp;<br />
-------------------------------------------------------------- <br />
-- Nicolas SOUQUET - 03/07/2009 - Fn_Couleur_GetHexadecimale - <br />
-------------------------------------------------------------- <br />
CREATE FUNCTION Fn_Couleur_GetHexadecimale <br />
&nbsp; (@nomCouleur VARCHAR(10)) <br />
&nbsp; RETURNS VARBINARY(3) <br />
&nbsp; WITH SCHEMABINDING <br />
AS <br />
BEGIN <br />
&nbsp; RETURN <br />
&nbsp; ( <br />
&nbsp; &nbsp; SELECT Hexadecimale <br />
&nbsp; &nbsp; FROM dbo.TbCouleur <br />
&nbsp; &nbsp; WHERE nomCouleur = @nomCouleur <br />
&nbsp; ) <br />
END</div></td></tr></tbody></table></div>
<p>On peut ensuite envisager le code d&rsquo;une procédure comme suit :</p>
<div class="codecolorer-container text 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 />17<br />18<br />19<br />20<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--------------------------------- <br />
-- Nicolas SOUQUET - 03/07/2009 - &nbsp;<br />
--------------------------------- <br />
CREATE PROCEDURE maProcedure <br />
&nbsp; @... <br />
&nbsp; @application <br />
AS <br />
BEGIN <br />
&nbsp; SELECT CASE ETAT <br />
&nbsp; &nbsp; &nbsp; &nbsp; WHEN uneValeur THEN CASE @application <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN 'HTML' THEN dbo.Fn_Couleur_GetHTML('Rouge') <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN 'EXE' THEN dbo.Fn_Couleur_GetDecimale('Rouge') <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; END <br />
&nbsp; &nbsp; &nbsp; &nbsp; ELSE CASE @application <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN 'HTML' THEN dbo.Fn_Couleur_GetHTML('Blanc') <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN 'EXE' THEN dbo.Fn_Couleur_GetDecimale('Blanc') <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; END <br />
&nbsp; &nbsp; &nbsp; END <br />
&nbsp; FROM dbo.TbEtat AS ETAT <br />
END</div></td></tr></tbody></table></div>
<p>Bien sûr il est préférable de stocker les divers états possibles dans une table et de gérer le CASE avec la valeur de clé de la table.<br />
On peut également penser à récupérer une représentation d&rsquo;une couleur à partir d&rsquo;une autre &#8230;</p>
<p>ElSuket</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
