<?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>Optimisation et diagnostic SQL Server &#187; T-SQL</title>
	<atom:link href="https://blog.developpez.com/babaluga/pcategory/t-sql/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.developpez.com/babaluga</link>
	<description>Articles et fragments de code pour l&#039;optimisation SQL Server</description>
	<lastBuildDate>Wed, 26 Mar 2014 09:30:39 +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>Modifier les colonnes TEXT en VARCHAR(MAX)</title>
		<link>https://blog.developpez.com/babaluga/p12518/t-sql/modifier-les-colonnes-text-en-varcharmax</link>
		<comments>https://blog.developpez.com/babaluga/p12518/t-sql/modifier-les-colonnes-text-en-varcharmax#comments</comments>
		<pubDate>Tue, 11 Mar 2014 12:03:31 +0000</pubDate>
		<dc:creator><![CDATA[rudib]]></dc:creator>
				<category><![CDATA[catalogue]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/babaluga/?p=54</guid>
		<description><![CDATA[Le type de données TEXT est déprécié depuis SQL Server 2005. Outre les problèmes de gestion dans le code et sa non compatibilité avec plusieurs fonctions de chaînes, et est gourmand en espace disque et ralentit les opérations de lecture et d&#8217;écriture, parce que le moteur de stockage doit créer une allocation spécifique aux LOB [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Le type de données TEXT est déprécié depuis SQL Server 2005. Outre les problèmes de gestion dans le code et sa non compatibilité avec plusieurs fonctions de chaînes, et est gourmand en espace disque et ralentit les opérations de lecture et d&rsquo;écriture, parce que le moteur de stockage doit créer une allocation spécifique aux LOB pour chaque ligne insérée.<br />
Le type qui le remplace s&rsquo;appelle VARCHAR(MAX), et l&rsquo;allocation dans la page ou en LOB se fait dynamiquement selon le contenu inséré dans la colonne. Dans la pratique, cela prend beaucoup moins d&rsquo;espace en base.</p>
<p>Voici un code se basant sur une vue de catalogue, pour générer les instructions ALTER TABLE pour convertir les types de données, et ensuite pour reconstruire les tables (à partir de SQL Server 2008). Vous pouvez au besoin modifier le code pour accommoder des colonnes NTEXT, et IMAGE (à remplacer par VARBINARY(MAX)) si vous en avez.</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 <br />
&nbsp; &nbsp; 'ALTER TABLE ['+TABLE_SCHEMA+'].['+TABLE_NAME+'] ALTER COLUMN ['+COLUMN_NAME+'] VARCHAR(MAX) '<br />
&nbsp; &nbsp; +CASE IS_NULLABLE WHEN 'YES' THEN 'NULL' ELSE 'NOT NULL' END + ';'<br />
FROM INFORMATION_SCHEMA.COLUMNS<br />
WHERE DATA_TYPE = 'text';<br />
<br />
SELECT <br />
&nbsp; &nbsp; 'ALTER TABLE ['+TABLE_SCHEMA+'].['+TABLE_NAME+'] REBUILD;'<br />
FROM INFORMATION_SCHEMA.COLUMNS<br />
WHERE DATA_TYPE = 'text'<br />
GROUP BY TABLE_SCHEMA, TABLE_NAME;</div></div>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>fonction UPDATE() dans un déclencheur</title>
		<link>https://blog.developpez.com/babaluga/p12319/t-sql/fonction-update-dans-un-declencheur</link>
		<comments>https://blog.developpez.com/babaluga/p12319/t-sql/fonction-update-dans-un-declencheur#comments</comments>
		<pubDate>Sat, 09 Nov 2013 22:12:40 +0000</pubDate>
		<dc:creator><![CDATA[rudib]]></dc:creator>
				<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/babaluga/?p=16</guid>
		<description><![CDATA[Attention à la fonction UPDATE() utilisée dans les déclencheures (triggers). Ne pensez pas que cela vous indique que la valeur de la colonne a changé, mais simplement que la colonne a été mentionnée dans l&#8217;instruction. Un exemple : USE tempdb; GO CREATE TABLE dbo.Contact &#40;Id INT, Name VARCHAR&#40;50&#41;&#41; GO INSERT INTO dbo.Contact VALUES &#40;1, 'Fillon'&#41;, [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Attention à la fonction UPDATE() utilisée dans les déclencheures (triggers).<br />
Ne pensez pas que cela vous indique que la valeur de la colonne a changé, mais simplement<br />
que la colonne a été mentionnée dans l&rsquo;instruction. Un exemple :</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">USE</span> tempdb;<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> dbo<span style="color: #66cc66;">.</span>Contact <span style="color: #66cc66;">&#40;</span>Id <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">,</span> Name <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> dbo<span style="color: #66cc66;">.</span>Contact <br />
<span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Fillon'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'Copé'</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TRIGGER</span> atr_u_Contact<br />
<span style="color: #993333; font-weight: bold;">ON</span> dbo<span style="color: #66cc66;">.</span>Contact <br />
AFTER <span style="color: #993333; font-weight: bold;">UPDATE</span><br />
<span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">BEGIN</span><br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">UPDATE</span><span style="color: #66cc66;">&#40;</span>Name<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; PRINT <span style="color: #ff0000;">'ok'</span><br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">ELSE</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; PRINT <span style="color: #ff0000;">'non'</span><br />
<span style="color: #993333; font-weight: bold;">END</span><br />
<br />
<span style="color: #993333; font-weight: bold;">UPDATE</span> dbo<span style="color: #66cc66;">.</span>Contact <br />
<span style="color: #993333; font-weight: bold;">SET</span> Name <span style="color: #66cc66;">=</span> Name<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> Id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">2</span>;<br />
<span style="color: #808080; font-style: italic;">-- affiche oui</span><br />
<br />
<span style="color: #993333; font-weight: bold;">UPDATE</span> dbo<span style="color: #66cc66;">.</span>Contact <br />
<span style="color: #993333; font-weight: bold;">SET</span> Id <span style="color: #66cc66;">=</span> Id<br />
<span style="color: #993333; font-weight: bold;">WHERE</span> Id <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">2</span>;<br />
<span style="color: #808080; font-style: italic;">-- affiche non</span></div></div>
<p>Donc il vaut mieux tester s&rsquo;il y a eu réellement une modification avant d&rsquo;exécuter tout le reste. Par exemple, à la place de ceci :</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TRIGGER</span> atr_u_Contact<br />
<span style="color: #993333; font-weight: bold;">ON</span> dbo<span style="color: #66cc66;">.</span>Contact <br />
AFTER <span style="color: #993333; font-weight: bold;">UPDATE</span><br />
<span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">BEGIN</span><br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">UPDATE</span><span style="color: #66cc66;">&#40;</span>Name<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> ContactHistory <span style="color: #66cc66;">&#40;</span>Id<span style="color: #66cc66;">,</span> Name<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">SELECT</span> Id<span style="color: #66cc66;">,</span> Name<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FROM</span> deleted;<br />
<span style="color: #993333; font-weight: bold;">END</span></div></div>
<p>ceci :</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TRIGGER</span> atr_u_Contact<br />
<span style="color: #993333; font-weight: bold;">ON</span> dbo<span style="color: #66cc66;">.</span>Contact <br />
AFTER <span style="color: #993333; font-weight: bold;">UPDATE</span><br />
<span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #993333; font-weight: bold;">BEGIN</span><br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">IF</span> @@ROWCOUNT <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333; font-weight: bold;">RETURN</span><br />
<br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">IF</span> <span style="color: #993333; font-weight: bold;">UPDATE</span><span style="color: #66cc66;">&#40;</span>Name<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> ContactHistory <span style="color: #66cc66;">&#40;</span>Id<span style="color: #66cc66;">,</span> Name<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">SELECT</span> d<span style="color: #66cc66;">.</span>Id<span style="color: #66cc66;">,</span> d<span style="color: #66cc66;">.</span>Name<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">FROM</span> deleted d<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">JOIN</span> inserted i <span style="color: #993333; font-weight: bold;">ON</span> d<span style="color: #66cc66;">.</span>Id <span style="color: #66cc66;">=</span> i<span style="color: #66cc66;">.</span>Id<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">WHERE</span> d<span style="color: #66cc66;">.</span>Name &nbsp;i<span style="color: #66cc66;">.</span>Name;<br />
<span style="color: #993333; font-weight: bold;">END</span></div></div>
<p>Ici nous ajoutons aussi, en toute première ligne (c&rsquo;est important), l&rsquo;instruction</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">IF</span> @@ROWCOUNT <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333; font-weight: bold;">RETURN</span></div></div>
<p>pour tester si le déclencheur est appelé pour une bonne raison. En effet, une instruction comme celle-ci:</p>
<div class="codecolorer-container sql default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">UPDATE</span> dbo<span style="color: #66cc66;">.</span>Contact<br />
<span style="color: #993333; font-weight: bold;">SET</span> Name <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">UPPER</span><span style="color: #66cc66;">&#40;</span>Name<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>;</div></div>
<p>n’affecterait aucune ligne, mais appellerait pourtant le déclencheur, qui lancerait des requêtes pour rien. Le test sur @@ROWCOUNT permet de sortir immédiatement du trigger si aucune ligne n&rsquo;a été affectée.</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
