<?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>SQL - Etienne ZINZINDOHOUE &#187; SQL SERVER 2000</title>
	<atom:link href="https://blog.developpez.com/zinzineti/pcategory/sql-server-2000/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.developpez.com/zinzineti</link>
	<description>Bases de données</description>
	<lastBuildDate>Thu, 09 Aug 2012 13:57:35 +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>CHAR ou  VARCHAR   ?</title>
		<link>https://blog.developpez.com/zinzineti/p9653/sql-server-2005/char_ou_varchar</link>
		<comments>https://blog.developpez.com/zinzineti/p9653/sql-server-2005/char_ou_varchar#comments</comments>
		<pubDate>Mon, 10 Jan 2011 18:07:42 +0000</pubDate>
		<dc:creator><![CDATA[zinzineti]]></dc:creator>
				<category><![CDATA[SQL SERVER 2000]]></category>
		<category><![CDATA[SQL SERVER 2005]]></category>
		<category><![CDATA[SQL SERVER 2008]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Pour écconomiser de l&#8217;espace de stockage des données des tables, on est souvent confronté au choix du type CHAR ou VARCHAR. Pour une colonne dont la longueur est fixe, il n&#8217;y a pas de doute sur le choix du type de colonne : c&#8217;est CHAR qu&#8217;il faut utiliser. Mais lorsque la longueur de la colonne est variable, il faut se poser la question du choix du type de colonne. Ce n&#8217;est pas parce que les [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Pour écconomiser de l&rsquo;espace de stockage des données des tables, on est souvent confronté au choix du type CHAR ou VARCHAR.<br />
Pour une colonne dont la longueur est fixe, il n&rsquo;y a pas de doute sur le choix du type de colonne : c&rsquo;est CHAR qu&rsquo;il faut utiliser. Mais lorsque la longueur de la colonne est variable, il faut se poser la question du choix du type de colonne. Ce n&rsquo;est pas parce que les données dans la colonne varie qu&rsquo;il faut systématiquement choisir le type VARCHAR.<br />
Si la longueur de la colonne varie et est majoritairement supérieure à 4 caractères, il faut  choisir le type VARCHAR.<br />
Autrement dit même si la colonne est variable et si la longueur des données est majoritairement inférieure ou égale à 4 caractères il faut choisir le type CHAR.  <span id="more-38"></span> </p>
<p>Démo<br />
&#8212;&#8212;&#8212;&#8211;<br />
Soit les tables suivantes :</p>
<p>T_CHAR (col1 CHAR (X)) et T_VARCHAR (col1 VARCHAR (X))</p>
<p>X est la longueur maximale définit pour chaque type.</p>
<p>Le nombre d&rsquo;octets pour une colonne T_CHAR (col1) est égal à :  X<br />
Le nombre moyen d&rsquo;octets pour une colonne T_VARCHAR (col1) est égal à :  X/2 + 2</p>
<p>Pour quelles valeurs de X la colonne T_CHAR(col1) permet de gagner plus d&rsquo;espace de stockage que la colonne T_VARCHAR(col1) ? </p>
<p>La traduction mathématique de cette question peut s&rsquo;écrire de la façon suivante :</p>
<p><code class="codecolorer text default"><span class="text">X &lt; X/2 + 2</span></code></p>
<p>Résolvons cette inéquation</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">X &lt; X/2 + 2 <br />
&nbsp;<br />
X - X/2 &lt; + 2 <br />
&nbsp;<br />
X/2 &lt; 2 <br />
&nbsp;<br />
X &lt; 4</div></div>
<p><strong>Cela veut dire que si la colonne est variable et que le nombre de caractères est majoritairement inférieur ou égal à 4,<br />
il est plus bénéfique d&rsquo;utiliser le type CHAR</strong></p>
<p><a href="http://blog.developpez.com/sqlpro/p9490/langage-sql-norme/char-varchar-quel-difference/#more9490">Ici un autre billet de SQLPro relatif à la question CHAR ou VARCHAR ? </a></p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
&#8212; Auteur : Etienne ZINZINDOHOUE<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Estimation de la taille d&#8217;une base de données</title>
		<link>https://blog.developpez.com/zinzineti/p9646/sql-server-2005/estimation_de_la_taille_d_une_base_de_do</link>
		<comments>https://blog.developpez.com/zinzineti/p9646/sql-server-2005/estimation_de_la_taille_d_une_base_de_do#comments</comments>
		<pubDate>Sat, 08 Jan 2011 13:57:51 +0000</pubDate>
		<dc:creator><![CDATA[zinzineti]]></dc:creator>
				<category><![CDATA[OUTILS]]></category>
		<category><![CDATA[SQL SERVER 2000]]></category>
		<category><![CDATA[SQL SERVER 2005]]></category>
		<category><![CDATA[SQL SERVER 2008]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[D&#8217;habitude pour faire une estimation de la taille d&#8217;une base de données j&#8217;utilise l&#8217;outil POWER AMC. En fonction de différents paramètres : nombre de lignes dans chaque table, index, &#8230; POWER AMC indique une estimation de la taille de la base. Mais la méthode de calcul n&#8217;est pas affichée par POWER AMC &#8230; En fouillant sur le net, je suis tombé sur une procédure écrite par Tim Laqua pour faire une estimation de la taille [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>D&rsquo;habitude pour faire une estimation de la taille d&rsquo;une base de données j&rsquo;utilise l&rsquo;outil POWER AMC. En fonction de différents paramètres : nombre de lignes dans chaque table, index, &#8230; POWER AMC indique une estimation de la taille de la base. Mais la méthode de calcul n&rsquo;est pas affichée par POWER AMC &#8230;<br />
En fouillant sur le net, je suis tombé sur une procédure écrite par Tim Laqua pour faire une estimation de la taille d&rsquo;une table; la procédure stockée de Tim Laqua est basée sur un mode de calcul clair proposé par Microsoft qui est <a href="http://msdn.microsoft.com/en-us/library/ms178085.aspx">ici</a><br />
Dans ce billet, nous allons comparer les résultats donnés par la procédure stockée de Tim Laqua et l&rsquo;outil POWER AMC 9.5<br />
<span id="more-113"></span><br />
Voici la DDL de la table de test (cette table est tiré du livre SQL Server 2008 Internals &#8211; page 223)<br />
Pour des raisons de simplicité, nous n&rsquo;allons pas poser de clé primaire ni d&rsquo;index sur cette table.</p>
<blockquote>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 10px"><font color="#0000ff" size="2"><font color="#0000ff" size="2">CREATE</font></font><font color="#000000" size="2"> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">TABLE</font></font><font size="2"><font color="#000000"> customer</font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 10px"><font color="#808080" size="2"><font color="#808080" size="2">(</font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 10px"><font size="2">cust_id </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">smallint</font></font><font size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">NULL,</font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 10px"><font size="2">cust_name </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">varchar</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font size="2">50</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">NULL,</font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 10px"><font size="2">cust_addr1 </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">varchar</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font size="2">50</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">NULL,</font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 10px"><font size="2">cust_addr2 </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">varchar</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font size="2">50</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">NULL,</font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 10px"><font size="2">cust_city </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">varchar</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font size="2">50</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">NULL,</font></font></span></span></p>
<p>
<span style="font-family: courier new, courier, monospace"><span style="font-size: 10px"><font size="2">cust_state </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">char</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font size="2">2</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">NULL,</font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 10px"><font size="2">cust_postal_code </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">varchar</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font size="2">10</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">NULL,</font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 10px"><font size="2">cust_phone </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">varchar</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font size="2">20</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">NULL,</font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 10px"><font size="2">cust_fax </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">varchar</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font size="2">20</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">NULL,</font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 10px"><font size="2">cust_email </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">varchar</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font size="2">30</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">NULL,</font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 10px"><font size="2">cust_web_url </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">varchar</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font size="2">100</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">NULL</font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 10px"><font color="#808080" size="2"><font color="#808080" size="2">)</font></font></span></span></p>
</p>
</blockquote>
<p>Avec 1000 lignes dans la table voici le résultat fournit par POWER AMC 9.5</p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 10px"><font color="#0000ff" size="2"><font color="#0000ff" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8211;POWER AMC 9.5 </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 10px"><font color="#0000ff" size="2"><font color="#0000ff" size="2"><font color="#008000" size="2"><font color="#008000" size="2">Estimation de la taille de la base de donnes &quot;ModeleConceptuelDonnees_1&quot;&#8230;</font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 10px"><font color="#0000ff" size="2"><font color="#0000ff" size="2"><font color="#008000" size="2"><font color="#008000" size="2">Nombre                       Taille estime                Objet</font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 10px"><font color="#0000ff" size="2"><font color="#0000ff" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-   &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;       &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 10px"><font color="#0000ff" size="2"><font color="#0000ff" size="2"><font color="#008000" size="2"><font color="#008000" size="2">1.000                       405 Ko                        Table &quot;customer&quot; </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 10px"><font color="#0000ff" size="2"><font color="#0000ff" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-   &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;       &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 10px"><font color="#0000ff" size="2"><font color="#0000ff" size="2"><font color="#008000" size="2"><font color="#008000" size="2">                           405 Ko Espace total <font color="#008000" size="2"><font color="#008000" size="2">estim&eacute;<span style="display: none"> </span></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 10px"><font color="#0000ff" size="2"><font color="#0000ff" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><span style="display: none"> </span></font></font></font></font></span></span></p>
<p>Nous allons maintenant utiliser la procédure de Tim Laqua. cette procédure est détaillée <a href="http://timlaqua.com/2009/10/estimating-the-size-of-a-table-in-sql-server-2008/">ici</a></p>
<p><strong>&#8211;> La table de définition des types : les types Geography et Geometry ne sont pas pris en compte</strong></p>
<blockquote>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">CREATE TYPE TableColumnDefinition AS TABLE <br />
&nbsp; ([DataType] VARCHAR(16) NOT NULL <br />
&nbsp; ,[Length] INT <br />
&nbsp; ,[IsKey] BIT <br />
&nbsp; ,[DataLength] AS <br />
&nbsp; &nbsp; CASE &nbsp;<br />
&nbsp; &nbsp; &nbsp; WHEN [DataType] = 'BIT' THEN 0.125 <br />
&nbsp; &nbsp; &nbsp; WHEN [DataType] = 'TINYINT' THEN 1 <br />
&nbsp; &nbsp; &nbsp; WHEN [DataType] = 'SMALLINT' THEN 2 <br />
&nbsp; &nbsp; &nbsp; WHEN [DataType] IN ('INT', 'SMALLMONEY', 'REAL', 'SMALLDATETIME') THEN 4 <br />
&nbsp; &nbsp; &nbsp; WHEN [DataType] IN ('BIGINT', 'DATETIME', 'MONEY') THEN 8 <br />
&nbsp; &nbsp; &nbsp; WHEN [DataType] = 'UNIQUEIDENTIFIER' THEN 16 <br />
&nbsp; &nbsp; &nbsp; WHEN [DataType] = 'NVARCHAR' THEN [Length] * 2 + 2 <br />
&nbsp; &nbsp; &nbsp; WHEN [DataType] = 'VARCHAR' THEN [Length] + 2 &nbsp;-- DOESN'T SUPPORT MAX <br />
&nbsp; &nbsp; &nbsp; WHEN [Datatype] = 'DATE' THEN 3 &nbsp;<br />
&nbsp; &nbsp; &nbsp; WHEN [Datatype] = 'TIME' THEN 5 -- Worst case <br />
&nbsp; &nbsp; &nbsp; WHEN [Datatype] = 'DATETIME2' THEN 8 -- Worst case <br />
&nbsp; &nbsp; &nbsp; WHEN [Datatype] = 'DATETIMEOFFSET' THEN 10 -- Worst case <br />
&nbsp; &nbsp; &nbsp; WHEN [DataType] = 'CHAR' THEN [Length] <br />
&nbsp; &nbsp; &nbsp; WHEN [DataType] = 'NCHAR' THEN [Length] * 2 + 2 <br />
&nbsp; &nbsp; &nbsp; WHEN [DataType] = 'BINARY' THEN [Length] <br />
&nbsp; &nbsp; &nbsp; WHEN [DataType] = 'VARBINARY' THEN [Length] + 2 <br />
&nbsp; &nbsp; &nbsp; WHEN [Datatype] = 'TIMESTAMP' THEN 8 <br />
&nbsp; &nbsp; &nbsp; WHEN [DataType] = 'XML' THEN [Length] &nbsp;-- Estimate an average size in bytes, 2GB max <br />
&nbsp; &nbsp; &nbsp; WHEN [DataType] IN ('DECIMAL', 'NUMERIC') THEN &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; CASE &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN [Length] BETWEEN 1 AND 9 &nbsp;THEN 5 <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN [Length] BETWEEN 10 AND 19 THEN 9 <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN [Length] BETWEEN 20 AND 28 THEN 13 <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN [Length] BETWEEN 29 AND 38 THEN 17 <br />
&nbsp; &nbsp; &nbsp; &nbsp; END <br />
&nbsp; &nbsp; &nbsp; WHEN [DataType] = 'FLOAT' THEN <br />
&nbsp; &nbsp; &nbsp; &nbsp; CASE <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN [Length] BETWEEN 1 AND 24 THEN 4 <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN [Length] BETWEEN 25 AND 53 THEN 8 <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN [Length] IS NULL THEN 8 &nbsp;-- Default n is 53 <br />
&nbsp; &nbsp; &nbsp; &nbsp; END <br />
&nbsp; &nbsp; &nbsp; ELSE 0 <br />
&nbsp; &nbsp; END <br />
&nbsp; ,[IsVariableLength] AS <br />
&nbsp; &nbsp; CASE &nbsp;<br />
&nbsp; &nbsp; &nbsp; WHEN [DataType] IN ('VARCHAR', 'NVARCHAR', 'VARBINARY') THEN 1 <br />
&nbsp; &nbsp; &nbsp; ELSE 0 <br />
&nbsp; &nbsp; END)</div></div>
</blockquote>
<p><strong>&#8211;> La procédure stockée de calcul</strong></p>
<blockquote>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">/************************************************************************************* <br />
*** <br />
*** Procedure: &nbsp; &nbsp; &nbsp;[Utility_EstimateClusteredIndexSize] <br />
*** Purpose: &nbsp; &nbsp; &nbsp; &nbsp;Estimates the amount of space needed for a given clustered index <br />
*** Methodology: &nbsp; &nbsp;http://msdn.microsoft.com/en-us/library/ms178085.aspx <br />
*** &nbsp; &nbsp; &nbsp;<br />
*** &nbsp; &nbsp; &nbsp;<br />
*** Author: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Tim Laqua <br />
*** Date Created: &nbsp; &nbsp;2009-10-15 &nbsp;<br />
*** &nbsp;<br />
*** Revision History <br />
*** Date &nbsp; &nbsp;Author &nbsp; &nbsp; &nbsp;Description <br />
*** 2009-10-15 &nbsp;tl &nbsp; &nbsp; &nbsp; &nbsp;Created <br />
*************************************************************************************/ <br />
ALTER PROCEDURE [dbo].[Utility_EstimateClusteredIndexSize] <br />
( <br />
&nbsp; &nbsp;@Num_Rows FLOAT <br />
&nbsp; ,@ClusteredIndexIsUnique BIT <br />
&nbsp; ,@Fill_Factor FLOAT <br />
&nbsp; ,@TableColumn TableColumnDefinition READONLY <br />
) <br />
AS &nbsp;<br />
&nbsp;<br />
SET NOCOUNT ON <br />
&nbsp;<br />
DECLARE <br />
&nbsp; &nbsp;@Num_Cols FLOAT = 0 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-- total number of columns (fixed-length and variable-length) <br />
&nbsp; ,@Fixed_Data_Size FLOAT = 0 &nbsp; &nbsp; &nbsp;-- total byte size of all fixed-length columns <br />
&nbsp; ,@Num_Variable_Cols FLOAT &nbsp;= 0 &nbsp; &nbsp; &nbsp;-- number of variable-length columns <br />
&nbsp; ,@Max_Var_Size FLOAT &nbsp;= 0 &nbsp; &nbsp; &nbsp; &nbsp;-- maximum byte size of all variable-length columns <br />
&nbsp; ,@Variable_Data_Size FLOAT = 0 <br />
&nbsp;<br />
DECLARE @VAR TABLE <br />
&nbsp; ([Name] VARCHAR(64) <br />
&nbsp; ,[VALUE] FLOAT <br />
&nbsp; ,[DisplayValue] AS <br />
&nbsp; &nbsp; CASE <br />
&nbsp; &nbsp; &nbsp; WHEN LOWER([Name]) LIKE '%size%' OR LOWER([Name]) LIKE '%space%' THEN <br />
&nbsp; &nbsp; &nbsp; &nbsp; CASE &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN [VALUE] &lt; 1024 THEN CAST(CAST(ROUND([VALUE], 3) AS NUMERIC(16,0)) AS VARCHAR(16)) + ' B' <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN [VALUE] &lt; 1024*1024 THEN CAST(CAST(ROUND([VALUE]/1024, 3) AS NUMERIC(16,3)) AS VARCHAR(16)) + ' KB' <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WHEN [VALUE] &lt; 1024*1024*1024 THEN CAST(CAST(ROUND([VALUE]/1024/1024, 3) AS NUMERIC(16,3)) AS VARCHAR(16)) + ' MB' <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ELSE CAST(CAST(ROUND([VALUE]/1024/1024/1024, 3) AS NUMERIC(16,3)) AS VARCHAR(16)) + ' GB' <br />
&nbsp; &nbsp; &nbsp; &nbsp; END <br />
&nbsp; &nbsp; &nbsp; ELSE CAST(FLOOR([VALUE]) AS VARCHAR(16)) <br />
&nbsp; &nbsp; END) <br />
&nbsp;<br />
&nbsp;<br />
-- Leaf level data <br />
SELECT &nbsp;<br />
&nbsp; &nbsp;@Num_Variable_Cols = COUNT(1) <br />
&nbsp; ,@Max_Var_Size = ISNULL(SUM([DataLength]), 0) <br />
FROM @TableColumn WHERE [IsVariableLength] = 1 <br />
&nbsp;<br />
SELECT &nbsp;<br />
&nbsp; &nbsp;@Num_Cols = COUNT(1) + @Num_Variable_Cols <br />
&nbsp; ,@Fixed_Data_Size = CEILING(ISNULL(SUM([DataLength]), 0)) <br />
FROM @TableColumn WHERE [IsVariableLength] = 0 <br />
&nbsp;<br />
&nbsp;<br />
INSERT INTO @VAR VALUES ('@Fixed_Data_Size', @Fixed_Data_Size) <br />
&nbsp;<br />
IF @ClusteredIndexIsUnique &lt;&gt; 0 <br />
BEGIN <br />
&nbsp; SET @Num_Cols += 1 <br />
&nbsp; IF @Num_Variable_Cols &gt; 0 <br />
&nbsp; &nbsp; SET @Num_Variable_Cols += 1 <br />
&nbsp; IF @Max_Var_Size &gt; 0 <br />
&nbsp; &nbsp; SET @Max_Var_Size += 4 <br />
END <br />
&nbsp;<br />
DECLARE @Null_Bitmap INT = CEILING(2 + ((@Num_Cols + 7) / 8)) <br />
INSERT INTO @VAR VALUES ('@Null_Bitmap', @Null_Bitmap) <br />
&nbsp;<br />
-- if there are variable columns <br />
IF @Num_Variable_Cols &gt; 0 <br />
&nbsp; SET @Variable_Data_Size = 2 + (@Num_Variable_Cols * 2) + @Max_Var_Size &nbsp;<br />
INSERT INTO @VAR VALUES ('@Num_Cols', @Num_Cols) <br />
INSERT INTO @VAR VALUES ('@Num_Variable_Cols', @Num_Variable_Cols) <br />
INSERT INTO @VAR VALUES ('@Max_Var_Size', @Max_Var_Size) <br />
&nbsp;<br />
DECLARE @Row_Size FLOAT = CEILING(@Fixed_Data_Size + @Variable_Data_Size + @Null_Bitmap + 4 ) <br />
INSERT INTO @VAR VALUES ('@Row_Size', @Row_Size) <br />
&nbsp;<br />
DECLARE @Rows_Per_Page FLOAT = FLOOR(8096 / (@Row_Size + 2)) <br />
INSERT INTO @VAR VALUES ('@Rows_Per_Page', @Rows_Per_Page) <br />
&nbsp;<br />
DECLARE @Free_Rows_Per_Page FLOAT = FLOOR(8096 * ((100 - @Fill_Factor) / 100) / (@Row_Size + 2) ) <br />
INSERT INTO @VAR VALUES ('@Free_Rows_Per_Page', @Free_Rows_Per_Page) <br />
&nbsp;<br />
DECLARE @Num_Leaf_Pages FLOAT = CEILING(@Num_Rows / (@Rows_Per_Page - @Free_Rows_Per_Page) ) <br />
INSERT INTO @VAR VALUES ('@Num_Leaf_Pages', @Num_Leaf_Pages) <br />
&nbsp;<br />
DECLARE @Leaf_space_used FLOAT = 8192 * @Num_Leaf_Pages <br />
INSERT INTO @VAR VALUES ('@Leaf_space_used', @Leaf_space_used) <br />
&nbsp;<br />
-- Index Information <br />
DECLARE <br />
&nbsp; &nbsp;@Num_Key_Cols FLOAT = 0 &nbsp; &nbsp; &nbsp;-- total number of key columns (fixed-length and variable-length) <br />
&nbsp; ,@Fixed_Key_Size FLOAT = 0 &nbsp; &nbsp; &nbsp;-- total byte size of all fixed-length key columns <br />
&nbsp; ,@Num_Variable_Key_Cols FLOAT = 0 &nbsp;-- number of variable-length key columns <br />
&nbsp; ,@Max_Var_Key_Size FLOAT = 0 &nbsp; &nbsp;-- maximum byte size of all variable-length key columns <br />
&nbsp;<br />
&nbsp;<br />
SELECT &nbsp;<br />
&nbsp; &nbsp;@Num_Variable_Key_Cols = COUNT(1) <br />
&nbsp; ,@Max_Var_Key_Size = ISNULL(SUM([DataLength]), 0) <br />
FROM @TableColumn &nbsp;<br />
WHERE &nbsp;<br />
&nbsp; [IsVariableLength] = 1 &nbsp;<br />
&nbsp; AND [IsKey] = 1 <br />
&nbsp;<br />
SELECT &nbsp;<br />
&nbsp; &nbsp;@Num_Key_Cols = COUNT(1) + @Num_Variable_Key_Cols <br />
&nbsp; ,@Fixed_Key_Size = ISNULL(SUM([DataLength]), 0) <br />
FROM @TableColumn &nbsp;<br />
WHERE &nbsp;<br />
&nbsp; [IsVariableLength] = 0 <br />
&nbsp; AND [IsKey] = 1 <br />
&nbsp;<br />
IF @ClusteredIndexIsUnique &lt;&gt; 0 <br />
BEGIN <br />
&nbsp; SET @Num_Key_Cols += 1 <br />
&nbsp; IF @Num_Variable_Key_Cols &gt; 0 <br />
&nbsp; &nbsp; SET @Num_Variable_Key_Cols += 1 <br />
&nbsp; IF @Max_Var_Key_Size &gt; 0 <br />
&nbsp; &nbsp; SET @Max_Var_Key_Size += 4 <br />
END <br />
INSERT INTO @VAR VALUES ('@Num_Variable_Key_Cols', @Num_Variable_Key_Cols) <br />
INSERT INTO @VAR VALUES ('@Max_Var_Key_Size', @Max_Var_Key_Size) <br />
INSERT INTO @VAR VALUES ('@Num_Key_Cols', @Num_Key_Cols) <br />
&nbsp;<br />
-- Who would seriously have a nullable key column? &nbsp;ridiculous. <br />
DECLARE @Index_Null_Bitmap FLOAT = 2 + ((@Num_Key_Cols + 7) / 8) &nbsp;<br />
INSERT INTO @VAR VALUES ('@Index_Null_Bitmap', @Index_Null_Bitmap) <br />
&nbsp;<br />
DECLARE @Variable_Key_Size FLOAT = 0 <br />
&nbsp;<br />
IF @Max_Var_Key_Size &gt; 0 <br />
&nbsp; SET @Variable_Key_Size = 2 + (@Num_Variable_Key_Cols * 2) + @Max_Var_Key_Size &nbsp;<br />
INSERT INTO @VAR VALUES ('@Variable_Key_Size', @Variable_Key_Size) <br />
&nbsp;<br />
DECLARE @Index_Row_Size FLOAT = @Fixed_Key_Size + @Variable_Key_Size + @Index_Null_Bitmap + 1 + 6 <br />
INSERT INTO @VAR VALUES ('@Index_Row_Size',@Index_Row_Size ) <br />
&nbsp;<br />
DECLARE @Index_Rows_Per_Page &nbsp;FLOAT = FLOOR(8096 / (@Index_Row_Size + 2)) <br />
INSERT INTO @VAR VALUES ('@Index_Rows_Per_Page', @Index_Rows_Per_Page) <br />
&nbsp;<br />
DECLARE @Non_Leaf_Levels FLOAT = 1 <br />
&nbsp;<br />
-- Log base @Non_Leaf_Levels hack <br />
WHILE (@Num_Leaf_Pages / @Index_Rows_Per_Page) &gt; POWER(@Index_Rows_Per_Page,@Non_Leaf_Levels) <br />
&nbsp; SET @Non_Leaf_Levels += 1 <br />
&nbsp;<br />
SET @Non_Leaf_Levels += 1 <br />
INSERT INTO @VAR VALUES ('@Non_Leaf_Levels', @Non_Leaf_Levels) <br />
&nbsp;<br />
-- Summation hack <br />
DECLARE &nbsp;<br />
&nbsp; &nbsp;@Num_Index_Pages FLOAT = 0 <br />
&nbsp; ,@Summand INT = @Non_Leaf_Levels <br />
&nbsp;<br />
WHILE @Summand &gt; 0 <br />
BEGIN <br />
&nbsp; SET @Num_Index_Pages += CEILING(@Num_Leaf_Pages/POWER(@Index_Rows_Per_Page, @Summand)) <br />
&nbsp; SET @Summand -= 1 <br />
END <br />
INSERT INTO @VAR VALUES ('@Num_Index_Pages', @Num_Index_Pages) <br />
&nbsp;<br />
DECLARE @Index_Space_Used FLOAT = 8192 * @Num_Index_Pages &nbsp;<br />
INSERT INTO @VAR VALUES ('@Index_Space_Used', @Index_Space_Used) <br />
&nbsp;<br />
DECLARE @Clustered_Index_Size FLOAT = @Leaf_Space_Used + @Index_Space_used <br />
INSERT INTO @VAR VALUES ('@Clustered_Index_Size', @Clustered_Index_Size) <br />
&nbsp;<br />
SELECT * FROM @VAR</div></div>
</blockquote>
<p><strong>&#8211;> Test de la procédure en utilisant la définition (DDL) de la table customer</strong></p>
<blockquote>
<p>
	<font color="#0000ff" size="2"><font color="#0000ff" size="2">DECLARE</font></font><font size="2"><font color="#000000"> @TableColumn TableColumnDefinition</font></font></p>
<p>
	<font color="#0000ff" size="2"><font color="#0000ff" size="2">INSERT</font></font><font color="#000000" size="2"> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">INTO</font></font><font size="2"><font color="#000000"> @TableColumn</font></font></p>
<p>
	<font color="#0000ff" size="2"><font color="#0000ff" size="2">VALUES</font></font><font size="2"><font color="#000000"> </font></font></p>
<p>
	<font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;smallint&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">,NULL,</font></font><font color="#000000" size="2">0</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font></p>
<p>
	<font color="#808080" size="2"><font color="#808080" size="2">,(</font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;VARCHAR&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font size="2">50</font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font size="2">0</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font></p>
<p>
	<font color="#808080" size="2"><font color="#808080" size="2">,(</font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;VARCHAR&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font size="2">50</font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font size="2">0</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font></p>
<p>
	<font color="#808080" size="2"><font color="#808080" size="2">,(</font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;VARCHAR&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font size="2">50</font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font size="2">0</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font></p>
<p>
	<font color="#808080" size="2"><font color="#808080" size="2">,(</font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;VARCHAR&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font size="2">50</font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font size="2">0</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font></p>
<p>
	<font color="#808080" size="2"><font color="#808080" size="2">,(</font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;CHAR&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font size="2">2</font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font size="2">0</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font></p>
<p>
	<font color="#808080" size="2"><font color="#808080" size="2">,(</font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;VARCHAR&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font size="2">10</font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font size="2">0</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font></p>
<p>
	<font color="#808080" size="2"><font color="#808080" size="2">,(</font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;VARCHAR&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font size="2">20</font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font size="2">0</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font></p>
<p>
	<font color="#808080" size="2"><font color="#808080" size="2">,(</font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;VARCHAR&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font size="2">20</font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font size="2">0</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font></p>
<p>
	<font color="#808080" size="2"><font color="#808080" size="2">,(</font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;VARCHAR&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font size="2">30</font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font size="2">0</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font></p>
<p>
	<font color="#808080" size="2"><font color="#808080" size="2">,(</font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;VARCHAR&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font size="2">100</font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font size="2">0</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font></p>
<p>
	<font color="#0000ff" size="2"><font color="#0000ff" size="2">EXEC</font></font><font color="#000000" size="2"> [dbo]</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">[Utility_EstimateClusteredIndexSize]</font><font color="#0000ff" size="2"><font color="#0000ff" size="2"> </font></font></p>
<p>
	<font color="#000000" size="2">@Num_Rows </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font color="#000000" size="2"> 1 </font><font color="#008000" size="2"><font color="#008000" size="2">&#8211; number of rows in the table</font></font></p>
<p>
	<font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">@ClusteredIndexIsUnique </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font color="#000000" size="2"> 0 </font><font color="#008000" size="2"><font color="#008000" size="2">&#8211; is it?</font></font></p>
<p>
	<font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">@Fill_Factor </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font color="#000000" size="2"> 90 </font><font color="#008000" size="2"><font color="#008000" size="2">&#8211; page fill factor</font></font></p>
<p>
	<font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">@TableColumn </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font color="#000000" size="2"> @TableColumn </font><font color="#008000" size="2"><font color="#008000" size="2">&#8211; definition of table columns </font></font></p>
</blockquote>
<p>
	<span style="font-size: 12px"><span style="font-family: courier new, courier, monospace"><font color="#008000"><font color="#008000">R&eacute;sultat</font></font></span></span></p>
<p>
	<span style="font-size: 12px"><span style="font-family: courier new, courier, monospace"><font color="#008000"><font color="#008000">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</font></font></span></span></p>
<p>
	<span style="font-size: 12px"><span style="font-family: courier new, courier, monospace"><font color="#008000"><font color="#008000">@Row_Size          431 octets</font></font></span></span></p>
<p>
	<span style="font-size: 12px"><span style="font-family: courier new, courier, monospace"><font color="#008000"><font color="#008000">@Rows_Per_Page      18</font></font></span></span></p>
</p>
<p>
	<span style="font-size: 14px"><strong><span style="font-family: courier new, courier, monospace"><font color="#008000"><font color="#008000">Pour les 1000 lignes, l&#39;estimation de la taille de la base = <font color="#008000">431</font> octets X 1000 = 421 Ko</font></font></span></strong></span></p>
<p><strong>&#8211;> Que peut-on dire ?</strong></p>
<p>POWER AMC estime la taille de la base à <strong>405 Ko</strong> et la procédure basée sur la méthodologie Microsoft affiche <strong>421 Ko</strong>. à mon humble avis ces résultats sont proches.</p>
<p>Qu&rsquo;en pensez-vous ? </p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
Auteur : Etienne ZINZINDOHOUE<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clé primaire (PK) et ses clés étrangères (FK)</title>
		<link>https://blog.developpez.com/zinzineti/p9528/sql-server-2005/pk_et_ses_fk</link>
		<comments>https://blog.developpez.com/zinzineti/p9528/sql-server-2005/pk_et_ses_fk#comments</comments>
		<pubDate>Tue, 23 Nov 2010 18:02:50 +0000</pubDate>
		<dc:creator><![CDATA[zinzineti]]></dc:creator>
				<category><![CDATA[SQL SERVER 2000]]></category>
		<category><![CDATA[SQL SERVER 2005]]></category>
		<category><![CDATA[SQL SERVER 2008]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Ces procédures stockées permettent d&#8217;afficher les clés étrangères (FK) relatives à une clé primaire (PK). Ces procédures stockées permettent de présenter les liens entre tables sans utiliser le diagramme designer. Une aide donc pour identifier rapidement les colonnes à utiliser dans les jointures entre tables. /*=========================================================================================== &#8211; Procdure Stocke permettant d&#39;afficher les cls trangres(FK) relatives une cl primaire (PK) &#8211; SQL SERVER 2005/2008 &#8211; Version1 &#8211; Auteur : Etienne ZINZINDOHOUE &#8212;&#8212;&#8212;&#8212;&#8212;&#8212; Exemple d&#39;utilisation &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; P_Affiche_Liens_PK_FK_sql2005_2008 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Ces procédures stockées permettent d&rsquo;afficher les clés étrangères (FK) relatives à une clé primaire (PK). Ces procédures stockées permettent de présenter les liens entre tables sans utiliser le diagramme designer. Une aide donc pour identifier rapidement les colonnes à utiliser dans les jointures entre tables.<br />
<span id="more-61"></span></p>
<blockquote>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">/*===========================================================================================</font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8211; Procdure Stocke permettant d&#39;afficher les cls trangres(FK) relatives une cl primaire (PK) </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8211; SQL SERVER 2005/2008 &#8211; Version1</font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8211; Auteur : Etienne ZINZINDOHOUE</font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; Exemple d&#39;utilisation &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">P_Affiche_Liens_PK_FK_sql2005_2008 @schemaName = N&#39;HumanResources&#39;, @tableName = N&#39;Employee&#39;</font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8211;===========================================================================================*/</font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">USE</font></font><font size="2"><font color="#000000"> AdventureWorks </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">go</font></font><font size="2"><font color="#000000"> </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">CREATE</font></font><font color="#000000" size="2"> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">PROCEDURE</font></font><font size="2"><font color="#000000"> P_Affiche_Liens_PK_FK_sql2005_2008 </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font size="2">@schemaName </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">VARCHAR</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font size="2">50</font><font color="#808080" size="2"><font color="#808080" size="2">),</font></font><font size="2">@tableName </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">VARCHAR</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font size="2">50</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font size="2"> </font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">AS</font></font><font size="2"><font color="#000000"> </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">DECLARE</font></font><font color="#000000" size="2"> @pk </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">VARCHAR</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">max</font></font><font color="#808080" size="2"><font color="#808080" size="2">),</font></font><font color="#000000" size="2">@fk </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">VARCHAR</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">max</font></font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font size="2"><font color="#000000"> </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">DECLARE</font></font><font color="#000000" size="2"> @maTableVar </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">table </font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font size="2"><font color="#000000"> </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font size="2">PKTABLE_QUALIFIER </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">sysname</font></font><font size="2"> </font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">PKTABLE_OWNER </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">sysname</font></font><font size="2"><font color="#000000"> </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">PKTABLE_NAME </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">sysname</font></font><font size="2"><font color="#000000"> </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">PKCOLUMN_NAME </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">sysname</font></font><font size="2"><font color="#000000"> </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">FKTABLE_QUALIFIER </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">sysname</font></font><font size="2"><font color="#000000"> </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">FKTABLE_OWNER </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">sysname</font></font><font size="2"><font color="#000000"> </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">FKTABLE_NAME </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">sysname</font></font><font size="2"><font color="#000000"> </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">FKCOLUMN_NAME </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">sysname</font></font><font size="2"><font color="#000000"> </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">KEY_SEQ </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">smallint</font></font><font size="2"><font color="#000000"> </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">UPDATE_RULE </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">smallint</font></font><font size="2"><font color="#000000"> </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">DELETE_RULE </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">smallint</font></font><font size="2"><font color="#000000"> </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">FK_NAME </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">sysname</font></font><font size="2"><font color="#000000"> </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">PK_NAME </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">sysname</font></font><font size="2"><font color="#000000"> </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">DEFERRABILITY </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">smallint</font></font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font size="2"><font color="#000000"> </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">INSERT</font></font><font color="#000000" size="2"> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">INTO</font></font><font size="2"><font color="#000000"> @maTableVar </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">EXEC </font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;EXEC sp_fkeys @pktable_name = N&#39;&#39;&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2">@tableName </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;&#39;&#39;, @pktable_owner = N&#39;&#39;&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2">@schemaName </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;&#39;&#39;&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">);</font></font><font size="2"><font color="#000000"> </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">SELECT</font></font><font color="#000000" size="2"> PKTABLE_QUALIFIER</font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;.&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> PKTABLE_OWNER </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;.&#39;</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2">PKTABLE_NAME </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">AS</font></font><font size="2"><font color="#000000"> [Base.Schema.Table_Parent] </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">PKCOLUMN_NAME </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">AS</font></font><font size="2"><font color="#000000"> [PK] </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">FKTABLE_QUALIFIER </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;.&#39;</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> FKTABLE_OWNER </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;.&#39;</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> FKTABLE_NAME </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">AS</font></font><font size="2"><font color="#000000"> [Base.Schema.Table_Enfant] </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">FKCOLUMN_NAME </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">AS</font></font><font size="2"><font color="#000000"> [FK] </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">FROM</font></font><font size="2"><font color="#000000"> @maTableVar </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">ORDER</font></font><font color="#000000" size="2"> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">BY</font></font><font color="#000000" size="2"> [Base.Schema.Table_Parent]</font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">[PK]</font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">[Base.Schema.Table_Enfant]</font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font size="2"><font color="#000000">[FK] </font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">GO<span style="display: none"> </span></font></font></font></font></font></font></span></span></p>
</blockquote>
<blockquote>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">/*===========================================================================================</font></font></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8211; Procdure Stocke permettant d&#39;afficher les cls trangres(FK) relatives une cl primaire (PK) </font></font></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8211; SQL SERVER 2000 &#8211; Version1</font></font></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8211; Auteur : Etienne ZINZINDOHOUE</font></font></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; Exemple d&#39;utilisation &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</font></font></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">EXEC P_Affiche_Liens_PK_FK_sql2000 @schemaName = N&#39;dbo&#39;,@tableName = N&#39;Employees&#39;</font></font></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">===========================================================================================*/</font></font></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">USE</font></font><font size="2"><font color="#000000"> Northwind </font></font></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">GO</font></font></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">CREATE</font></font><font color="#000000" size="2"> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">PROCEDURE</font></font><font size="2"><font color="#000000"> P_Affiche_Liens_PK_FK_sql2000 </font></font></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font size="2">@schemaName </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">VARCHAR</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font size="2">50</font><font color="#808080" size="2"><font color="#808080" size="2">),</font></font><font size="2">@tableName </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">VARCHAR</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font size="2">50</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font size="2"> </font></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">AS</font></font><font size="2"><font color="#000000"> </font></font></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">SELECT</font></font><font color="#000000" size="2"> t1</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">TABLE_CATALOG </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;.&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2">t1</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">TABLE_SCHEMA </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;.&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> t1</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">TABLE_NAME </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">AS</font></font><font size="2"><font color="#000000"> [Base.Schema.Table_Parent] </font></font></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">c1</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">name </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">AS</font></font><font size="2"><font color="#000000"> [PK] </font></font></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">t2</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">TABLE_CATALOG </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;.&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2">t2</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">TABLE_SCHEMA </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;.&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> t2</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">TABLE_NAME </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">AS</font></font><font size="2"><font color="#000000"> [Base.Schema.Table_Enfant] </font></font></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">c2</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">name </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">AS</font></font><font size="2"><font color="#000000"> [FK] </font></font></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">FROM</font></font><font color="#000000" size="2"> </font><font color="#008000" size="2"><font color="#008000" size="2">sysforeignkeys</font></font><font size="2"><font color="#000000"> f </font></font></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">INNER</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">JOIN</font></font><font color="#000000" size="2"> </font><font color="#008000" size="2"><font color="#008000" size="2">syscolumns</font></font><font color="#000000" size="2"> c1 </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">ON</font></font><font color="#000000" size="2"> f</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">rkeyid </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font color="#000000" size="2"> c1</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">id </font><font color="#808080" size="2"><font color="#808080" size="2">AND</font></font><font color="#000000" size="2"> f</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">rkey </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font color="#000000" size="2"> c1</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font size="2"><font color="#000000">colid </font></font></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">INNER</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">JOIN</font></font><font color="#000000" size="2"> </font><font color="#008000" size="2"><font color="#008000" size="2">syscolumns</font></font><font color="#000000" size="2"> c2 </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">ON</font></font><font color="#000000" size="2"> f</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">fkeyid </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font color="#000000" size="2"> c2</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">id </font><font color="#808080" size="2"><font color="#808080" size="2">AND</font></font><font color="#000000" size="2"> f</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">fkey </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font color="#000000" size="2"> c2</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font size="2"><font color="#000000">colid </font></font></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">INNER</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">JOIN</font></font><font color="#000000" size="2"> </font><font color="#008000" size="2"><font color="#008000" size="2">INFORMATION_SCHEMA</font></font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#008000" size="2"><font color="#008000" size="2">TABLES</font></font><font color="#000000" size="2"> t1 </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">ON</font></font><font color="#000000" size="2"> t1</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">TABLE_NAME </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font color="#000000" size="2"> </font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">object_name</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#000000" size="2">f</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">rkeyid</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font size="2"><font color="#000000"> </font></font></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">INNER</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">JOIN</font></font><font color="#000000" size="2"> </font><font color="#008000" size="2"><font color="#008000" size="2">INFORMATION_SCHEMA</font></font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#008000" size="2"><font color="#008000" size="2">TABLES</font></font><font color="#000000" size="2"> t2 </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">ON</font></font><font color="#000000" size="2"> t2</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">TABLE_NAME </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font color="#000000" size="2"> </font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">object_name</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#000000" size="2">f</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">fkeyid</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font size="2"><font color="#000000"> </font></font></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">WHERE</font></font><font color="#000000" size="2"> </font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">object_name</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#000000" size="2">f</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">rkeyid</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> @tableName </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39; &#39;</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">AND</font></font><font color="#000000" size="2"> t1</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">TABLE_SCHEMA </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font size="2"><font color="#000000"> @schemaName </font></font></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">ORDER</font></font><font color="#000000" size="2"> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">BY</font></font><font color="#000000" size="2"> [Base.Schema.Table_Parent]</font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">[PK]</font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">[Base.Schema.Table_Enfant]</font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font size="2"><font color="#000000">[FK] </font></font></font></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">GO</font></font></font></font></font></font></font></font></span></span></p>
</blockquote>
<p>=============================================================<br />
<strong>Pour une purge saine [je parle des données ;-)]  </strong><br />
=============================================================</p>
<p>Ces procédures stockées peuvent être utiles par exemple lors de la mis en place de batch de purge. Une des principales étapes de la mise en place de la purge d&rsquo;une base de données est d&rsquo;identifier les liens PK-FK entre tables. Ceci afin d&rsquo;éviter les lignes orphelines (dans les &laquo;&nbsp;tables Enfant&nbsp;&raquo;). La conséquence logique de cette précaution est d&rsquo;assurer la consistance et la qualité des données dans la base, lorsque l&rsquo;option <a href="http://blog.developpez.com/zinzineti/p9035/sql-server-2000/on-delete-cascade-on-update-cascade/#more9035">ON DELETE CASCADE</a> n&rsquo;est pas implémentée.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
&#8212; Etienne ZINZINDOHOUE<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Montre-moi tes tables et je te dirai ce que vaut ta base  &#8230;</title>
		<link>https://blog.developpez.com/zinzineti/p9530/sql-server-2005/montre_moi_tes_tables_et_je_te_dirai_ce</link>
		<comments>https://blog.developpez.com/zinzineti/p9530/sql-server-2005/montre_moi_tes_tables_et_je_te_dirai_ce#comments</comments>
		<pubDate>Wed, 24 Nov 2010 12:53:12 +0000</pubDate>
		<dc:creator><![CDATA[zinzineti]]></dc:creator>
				<category><![CDATA[SQL SERVER 2000]]></category>
		<category><![CDATA[SQL SERVER 2005]]></category>
		<category><![CDATA[SQL SERVER 2008]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[La théorie relationnelle EXIGE que chaque table DOIT avoir une clé primaire. Mais les éditeurs de SGBDR sont permissifs vis à vis de cette recommandation. Conséquence on voit dans des bases de données des tables contenant des milliers de lignes sans clé primaire (PK) ! Le constat est décevant &#8230; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; &#8211;&#62; SQL 2005/2008 : Afficher les tables sans PK sans FK : &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; SELECT DB_NAME() + &#39;.&#39; + object_schema_name(t.object_id) + &#39;.&#39; + t.name AS [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>La théorie relationnelle EXIGE que chaque table <strong>DOIT </strong>avoir une clé primaire.<br />
Mais les éditeurs de SGBDR sont permissifs vis à vis de cette recommandation. Conséquence on voit dans des bases de données<br />
des tables contenant des milliers de lignes sans clé primaire (PK) ! Le constat est décevant &#8230;<br />
<span id="more-62"></span></p>
<blockquote>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 14px"><font color="#0000ff"><font color="#0000ff"><span style="display: none"> </span><span style="display: none"> </span><span style="display: none"> </span></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 14px"><font color="#0000ff"><font color="#0000ff"><font color="#000000"><span style="display: none"> </span><span style="display: none"> </span> </font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 14px"><font color="#0000ff"><font color="#0000ff"><font color="#000000"><font color="#008000" size="2"><font color="#008000" size="2"><span id="cke_bm_169S" style="display: none"> </span><span id="cke_bm_168S" style="display: none"> </span>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 14px"><font color="#0000ff"><font color="#0000ff"><font color="#000000"><font color="#008000" size="2"><font color="#008000" size="2">&#8211;&gt; SQL 2005/2008 : Afficher les tables sans PK sans FK : </font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 14px"><font color="#0000ff"><font color="#0000ff"><font color="#000000"><font color="#008000" size="2"><font color="#008000" size="2">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 14px"><font color="#0000ff"><font color="#0000ff"><font color="#000000"><font color="#0000ff" size="2"><font color="#0000ff" size="2">SELECT</font></font><font color="#000000" size="2"> </font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">DB_NAME</font></font><font color="#808080" size="2"><font color="#808080" size="2">()</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;.&#39;</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> </font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">object_schema_name</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#000000" size="2">t</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">object_id</font></font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;.&#39;</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> t</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">name </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">AS</font></font><font size="2"><font color="#000000"> [Tables sans PK sans FK] </font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 14px"><font color="#0000ff"><font color="#0000ff"><font color="#000000"><font color="#0000ff" size="2"><font color="#0000ff" size="2">FROM</font></font><font color="#000000" size="2"> </font><font color="#008000" size="2"><font color="#008000" size="2">sys</font></font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#008000" size="2"><font color="#008000" size="2">tables</font></font><font size="2"><font color="#000000"> t </font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 14px"><font color="#0000ff"><font color="#0000ff"><font color="#000000"><font color="#0000ff" size="2"><font color="#0000ff" size="2">WHERE</font></font><font color="#000000" size="2"> </font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">OBJECTPROPERTY</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">object_id</font></font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;IsPrimaryKey&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font size="2"><font color="#000000"> 0 </font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 14px"><font color="#0000ff"><font color="#0000ff"><font color="#000000"><font color="#808080" size="2"><font color="#808080" size="2">AND</font></font><font color="#000000" size="2"> </font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">OBJECTPROPERTY</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">object_id</font></font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;TableHasPrimaryKey&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font size="2"><font color="#000000"> 0 </font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 14px"><font color="#0000ff"><font color="#0000ff"><font color="#000000"><font color="#808080" size="2"><font color="#808080" size="2">AND</font></font><font color="#000000" size="2"> </font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">OBJECTPROPERTY</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">object_id</font></font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;IsForeignKey&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font size="2"><font color="#000000"> 0 </font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 14px"><font color="#0000ff"><font color="#0000ff"><font color="#000000"><font color="#808080" size="2"><font color="#808080" size="2">AND</font></font><font color="#000000" size="2"> </font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">OBJECTPROPERTY</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">object_id</font></font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;TableHasForeignKey&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font size="2"><font color="#000000"> 0 </font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 14px"><font color="#0000ff"><font color="#0000ff"><font color="#000000"><font color="#808080" size="2"><font color="#808080" size="2">AND</font></font><font color="#000000" size="2"> </font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">OBJECTPROPERTY</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">object_id</font></font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;TableHasForeignRef&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font size="2"><font color="#000000"> 0 </font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 14px"><font color="#0000ff"><font color="#0000ff"><font color="#000000"><font color="#808080" size="2"><font color="#808080" size="2">AND</font></font><font color="#000000" size="2"> </font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">OBJECTPROPERTY</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">object_id</font></font><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;IsUserTable&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font size="2"><font color="#000000"> 1 </font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 14px"><font color="#0000ff"><font color="#0000ff"><font color="#000000"><font color="#0000ff" size="2"><font color="#0000ff" size="2">ORDER</font></font><font color="#000000" size="2"> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">BY</font></font><font size="2"><font color="#000000"> [Tables sans PK sans FK]<span id="cke_bm_169E" style="display: none"> </span><span id="cke_bm_168E" style="display: none"> </span></font></font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 14px"><font color="#0000ff"><font color="#0000ff"><font color="#000000"><span style="display: none"> </span> <span style="display: none"> </span><span style="display: none"> </span></font></font></font></span></span></p>
</blockquote>
<blockquote>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2">&#8211;&gt; SQL 2000 : Afficher les tables sans PK sans FK </font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#0000ff" size="2"><font color="#0000ff" size="2">SELECT</font></font><font color="#000000" size="2"> t</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">TABLE_CATALOG </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;.&#39;</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> t</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">TABLE_SCHEMA </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;.&#39;</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> t</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">TABLE_NAME </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">AS</font></font><font size="2"><font color="#000000"> [Tables sans PK sans FK] </font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#0000ff" size="2"><font color="#0000ff" size="2">FROM</font></font><font color="#000000" size="2"> </font><font color="#008000" size="2"><font color="#008000" size="2">INFORMATION_SCHEMA</font></font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#008000" size="2"><font color="#008000" size="2">tables</font></font><font size="2"><font color="#000000"> t </font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#0000ff" size="2"><font color="#0000ff" size="2">WHERE</font></font><font color="#000000" size="2"> </font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">OBJECTPROPERTY</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">OBJECT_ID</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#000000" size="2">TABLE_NAME</font><font color="#808080" size="2"><font color="#808080" size="2">),</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;IsPrimaryKey&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font size="2"><font color="#000000"> 0 </font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#808080" size="2"><font color="#808080" size="2">AND</font></font><font color="#000000" size="2"> </font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">OBJECTPROPERTY</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">OBJECT_ID</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#000000" size="2">TABLE_NAME</font><font color="#808080" size="2"><font color="#808080" size="2">),</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;TableHasPrimaryKey&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font size="2"><font color="#000000"> 0 </font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#808080" size="2"><font color="#808080" size="2">AND</font></font><font color="#000000" size="2"> </font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">OBJECTPROPERTY</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">OBJECT_ID</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#000000" size="2">TABLE_NAME</font><font color="#808080" size="2"><font color="#808080" size="2">),</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;IsForeignKey&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font size="2"><font color="#000000"> 0 </font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#808080" size="2"><font color="#808080" size="2">AND</font></font><font color="#000000" size="2"> </font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">OBJECTPROPERTY</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">OBJECT_ID</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#000000" size="2">TABLE_NAME</font><font color="#808080" size="2"><font color="#808080" size="2">),</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;TableHasForeignKey&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font size="2"><font color="#000000"> 0 </font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#808080" size="2"><font color="#808080" size="2">AND</font></font><font color="#000000" size="2"> </font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">OBJECTPROPERTY</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">OBJECT_ID</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#000000" size="2">TABLE_NAME</font><font color="#808080" size="2"><font color="#808080" size="2">),</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;TableHasForeignRef&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font size="2"><font color="#000000"> 0 </font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#808080" size="2"><font color="#808080" size="2">AND</font></font><font color="#000000" size="2"> </font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">OBJECTPROPERTY</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#ff00ff" size="2"><font color="#ff00ff" size="2">OBJECT_ID</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#000000" size="2">TABLE_NAME</font><font color="#808080" size="2"><font color="#808080" size="2">),</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;IsUserTable&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font size="2"><font color="#000000"> 1 </font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#0000ff" size="2"><font color="#0000ff" size="2">ORDER</font></font><font color="#000000" size="2"> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">BY</font></font><font size="2"><font color="#000000"> [Tables sans PK sans FK] </font></font></span></span></p>
</blockquote>
<blockquote>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">/*===========================================================================================</font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8211;&gt; PROCEDURES STOCKEES </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8211; Procdure Stocke permettant d&#39;afficher les tables sans cl primaire(PK) </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8211; SQL SERVER 2005-2008 </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8211; Version1</font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8211; Auteur : Etienne ZINZINDOHOUE</font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; Exemple d&#39;utilisation &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">EXEC usp_AfficheTableSansPK_2005_2008 @InstanceName = N&#39;MONSERVEUR\MONINSTANCE&#39;, @BaseName = N&#39;AdventureWorks&#39;</font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">===========================================================================================*/</font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">/***************************** PRE-REQUIS **********************************************</font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">Pour utiliser la procdure &#39;sp_primarykeys&#39; il faut configurer l&#39;option DATA ACCESS; du serveur</font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">En excutant la commande suivante :</font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">EXEC sp_serveroption &#39;MONSERVEUR\MONINSTANCE&#39;, &#39;data access&#39;, &#39;true&#39; </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">Si non lors de l&#39;excution de la procdure vous aurez le message d&#39;erreur :</font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">Msg 7411, Level 16, State 1, Procedure sp_primarykeys, Line 10</font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">Server &#39;MONSERVEUR\MONINSTANCE&#39; is not configured for DATA ACCESS.</font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8211;OU En franais </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">Msg 7411, Level 16, State 1, Procedure sp_primarykeys, Line 10</font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">Le serveur &#39;MONSERVEUR\MONINSTANCE&#39; n&#39;est pas configur pour DATA ACCESS.</font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#008000" size="2">****************************************************************************************/</font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">CREATE</font></font><font color="#000000" size="2"> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">PROCEDURE</font></font><font size="2"><font color="#000000"> usp_AfficheTableSansPK_2005_2008 </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font size="2">@InstanceName </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">VARCHAR</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font size="2">50</font><font color="#808080" size="2"><font color="#808080" size="2">),</font></font><font size="2">@BaseName </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">VARCHAR</font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font size="2">50</font><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font size="2"> </font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">AS</font></font><font size="2"><font color="#000000"> </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">BEGIN</font></font><font size="2"><font color="#000000"> </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">SET</font></font><font color="#000000" size="2"> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">NOCOUNT</font></font><font color="#000000" size="2"> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">ON</font></font><font size="2"><font color="#000000"> </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">DECLARE</font></font><font color="#000000" size="2"> @maTableVar </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">table </font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font size="2"><font color="#000000"> </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font size="2">TABLE_CAT </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">sysname</font></font><font size="2"> </font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">TABLE_SCHEM </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">sysname</font></font><font size="2"><font color="#000000"> </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">TABLE_NAME </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">sysname</font></font><font size="2"><font color="#000000"> </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">COLUMN_NAME </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">sysname</font></font><font size="2"><font color="#000000"> </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">KEY_SEQ </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">int</font></font><font size="2"><font color="#000000"> </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">,</font></font><font color="#000000" size="2">PK_NAME </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">sysname</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">NULL</font></font><font size="2"><font color="#000000"> </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#808080" size="2"><font color="#808080" size="2">)</font></font><font size="2"><font color="#000000"> </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">INSERT</font></font><font color="#000000" size="2"> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">INTO</font></font><font size="2"><font color="#000000"> @maTableVar </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">EXEC </font></font><font color="#808080" size="2"><font color="#808080" size="2">(</font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;EXEC sp_primarykeys @table_server = N&#39;&#39;&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2">@InstanceName </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;&#39;&#39;, @table_catalog = N&#39;&#39;&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2">@BaseName </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;&#39;&#39;&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">);</font></font><font size="2"><font color="#000000"> </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">SELECT</font></font><font color="#000000" size="2"> t</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">TABLE_CATALOG </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;.&#39;</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> t</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">TABLE_SCHEMA </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;.&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> t</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">TABLE_NAME </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">AS</font></font><font size="2"><font color="#000000"> [Base.Schema.Table] </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">FROM</font></font><font color="#000000" size="2"> </font><font color="#008000" size="2"><font color="#008000" size="2">INFORMATION_SCHEMA</font></font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#008000" size="2"><font color="#008000" size="2">TABLES</font></font><font size="2"><font color="#000000"> t </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">WHERE</font></font><font color="#000000" size="2"> t</font><font color="#808080" size="2"><font color="#808080" size="2">.</font></font><font color="#000000" size="2">TABLE_CATALOG </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> @BaseName </font><font color="#808080" size="2"><font color="#808080" size="2">AND</font></font><font color="#000000" size="2"> TABLE_TYPE </font><font color="#808080" size="2"><font color="#808080" size="2">=</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;BASE TABLE&#39;</font></font><font size="2"><font color="#000000"> </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">EXCEPT</font></font><font size="2"><font color="#000000"> </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">SELECT</font></font><font color="#000000" size="2"> </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">DISTINCT</font></font><font color="#000000" size="2"> TABLE_CAT </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> </font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;.&#39;</font></font><font color="#000000" size="2"> </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> TABLE_SCHEM </font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#ff0000" size="2"><font color="#ff0000" size="2">&#39;.&#39;</font></font><font color="#808080" size="2"><font color="#808080" size="2">+</font></font><font color="#000000" size="2"> TABLE_NAME </font><font color="#0000ff" size="2"><font color="#0000ff" size="2">AS</font></font><font size="2"><font color="#000000"> [Base.Schema.Table] </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">FROM</font></font><font size="2"><font color="#000000"> @maTableVar </font></font></font></font></span></span></p>
<p>
	<span style="font-family: courier new, courier, monospace"><span style="font-size: 8px"><font color="#008000" size="2"><font color="#008000" size="2"><font color="#0000ff" size="2"><font color="#0000ff" size="2">END</font></font><font size="2"><font color="#000000"> </font></font></font></font></span></span></p>
</blockquote>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
<strong>&#8211;> Discussion associée</strong><br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p><a href="http://www.developpez.net/forums/d1003616/bases-donnees/ms-sql-server/administration/table-pk-sgbdr-permissif-pourquoi/">http://www.developpez.net/forums/d1003616/bases-donnees/ms-sql-server/administration/table-pk-sgbdr-permissif-pourquoi/</a></p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
Etienne ZINZINDOHOUE<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Pour une bonne utilisation des indexes dans la clause WHERE</title>
		<link>https://blog.developpez.com/zinzineti/p9217/sql-server-2005/methode_d_identification_des_fonctions_q</link>
		<comments>https://blog.developpez.com/zinzineti/p9217/sql-server-2005/methode_d_identification_des_fonctions_q#comments</comments>
		<pubDate>Wed, 18 Aug 2010 16:49:08 +0000</pubDate>
		<dc:creator><![CDATA[zinzineti]]></dc:creator>
				<category><![CDATA[SQL SERVER 2000]]></category>
		<category><![CDATA[SQL SERVER 2005]]></category>
		<category><![CDATA[SQL SERVER 2008]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Le premier contrôle à effectuer en cas de lenteur d&#8217;une requête est l&#8217;analyse des indexes. Cette analyse oblige à se poser les questions suivantes : • 1. est ce que les bons indexes sont créés ? • 2. si oui est ce que la requête profite bien des l&#8217;index crées ? La réponse à ces questions nécessite une bonne connaissance de comment fonctionne l&#8217;optimiseur du SGBD. Créer les bons indexes ne veut pas dire créer [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Le premier contrôle à effectuer en cas de lenteur d&rsquo;une requête est l&rsquo;analyse des indexes. Cette analyse oblige à se poser les questions suivantes :<br />
• 1. est ce que les bons indexes sont créés ?<br />
• 2. si oui est ce que la requête profite bien des l&rsquo;index crées ?<br />
La réponse à ces questions nécessite une bonne connaissance de comment fonctionne l&rsquo;optimiseur du SGBD. Créer les bons indexes ne veut pas dire créer des indexes sur toutes les colonnes membres de la clause WHERE&#8230; Non, Non et Non ça ne marche pas comme ça. La réponse à la question 2. nécessite une bonne connaissance de ce que les Anglo-saxons appelent <strong>Search ARGument(SARG)</strong> ou argument de recherche. Dans ce billet nous allons examiner les précautions à prendre pour rendre la clause WHERE SARGable, c&rsquo;est à dire optimisée.<br />
<span id="more-20"></span></p>
<p>De façon générale, une clause WHERE SARGable est sous la forme :</p>
<p>•	<em><code class="codecolorer text default"><span class="text">Colonne &nbsp;opérateur &nbsp;&lt;constante ou variable&gt;</span></code> </em><br />
•	<em> <code class="codecolorer text default"><span class="text">&lt;constante ou variable&gt; &nbsp;opérateur Colonne</span></code></em></p>
<p>avec comme opérateur l&rsquo;une des valeurs suivantes :  </p>
<blockquote><p><code class="codecolorer text default"><span class="text">=, &gt;, &lt;, &gt;=, &lt;=, IN, BETWEEN et parfois LIKE (dans les cas des concordances des préfixes, tels que LIKE 'etie%')</span></code></p></blockquote>
<p>Est donc <strong>Non-SARG</strong> un WHERE contenant comme opérateur l&rsquo;une des valeurs suivantes : </p>
<blockquote><p><code class="codecolorer text default"><span class="text">NOT, &lt;&gt;, NOT EXISTS, NOT IN, NOT LIKE (LIKE '%nne' ou LIKE '%tie%')</span></code></p></blockquote>
<blockquote><p><strong>Remarque</strong> :<br />
===========<br />
 /!\ Le comportement de l&rsquo;opérateur <strong>LIKE </strong> varie en fonction de la version SQL SERVER et de l&rsquo;édition</p></blockquote>
<p>Pour la démo nous allons utiliser le jeu de données suivant :</p>
<p><strong>&#8211;Création de la table de test T_TEST</strong><br />
&#8211;============================================================</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">USE [MaBase] <br />
SET NOCOUNT ON <br />
IF OBJECT_ID('dbo.T_TEST') IS NOT NULL <br />
DROP TABLE dbo.T_TEST <br />
CREATE TABLE dbo.T_TEST <br />
( <br />
&nbsp; &nbsp; id int identity(1,1), <br />
&nbsp; &nbsp; val varchar(10), <br />
&nbsp; &nbsp; creation_date datetime <br />
)</div></div>
<p><strong>&#8211;Chargement d&rsquo;un million de lignes dans la table </strong>: Cette opération a duré environ 9 minutes sur ma machine (SQL2K8 Edition Entreprise (version Evaluation) &#8211; OS: WXP SP3 &#8211; 2CPU T5600 @1.83GHZ &#8211; Mémoire = 2Go )<br />
&#8211;============================================================</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">DECLARE @counter int; <br />
SET @counter = 1; <br />
WHILE @counter &lt;= 1000000 <br />
&nbsp; &nbsp;BEGIN <br />
&nbsp; &nbsp;INSERT INTO T_TEST(val,creation_date) VALUES(convert(varchar(10),(left(convert(bigint,RAND()*10000000),6))),getdate()); <br />
&nbsp; &nbsp; &nbsp; SET @counter = @counter + 1 <br />
END;</div></div>
<blockquote><p>
<strong>&#8211; Ce qu&rsquo;il faut savoir concernant le plan d&rsquo;exécution des requêtes</strong><br />
&#8211;============================================================<br />
• « <strong>Table Scan</strong> » indique la lecture complète de la table donc pas d&rsquo;index utilisé</p>
<p>•  « <strong>Index Scan</strong> » indique que la table est analysée à travers l&rsquo;index, cela ne veut pas dire que l&rsquo;index est utilisé pour accéder directement aux lignes individuelles. Autrement dit la requête ne profite pas de l&rsquo;indexe, c&rsquo;est un opérateur similaire à « table scan ».    </p>
<p>•  « <strong>Index Seek</strong> » indique que l&rsquo;optimiseur utilise l&rsquo;index à bon escient, utilisation correcte de l&rsquo;index par le moteur</p>
</blockquote>
<blockquote><p><strong> &#8211;> Conseil N° 1 : Éviter l&rsquo;usage des opérateurs Non-SARG dans la clause WHERE </strong><br />
&#8211;============================================================</p></blockquote>
<p><strong>&#8211;> Démo : Conseil N° 1</strong><br />
&#8212; ===============================================================================</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--Créons un index clustered sur la colonne val &nbsp;de la test T_TEST <br />
CREATE CLUSTERED INDEX IX_VAL ON dbo.T_TEST(val) <br />
--Soit la requête ci-dessous avec l'opérateur NOT IN <br />
SELECT val <br />
FROM T_TEST <br />
WHERE val NOT IN ('456','789','980')</div></div>
<p>&#8211;Résultat : le plan d&rsquo;exécution montre un <strong>Index Scan</strong> => la requête ne profite pas de l&rsquo;indexe</p>
<table style="width:auto;">
<tr>
<td><a href="http://picasaweb.google.fr/lh/photo/GdepvCc7YH0ECfSyfiDV9A?feat=embedwebsite"><img src="http://lh3.ggpht.com/_V0OXnmvK0ec/TG0WWOPl6pI/AAAAAAAAAVo/qCTHoSmEchk/s800/Demo1.jpg" /></a></td>
</tr>
<tr>
<td style="font-family:arial,sans-serif; font-size:11px; text-align:right"></td>
</tr>
</table>
<blockquote><p><strong> &#8211;> Conseil N° 2 : Éviter l&rsquo;usage des opérations arithmétiques sur les colonnes dans la clause WHERE </strong><br />
&#8211;============================================================</p></blockquote>
<p><strong>&#8211;> Démo : Conseil N° 2</strong><br />
&#8212; ===============================================================================</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--Supprimons l'index clustered IX_VAL &nbsp;créé dans la Démo 1 <br />
DROP INDEX IX_VAL ON dbo.T_TEST <br />
&nbsp;<br />
--Créons un index cluster sur la colonne ID <br />
CREATE CLUSTERED INDEX IX_ID ON dbo.T_TEST(id) <br />
--Soit la requête ci-dessous avec une opération de multiplication sur la colonne id <br />
&nbsp;<br />
SELECT id <br />
FROM T_TEST <br />
WHERE id * 100 = 10000</div></div>
<p>&#8211;Résultat : le plan d&rsquo;exécution montre un <strong>Index Scan</strong> => la requête ne profite pas de l&rsquo;indexe</p>
<table style="width:auto;">
<tr>
<td><a href="http://picasaweb.google.fr/lh/photo/UQcj8LW_o81w1q4uC0OdOA?feat=embedwebsite"><img src="http://lh6.ggpht.com/_V0OXnmvK0ec/TG0ZS5R1TvI/AAAAAAAAAVw/asarc9mkVPc/s800/Demo2.jpg" /></a></td>
</tr>
<tr>
<td style="font-family:arial,sans-serif; font-size:11px; text-align:right"></td>
</tr>
</table>
<blockquote><p><strong> &#8211;> Conseil N° 3 : Éviter l&rsquo;usage des fonctions sur les colonnes dans la clause WHERE </strong><br />
&#8211;============================================================</p></blockquote>
<p><strong>&#8211;> Démo : Conseil N° 3</strong><br />
&#8212; ===============================================================================</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--Gardons l'index clustered IX_ID &nbsp;créé dans la Démo 2 <br />
&nbsp;<br />
--Soit la requête ci-dessous avec une fonction convert appliquée sur la colonne id <br />
&lt;code&gt;SELECT id &nbsp;<br />
FROM T_TEST <br />
WHERE convert(varchar(10),id) = '123456'</div></div>
<p>&#8211;Résultat : le plan d&rsquo;exécution montre un <strong>Index Scan</strong> => la requête ne profite pas de l&rsquo;indexe</p>
<table style="width:auto;">
<tr>
<td><a href="http://picasaweb.google.fr/lh/photo/-0eH5ejkSb3E1QvAeq-DyA?feat=embedwebsite"><img src="http://lh4.ggpht.com/_V0OXnmvK0ec/TGv3oQfnTsI/AAAAAAAAAUw/zqDdt9nyPUA/s800/Ecran4.jpg" /></a></td>
</tr>
<tr>
<td style="font-family:arial,sans-serif; font-size:11px; text-align:right"></td>
</tr>
</table>
<p>Alors que la même requête sans la fonction convert sur la colonne id utilise bien l&rsquo;indexe créé => présence de l&rsquo;<strong>Index Seek</strong></p>
<table style="width:auto;">
<tr>
<td><a href="http://picasaweb.google.fr/lh/photo/_y7CrS_zahBnTldKcucj_A?feat=embedwebsite"><img src="http://lh6.ggpht.com/_V0OXnmvK0ec/TG0izjimUOI/AAAAAAAAAWM/17y-SLBn2rM/s800/Demo3Bis.jpg" /></a></td>
</tr>
<tr>
<td style="font-family:arial,sans-serif; font-size:11px; text-align:right"></td>
</tr>
</table>
<p><strong>Autre exemple avec les fonctions horodatages</strong></p>
<p>&#8212; Supprimons l&rsquo;index placé sur la colonne ID de la table T_TEST<br />
<code class="codecolorer text default"><span class="text">DROP INDEX IX_ID ON dbo.T_TEST</span></code></p>
<p>&#8211;Créons un index clustered sur la colonne &laquo;&nbsp;creation_date&nbsp;&raquo;<br />
<code class="codecolorer text default"><span class="text">CREATE CLUSTERED INDEX IX_creationdate ON dbo.T_TEST(creation_date)</span></code></p>
<p>&#8211;Vérifions le bon usage de l&rsquo;indexe crée</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">SELECT creation_date &nbsp;<br />
FROM T_TEST <br />
WHERE creation_date = getdate()</div></div>
<p>&#8211;Résultat : Présence d&rsquo;<strong>Index Seek</strong> => Bonne utilisation de l&rsquo;index crée</p>
<table style="width:auto;">
<tr>
<td><a href="http://picasaweb.google.fr/lh/photo/KIwh8bl6VnSf9MqNFE8mEA?feat=embedwebsite"><img src="http://lh4.ggpht.com/_V0OXnmvK0ec/TGv30CBACYI/AAAAAAAAAVI/v8pfO1Dhmow/s800/Ecran10.jpg" /></a></td>
</tr>
<tr>
<td style="font-family:arial,sans-serif; font-size:11px; text-align:right"></td>
</tr>
</table>
<p><strong>Appliquons maintenant des fonctions sur la colonne date</strong></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">SELECT creation_date &nbsp;<br />
FROM T_TEST <br />
WHERE year(creation_date) = 2010</div></div>
<p>&#8211;Résultat : le plan d&rsquo;exécution montre un <strong>Index Scan</strong> => la requête ne profite pas de l&rsquo;indexe</p>
<table style="width:auto;">
<tr>
<td><a href="http://picasaweb.google.fr/lh/photo/LaBfvOwWaycinesjNczG7w?feat=embedwebsite"><img src="http://lh4.ggpht.com/_V0OXnmvK0ec/TGv37mSaA-I/AAAAAAAAAVM/jqscU1aYC-0/s800/Ecran11.jpg" /></a></td>
</tr>
<tr>
<td style="font-family:arial,sans-serif; font-size:11px; text-align:right"></td>
</tr>
</table>
<p>On peut bien sûr rendre SARGable la requête précédente en l&rsquo;écrivant autrement.</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">SELECT creation_date &nbsp;<br />
FROM T_TEST <br />
WHERE creation_date &gt;= '2010-01-01' AND creation_date &lt;='2010-12-12'</div></div>
<p>&#8211;Résultat : Présence d&rsquo;<strong>Index Seek</strong> => Bonne utilisation de l&rsquo;index crée</p>
<table style="width:auto;">
<tr>
<td><a href="http://picasaweb.google.fr/lh/photo/k2Lf3UhNET69lVaJ9mpchQ?feat=embedwebsite"><img src="http://lh6.ggpht.com/_V0OXnmvK0ec/TG06fIkQeBI/AAAAAAAAAWs/FuNxLS3vXpg/s800/DemoYearModifie.jpg" /></a></td>
</tr>
<tr>
<td style="font-family:arial,sans-serif; font-size:11px; text-align:right"></td>
</tr>
</table>
<blockquote><p><strong> &#8211;> Conseil N° 4 : Éviter d&rsquo;avoir WHERE Table1.colonne1 <em>opérateur</em> Table1.colonne2  (colonne1 et colonne2 appartiennent à la même table) </strong><br />
&#8211;============================================================</p></blockquote>
<p><strong>&#8211;> Démo : Conseil N° 4</strong><br />
&#8212; ===============================================================================</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--Gardons l'index clustered IX_ID &nbsp;créé dans la Démo 2 <br />
&nbsp;<br />
--Soit la requête ci-dessous &nbsp;<br />
SELECT id &nbsp;<br />
FROM T_TEST <br />
WHERE id = val</div></div>
<p>&#8211;Résultat : le plan d&rsquo;exécution montre un <strong>Index Scan</strong> => la requête ne profite pas de l&rsquo;indexe<br />
&#8211;<strong>Remarque</strong> : dans notre cas la colonnes id (int) et val (varchar) ne sont pas typées pareil. Et pourtant la requête s&rsquo;exécute sans erreur. Ceci parce que l&rsquo;optimiseur a fait une conversion implicite. Il faut a tout prix éviter ce genre de chose  c&rsquo;est à dire respecter les types de données car ceci a d&rsquo;impact sur la performance de la requête.<br />
Pour notre Démo, si les colonnes id et val sont du même type on a également un Index Scan.    </p>
<table style="width:auto;">
<tr>
<td><a href="http://picasaweb.google.fr/lh/photo/ekrkmLt7n0E8RqyJwN_9LA?feat=embedwebsite"><img src="http://lh5.ggpht.com/_V0OXnmvK0ec/TG0poCUX03I/AAAAAAAAAWU/_ACUXvHDdjM/s800/Demo4.jpg" /></a></td>
</tr>
<tr>
<td style="font-family:arial,sans-serif; font-size:11px; text-align:right"></td>
</tr>
</table>
<blockquote><p><strong> &#8211;> Conseil N° 5 : Éviter d&rsquo;avoir dans la clause WHERE <em>Non-SARG OR SARG</em> </strong><br />
&#8211;============================================================</p></blockquote>
<p><strong>&#8211;> Démo : Conseil N° 5</strong><br />
&#8212; ===============================================================================</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--Gardons l'index clustered IX_ID &nbsp;créé dans la Démo 2 <br />
&nbsp;<br />
--Soit la requête ci-dessous &nbsp;<br />
SELECT id <br />
FROM T_TEST <br />
WHERE id = 123456 OR convert(varchar,id) ='456'</div></div>
<p>&#8211;Résultat : le plan d&rsquo;exécution montre un <strong>Index Scan</strong> => la requête ne profite pas de l&rsquo;indexe</p>
<table style="width:auto;">
<tr>
<td><a href="http://picasaweb.google.fr/lh/photo/cdzxcJCjG9xoZFCKXF0doA?feat=embedwebsite"><img src="http://lh6.ggpht.com/_V0OXnmvK0ec/TG0wiVUbZRI/AAAAAAAAAWc/46Ag4FrOlfM/s800/Demo5.jpg" /></a></td>
</tr>
<tr>
<td style="font-family:arial,sans-serif; font-size:11px; text-align:right"></td>
</tr>
</table>
<p>Par contre un <strong>WHERE Non-SARG AND SARG peut être SARGable  </strong><br />
Voici un exemple</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">SELECT id <br />
FROM T_TEST <br />
WHERE id = 123456 &nbsp;<br />
AND SUBSTRING(convert(varchar,id),1,3) ='123'</div></div>
<table style="width:auto;">
<tr>
<td><a href="http://picasaweb.google.fr/lh/photo/yrXcqesXDIiP4ynH7nZ-7A?feat=embedwebsite"><img src="http://lh5.ggpht.com/_V0OXnmvK0ec/TG0yKR5-gjI/AAAAAAAAAWk/3o_pqqjDnxY/s800/Demo5Bis.jpg" /></a></td>
</tr>
<tr>
<td style="font-family:arial,sans-serif; font-size:11px; text-align:right"></td>
</tr>
</table>
<blockquote><p><strong> &#8211;> Conseil N° 6 : N&rsquo;oublier pas les exceptions suivantes : </strong><br />
&#8211;============================================================</p></blockquote>
<p><strong>Fonction ISNULL </strong><br />
===============================<br />
&#8212; Supprimons tous les index précédents [pour être sûr qu&rsquo;il y a rien qui traine ;-)]</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">DROP INDEX IX_VAL ON dbo.T_TEST <br />
DROP INDEX IX_ID ON dbo.T_TEST <br />
DROP INDEX IX_creationdate ON dbo.T_TEST</div></div>
<p>&#8211;Créons un index sur la colonne id<br />
<code class="codecolorer text default"><span class="text">CREATE CLUSTERED INDEX IX_ID ON dbo.T_TEST(id)</span></code></p>
<p>&#8211;Exécutons la requête</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">SELECT id <br />
FROM T_TEST <br />
WHERE ISNULL(id,123456) = 123456</div></div>
<p>&#8211;Résultat : Présence d&rsquo;<strong>Index Seek</strong> => Bonne utilisation de l&rsquo;index crée malgré l&rsquo;application de la fonction ISNULL à la colonne id !</p>
<table style="width:auto;">
<tr>
<td><a href="http://picasaweb.google.fr/lh/photo/7V9CsCGhLa5_aFFVE1kkRQ?feat=embedwebsite"><img src="http://lh4.ggpht.com/_V0OXnmvK0ec/TG0_t1v_qrI/AAAAAAAAAW0/flJmRX3khq0/s800/Demo6_1.jpg" /></a></td>
</tr>
<tr>
<td style="font-family:arial,sans-serif; font-size:11px; text-align:right"></td>
</tr>
</table>
<p><strong>Fonction IS NULL </strong><br />
===============================<br />
&#8211;Supprimons l&rsquo;index IX_ID<br />
<code class="codecolorer text default"><span class="text">DROP INDEX IX_ID ON dbo.T_TEST</span></code></p>
<p>&#8211;Créons l&rsquo;index sur la colonne val<br />
<code class="codecolorer text default"><span class="text">CREATE CLUSTERED INDEX IX_VAL ON dbo.T_TEST(val)</span></code></p>
<p>&#8212; Exécutons la requête</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">SELECT val <br />
FROM T_TEST <br />
WHERE val IS NULL</div></div>
<p>&#8211;Résultat : Présence d&rsquo;<strong>Index Seek</strong> => Bonne utilisation de l&rsquo;index crée !</p>
<table style="width:auto;">
<tr>
<td><a href="http://picasaweb.google.fr/lh/photo/Y_3QxKmAO3Rq850uIFA5Vw?feat=embedwebsite"><img src="http://lh4.ggpht.com/_V0OXnmvK0ec/TG1DT2IPIqI/AAAAAAAAAW8/y4JSlwg_dMo/s800/Demo6_2.jpg" /></a></td>
</tr>
<tr>
<td style="font-family:arial,sans-serif; font-size:11px; text-align:right"></td>
</tr>
</table>
<p><strong>Fonction IS NOT NULL </strong><br />
===============================<br />
Comme précédemment on a il y a utilisation de l&rsquo;index pour la requête</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">SELECT val <br />
FROM T_TEST <br />
WHERE val IS NOT NULL</div></div>
<table style="width:auto;">
<tr>
<td><a href="http://picasaweb.google.fr/lh/photo/e4loUdT_Vhw6Gx4evrlunw?feat=embedwebsite"><img src="http://lh4.ggpht.com/_V0OXnmvK0ec/TG1D_xcXnUI/AAAAAAAAAXA/MO0dJBRaHIY/s800/Demo6_3.jpg" /></a></td>
</tr>
<tr>
<td style="font-family:arial,sans-serif; font-size:11px; text-align:right"></td>
</tr>
</table>
<blockquote><p><strong> &#8211;> L&rsquo;opérateur &laquo;&nbsp;IN&nbsp;&raquo; est-il SARGable ? </strong><br />
&#8211;============================================================</p></blockquote>
<p>&#8211;Supprimons tous les indexes créés précédement</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">DROP INDEX IX_VAL ON dbo.T_TEST <br />
DROP INDEX IX_ID ON dbo.T_TEST <br />
DROP INDEX IX_creationdate ON dbo.T_TEST</div></div>
<p>&#8212; Créons à nouveau l&rsquo;indexe sur la colonne val<br />
<code class="codecolorer text default"><span class="text">CREATE CLUSTERED INDEX IX_VAL ON dbo.T_TEST(val)</span></code></p>
<p>&#8212; Testons la requête suivante :</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">SELECT val <br />
FROM T_TEST <br />
WHERE val IN ('123','456','789')</div></div>
<p>&#8211;Résultat : Présence d&rsquo;<strong>Index Seek</strong> => Bonne utilisation de l&rsquo;index crée !</p>
<table style="width:auto;">
<tr>
<td><a href="http://picasaweb.google.fr/lh/photo/hZLbVZDy6yrhWgQ79cV3jg?feat=embedwebsite"><img src="http://lh5.ggpht.com/_V0OXnmvK0ec/TG5GRTZV4DI/AAAAAAAAAXM/HCHBwsQEwHc/s800/DemoIn2.jpg" /></a></td>
</tr>
<tr>
<td style="font-family:arial,sans-serif; font-size:11px; text-align:right"></td>
</tr>
</table>
<p><strong>On voit donc dans cet exemple que l&rsquo;opérateur IN est SARGable dans les conditions de notre test (SQL 2008 version Entreprise)</strong></p>
<blockquote><p><strong> &#8211;> L&rsquo;opérateur &laquo;&nbsp;OR&nbsp;&raquo; est-il SARGable ? </strong><br />
&#8211;============================================================</p></blockquote>
<p>&#8211;Gardons toujours l&rsquo;index IX_VAL précédemment crée puis examinons la requête suivante</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">SELECT val <br />
FROM T_TEST <br />
WHERE val = '123' OR val = '456' OR val ='789'</div></div>
<p>&#8211;Résultat : Présence d&rsquo;<strong>Index Seek</strong> => Bonne utilisation de l&rsquo;index crée !</p>
<table style="width:auto;">
<tr>
<td><a href="http://picasaweb.google.fr/lh/photo/8U6Ze2l-Qt72s93eQrQj1A?feat=embedwebsite"><img src="http://lh3.ggpht.com/_V0OXnmvK0ec/TG5LI2QNafI/AAAAAAAAAXU/JduJm-r0dkQ/s800/DemoOR.jpg" /></a></td>
</tr>
<tr>
<td style="font-family:arial,sans-serif; font-size:11px; text-align:right"></td>
</tr>
</table>
<p><strong>On voit donc dans cet exemple que l&rsquo;opérateur OR est SARGable dans les conditions de notre test (SQL 2008 version Entreprise)</strong></p>
<p>===========================================================================================================================<br />
Il y a encore beaucoup de choses à dire sur l&rsquo;optimisation des requêtes, là c&rsquo;est juste une petite analyse sous le prisme de la clause WHERE. On peut également analyser les clauses <strong>JOIN </strong>,<strong>HAVING </strong>, condition de trie <strong>ORDER BY</strong> et les <strong>fonctions d&rsquo;aggrégations</strong>.</p>
<p>===========================================================================================================================<br />
Vos commentaires sont les bienvenus <img src="https://blog.developpez.com/zinzineti/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley" /></p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SUPPRESSION DES DONNEES DANS DES TABLES PARENT-ENFANT</title>
		<link>https://blog.developpez.com/zinzineti/p9033/sql-server-2005/suppression_des_donnees_dans_des_tables_2</link>
		<comments>https://blog.developpez.com/zinzineti/p9033/sql-server-2005/suppression_des_donnees_dans_des_tables_2#comments</comments>
		<pubDate>Mon, 21 Jun 2010 11:57:08 +0000</pubDate>
		<dc:creator><![CDATA[zinzineti]]></dc:creator>
				<category><![CDATA[SQL SERVER 2000]]></category>
		<category><![CDATA[SQL SERVER 2005]]></category>
		<category><![CDATA[SQL SERVER 2008]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Il arrive souvent que le modèle de données d&#8217;une base SQL SERVER ne prévoit pas la suppression en cascade ou la mise à jour en cascade entre les tables PARENT-ENFANT. Pour effectuer donc un DELETE ou un UPDATE dans la table PARENT il faut d&#8217;abord faire en amont la même opération dans toutes les tables ENFANTS. Comment faire donc un DELETE ou UPDATE en cascade sans toucher au modèle de données et sans trigger ? [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Il arrive souvent que le modèle de données d&rsquo;une base SQL SERVER ne prévoit pas la suppression en cascade ou la mise à jour en cascade entre les tables PARENT-ENFANT. Pour effectuer donc un DELETE ou un UPDATE dans la table PARENT il faut d&rsquo;abord faire en amont la même opération dans toutes les tables ENFANTS. Comment faire donc un DELETE ou UPDATE en cascade sans toucher au modèle de données et sans trigger ?<br />
<span id="more-109"></span><br />
Nous allons utiliser la base <strong>AdventureWorks</strong> et les tables :<br />
&#8211; <strong>HumanResources.Department</strong><br />
&#8211; <strong>HumanResources.EmployeeDepartmentHistory</strong></p>
<p>voici le script de création de ces tables :</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">-- Table HumanResources.Department <br />
CREATE TABLE HumanResources.Department <br />
( <br />
DepartmentID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SMALLINT IDENTITY(1,1) NOT NULL, <br />
Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NAME NOT NULL, <br />
GroupName &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NAME NOT NULL, <br />
ModifiedDate &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DATETIME DEFAULT (getdate()) NOT NULL, <br />
CONSTRAINT PK_Department_DepartmentID PRIMARY KEY CLUSTERED ( DepartmentID ) <br />
) <br />
ON [PRIMARY] <br />
&nbsp;<br />
-- Table HumanResources.EmployeeDepartmentHistory <br />
CREATE TABLE HumanResources.EmployeeDepartmentHistory <br />
( <br />
EmployeeID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;INT NOT NULL, <br />
DepartmentID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SMALLINT NOT NULL, <br />
ShiftID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TINYINT NOT NULL, <br />
StartDate &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DATETIME NOT NULL, <br />
EndDate &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DATETIME, <br />
ModifiedDate &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DATETIME DEFAULT (getdate()) NOT NULL, <br />
CONSTRAINT CK_EmployeeDepartmentHistory_EndDate CHECK ([EndDate]&gt;=[StartDate] OR [EndDate] IS NULL), <br />
CONSTRAINT PK_EmployeeDepartmentHistory_EmployeeID_StartDate_DepartmentID PRIMARY KEY CLUSTERED ( EmployeeID,StartDate,DepartmentID,ShiftID ) <br />
) <br />
ON [PRIMARY] <br />
/*-------- Foreign Keys-----------------*/ <br />
ALTER TABLE HumanResources.EmployeeDepartmentHistory <br />
&nbsp; &nbsp; ADD CONSTRAINT FK_EmployeeDepartmentHistory_Department_DepartmentID FOREIGN KEY( DepartmentID ) <br />
&nbsp; &nbsp; REFERENCES [HumanResources].[Department] ( DepartmentID ) <br />
&nbsp; &nbsp; ON UPDATE NO ACTION <br />
&nbsp; &nbsp; ON DELETE NO ACTION; <br />
ALTER TABLE HumanResources.EmployeeDepartmentHistory <br />
&nbsp; &nbsp; ADD CONSTRAINT FK_EmployeeDepartmentHistory_Employee_EmployeeID FOREIGN KEY( EmployeeID ) <br />
&nbsp; &nbsp; REFERENCES [HumanResources].[Employee] ( EmployeeID ) <br />
&nbsp; &nbsp; ON UPDATE NO ACTION <br />
&nbsp; &nbsp; ON DELETE NO ACTION; <br />
ALTER TABLE HumanResources.EmployeeDepartmentHistory <br />
&nbsp; &nbsp; ADD CONSTRAINT FK_EmployeeDepartmentHistory_Shift_ShiftID FOREIGN KEY( ShiftID ) <br />
&nbsp; &nbsp; REFERENCES [HumanResources].[Shift] ( ShiftID ) <br />
&nbsp; &nbsp; ON UPDATE NO ACTION <br />
&nbsp; &nbsp; ON DELETE NO ACTION;</div></div>
<p><strong>1. ) Erreur générée lors du DELETE aveugle dans la table HumanResources.Department</strong><br />
Quand on exécute directement la commande :</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">DELETE FROM HumanResources.Department <br />
WHERE GroupName = 'Research and Development'</div></div>
<p>On a cette erreur :<br />
<strong>L&rsquo;instruction DELETE est en conflit avec la contrainte REFERENCE &laquo;&nbsp;FK_EmployeeDepartmentHistory_Department_DepartmentID&nbsp;&raquo;.<br />
Le conflit s&rsquo;est produit dans la base de données &laquo;&nbsp;AdventureWorks&nbsp;&raquo;, table &laquo;&nbsp;HumanResources.EmployeeDepartmentHistory&nbsp;&raquo;, column &lsquo;DepartmentID&rsquo;.<br />
L&rsquo;instruction a été arrêtée.</strong></p>
<p><strong>2. ) La solution revient à faire d&rsquo;abord le DELETE approprié dans la table ENFANT HumanResources.EmployeeDepartmentHistory avant la suppression dans la table PARENT HumanResources.Department</strong></p>
<p>Voici le script T-SQL pour cette opération :</p>
<blockquote>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">BEGIN TRANSACTION <br />
&nbsp;<br />
--&gt; DELETE DE LA TABLE ENFANT &nbsp;<br />
DELETE FROM HumanResources.EmployeeDepartmentHistory &nbsp;<br />
WHERE &nbsp;HumanResources.EmployeeDepartmentHistory.DepartmentID <br />
IN &nbsp;<br />
(SELECT Department.DepartmentID <br />
FROM HumanResources.Department Department INNER JOIN HumanResources.EmployeeDepartmentHistory EmployeeDepartmentHistory &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ON Department.DepartmentID = EmployeeDepartmentHistory.DepartmentID <br />
WHERE Department.GroupName = 'Research and Development' <br />
) <br />
--&gt; DELETE TABLE PARENT &nbsp; &nbsp; &nbsp;<br />
DELETE FROM HumanResources.Department <br />
WHERE GroupName = 'Research and Development' <br />
&nbsp;<br />
COMMIT</div></div>
</blockquote>
<p>*********************************************************<br />
Auteur : Etienne ZINZINDOHOUE<br />
*********************************************************</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Curseur plus performant que SELECT TOP 1 &#8230;DELETE</title>
		<link>https://blog.developpez.com/zinzineti/p9105/sql-server-2005/curseur_plus_performant_que_select_top_1</link>
		<comments>https://blog.developpez.com/zinzineti/p9105/sql-server-2005/curseur_plus_performant_que_select_top_1#comments</comments>
		<pubDate>Mon, 12 Jul 2010 09:05:37 +0000</pubDate>
		<dc:creator><![CDATA[zinzineti]]></dc:creator>
				<category><![CDATA[SQL SERVER 2000]]></category>
		<category><![CDATA[SQL SERVER 2005]]></category>
		<category><![CDATA[SQL SERVER 2008]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Depuis un certain temps je fais tout pour éviter d&#8217;utiliser les curseurs de façon explicite. la solution que j&#8217;ai trouvée et qui marche presque à tous les coups est la suivante : 1) Création de table temporaire (#Tmptable) 2) Remplissage de la table temporaire 3) WHILE EXISTS ( SELECT &#8230; FROM #Tmptable) BEGIN SET @ligneAtraiter = SELECT TOP 1 &#8230; FROM #Tmptable instrustion à exécuter avec @ligneAtraiter DROP de la ligne traitée dans la table [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Depuis un certain temps je fais tout pour éviter d&rsquo;utiliser les curseurs de façon explicite.<br />
la solution que j&rsquo;ai trouvée et qui marche presque à tous les coups est la suivante :<br />
1) Création de table temporaire (#Tmptable)<br />
2) Remplissage de la table temporaire<br />
3) WHILE EXISTS ( SELECT &#8230; FROM #Tmptable)  <br />
BEGIN<br />
   SET @ligneAtraiter = SELECT TOP 1 &#8230; FROM #Tmptable<br />
   instrustion à exécuter avec @ligneAtraiter<br />
   DROP de la ligne traitée dans la table #Tmptable<br />
END<br />
4) DROP de la table #Tmptable à la fin <br />
Et j&rsquo;en étais fier de ne pas utiliser les curseurs. Et les arguments ne manquent pas pour justifier ce choix&#8230;Mais à ma grande déception le curseur explicite est plus performant que mes SELECT TOP1 &#8230; DELETE<br />
Voici la démonstration :<br />
<span id="more-111"></span><br />
<strong>Préparation : Mesure de la durée d&rsquo;exécution et pages logiques lues</strong><br />
=========================================================================================<br />
D&rsquo;abord j&rsquo;ai préparé une instruction pour mesurer le temps CPU et le nombre de pages logiques lues par mes requêtes.</p>
<blockquote>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">--&gt;Initialisation des mesures &lt;br /&gt;<br />
SET NOCOUNT ON &lt;br /&gt;<br />
DECLARE @cpu_timedebut INT &lt;br /&gt;<br />
DECLARE @logical_reads_debut INT &lt;br /&gt;<br />
BEGIN &lt;br /&gt;<br />
DBCC DROPCLEANBUFFERS &lt;br /&gt;<br />
DBCC FREEPROCCACHE &lt;br /&gt;<br />
DBCC FREESYSTEMCACHE ('ALL') &lt;br /&gt;<br />
SELECT @cpu_timedebut = cpu_time &nbsp; &nbsp; &nbsp; -- Temps CPU (en millisecondes) utilisé par la demande &lt;br /&gt;<br />
, @logical_reads_debut = logical_reads -- Nombre de lectures logiques effectuées par la demande &lt;br /&gt;<br />
FROM sys.dm_exec_requests &lt;br /&gt;<br />
WHERE session_id = @@spid &nbsp;&lt;br /&gt;<br />
&nbsp;&lt;br /&gt;<br />
--&gt;====== Début de l'instrustion à mesurer &nbsp;====== &lt;br /&gt;<br />
..... &lt;br /&gt;<br />
..... &lt;br /&gt;<br />
..... &lt;br /&gt;<br />
--&gt;====== Fin de l'instrustion à mesurer &nbsp;====== &nbsp;&lt;br /&gt;<br />
&nbsp;&lt;br /&gt;<br />
--&gt;Afficher les mesures &lt;br /&gt;<br />
SELECT cpu_time - @cpu_timedebut AS [Temps CPU (ms)] &nbsp;&lt;br /&gt;<br />
, logical_reads - @logical_reads_debut AS [Nb Lectures Logiques] &lt;br /&gt;<br />
FROM sys.dm_exec_requests &lt;br /&gt;<br />
WHERE session_id = @@spid ;</div></div>
</blockquote>
<p><strong>Préparation : Configuration de ma machine de test </strong><br />
=========================================================================================</p>
<blockquote><p>
Nom du système d&rsquo;exploitation : Microsoft Windows XP Professionnel<br />
Version du système : 5.1.2600 Service Pack 3 version 2600<br />
Processeur(s):  1 processeur(s) installé(s).[01]: x86 Family 6 Model 15 Stepping 6 GenuineIntel ~1828 MHz     <br />
Mémoire physique totale : 2 046 Mo
</p></blockquote>
<p><strong>Mesure pour le curseur </strong><br />
=========================================================================================<br />
Scénario : afficher toutes les bases de données de mon instance dont le nom commence par AdventureWorks. Dans mon cas j&rsquo;ai la liste suivante qui s&rsquo;affiche :<br />
AdventureWorks<br />
AdventureWorksDW<br />
AdventureWorksLT<br />
AdventureWorksLT2008</p>
<p></p>
<blockquote>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">/******************************************************************************************** &lt;br /&gt;<br />
DESCRIPTION : Utilisation du CURSEUR pour afficher toutes les bases de données de mon instance dont le nom commence par AdventureWorks. &nbsp;&lt;br /&gt;<br />
Auteur : Etienne ZINZINDOHOUE &lt;br /&gt;<br />
*********************************************************************************************/ &lt;br /&gt;<br />
--Initialisation des mesures &lt;br /&gt;<br />
SET NOCOUNT ON &lt;br /&gt;<br />
DECLARE @cpu_timedebut INT &lt;br /&gt;<br />
DECLARE @logical_reads_debut INT &lt;br /&gt;<br />
BEGIN &lt;br /&gt;<br />
DBCC DROPCLEANBUFFERS &lt;br /&gt;<br />
DBCC FREEPROCCACHE &lt;br /&gt;<br />
DBCC FREESYSTEMCACHE ('ALL') &lt;br /&gt;<br />
&nbsp;&lt;br /&gt;<br />
SELECT @cpu_timedebut = cpu_time &nbsp; &nbsp; &nbsp; -- Temps CPU (en millisecondes) utilisé par l'instrustion &lt;br /&gt;<br />
, @logical_reads_debut = logical_reads -- Nombre de lectures logiques effectuées par l'instrustion &lt;br /&gt;<br />
FROM sys.dm_exec_requests &lt;br /&gt;<br />
WHERE session_id = @@spid &nbsp;&lt;br /&gt;<br />
&nbsp;&lt;br /&gt;<br />
--====== DEBUT DE L'INSTRUSTION &nbsp;====== &lt;br /&gt;<br />
DECLARE @BaseEnCoursTraitement VARCHAR(50); &lt;br /&gt;<br />
DECLARE baseATraiter_cursor CURSOR STATIC LOCAL FORWARD_ONLY FOR &lt;br /&gt;<br />
SELECT [name] AS [NomBase] &lt;br /&gt;<br />
FROM sys.databases &lt;br /&gt;<br />
WHERE [name] LIKE 'AdventureWorks%' &lt;br /&gt;<br />
ORDER BY [NomBase] &lt;br /&gt;<br />
&nbsp;&lt;br /&gt;<br />
OPEN baseATraiter_cursor &lt;br /&gt;<br />
FETCH NEXT FROM baseATraiter_cursor INTO @BaseEnCoursTraitement &lt;br /&gt;<br />
WHILE @@FETCH_STATUS = 0 &lt;br /&gt;<br />
BEGIN &lt;br /&gt;<br />
PRINT @BaseEnCoursTraitement &lt;br /&gt;<br />
-- Au suivant &lt;br /&gt;<br />
FETCH NEXT FROM baseATraiter_cursor INTO @BaseEnCoursTraitement &lt;br /&gt;<br />
END &lt;br /&gt;<br />
CLOSE baseATraiter_cursor &lt;br /&gt;<br />
DEALLOCATE baseATraiter_cursor &lt;br /&gt;<br />
--====== FIN DE L'INSTRUSTION ====== &lt;br /&gt;<br />
--Afficher les mesures &lt;br /&gt;<br />
SELECT cpu_time - @cpu_timedebut AS [Temps CPU (ms)] &nbsp;&lt;br /&gt;<br />
, logical_reads - @logical_reads_debut AS [Nb Lectures Logiques] &lt;br /&gt;<br />
FROM sys.dm_exec_requests &lt;br /&gt;<br />
WHERE session_id = @@spid ; &lt;br /&gt;<br />
END &nbsp;&lt;br /&gt;</div></div>
</blockquote>
<p><strong>Mesure pour le SELECT TOP 1 &#8230; DELETE </strong><br />
=========================================================================================</p>
<blockquote>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;&lt;br /&gt;<br />
/***************************************************************************************** &lt;br /&gt;<br />
DESCRIPTION : Utilisation du SELECT TOP 1 ... DELETE pour afficher toutes les bases de données de mon instance dont le nom commence par AdventureWorks. &nbsp;&lt;br /&gt;<br />
Auteur : Etienne ZINZINDOHOUE &nbsp;&lt;br /&gt;<br />
*****************************************************************************************/ &lt;br /&gt;<br />
--Initialisation des mesures &lt;br /&gt;<br />
SET NOCOUNT ON &lt;br /&gt;<br />
DECLARE @cpu_timedebut INT &lt;br /&gt;<br />
DECLARE @logical_reads_debut INT &lt;br /&gt;<br />
BEGIN &lt;br /&gt;<br />
DBCC DROPCLEANBUFFERS &lt;br /&gt;<br />
DBCC FREEPROCCACHE &lt;br /&gt;<br />
DBCC FREESYSTEMCACHE ('ALL') &lt;br /&gt;<br />
&nbsp;&lt;br /&gt;<br />
SELECT @cpu_timedebut = cpu_time &nbsp; &nbsp; &nbsp; -- Temps CPU (en millisecondes) utilisé par la demande &lt;br /&gt;<br />
, @logical_reads_debut = logical_reads -- Nombre de lectures logiques effectuées par la demande &lt;br /&gt;<br />
FROM sys.dm_exec_requests &lt;br /&gt;<br />
WHERE session_id = @@spid &nbsp;&lt;br /&gt;<br />
&nbsp;&lt;br /&gt;<br />
--====== DEBUT DE L'INSTRUSTION &nbsp;====== &lt;br /&gt;<br />
DECLARE @BaseEnCoursTraitement VARCHAR(50),@SQL VARCHAR (max), @DEL VARCHAR (max) &nbsp;&lt;br /&gt;<br />
CREATE TABLE #LISTE_BASES (nom_base varchar (50)) &nbsp;&lt;br /&gt;<br />
INSERT INTO #LISTE_BASES &nbsp; &lt;br /&gt;<br />
SELECT [name] AS [NomBase] &nbsp;&lt;br /&gt;<br />
FROM sys.databases &nbsp;&lt;br /&gt;<br />
WHERE [name] LIKE 'AdventureWorks%' &lt;br /&gt;<br />
ORDER BY [NomBase] &nbsp;&lt;br /&gt;<br />
-- Tant qu'il y a des lignes dans la table temporaire &nbsp;&lt;br /&gt;<br />
WHILE EXISTS ( SELECT nom_base &nbsp;&lt;br /&gt;<br />
&nbsp; FROM #LISTE_BASES &nbsp;&lt;br /&gt;<br />
&nbsp; ) &nbsp; &lt;br /&gt;<br />
BEGIN &nbsp;&lt;br /&gt;<br />
--Se positionner sur la 1ère ligne de la table temporaire &nbsp;&lt;br /&gt;<br />
SET @BaseEnCoursTraitement = (SELECT TOP 1 nom_base FROM #LISTE_BASES) &nbsp;&lt;br /&gt;<br />
-- Appliquer le Niveau de compatibilité SQL 2005 &nbsp;&lt;br /&gt;<br />
PRINT @BaseEnCoursTraitement &lt;br /&gt;<br />
-- Supprimer la base traitée de la table temporaire &nbsp;&lt;br /&gt;<br />
SET @DEL = 'DELETE FROM #LISTE_BASES WHERE nom_base = '''+ @BaseEnCoursTraitement + ''';' &nbsp;&lt;br /&gt;<br />
EXEC (@DEL) &nbsp;&lt;br /&gt;<br />
END &nbsp;&lt;br /&gt;<br />
--Supprimer la table temporaire &nbsp;&lt;br /&gt;<br />
DROP TABLE #LISTE_BASES &nbsp; &lt;br /&gt;<br />
--====== FIN DE L'INSTRUSTION ====== &lt;br /&gt;<br />
--Afficher les mesures &lt;br /&gt;<br />
SELECT cpu_time - @cpu_timedebut AS [Temps CPU (ms)] &nbsp;&lt;br /&gt;<br />
, logical_reads - @logical_reads_debut AS [Nb Lectures Logiques] &lt;br /&gt;<br />
FROM sys.dm_exec_requests &lt;br /&gt;<br />
WHERE session_id = @@spid ; &lt;br /&gt;<br />
END</div></div>
<p>
</p></blockquote>
<p><strong>Résultats </strong><br />
=========================================================================================</p>
<p><strong>&#8211;#### 1er Essai ###</strong></p>
<blockquote><p><strong>&#8211;> Avec Le curseur</strong><br />
Temps CPU (ms)	   Nb Lectures Logiques<br />
0                                 85</p>
<p><strong>&#8211;> Avec Le SELECT TOP 1 &#8230;DELETE</strong><br />
Temps CPU (ms)	   Nb Lectures Logiques<br />
47                                	          477</p></blockquote>
<p><strong>&#8211;#### 2ème Essai ###</strong></p>
<blockquote><p><strong>&#8211;> Avec Le curseur</strong><br />
Temps CPU (ms)	  Nb Lectures Logiques<br />
0                                85</p>
<p><strong>&#8211;> Avec Le SELECT TOP 1 &#8230;DELETE</strong><br />
Temps CPU (ms)  	Nb Lectures Logiques<br />
78                                471
</p></blockquote>
<p><strong>&#8211;#### 3ème Essai ###</strong></p>
<blockquote><p><strong>&#8211;> Avec Le curseur</strong><br />
Temps CPU (ms)  Nb Lectures Logiques<br />
0                                85</p>
<p><strong>&#8211;> Avec Le SELECT TOP 1 &#8230;DELETE</strong><br />
Temps CPU (ms)  Nb Lectures Logiques<br />
47                                472
</p></blockquote>
<p>
<strong>Pour les 3 essais le constat est que : le CURSEUR est plus performant que mes SELECT TOP 1 &#8230; DELETE</strong></p>
<p></p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MONITORING DES SERVICES SQL DE TOUTES LES INSTANCES DE BASE DE DONNEES</title>
		<link>https://blog.developpez.com/zinzineti/p9065/sql-server-2005/monitoring_des_services_sql_de_toutes_le_2</link>
		<comments>https://blog.developpez.com/zinzineti/p9065/sql-server-2005/monitoring_des_services_sql_de_toutes_le_2#comments</comments>
		<pubDate>Wed, 30 Jun 2010 17:16:49 +0000</pubDate>
		<dc:creator><![CDATA[zinzineti]]></dc:creator>
				<category><![CDATA[POWERSHELL - SQLSERVER]]></category>
		<category><![CDATA[SQL SERVER 2000]]></category>
		<category><![CDATA[SQL SERVER 2005]]></category>
		<category><![CDATA[SQL SERVER 2008]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Comment monitorer les principaux services SQL de tous les serveurs de base de données du réseau ? Les différentes étapes : 0.) Configurer votre poste de façon à pouvoir exécuter un script Powershell. J&#8217;ai écrit un petit billet pour ça ICI 1.) Créer un fichier contenant les informations d&#8217;accès aux serveurs Ce fichier contenant la liste des serveurs nommée : listeserveurs.txt a la structure suivante : AdresseIP,Domaine\Login,Password,NomInstance AdresseIP = Adresse IP (ou nom) du serveur [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Comment monitorer les principaux services SQL de tous les serveurs de base de données du réseau ?<br />
<span id="more-116"></span><br />
Les différentes étapes : </p>
<p><strong>0.) Configurer votre poste de façon à pouvoir exécuter un script Powershell. </strong><br />
J&rsquo;ai écrit un petit billet pour ça <a href="http://blog.developpez.com/zinzineti/p9062/powershell-sqlserver/title-168/#more9062">ICI</a></p>
<p><strong>1.) Créer un fichier contenant les informations d&rsquo;accès aux serveurs</strong></p>
<p>Ce fichier contenant la liste des serveurs nommée : <strong>listeserveurs.txt</strong> a la structure suivante : </p>
<blockquote><p>AdresseIP,Domaine\Login,Password,NomInstance</p></blockquote>
<p><strong>AdresseIP</strong> = Adresse IP (ou nom) du serveur de base de données<br />
<strong>Domaine </strong> = Domaine<br />
<strong>Login </strong> = login<br />
<strong>Password </strong> = mot de passe<br />
<strong>NomInstance </strong> = Nom de l&rsquo;instance de la base de données à monitorer.<br />
Valeur par défaut <strong>default </strong> pour monitorer l&rsquo;instance par défaut et <strong>NomInstance</strong> pour monitorer une instance nommée <strong>NomInstance</strong></p>
<p>Exemple de fichier <strong>listeserveurs.txt</strong> : </p>
<blockquote><p>IP0,Domaine0\Login0,Password0,default<br />
IP1,Domaine1\Login1,Password1,NomInstance1<br />
IP2,Domaine2\Login2,Password2,NomInstance2</p></blockquote>
<p><strong>2.) Script de monitoring des principaux services : Moteur SQL et Agent SQL </strong></p>
<blockquote>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">################ Monitoring ########################################################################## <br />
#Lire le contenu du fichier contenant les infos des serveurs <br />
$listserveurs = get-content &quot;listeserveurs.txt&quot; <br />
#Traiter chaque ligne du fichier <br />
foreach ($ligne in $listserveurs) { <br />
&nbsp; &nbsp;# Découpage de la ligne &nbsp;<br />
&nbsp; &nbsp;$decoupageligne = $ligne.split(&quot;,&quot;) <br />
&nbsp; &nbsp;# Nom du serveur &nbsp;<br />
&nbsp; &nbsp;$serveur = $decoupageligne[0] <br />
&nbsp; &nbsp;# le login pour ce serveur &nbsp;<br />
&nbsp; &nbsp;$login = &nbsp; $decoupageligne[1] <br />
&nbsp; &nbsp;# le password du serveur &nbsp;<br />
&nbsp; &nbsp;$stringpwd = $decoupageligne[2] <br />
&nbsp; &nbsp;$pwd = convertto-securestring $stringpwd -asplaintext -force; <br />
&nbsp; &nbsp;# Construire la chaine d'authentification <br />
&nbsp; &nbsp;$credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $login,$pwd -ErrorAction &quot;silentlycontinue&quot;; <br />
&nbsp; &nbsp;# Instance à traiter &nbsp;<br />
&nbsp; &nbsp;$instance = $decoupageligne[3] <br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp;<br />
# Pingé le serveur courant <br />
$resultatsPing = Get-WMIObject -query &quot;select StatusCode from Win32_PingStatus where Address = '$serveur'&quot; <br />
$reponse = $false <br />
ForEach($resultat in $resultatsPing) { <br />
&nbsp; if ($resultatsPing.statuscode -eq 0) { <br />
&nbsp; &nbsp; &nbsp; &nbsp; $reponse = $true <br />
&nbsp; } <br />
&nbsp; } &nbsp;<br />
&nbsp; if ($reponse) { <br />
&nbsp; # le serveur courant répond au ping <br />
&nbsp; Write-Host -foregroundcolor &quot;green&quot; **************************************************************************************************** <br />
&nbsp; Write-Host -foregroundcolor &quot;green&quot; Ping Serveur $serveur &nbsp;--- &nbsp;OK &nbsp;<br />
&nbsp; Write-Host -foregroundcolor &quot;green&quot; **************************************************************************************************** <br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; ######## ETATS DES SERVICES ############ <br />
&nbsp; if ($instance -eq 'default'){ <br />
&nbsp; &nbsp; Write-Host ------------------------------------------------------ <br />
&nbsp; Write-Host &nbsp;SERVEUR -- $serveur INSTANCE &nbsp;-- $instance <br />
&nbsp; Write-Host ------------------------------------------------------ &nbsp; <br />
&nbsp; &nbsp; $instancetraitemoteurdefault = 'MSSQLSERVER' <br />
&nbsp; &nbsp; $instancetraiteagentdefault = 'SQLSERVERAGENT' <br />
&nbsp; &nbsp; #Pour le moteur SQL <br />
&nbsp; &nbsp; $statemoteursqldefault = Get-WMIObject -query &quot;select state from win32_service where name = '$instancetraitemoteurdefault'&quot; -computername $serveur -credential $credential &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;if ($statemoteursqldefault.state -eq 'Running'){ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Write-Host -foregroundcolor &quot;green&quot; Serveur $serveur -- Moteur SQL $instancetraitemoteurdefault -- Démarré &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; Get-WMIObject win32_service -filter &quot;name ='$instancetraitemoteurdefault'&quot; &nbsp;-computername $serveur -credential $credential &nbsp;| format-table -property displayname,state,StartMode,status <br />
&nbsp; &nbsp; &nbsp; }else <br />
&nbsp; &nbsp; &nbsp; {Write-Host -foregroundcolor &quot;red&quot; Serveur $serveur -- Moteur SQL &nbsp;$instancetraitemoteurdefault -- Arrêté &nbsp;<br />
&nbsp; &nbsp; &nbsp; Get-WMIObject win32_service -filter &quot;name ='$instancetraitemoteurdefault'&quot; &nbsp;-computername $serveur -credential $credential &nbsp;| format-table -property displayname,state,StartMode,status <br />
&nbsp; &nbsp; &nbsp; } <br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; #Pour l'agent SQL <br />
&nbsp; &nbsp; $stateagentsqldefault = Get-WMIObject -query &quot;select state from win32_service where name = '$instancetraiteagentdefault'&quot; -computername $serveur -credential $credential &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;if ($stateagentsqldefault.state -eq 'Running'){ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Write-Host -foregroundcolor &quot;green&quot; Serveur $serveur -- Agent SQL $instancetraiteagentdefault -- Démarré &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; Get-WMIObject win32_service -filter &quot;name ='$instancetraiteagentdefault'&quot; &nbsp;-computername $serveur -credential $credential &nbsp;| format-table -property displayname,state,StartMode,status <br />
&nbsp; &nbsp; &nbsp; }else <br />
&nbsp; &nbsp; &nbsp; {Write-Host -foregroundcolor &quot;red&quot; Serveur $serveur -- Agent SQL $instancetraiteagentdefault -- Arrêté &nbsp;<br />
&nbsp; &nbsp; &nbsp; Get-WMIObject win32_service -filter &quot;name ='$instancetraiteagentdefault'&quot; &nbsp;-computername $serveur -credential $credential &nbsp;| format-table -property displayname,state,StartMode,status <br />
&nbsp; &nbsp; &nbsp; } <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; } <br />
&nbsp; &nbsp; else{ <br />
&nbsp; &nbsp; #Traitement des instances nommées <br />
&nbsp; &nbsp; Write-Host ------------------------------------------------------ <br />
&nbsp; Write-Host &nbsp;SERVEUR -- $serveur INSTANCE &nbsp;-- $instance <br />
&nbsp; Write-Host ------------------------------------------------------ &nbsp; <br />
&nbsp; &nbsp; $instancetraitemoteur = 'MSSQL$'+$instance <br />
&nbsp; &nbsp; $instancetraiteagent = 'SQLAgent$'+$instance &nbsp; &nbsp;<br />
&nbsp; &nbsp; #Pour le moteur SQL <br />
&nbsp; &nbsp; $statemoteursql = Get-WMIObject -query &quot;select state from win32_service where name = '$instancetraitemoteur'&quot; -computername $serveur -credential $credential &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;if ($statemoteursql.state -eq 'Running'){ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Write-Host -foregroundcolor &quot;green&quot; Serveur $serveur -- Moteur SQL $instancetraitemoteur -- Démarré &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; Get-WMIObject win32_service -filter &quot;name ='$instancetraitemoteur'&quot; &nbsp;-computername $serveur -credential $credential &nbsp;| format-table -property displayname,state,StartMode,status <br />
&nbsp; &nbsp; &nbsp; }else <br />
&nbsp; &nbsp; &nbsp; {Write-Host -foregroundcolor &quot;red&quot; Serveur $serveur -- Moteur SQL &nbsp;$instancetraitemoteur -- Arrêté &nbsp;<br />
&nbsp; &nbsp; &nbsp; Get-WMIObject win32_service -filter &quot;name ='$instancetraitemoteur'&quot; &nbsp;-computername $serveur -credential $credential &nbsp;| format-table -property displayname,state,StartMode,status <br />
&nbsp; &nbsp; &nbsp; } <br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp;#Pourl'agent SQL <br />
&nbsp; &nbsp; &nbsp;$stateagent = Get-WMIObject -query &quot;select state from win32_service where name = '$instancetraiteagent'&quot; -computername $serveur -credential $credential &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;if ($stateagent.state -eq 'Running'){ <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Write-Host -foregroundcolor &quot;green&quot; Serveur $serveur -- Agent SQL &nbsp; $instancetraiteagent -- Démarré &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; Get-WMIObject win32_service -filter &quot;name ='$instancetraiteagent'&quot; &nbsp;-computername $serveur -credential $credential &nbsp;| format-table -property displayname,state,StartMode,status <br />
&nbsp; &nbsp; &nbsp; }else <br />
&nbsp; &nbsp; &nbsp; {Write-Host -foregroundcolor &quot;red&quot; Serveur $serveur -- Agent SQL &nbsp;$instancetraiteagent -- Arrêté &nbsp;<br />
&nbsp; &nbsp; &nbsp; Get-WMIObject win32_service -filter &quot;name ='$instancetraiteagent'&quot; &nbsp;-computername $serveur -credential $credential &nbsp;| format-table -property displayname,state,StartMode,status <br />
&nbsp; &nbsp; &nbsp; } <br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; } <br />
&nbsp; &nbsp;} <br />
&nbsp;<br />
&nbsp; else { <br />
&nbsp; # le serveur courant ne répond au ping <br />
&nbsp; &nbsp; &nbsp; &nbsp; Write-Host -foregroundcolor &quot;red&quot; Le Serveur $serveur ne répond pas /!\ -- KO <br />
&nbsp; Write-Host -foregroundcolor &quot;red&quot; Ping Serveur $serveur -- KO <br />
&nbsp; } <br />
}</div></div>
</blockquote>
<p><strong>3.) Résultat  </strong></p>
<blockquote><p>****************************************************************************************************<br />
<strong>Ping Serveur IP0 &#8212; OK</strong><br />
****************************************************************************************************<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
<strong>SERVEUR IP0  INSTANCE &#8212; default</strong><br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
<strong>Serveur IP0  Moteur SQL MSSQLSERVER &#8212; Démarré</strong></p>
<p>displayname        state      StartMode      status<br />
&#8212;&#8212;&#8212;&#8211;       &#8212;&#8211;      &#8212;&#8212;&#8212;      &#8212;&#8212;<br />
MSSQLSERVER       Running     Auto          OK</p>
<p><strong>Serveur IP0  Agent SQL SQLSERVERAGENT &#8212; Démarré</strong></p>
<p>displayname        state      StartMode      status<br />
&#8212;&#8212;&#8212;&#8211;       &#8212;&#8211;      &#8212;&#8212;&#8212;      &#8212;&#8212;<br />
SQLSERVERAGENT       Running     Auto          OK</p>
<p><strong>&#8230;.</strong></p>
<p><strong>Remarque </strong>: il faut trouver un moyen de sécuriser le fichier contenant des logins et password. par exemple le crypté <img src="https://blog.developpez.com/zinzineti/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley" /></p>
<p>/*****************************************************************************<br />
&#8212; MONITORING DES SERVICES SQL DE TOUTES LES INSTANCES DE BASE DE DONNEES<br />
&#8212; Auteur : Etienne ZINZINDOHOUE<br />
*****************************************************************************/</p>
</blockquote>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL 2000 &#8211; AUDIT FICHIER BACKUP</title>
		<link>https://blog.developpez.com/zinzineti/p9049/sql-server-2000/sql_2000_audit_fichier_backup</link>
		<comments>https://blog.developpez.com/zinzineti/p9049/sql-server-2000/sql_2000_audit_fichier_backup#comments</comments>
		<pubDate>Sat, 26 Jun 2010 14:32:36 +0000</pubDate>
		<dc:creator><![CDATA[zinzineti]]></dc:creator>
				<category><![CDATA[SQL SERVER 2000]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Comment auditer un fichier backup sous SQL 2000 ? Voici un script T-SQL pour obtenir les informations suivantes : &#8211;>Nom du compte utilisateur sous lequel le backup est fait &#8211;>Nom du serveur sur lequel le backup est fait &#8211;>Nom de la base de données sauvegardée &#8211;>Version du logiciel qui a créé la sauvegarde &#8211;>Version de la base de donnée sauvegardée &#8211;>Niveau de compatibilité de la sauvegarde &#8211;>Collation utilisée par la base de données backupée &#8211;>le [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Comment auditer un fichier backup sous SQL 2000 ?<br />
Voici un script T-SQL pour obtenir les informations suivantes :<br />
&#8211;>Nom du compte utilisateur sous lequel le backup est fait<br />
&#8211;>Nom du serveur sur lequel le backup est fait<br />
&#8211;>Nom de la base de données sauvegardée<br />
&#8211;>Version du logiciel qui a créé la sauvegarde<br />
&#8211;>Version de la base de donnée sauvegardée<br />
&#8211;>Niveau de compatibilité de la sauvegarde<br />
&#8211;>Collation utilisée par la base de données backupée<br />
&#8211;>le fichier backup est-il compressé ?<br />
&#8211;>&#8230;</p>
<p><span id="more-110"></span></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">DECLARE @FichierBackup VARCHAR (1000); <br />
DECLARE @SQL VARCHAR(1000); <br />
--Modifier la ligne en indiquant le chemin de votre fichier de backup à analyser <br />
SET &nbsp;@FichierBackup = 'E:\BACKUP\monFichierBackup.bak' &nbsp; &nbsp;<br />
-- Créer une table temporaire <br />
IF OBJECT_ID('msdb.dbo.maTableTempo', 'U') IS NOT NULL <br />
&nbsp; DROP TABLE msdb.dbo.maTableTempo <br />
CREATE TABLE msdb.dbo.maTableTempo ( <br />
&nbsp;BackupName &nbsp;nvarchar(128) <br />
,BackupDescription &nbsp;nvarchar(255) NULL <br />
,BackupType &nbsp;smallint &nbsp;<br />
,ExpirationDate &nbsp;datetime NULL <br />
,Compressed &nbsp;tinyint &nbsp;<br />
,Position &nbsp;smallint &nbsp;<br />
,DeviceType &nbsp;tinyint <br />
,UserName &nbsp;nvarchar(128) <br />
,ServerName &nbsp;nvarchar(128) <br />
,DatabaseName &nbsp;nvarchar(128) <br />
,DatabaseVersion &nbsp;bigint <br />
,DatabaseCreationDate &nbsp;datetime <br />
,BackupSize &nbsp;numeric(20,0) &nbsp;<br />
,FirstLSN &nbsp;numeric(25,0) &nbsp;<br />
,LastLSN &nbsp;numeric(25,0) &nbsp;<br />
,CheckpointLSN &nbsp;numeric(25,0) &nbsp;<br />
,DifferentialBaseLsn &nbsp;numeric(25,0) &nbsp;<br />
,BackupStartDate &nbsp;datetime <br />
,BackupFinishDate &nbsp;datetime <br />
,SortOrder &nbsp;smallint <br />
,CodePage &nbsp;smallint <br />
,UnicodeLocaleId &nbsp;bigint <br />
,UnicodeComparisonStyle &nbsp;bigint <br />
,CompatibilityLevel &nbsp;tinyint <br />
,SoftwareVendorId &nbsp;bigint <br />
,SoftwareVersionMajor &nbsp;bigint <br />
,SoftwareVersionMinor &nbsp;bigint <br />
,SoftwareVersionBuild &nbsp;bigint <br />
,MachineName &nbsp;nvarchar(128) <br />
,Flags &nbsp;bigint <br />
,BindingID &nbsp;uniqueidentifier <br />
,RecoveryForkID &nbsp;uniqueidentifier &nbsp;<br />
,Collation &nbsp;nvarchar(128)); <br />
&nbsp;<br />
SET @SQL = 'INSERT INTO msdb.dbo.maTableTempo EXEC (''RESTORE HEADERONLY FROM DISK = N'''''+ @FichierBackup + ''''' WITH NOUNLOAD'')' <br />
--print (@SQL); <br />
EXEC (@SQL); <br />
IF (@@ERROR = 0) <br />
-- S'il n'y a pas d'erreur, afficher les informations du fichier backup <br />
BEGIN <br />
SELECT BackupName [NomBackup] <br />
,BackupDescription[Description] <br />
,CASE BackupType &nbsp;<br />
WHEN '1' THEN 'Sauvegarde de base de données' <br />
WHEN '2' THEN 'Sauvegarde Journal des transactions' <br />
WHEN '4' THEN 'Sauvegarde Fichier' <br />
WHEN '5' THEN 'Sauvegarde différentielle de Base de données' <br />
WHEN '6' THEN 'Sauvegarde Fichier différentiel' <br />
WHEN '7' THEN 'Sauvegarde Partielle' <br />
WHEN '8' THEN 'Sauvegarde Partielle différentielle' <br />
ELSE '' <br />
END [Type de sauvegarde] <br />
,ExpirationDate [Date d'expiration du jeu de sauvegarde] <br />
, CASE Compressed &nbsp;<br />
WHEN '0' THEN 'Non' <br />
WHEN '1' THEN 'Oui' <br />
ELSE '' <br />
END [Fichier backup est compressé ?] <br />
,UserName [Utilisateur ayant effectué la sauvegarde] <br />
,ServerName [Serveur sur lequel la sauvegarde est faite] <br />
,DatabaseName [Base de données sauvegardée] <br />
,CASE DatabaseVersion &nbsp;<br />
WHEN '515' THEN 'SQL 7.0' <br />
WHEN '539' THEN 'SQL 2000' <br />
WHEN '611' THEN 'SQL 2005' <br />
WHEN '612' THEN 'SQL 2005' <br />
WHEN '655' THEN 'SQL 2008' <br />
ELSE '' <br />
END 'Version de la base de données à partir de laquelle la sauvegarde a été créée' <br />
,DatabaseCreationDate [Date création de la base] <br />
,ROUND((BackupSize/1024),0) [Taille de la sauvegarde (en Ko)] <br />
,BackupStartDate [Date et Heure démarrage du backup] <br />
,BackupFinishDate [Date et Heure fin du backup] <br />
,CASE CompatibilityLevel &nbsp;<br />
WHEN '80' THEN &nbsp;'SQL 2000' <br />
WHEN '90' THEN &nbsp;'SQL 2005' <br />
WHEN '100' THEN 'SQL 2008' <br />
ELSE '' <br />
END [niveau de compatibilité de la base à partir de laquelle la sauvegarde a été créée] <br />
,SoftwareVersionMajor [version du logiciel qui a cré la sauvegarde] <br />
,MachineName &nbsp;[Machine ayant effectuée la sauvegarde] <br />
,Collation [Collation utilisé par la base de données] <br />
FROM msdb.dbo.maTableTempo <br />
--Supprimer la table temporaire <br />
DROP TABLE msdb.dbo.maTableTempo ; <br />
END</div></div>
<p><strong>Remarques</strong> :<br />
<strong>1. )</strong> Ce script ne marche que sous SQL 2000<br />
Si vous essayer de l&rsquo;exécuter sous SQL Server Management Studio (SSMS)2005 ou 2008, vous aurez une erreur du type :</p>
<blockquote><p>Msg 213, Niveau 16, État 7, Ligne 1<br />
Le nom ou le numéro de colonne des valeurs fournies ne correspond pas à la définition de la table.<br />
Msg 3013, Niveau 16, État 1, Ligne 1<br />
RESTORE HEADERONLY s&rsquo;est terminé anormalement.</p></blockquote>
<p><strong>2.)</strong><br />
Vous ne pouvez pas utiliser ce script pour obtenir les infos d&rsquo;un fichier backupé sous SQL 2005 ou SQL 2008.<br />
Vous aurez dans ce cas une erreur du type : </p>
<blockquote><p>Serveur : Msg 3205, Niveau 16, État 2, Ligne 1<br />
Trop d&rsquo;unités de sauvegarde spécifiées pour la sauvegarde ou la restauration. Seules 64 sont autorisées.<br />
Serveur : Msg 3013, Niveau 16, État 1, Ligne 1<br />
RESTORE HEADERONLY s&rsquo;est terminé anormalement.</p></blockquote>
<p>/**************************************************<br />
&#8212; SQL 2000 &#8211; INFORMATIONS DU FICHIER BACKUP<br />
&#8212; Auteur : Etienne ZINZINDOHOUE<br />
*************************************************/</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Modifier la colonne IDENTITY d&#8217;une table</title>
		<link>https://blog.developpez.com/zinzineti/p9042/sql-server-2005/modifier_la_colonne_identity_d_une_table_1</link>
		<comments>https://blog.developpez.com/zinzineti/p9042/sql-server-2005/modifier_la_colonne_identity_d_une_table_1#comments</comments>
		<pubDate>Wed, 23 Jun 2010 21:10:37 +0000</pubDate>
		<dc:creator><![CDATA[zinzineti]]></dc:creator>
				<category><![CDATA[SQL SERVER 2000]]></category>
		<category><![CDATA[SQL SERVER 2005]]></category>
		<category><![CDATA[SQL SERVER 2008]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Pour des raisons X ou Y on peut être amener à modifier la colonne auto-incrément d&#8217;une table. Comment donc changer par exemple la colonne ID(1,1) pour avoir ID(2,2) ?. On peut penser qu&#8217;un ALTER TABLE suffit à faire la modification. Mais Non&#8230; /*********************************************************** &#8212; Description : Modifier la colonne IDENTITY d&#8217;une table. &#8212; Auteur : Etienne ZINZINDOHOUE ***********************************************************/ &#8211;Soit la table initiale avec une colonne autoincrément(1,1) CREATE TABLE [dbo].[UTILISATEUR]( &#160; [ID_USER] [bigint] IDENTITY(1,1) NOT FOR [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Pour des raisons X ou Y on peut être amener à modifier la <strong>colonne auto-incrément</strong> d&rsquo;une table. Comment donc changer par exemple la colonne <strong>ID(1,1)</strong> pour avoir <strong>ID(2,2)</strong> ?. On peut penser qu&rsquo;un ALTER TABLE suffit à faire la modification. Mais Non&#8230;</p>
<p>                                             <span id="more-7"></span></p>
<p>/***********************************************************<br />
&#8212; Description : Modifier la colonne IDENTITY d&rsquo;une table.<br />
&#8212; Auteur : Etienne ZINZINDOHOUE<br />
***********************************************************/<br />
&#8211;Soit la table initiale avec une colonne autoincrément(1,1)</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">CREATE TABLE [dbo].[UTILISATEUR]( <br />
&nbsp; [ID_USER] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL, <br />
&nbsp; [LOGIN] [varchar](20) NULL, <br />
&nbsp; [PWD] [varchar](15) NULL, <br />
&nbsp; [NOM] [varchar](20) NULL, <br />
&nbsp; [PRENOM] [varchar](20) NULL <br />
&nbsp;<br />
-- Clé primaire &nbsp;(PK) <br />
CONSTRAINT [PK_UTILISATEUR] PRIMARY KEY CLUSTERED &nbsp;<br />
( <br />
&nbsp; [ID_USER] ASC <br />
)WITH (PAD_INDEX &nbsp;= OFF, STATISTICS_NORECOMPUTE &nbsp;= OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS &nbsp;= ON, ALLOW_PAGE_LOCKS &nbsp;= ON) ON [PRIMARY] <br />
) ON [PRIMARY] <br />
&nbsp;<br />
-- Clé étrangère (FK) ----- <br />
ALTER TABLE [dbo].[UTILISATEUR] &nbsp;WITH CHECK ADD &nbsp;CONSTRAINT [FK_USER_IDUSER] FOREIGN KEY([ID_USER]) <br />
REFERENCES [dbo].[GROUPE] ([ID_USER]) <br />
GO <br />
ALTER TABLE [dbo].[UTILISATEUR] CHECK CONSTRAINT [FK_USER_IDUSER] <br />
GO</div></div>
<p>Pour des raisons X ou Y on peut être amener à modifier la<br />
colonne<br />
auto-incrément <strong>ID_USER (1,1)</strong> pour le mettre à <strong>ID_USER (2,2)</strong> par exemple.<br />
On pourrait penser qu&rsquo;un ALTER TABLE suffit à faire la modification. Mais Non&#8230;<br />
Vous ne pouvez pas faire la modification avec la comande :</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ALTER TABLE [dbo].[UTILISATEUR] <br />
ALTER COLUMN [ID_USER] [bigint] IDENTITY(2,2) NOT FOR REPLICATION NOT NULL</div></div>
<p>Vous aurez dans ce cas une erreur. </p>
<p>Alors qu&rsquo;il bien possible de modifier par exemple la colonne <strong>LOGIN </strong> avec la même commande :</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">ALTER TABLE [dbo].[UTILISATEUR] <br />
ALTER COLUMN [LOGIN] [varchar](21)</div></div>
<p><strong>Alors comment modifier la colonne IDENTITY d&rsquo;une table ? </strong></p>
<p>Voici la solution :<br />
&#8211;> 1.) Créer une table temporaire ayant la même structure que la table à modifier<br />
&#8211;> 2.) Importer des données de la table initiale vers la table temporaire<br />
&#8211;> 3.) Supprimer les clées primaires (PK) et étrangères (FK) de la table initiale<br />
&#8211;> 4.) Renommer la table temporaire (nom de la table initiale à modifier)<br />
&#8211;> 5.) Ajouter les clés primaires (PK) et étrangères (FK)</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">-- Début de la transaction <br />
BEGIN TRANSACTION <br />
USE MA_BD <br />
-- Creer une table temporaire ayant les mêmes définitions que la table initiale qu'on désire modifier excepté les clés primaires et étrangères (PK,FK,...) &nbsp;<br />
CREATE TABLE Tmp_UTILISATEUR <br />
&nbsp; ( <br />
&nbsp; [ID_USER] BIGINT IDENTITY(2,2) NOT FOR REPLICATION NOT NULL, <br />
&nbsp; [LOGIN] VARCHAR(20) NULL, <br />
&nbsp; [PWD] VARCHAR(15) NULL, <br />
&nbsp; [NOM] VARCHAR(20) NULL, <br />
&nbsp; [PRENOM] VARCHAR(20) NULL <br />
&nbsp; ) &nbsp;ON [PRIMARY] <br />
-- Autoriser l'insertion de valeurs explicites dans la colonne IDENTITY(auto incrément) <br />
SET IDENTITY_INSERT Tmp_UTILISATEUR ON <br />
-- Copier les données de la table initiale vers la table temporaire <br />
IF EXISTS(SELECT * FROM [UTILISATEUR]) <br />
EXEC('INSERT INTO dbo.Tmp_UTILISATEUR(ID_USER,LOGIN,PWD,NOM,PRENOM) <br />
&nbsp; &nbsp; SELECT ID_USER,LOGIN,PWD,NOM,PRENOM <br />
&nbsp; &nbsp; &nbsp; FROM dbo.[UTILISATEUR] WITH (HOLDLOCK TABLOCKX)') <br />
-- Désactiver l'insertion de valeurs explicites dans la colonne IDENTITY(auto incrément) <br />
SET IDENTITY_INSERT Tmp_USER OFF <br />
-- Supprimer la clé étrangères (FK) de la table [UTILISATEUR] <br />
ALTER TABLE [dbo].[USER] &nbsp; <br />
DROP CONSTRAINT [FK_USER_IDUSER] &nbsp;<br />
-- Supprimer la table initiale <br />
DROP TABLE [UTILISATEUR] <br />
-- Renommer la table temporaire de façon à prendre maintenant le nom de la table initiale <br />
EXECUTE sp_rename N'Tmp_UTILISATEUR', N'UTILISATEUR', 'OBJECT' &nbsp;<br />
-- Ajouter la contrainte de clé primaire (PK) <br />
ALTER TABLE [UTILISATEUR] ADD CONSTRAINT <br />
&nbsp; PK_USER PRIMARY KEY CLUSTERED &nbsp;<br />
&nbsp; ( <br />
&nbsp; ID_USER <br />
&nbsp; ) WITH( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] <br />
&nbsp;<br />
-- Ajouter la contrainte de clé étrangère (FK) <br />
ALTER TABLE [dbo].[UTILISATEUR] &nbsp;WITH CHECK ADD &nbsp;CONSTRAINT [FK_USER_IDUSER] FOREIGN KEY([ID_USER]) <br />
REFERENCES [dbo].[GROUPE] ([ID_USER]) &nbsp;<br />
&nbsp;<br />
-- Fin de la transaction <br />
COMMIT TRANSACTION</div></div>
<p>/***********************************************************<br />
&#8212; Description : Modifier la colonne IDENTITY d&rsquo;une table.<br />
&#8212; Auteur : Etienne ZINZINDOHOUE<br />
***********************************************************/</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
