<?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>&#8235;Madeira &#187; גיא גלנצר&#8236;</title>	<atom:link href="http://www.madeira.co.il/author/guyglantser/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.madeira.co.il</link>
	<description>&#8235;SQL Server Services&#8236;</description>	<lastBuildDate>Sat, 19 May 2012 09:04:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>he</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>&#8235;My Presentation from the 117th ISSUG Meeting&#8236;</title>		<link>http://www.madeira.co.il/my-presentation-from-the-117th-issug-meeting/</link>
		<comments>http://www.madeira.co.il/my-presentation-from-the-117th-issug-meeting/#comments</comments>
		<pubDate>Fri, 06 Apr 2012 12:50:39 +0000</pubDate>
		<dc:creator>&#8235;גיא גלנצר&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[כללי]]></category>
		<category><![CDATA[מקודמות]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[FileTables]]></category>
		<category><![CDATA[SQL Server 2012]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=4737</guid>
		<description><![CDATA[&#8235;Here are the materials from my presentation about FileTables in the last SQL Server users group meeting...&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p><img src='http://www.madeira.co.il/wp-content/plugins/simple-post-thumbnails/timthumb.php?src=/wp-content/thumbnails/4737.jpg&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p dir="ltr" align="justify">In the last SQL Server users group meeting (#117) on April 2<sup>nd</sup> I presented a session about FileTables – a new exciting feature in SQL Server 2012 that enables applications to manage structured as well as unstructured data seamlessly.</p>
<p dir="ltr" align="justify">Here are the materials from my presentation:</p>
<p dir="ltr" align="justify"><a href="http://www.madeira.co.il/wp-content/uploads/2012/02/MDRS-FT-2012.zip" target="_blank"><img src="http://www.madeira.co.il/wp-content/uploads/2009/12/zip.png" alt="FileTables Presentation Materials"/></a></p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/my-presentation-from-the-117th-issug-meeting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8235;The Best of SQL Server &#8211; Event Summary&#8236;</title>		<link>http://www.madeira.co.il/the-best-of-sql-server-event-summary/</link>
		<comments>http://www.madeira.co.il/the-best-of-sql-server-event-summary/#comments</comments>
		<pubDate>Fri, 02 Mar 2012 16:33:56 +0000</pubDate>
		<dc:creator>&#8235;גיא גלנצר&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[כללי]]></category>
		<category><![CDATA[מקודמות]]></category>
		<category><![CDATA[Extended Events]]></category>
		<category><![CDATA[Optimizer]]></category>
		<category><![CDATA[Parameterization]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server Community]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=4465</guid>
		<description><![CDATA[&#8235;Here is a summary of the SQL Server event we had in the north this week - "The Best of SQL Server"...&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p><img src='http://www.madeira.co.il/wp-content/plugins/simple-post-thumbnails/timthumb.php?src=/wp-content/thumbnails/4465.png&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p dir="ltr" align="justify">On February 29<sup>th</sup> we organized a SQL Server event – <a href="http://www.madeira.co.il/conferences/best-of-sql-server-2012" target="_blank"><font color="blue">The Best of SQL Server</font></a> – together with <a href="http://www.mediatech.co.il/Pages/home.aspx" target="_blank"><font color="blue">Media-Tech College</font></a>. This event was the opening shot for our new partnership with Media-Tech College. We plan to provide together high quality professional training solutions in the field of Microsoft SQL Server to businesses and individuals in the north. The event was very successful with around 50 attendees and with 3 excellent presentations.</p>
<p dir="ltr" align="center"><a href="http://www.madeira.co.il/wp-content/uploads/2012/03/Guy-Glantser1.jpg" rel="wp-prettyPhoto[g4465]"><img title="Guy Glantser" src="http://www.madeira.co.il/wp-content/uploads/2012/03/Guy-Glantser1-224x300.jpg" alt="Guy Glantser"/></a></p>
<p dir="ltr" align="justify"><a href="http://www.linkedin.com/in/yarivin" target="_blank"><font color="blue">Yariv Inbar</font></a> (Media-Tech) opened the event and talked about our new exciting partnership and our plans for the future, such as: professional courses, community events, hands-on seminars, and more. I presented a session about advanced query processing, in which I spoke about various aspects of parameterization in SQL Server. <a href="http://www.linkedin.com/pub/assaf-shalem/4/a34/49" target="_blank"><font color="blue">Assaf Shalem</font></a> (TopTix) introduced Extended Events and presented some practical troubleshooting scenarios. <a href="http://www.madeira.co.il/author/shy/" target="_blank"><font color="blue">Shy Engelberg</font></a> (Madeira) presented an overview of the new exciting features in SQL Server 2012.</p>
<p dir="ltr" align="center"><a href="http://www.madeira.co.il/wp-content/uploads/2012/03/Shy-Engelberg.jpg" rel="wp-prettyPhoto[g4465]"><img title="Shy Engelberg" src="http://www.madeira.co.il/wp-content/uploads/2012/03/Shy-Engelberg-300x224.jpg" alt="Shy Engelberg"/></a></p>
<p dir="ltr" align="justify">Here are all the materials from the presentations:</p>
<p dir="ltr" align="justify"><a href="http://www.madeira.co.il/wp-content/uploads/2012/03/The-Best-of-SQL-Server.zip" target="_blank"><img title="Presentation Materials" src="http://www.madeira.co.il/wp-content/uploads/2009/12/zip.png" alt="Presentation Materials"/></a></p>
<p dir="ltr" align="justify">I hope you enjoyed the event. I'll be happy to hear your feedback…</p>
<p dir="ltr" align="justify">Thanks,<br />
Guy</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/the-best-of-sql-server-event-summary/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8235;A Journey into SQL Server 2012&#8236;</title>		<link>http://www.madeira.co.il/a-journey-into-sql-server-2012/</link>
		<comments>http://www.madeira.co.il/a-journey-into-sql-server-2012/#comments</comments>
		<pubDate>Wed, 22 Feb 2012 13:51:25 +0000</pubDate>
		<dc:creator>&#8235;גיא גלנצר&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[כללי]]></category>
		<category><![CDATA[מקודמות]]></category>
		<category><![CDATA[AlwaysOn]]></category>
		<category><![CDATA[BI]]></category>
		<category><![CDATA[FileTables]]></category>
		<category><![CDATA[SQL Azure]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server Community]]></category>
		<category><![CDATA[SQL Server Data Tools]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=4386</guid>
		<description><![CDATA[&#8235;A summary of the Microsoft event – "A Journey into SQL Server 2012"...&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p><img src='http://www.madeira.co.il/wp-content/plugins/simple-post-thumbnails/timthumb.php?src=/wp-content/thumbnails/4386.jpg&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p dir="ltr" align="justify">On February 20<sup>th</sup> I attended the Microsoft event – <a href="http://ismsdn-sql-feb2012-eorg.eventbrite.com/" target="_blank"><font color="blue">A Journey into SQL Server 2012</font></a>. The event was very interesting, and included many great sessions…</p>
<p dir="ltr" align="justify">Roy Pasternak (Microsoft) presented a short overview of the new features in SQL Server 2012. There are so many of them. SQL Server 2012 is truly a big advancement in all areas of the product – mission-critical confidence, dynamic development, breakthrough insight and cloud-ready. More information can be found <a href="http://www.microsoft.com/sqlserver/en/us/future-editions.aspx" target="_blank"><font color="blue">here</font></a>.</p>
<p dir="ltr" align="justify"><a href="http://blogs.microsoft.co.il/blogs/bei/" target="_blank"><font color="blue">Yossi Elkayam</font></a> (Microsoft) then drilled down into the new BI features, like <a href="http://blogs.msdn.com/b/sqlrsteamblog/archive/2011/07/12/sql-server-codename-quot-denali-quot-ctp3-including-project-quot-crescent-quot-is-now-publically-available.aspx" target="_blank"><font color="blue">Power View</font></a> and <a href="http://download.microsoft.com/download/0/D/2/0D22DFA8-054A-48D6-871A-24DD524C248B/SQL_Server_2012_DQS_Datasheet_Oct2011.pdf" target="_blank"><font color="blue">Data Quality Services</font></a>. There are really so many…</p>
<p dir="ltr" align="justify"><a href="http://blogs.microsoft.co.il/blogs/dannyr/" target="_blank"><font color="blue">Danny Ravid</font></a> (Brillix) presented the new high availability solution in SQL Server 2012 – AlwaysOn. This is one of the most exciting new features in SQL Server 2012, in my opinion. Microsoft took the best parts of Failover Clustering and Database Mirroring, added some more, and came up with a robust platform that enables true high availability as well as load balancing at the database level. With this platform we can have multiple readable replicas of a group of databases with a single virtual name for the application. More information can be found <a href="http://www.microsoft.com/sqlserver/en/us/future-editions/mission-critical/SQL-Server-2012-high-availability.aspx" target="_blank"><font color="blue">here</font></a>.</p>
<p dir="ltr" align="justify"><a href="http://blogs.microsoft.co.il/blogs/itaib/" target="_blank"><font color="blue">Itai Binyamin</font></a> (Veracity) presented the new tool for developers in SQL Server 2012 – <a href="http://msdn.microsoft.com/en-us/data/gg427686" target="_blank"><font color="blue">SQL Server Data Tools</font></a>. The SSDT brings together all the great functionality from Visual Studio 2010 and SQL Server Management Studio, and it creates a unified and rich development environment for application developers and database developers alike. And it's part of the SQL Server product stack, so you get it for free.</p>
<p dir="ltr" align="justify"><a href="http://www.dbsophic.com/SQL-Server-Tuning-Blog/Latest.html" target="_blank"><font color="blue">Ami Levin</font></a> (DBSophic) presented some new T-SQL features, like metadata discovery, paging and the new Sequence object.</p>
<p dir="ltr" align="justify"><a href="http://blogs.microsoft.co.il/members/Rachelye.aspx" target="_blank"><font color="blue">Rachel Yehezkel</font></a> (Microsoft) presented a short overview of the <a href="http://www.windowsazure.com/en-us/home/tour/sql-azure/" target="_blank"><font color="blue">SQL Azure</font></a> platform – Microsoft's data platform in the cloud. If you have questions about SQL Azure, then there is a new forum in Hebrew about Windows Azure and SQL Azure. You can find it <a href="http://social.msdn.microsoft.com/Forums/he-IL/azurehe/threads" target="_blank"><font color="blue">here</font></a>.</p>
<p dir="ltr" align="justify">I presented a session about <a href="http://msdn.microsoft.com/en-us/library/ff929144(v=sql.110).aspx" target="_blank"><font color="blue">FileTables</font></a> – a new exciting feature in SQL Server 2012 that enables applications to manage structured as well as unstructured data seamlessly. I plan to write more about FileTables in the near future. In the meanwhile, here are the materials from my presentation:</p>
<p dir="ltr" align="justify"><a href="http://www.madeira.co.il/wp-content/uploads/2012/02/MDRS-FT-2012.zip" target="_blank"><img src="http://www.madeira.co.il/wp-content/uploads/2009/12/zip.png" alt="FileTables Presentation Materials"/></a></p>
<p dir="ltr" align="justify">I hope you enjoyed the event in general and my presentation in particular. I'll be glad to hear your feedback…</p>
<p dir="ltr" align="justify">Thanks,<br />
Guy</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/a-journey-into-sql-server-2012/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8235;CASE Study&#8236;</title>		<link>http://www.madeira.co.il/case-study/</link>
		<comments>http://www.madeira.co.il/case-study/#comments</comments>
		<pubDate>Mon, 09 Jan 2012 06:00:27 +0000</pubDate>
		<dc:creator>&#8235;גיא גלנצר&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[מקודמות]]></category>
		<category><![CDATA[CASE]]></category>
		<category><![CDATA[NEWID]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=3294</guid>
		<description><![CDATA[&#8235;The CASE statement has two flavors: "Simple CASE" and "Searched CASE". But what really goes on behind the scenes?&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p><img src='http://www.madeira.co.il/wp-content/plugins/simple-post-thumbnails/timthumb.php?src=/wp-content/thumbnails/3294.jpg&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p dir="ltr">Sometimes we want to populate a table with random data, mainly for testing purposes. For example, let's consider the following table:</p>
<p dir="ltr">
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<pre id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="color: #0000ff;">CREATE</span> <span style="color: #0000ff;">TABLE</span>
    Sales.Orders
(
    Id            <span style="color: #0000ff;">INT</span>             <span style="color: #0000ff;">NOT</span> <span style="color: #0000ff;">NULL</span>    <span style="color: #0000ff;">IDENTITY</span>(1,1) ,
    CustomerId    <span style="color: #0000ff;">INT</span>             <span style="color: #0000ff;">NOT</span> <span style="color: #0000ff;">NULL</span> ,
    DateAndTime   <span style="color: #0000ff;">DATETIME2</span>(0)    <span style="color: #0000ff;">NOT</span> <span style="color: #0000ff;">NULL</span> ,
    OrderStatus   <span style="color: #0000ff;">NVARCHAR</span>(50)    <span style="color: #0000ff;">NULL</span>
);
<span style="color: #0000ff;">GO</span>
</span></span></pre>
</div>
<p dir="ltr">Let's say we want to insert 100,000 rows with random data. We want to generate random values between 1 and 1,000,000 for the &quot;CustomerId&quot; column, and we want to generate random date &amp; time values in the past 5 years for the &quot;DateAndTime&quot; column. For now, let's ignore the &quot;OrderStatus&quot; column. Here is how I would do it:</p>
<p dir="ltr">
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<pre id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="font-size: small;"><span style="color: #0000ff;">INSERT INTO</span>
    Sales.Orders <span style="color: #0000ff;">WITH</span> (TABLOCK)
(
    CustomerId ,
    DateAndTime
)
<span style="color: #0000ff;">SELECT</span> <span style="color: #0000ff;">TOP</span> (100000)
    CustomerId    = <span style="color: #ff00ff;">ABS</span> (<span style="color: #ff00ff;">CHECKSUM</span> (<span style="color: #ff00ff;">NEWID</span> ())) % 1000000 + 1 ,
    DateAndTime   = <span style="color: #ff00ff;">DATEADD</span> (<span style="color: #0000ff;">SECOND</span> , - <span style="color: #ff00ff;">ABS</span> (<span style="color: #ff00ff;">CHECKSUM</span> (<span style="color: #ff00ff;">NEWID</span> ())) % (60 * 60 * 24 * 365 * 5) , <span style="color: #ff00ff;">SYSDATETIME</span> ())
<span style="color: #0000ff;">FROM</span>
    <span style="color: #75be56;">sys.all_columns</span> <span style="color: #0000ff;">AS</span> T1
<span style="color: #0000ff;">CROSS</span> <span style="color: #0000ff;">JOIN</span>
    <span style="color: #75be56;">sys.all_columns</span> <span style="color: #0000ff;">AS</span> T2;
<span style="color: #0000ff;">GO</span>
</span></pre>
</div>
<p dir="ltr">Notice a few things:</p>
<p dir="ltr">
<ol dir="ltr">
<li>
<div>I'm using the <span style="font-family: &amp;amp;amp; color: #0000ff;">TABLOCK</span> hint in order to use minimal logging. For more information about minimally logged inserts, I recommend <a href="http://www.sqlmag.com/article/tsql3/minimally-logged-inserts" target="_blank">this article</a> by Itzik Ben-Gan.</div>
</li>
<li>
<div>I'm retrieving rows from <span style="font-family: &amp;amp;amp; color: #75be56;">sys.all_columns</span> cross joined to itself. It really doesn’t matter what table/s I'm using in this case. All I need is to make sure that the data set contains at least 100,000 rows. The <span style="font-family: &amp;amp;amp; color: #0000ff;">TOP</span> clause limits the number of rows returned to 100,000.</div>
</li>
<li>
<div>I'm using the <span style="font-family: &amp;amp;amp; color: #ff00ff;">NEWID</span> function in order to generate random values. If you try to use the <span style="font-family: &amp;amp;amp; color: #ff00ff;">RAND</span> function, it will return the same value for the whole recordset, because the <span style="font-family: &amp;amp;amp; color: #ff00ff;">RAND</span> function is evaluated only once for the query. If you want a different value for each row (of course you do), then you need to use the <span style="font-family: &amp;amp;amp; color: #ff00ff;">NEWID</span> function.</div>
</li>
</ol>
<p dir="ltr">Here is how the table looks like (10 first rows) after the insert:</p>
<table dir="ltr" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr align="center" bgcolor="#a5a5a5">
<td width="113"><strong><span style="font-size: medium;"><span style="font-size: small;">Id</span></span></strong></td>
<td width="113"><strong><span style="font-size: medium;"><span style="font-size: small;">CustomerId</span></span></strong></td>
<td width="151"><strong><span style="font-size: medium;"><span style="font-size: small;">DateAndTime</span></span></strong></td>
<td width="113"><strong><span style="font-size: medium;"><span style="font-size: small;">OrderStatus</span></span></strong></td>
</tr>
<tr>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">1</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">127693</span></span></td>
<td width="151"><span style="font-size: medium;"><span style="font-size: small;">2009-09-30 02:18:25</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">NULL</span></span></td>
</tr>
<tr>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">2</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">135764</span></span></td>
<td width="151"><span style="font-size: medium;"><span style="font-size: small;">2008-04-26 07:25:29</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">NULL</span></span></td>
</tr>
<tr>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">3</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">516346</span></span></td>
<td width="151"><span style="font-size: medium;"><span style="font-size: small;">2010-04-17 13:19:24</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">NULL</span></span></td>
</tr>
<tr>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">4</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">523668</span></span></td>
<td width="151"><span style="font-size: medium;"><span style="font-size: small;">2009-05-20 21:12:51</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">NULL</span></span></td>
</tr>
<tr>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">5</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">249878</span></span></td>
<td width="151"><span style="font-size: medium;"><span style="font-size: small;">2010-04-14 10:43:30</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">NULL</span></span></td>
</tr>
<tr>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">6</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">720186</span></span></td>
<td width="151"><span style="font-size: medium;"><span style="font-size: small;">2008-11-20 07:00:06</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">NULL</span></span></td>
</tr>
<tr>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">7</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">80792</span></span></td>
<td width="151"><span style="font-size: medium;"><span style="font-size: small;">2011-03-08 02:39:24</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">NULL</span></span></td>
</tr>
<tr>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">8</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">46122</span></span></td>
<td width="151"><span style="font-size: medium;"><span style="font-size: small;">2011-08-24 10:47:45</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">NULL</span></span></td>
</tr>
<tr>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">9</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">737056</span></span></td>
<td width="151"><span style="font-size: medium;"><span style="font-size: small;">2009-12-15 02:14:43</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">NULL</span></span></td>
</tr>
<tr>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">10</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">304384</span></span></td>
<td width="151"><span style="font-size: medium;"><span style="font-size: small;">2008-03-17 04:51:10</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">NULL</span></span></td>
</tr>
</tbody>
</table>
<p dir="ltr">Now, let's add the &quot;OrderStatus&quot; column to the game. This column should contain one of the following values: &quot;New&quot;, &quot;Open&quot;, &quot;Closed&quot;, &quot;Cancelled&quot; and &quot;Archived&quot;. Generating random values for this column is a bit trickier. We can achieve this by using the <span style="font-family: &amp;amp;amp; color: #0000ff;">CASE </span>statement. But first, a few words about this statement…</p>
<p dir="ltr">
<p dir="ltr">The <a href="http://msdn.microsoft.com/en-us/library/ms181765.aspx" target="_blank">CASE</a> statement has two flavors. One is called &quot;Simple CASE&quot;, and it compares a single expression to a set of simple expressions. The second is called &quot;Searched CASE&quot;, and it evaluates a set of general Boolean expressions.</p>
<p dir="ltr">Here is an example of a simple CASE:</p>
<p dir="ltr">
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<pre id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="color: #0000ff;">CASE</span> CustomerTypeId
    <span style="color: #0000ff;">WHEN</span> 1    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'Regular'</span>
    <span style="color: #0000ff;">WHEN</span> 2    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'VIP'</span>
    <span style="color: #0000ff;">WHEN</span> 3    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'New'</span>
    <span style="color: #0000ff;">WHEN</span> 4    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'Blocked'</span>
    <span style="color: #0000ff;">ELSE</span> <span style="color: #ff0000;">N'Unknown'</span>
<span style="color: #0000ff;">END</span>
</span></span></pre>
</div>
<p dir="ltr">And here is an example of a searched CASE:</p>
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<pre id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="font-size: x-small;"><span style="font-size: small;"><span style="color: #0000ff;">CASE</span>
    <span style="color: #0000ff;">WHEN</span> Price &lt;= $100.00                       <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'Cheap'</span>
    <span style="color: #0000ff;">WHEN</span> Price &gt; $100.00 <span style="color: #0000ff;">AND</span> ProductType = 1    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'Medium'</span>
    <span style="color: #0000ff;">WHEN</span> Price &gt; $100.00 <span style="color: #0000ff;">AND</span> ProductType = 2    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'Expensive'</span>
    <span style="color: #0000ff;">WHEN</span> Price <span style="color: #0000ff;">IS</span> <span style="color: #0000ff;">NULL</span>                          <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'Unknown Price'</span>
    <span style="color: #0000ff;">ELSE</span> <span style="color: #ff0000;">N'Unknown Product Type'</span>
<span style="color: #0000ff;">END</span>
</span></span></pre>
</div>
<p dir="ltr">In both cases, the first condition that evaluates to TRUE determines the result. The <span style="font-family: &amp;amp;amp; color: #0000ff;">ELSE </span>clause is optional. If all conditions evaluate to FALSE and there is no <span style="font-family: &amp;amp;amp; color: #0000ff;">ELSE </span>clause, then the result is NULL.</p>
<p dir="ltr">Now, let's go back to our example. In order to generate a random value for the &quot;OrderStatus&quot; column, we can generate a random value between 1 and 5, and use the simple <span style="font-family: &amp;amp;amp; color: #0000ff;">CASE </span>statement in order to translate these numbers into text values, like this:</p>
<p dir="ltr">
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<pre id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="font-size: small;"><span style="color: #0000ff;">TRUNCATE</span> <span style="color: #0000ff;">TABLE</span>
    Sales.Orders;
<span style="color: #0000ff;">GO</span>

<span style="color: #0000ff;">INSERT INTO</span>
    Sales.Orders <span style="color: #0000ff;">WITH</span> (TABLOCK)
(
    CustomerId ,
    DateAndTime ,
    OrderStatus
)
<span style="color: #0000ff;">SELECT</span> <span style="color: #0000ff;">TOP</span> (100000)
    CustomerId    = <span style="color: #ff00ff;">ABS</span> (<span style="color: #ff00ff;">CHECKSUM</span> (<span style="color: #ff00ff;">NEWID</span> ())) % 1000000 + 1 ,
    DateAndTime   = <span style="color: #ff00ff;">DATEADD</span> (<span style="color: #0000ff;">SECOND</span> , - <span style="color: #ff00ff;">ABS</span> (<span style="color: #ff00ff;">CHECKSUM</span> (<span style="color: #ff00ff;">NEWID</span> ())) % (60 * 60 * 24 * 365 * 5) , <span style="color: #ff00ff;">SYSDATETIME</span> ()) ,
    OrderStatus   = <span style="color: #0000ff;">CASE</span> <span style="color: #ff00ff;">ABS</span> (<span style="color: #ff00ff;">CHECKSUM</span> (<span style="color: #ff00ff;">NEWID</span> ())) % 5 + 1
                        <span style="color: #0000ff;">WHEN</span> 1    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'New'</span>
                        <span style="color: #0000ff;">WHEN</span> 2    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'Open'</span>
                        <span style="color: #0000ff;">WHEN</span> 3    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'Closed'</span>
                        <span style="color: #0000ff;">WHEN</span> 4    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'Cancelled'</span>
                        <span style="color: #0000ff;">WHEN</span> 5    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'Archived'</span>
                    <span style="color: #0000ff;">END</span>
<span style="color: #0000ff;">FROM</span>
    <span style="color: #75be56;">sys.all_columns</span> <span style="color: #0000ff;">AS</span> T1
<span style="color: #0000ff;">CROSS</span> <span style="color: #0000ff;">JOIN</span>
    <span style="color: #75be56;">sys.all_columns</span> <span style="color: #0000ff;">AS</span> T2;
<span style="color: #0000ff;">GO</span>
</span></pre>
</div>
<p dir="ltr">Here are the results (first 10 rows):</p>
<table dir="ltr" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr align="center" bgcolor="#a5a5a5">
<td width="113"><strong><span style="font-size: medium;"><span style="font-size: small;">Id</span></span></strong></td>
<td width="113"><strong><span style="font-size: medium;"><span style="font-size: small;">CustomerId</span></span></strong></td>
<td width="113"><strong><span style="font-size: medium;"><span style="font-size: small;">DateAndTime</span></span></strong></td>
<td width="113"><strong><span style="font-size: medium;"><span style="font-size: small;">OrderStatus</span></span></strong></td>
</tr>
<tr>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">1</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">116062</span></span></td>
<td width="151"><span style="font-size: medium;"><span style="font-size: small;">2008-09-24 19:50:01</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">NULL</span></span></td>
</tr>
<tr>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">2</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">3469</span></span></td>
<td width="151"><span style="font-size: medium;"><span style="font-size: small;">2008-02-24 00:59:53</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">Cancelled</span></span></td>
</tr>
<tr>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">3</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">635318</span></span></td>
<td width="151"><span style="font-size: medium;"><span style="font-size: small;">2009-07-23 09:44:25</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">Open</span></span></td>
</tr>
<tr>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">4</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">413157</span></span></td>
<td width="151"><span style="font-size: medium;"><span style="font-size: small;">2010-04-27 01:14:15</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">NULL</span></span></td>
</tr>
<tr>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">5</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">505098</span></span></td>
<td width="151"><span style="font-size: medium;"><span style="font-size: small;">2009-08-20 22:42:10</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">NULL</span></span></td>
</tr>
<tr>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">6</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">113948</span></span></td>
<td width="151"><span style="font-size: medium;"><span style="font-size: small;">2010-01-26 21:47:57</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">NULL</span></span></td>
</tr>
<tr>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">7</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">981939</span></span></td>
<td width="151"><span style="font-size: medium;"><span style="font-size: small;">2007-10-20 21:12:36</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">NULL</span></span></td>
</tr>
<tr>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">8</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">258577</span></span></td>
<td width="151"><span style="font-size: medium;"><span style="font-size: small;">2007-05-20 20:59:16</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">Open</span></span></td>
</tr>
<tr>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">9</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">704882</span></span></td>
<td width="151"><span style="font-size: medium;"><span style="font-size: small;">2007-10-01 16:13:24</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">Cancelled</span></span></td>
</tr>
<tr>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">10</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">668201</span></span></td>
<td width="151"><span style="font-size: medium;"><span style="font-size: small;">2010-04-08 15:12:40</span></span></td>
<td width="113"><span style="font-size: medium;"><span style="font-size: small;">NULL</span></span></td>
</tr>
</tbody>
</table>
<p dir="ltr">Notice that there are many NULL values in the &quot;OrderStatus&quot; column. This is strange, cause the expression we used (ABS (CHECKSUM (NEWID ())) % 5 + 1) <span style="text-decoration: underline;">always</span> returns a number between 1 and 5, so one of the conditions in the <span style="font-family: &amp;amp;amp; color: #0000ff;">CASE </span>clause should have been evaluated to TRUE. What’s going on?</p>
<p dir="ltr">Well, I'm glad you asked, because this is what I wanted to blog about this time. SQL Server doesn't really have two flavors of the <span style="font-family: &amp;amp;amp; color: #0000ff;">CASE </span>statement. It's actually two interfaces for our convenience. When we use the simple <span style="font-family: &amp;amp;amp; color: #0000ff;">CASE </span>statement, SQL Server translates it into the searched <span style="font-family: &amp;amp;amp; color: #0000ff;">CASE </span>format, because this is actually the only format that exists for the query processor. So the previous <span style="font-family: &amp;amp;amp; color: #0000ff;">INSERT </span>statement is translated by SQL Server to this:</p>
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<pre id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="font-size: small;"><span style="color: #0000ff;">TRUNCATE</span> <span style="color: #0000ff;">TABLE</span>
    Sales.Orders;
<span style="color: #0000ff;">GO</span>

<span style="color: #0000ff;">INSERT INTO</span>
    Sales.Orders <span style="color: #0000ff;">WITH</span> (TABLOCK)
(
    CustomerId ,
    DateAndTime ,
    OrderStatus
)
<span style="color: #0000ff;">SELECT</span> <span style="color: #0000ff;">TOP</span> (100000)
    CustomerId    = <span style="color: #ff00ff;">ABS</span> (<span style="color: #ff00ff;">CHECKSUM</span> (<span style="color: #ff00ff;">NEWID</span> ())) % 1000000 + 1 ,
    DateAndTime   = <span style="color: #ff00ff;">DATEADD</span> (<span style="color: #0000ff;">SECOND</span> , - <span style="color: #ff00ff;">ABS</span> (<span style="color: #ff00ff;">CHECKSUM</span> (<span style="color: #ff00ff;">NEWID</span> ())) % (60 * 60 * 24 * 365 * 5) , <span style="color: #ff00ff;">SYSDATETIME</span> ()) ,
    OrderStatus   = <span style="color: #0000ff;">CASE</span>
                        <span style="color: #0000ff;">WHEN</span> <span style="color: #ff00ff;">ABS</span> (<span style="color: #ff00ff;">CHECKSUM</span> (<span style="color: #ff00ff;">NEWID</span> ())) % 5 + 1 = 1    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'New'</span>
                        <span style="color: #0000ff;">WHEN</span> <span style="color: #ff00ff;">ABS</span> (<span style="color: #ff00ff;">CHECKSUM</span> (<span style="color: #ff00ff;">NEWID</span> ())) % 5 + 1 = 2    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'Open'</span>
                        <span style="color: #0000ff;">WHEN</span> <span style="color: #ff00ff;">ABS</span> (<span style="color: #ff00ff;">CHECKSUM</span> (<span style="color: #ff00ff;">NEWID</span> ())) % 5 + 1 = 3    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'Closed'</span>
                        <span style="color: #0000ff;">WHEN</span> <span style="color: #ff00ff;">ABS</span> (<span style="color: #ff00ff;">CHECKSUM</span> (<span style="color: #ff00ff;">NEWID</span> ())) % 5 + 1 = 4    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'Cancelled'</span>
                        <span style="color: #0000ff;">WHEN</span> <span style="color: #ff00ff;">ABS</span> (<span style="color: #ff00ff;">CHECKSUM</span> (<span style="color: #ff00ff;">NEWID</span> ())) % 5 + 1 = 5    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'Archived'</span>
                    <span style="color: #0000ff;">END</span>
<span style="color: #0000ff;">FROM</span>
    <span style="color: #75be56;">sys.all_columns</span> <span style="color: #0000ff;">AS</span> T1
<span style="color: #0000ff;">CROSS</span> <span style="color: #0000ff;">JOIN</span>
    <span style="color: #75be56;">sys.all_columns</span> <span style="color: #0000ff;">AS</span> T2;
<span style="color: #0000ff;">GO</span></span></pre>
</div>
<p dir="ltr">
<p dir="ltr">Now you can see that the expression ABS (CHECKSUM (NEWID ())) % 5 + 1 is calculated separately for each condition in the Searched CASE statement. For the first condition, a random value between 1 and 5 is calculated and compared to &quot;1". The probability of a match is 20%. If it's FALSE, then we move one to the second condition, for which another (different) random value between 1 and 5 is calculated and, this time, compared to &quot;2". Again, we have a probability of 20% for a match, and so on. It is very likely that all conditions will evaluate to FALSE, and in that case the result will be NULL. Actually, the probability of getting NULL from this <span style="font-family: &amp;amp;amp; color: #0000ff;">CASE </span>expression is 0.85 = 32.768%. If you count the number of NULL values in the table, you'll find that there are around 32,768 such rows (out of 100,000 rows in total).</p>
<p dir="ltr">OK, so we learned that the Simple <span style="font-family: &amp;amp;amp; color: #0000ff;">CASE </span>is actually a Searched <span style="font-family: &amp;amp;amp; color: #0000ff;">CASE </span>behind the scenes, and that for this reason, using a random expression with the Simple <span style="font-family: &amp;amp;amp; color: #0000ff;">CASE </span>is problematic. That's good to know (I think), but I still want to populate the &quot;OrderStatus&quot; column with random values. How do I do that?</p>
<p dir="ltr">Well, I'm glad you asked again. There are several options to solve this problem. One of them is to use a small table with a single column containing the 5 text values, cross join to this table as well and sort the whole recordset randomly. Like this:</p>
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<pre id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="font-size: small;"><span style="color: #0000ff;">CREATE</span> <span style="color: #0000ff;">TABLE</span>
    Sales.OrderStatuses
(
    OrderStatus <span style="color: #0000ff;">NVARCHAR</span>(50) <span style="color: #0000ff;">NOT</span> <span style="color: #0000ff;">NULL</span>
);
<span style="color: #0000ff;">GO</span>

<span style="color: #0000ff;">INSERT INTO</span>
    Sales.OrderStatuses
(
    OrderStatus
)
<span style="color: #0000ff;">VALUES</span>
    (<span style="color: #ff0000;">N'New'</span>) ,
    (<span style="color: #ff0000;">N'Open'</span>) ,
    (<span style="color: #ff0000;">N'Closed'</span>) ,
    (<span style="color: #ff0000;">N'Cancelled'</span>) ,
    (<span style="color: #ff0000;">N'Archived'</span>);
<span style="color: #0000ff;">GO</span>

<span style="color: #0000ff;">TRUNCATE</span> <span style="color: #0000ff;">TABLE</span>
    Sales.Orders;
<span style="color: #0000ff;">GO</span>

<span style="color: #0000ff;">INSERT INTO</span>
    Sales.Orders <span style="color: #0000ff;">WITH</span> (TABLOCK)
(
    CustomerId ,
    DateAndTime ,
    OrderStatus
)
<span style="color: #0000ff;">SELECT</span> <span style="color: #0000ff;">TOP</span> (100000)
    CustomerId    = <span style="color: #ff00ff;">ABS</span> (<span style="color: #ff00ff;">CHECKSUM</span> (<span style="color: #ff00ff;">NEWID</span> ())) % 1000000 + 1 ,
    DateAndTime   = <span style="color: #ff00ff;">DATEADD</span> (<span style="color: #0000ff;">SECOND</span> , - <span style="color: #ff00ff;">ABS</span> (<span style="color: #ff00ff;">CHECKSUM</span> (<span style="color: #ff00ff;">NEWID</span> ())) % (60 * 60 * 24 * 365 * 5) , <span style="color: #ff00ff;">SYSDATETIME</span> ()) ,
    OrderStatus   = OrderStatus
<span style="color: #0000ff;">FROM</span>
    <span style="color: #75be56;">sys.all_columns</span> <span style="color: #0000ff;">AS</span> T1
<span style="color: #0000ff;">CROSS</span> <span style="color: #0000ff;">JOIN</span>
    <span style="color: #75be56;">sys.all_columns</span> <span style="color: #0000ff;">AS</span> T2
<span style="color: #0000ff;">CROSS</span> <span style="color: #0000ff;">JOIN</span>
    Sales.OrderStatuses
<span style="color: #0000ff;">ORDER</span> <span style="color: #0000ff;">BY</span>
    <span style="color: #ff00ff;">NEWID</span> () <span style="color: #0000ff;">ASC</span>;
<span style="color: #0000ff;">GO</span></span></pre>
</div>
<p dir="ltr">This method works, but it's very inefficient, because the query processor has to complete all the join operations first, then calculate the <span style="color: #ff00ff;">NEWID</span> function for every row, then sort the whole recordset according to the values of <span style="color: #ff00ff;">NEWID</span>, and only then retrieve the top 100,000 rows. Since the recordset is huge (around 134,000,000 in my case), the sort operation is going to take forever. You can try to replace <span style="color: #75be56;">sys.all_columns</span> with another table that has fewer rows in order to get a recordset of a size close to 100,000, but it's still not going to be an easy task.</p>
<p dir="ltr">Another solution to the problem is to insert the 100,000 rows one by one in a loop. Inside the loop we can calculate the random value once and put it in a variable, and then we can use the Simple <span style="font-family: &amp;amp;amp;">CASE </span>statement with the variable. Like this:</p>
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<pre id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="font-size: small;"><span style="color: #0000ff;">DECLARE</span>
    @Counter                    <span style="color: #0000ff;">AS</span> <span style="color: #0000ff;">INT</span>        = 100000 ,
    @RandomValueBetween1And5    <span style="color: #0000ff;">AS TINYINT</span>;

<span style="color: #0000ff;">TRUNCATE</span> <span style="color: #0000ff;">TABLE</span>
    Sales.Orders;

<span style="color: #0000ff;">WHILE</span>
    @Counter &gt; 0
<span style="color: #0000ff;">BEGIN</span>

    <span style="color: #0000ff;">SET</span> @RandomValueBetween1And5 = <span style="color: #ff00ff;">ABS</span> (<span style="color: #ff00ff;">CHECKSUM</span> (<span style="color: #ff00ff;">NEWID</span> ())) % 5 + 1;

    <span style="color: #0000ff;">INSERT INTO</span>
        Sales.Orders <span style="color: #0000ff;">WITH</span> (TABLOCK)
    (
        CustomerId ,
        DateAndTime ,
        OrderStatus
    )
<span style="color: #0000ff;">    SELECT</span> <span style="color: #0000ff;">TOP</span> (100000)
        CustomerId    = <span style="color: #ff00ff;">ABS</span> (<span style="color: #ff00ff;">CHECKSUM</span> (<span style="color: #ff00ff;">NEWID</span> ())) % 1000000 + 1 ,
        DateAndTime   = <span style="color: #ff00ff;">DATEADD</span> (<span style="color: #0000ff;">SECOND</span> , - <span style="color: #ff00ff;">ABS</span> (<span style="color: #ff00ff;">CHECKSUM</span> (<span style="color: #ff00ff;">NEWID</span> ())) % (60 * 60 * 24 * 365 * 5) , <span style="color: #ff00ff;">SYSDATETIME</span> ()) ,
        OrderStatus   = <span style="color: #0000ff;">CASE</span> @RandomValueBetween1And5
                            <span style="color: #0000ff;">WHEN</span> 1    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'New'</span>
                            <span style="color: #0000ff;">WHEN</span> 2    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'Open'</span>
                            <span style="color: #0000ff;">WHEN</span> 3    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'Closed'</span>
                            <span style="color: #0000ff;">WHEN</span> 4    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'Cancelled'</span>
                            <span style="color: #0000ff;">WHEN</span> 5    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'Archived'</span>
                        <span style="color: #0000ff;">END</span>;

    <span style="color: #0000ff;">SET</span> @Counter -= 1;

<span style="color: #0000ff;">END</span>;
<span style="color: #0000ff;">GO</span>
</span></pre>
</div>
<p dir="ltr">This method also works, of course, but it is also inefficient, because there are 100,000 <span style="font-family: &amp;amp;amp;">INSERT </span>operations instead of one.</p>
<p dir="ltr">There are more inefficient ways to solve this problem, but let's look at an efficient one. Let's go back to the original query with the Simple <span style="font-family: &amp;amp;amp;">CASE </span>statement. We can still use the same query, but we need to make sure that the expression in the Simple <span style="font-family: &amp;amp;amp;">CASE </span>statement is calculated only once, and then compared to the other values (1 through 5). We can achieve this by performing the calculation of the random value in a derived table, like this:</p>
<div id="codeSnippetWrapper" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;">
<pre id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="font-size: small;"><span style="color: #0000ff;">TRUNCATE</span> <span style="color: #0000ff;">TABLE</span>
    Sales.Orders;
<span style="color: #0000ff;">GO</span>

<span style="color: #0000ff;">INSERT INTO</span>
    Sales.Orders <span style="color: #0000ff;">WITH</span> (TABLOCK)
(
    CustomerId ,
    DateAndTime ,
    OrderStatus
)
<span style="color: #0000ff;">SELECT</span> <span style="color: #0000ff;">TOP</span> (100000)
    CustomerId    = <span style="color: #ff00ff;">ABS</span> (<span style="color: #ff00ff;">CHECKSUM</span> (<span style="color: #ff00ff;">NEWID</span> ())) % 1000000 + 1 ,
    DateAndTime   = <span style="color: #ff00ff;">DATEADD</span> (<span style="color: #0000ff;">SECOND</span> , - <span style="color: #ff00ff;">ABS</span> (<span style="color: #ff00ff;">CHECKSUM</span> (<span style="color: #ff00ff;">NEWID</span> ())) % (60 * 60 * 24 * 365 * 5) , <span style="color: #ff00ff;">SYSDATETIME</span> ()) ,
    OrderStatus   = <span style="color: #0000ff;">CASE</span> RandomValueTable.RandomValue
                        <span style="color: #0000ff;">WHEN</span> 1    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'New'</span>
                        <span style="color: #0000ff;">WHEN</span> 2    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'Open'</span>
                        <span style="color: #0000ff;">WHEN</span> 3    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'Closed'</span>
                        <span style="color: #0000ff;">WHEN</span> 4    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'Cancelled'</span>
                        <span style="color: #0000ff;">WHEN</span> 5    <span style="color: #0000ff;">THEN</span> <span style="color: #ff0000;">N'Archived'</span>
                    <span style="color: #0000ff;">END</span>
<span style="color: #0000ff;">FROM</span>
    <span style="color: #75be56;">sys.all_columns</span> <span style="color: #0000ff;">AS</span> T1
<span style="color: #0000ff;">CROSS</span> <span style="color: #0000ff;">JOIN</span>
    <span style="color: #75be56;">sys.all_columns</span> <span style="color: #0000ff;">AS</span> T2
<span style="color: #0000ff;">CROSS</span> <span style="color: #0000ff;">JOIN</span>
    (
        <span style="color: #0000ff;">SELECT</span>
            RandomValue = <span style="color: #ff00ff;">ABS</span> (<span style="color: #ff00ff;">CHECKSUM</span> (<span style="color: #ff00ff;">NEWID</span> ())) % 5 + 1
    )
    <span style="color: #0000ff;">AS</span>
        RandomValueTable;
<span style="color: #0000ff;">GO</span></span></pre>
</div>
<p dir="ltr">Now the random value is calculated once inside the derived table, and it is exposed to the outer query as a column. When SQL Server transforms the Simple <span style="font-family: &amp;amp;amp;">CASE </span>statement into a Searched <span style="font-family: &amp;amp;amp;">CASE </span>format, it's still OK, because the &quot;RandomValue&quot; column is not calculated separately for each condition. It's actually similar to the solution using a loop and a variable to hold the random value. Here, we use a column in a derived table to hold the random value instead of a variable.</p>
<p dir="ltr">Now we have a solution that works and is also very efficient. Here are the results:</p>
<table dir="ltr" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr align="center" bgcolor="#a5a5a5">
<td width="113"><strong>Id</strong></td>
<td width="113"><strong>CustomerId</strong></td>
<td width="151"><strong>DateAndTime</strong></td>
<td width="113"><strong>OrderStatus</strong></td>
</tr>
<tr>
<td width="113" valign="top">1</td>
<td width="113" valign="top">369111</td>
<td width="151" valign="top">2011-01-01 03:31:42</td>
<td width="113" valign="top">Archived</td>
</tr>
<tr>
<td width="113" valign="top">2</td>
<td width="113" valign="top">204392</td>
<td width="151" valign="top">2009-05-02 09:00:00</td>
<td width="113" valign="top">Cancelled</td>
</tr>
<tr>
<td width="113" valign="top">3</td>
<td width="113" valign="top">997423</td>
<td width="151" valign="top">2010-12-09 00:22:51</td>
<td width="113" valign="top">Archived</td>
</tr>
<tr>
<td width="113" valign="top">4</td>
<td width="113" valign="top">542308</td>
<td width="151" valign="top">2009-12-04 03:10:26</td>
<td width="113" valign="top">Cancelled</td>
</tr>
<tr>
<td width="113" valign="top">5</td>
<td width="113" valign="top">960822</td>
<td width="151" valign="top">2009-03-12 07:26:03</td>
<td width="113" valign="top">Closed</td>
</tr>
<tr>
<td width="113" valign="top">6</td>
<td width="113" valign="top">216770</td>
<td width="151" valign="top">2007-05-02 21:03:30</td>
<td width="113" valign="top">Closed</td>
</tr>
<tr>
<td width="113" valign="top">7</td>
<td width="113" valign="top">1314</td>
<td width="151" valign="top">2008-07-23 22:26:21</td>
<td width="113" valign="top">Archived</td>
</tr>
<tr>
<td width="113" valign="top">8</td>
<td width="113" valign="top">886849</td>
<td width="151" valign="top">2007-08-27 15:56:29</td>
<td width="113" valign="top">Archived</td>
</tr>
<tr>
<td width="113" valign="top">9</td>
<td width="113" valign="top">176455</td>
<td width="151" valign="top">2009-08-31 20:43:28</td>
<td width="113" valign="top">Archived</td>
</tr>
<tr>
<td width="113" valign="top">10</td>
<td width="113" valign="top">949195</td>
<td width="151" valign="top">2010-07-31 02:41:29</td>
<td width="113" valign="top">New</td>
</tr>
</tbody>
</table>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/case-study/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>&#8235;About Snapshots and Ghosts&#8236;</title>		<link>http://www.madeira.co.il/about-snapshots-and-ghosts/</link>
		<comments>http://www.madeira.co.il/about-snapshots-and-ghosts/#comments</comments>
		<pubDate>Fri, 30 Dec 2011 17:38:33 +0000</pubDate>
		<dc:creator>&#8235;גיא גלנצר&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[מקודמות]]></category>
		<category><![CDATA[Isolation Levels]]></category>
		<category><![CDATA[Row Versioning]]></category>
		<category><![CDATA[אינדקסים]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=2830</guid>
		<description><![CDATA[&#8235;Here is a short example of how SQL Server uses row versioning in conjunction with the snapshot isolation level...&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p><img src='http://www.madeira.co.il/wp-content/plugins/simple-post-thumbnails/timthumb.php?src=/wp-content/thumbnails/2830.jpg&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p style="direction: ltr;">A few weeks ago <a href="http://sqlblog.com/blogs/kalen_delaney/default.aspx">Kalen Delaney</a> presented a <a href="http://www.naya-college.co.il/files/Syllabus-KALEN.pdf">3-day seminar</a> in Israel. Unfortunately, I wasn't able to attend the seminar, but some of our consultants attended and returned from the seminar with some interesting insights.</p>
<p style="direction: ltr;">The first day was about concurrency control. Among other things, Kalen spoke about the snapshot isolation level and how row versioning works when this isolation level is used.</p>
<p style="direction: ltr;">One question was remained unanswered. Suppose you have an index on column &quot;Col1", and you update this column while a snapshot transaction is open. The index key is, of course, updated and moved to the new position in the index, and it also now has an additional 14-bytes pointer to the versioning info in tempdb. As long as queries in the snapshot transaction access the table, they will retrieve the old value from the version store. Let's assume you updated the column from 2 to 5. Now, what happens if a query in the snapshot transaction tries to retrieves the rows with Col1 = 2? The information about the previous value (2) is stored in tempdb, and the pointer is stored in the index under the new value (5). So if a query performs an index seek in order to retrieve only the rows with a value of 2, how would SQL Server know about the old value stored in tempdb?</p>
<p style="direction: ltr;">Let's look at an example. First, here is the setup:</p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">USE</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span><span><span style="color: #0000ff;">master</span></span></span><span><span style="color: #808080;">;</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">GO</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #008000;">&#8211; Create the SnapshotTest database</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #008000;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">IF</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span><span><span style="color: #ff00ff;">DB_ID</span></span><span><span style="color: #0000ff;"> </span></span><span><span style="color: #808080;">(</span></span><span><span style="color: #ff0000;">N'SnapshotTest'</span></span><span><span style="color: #808080;">)</span></span> <span><span style="color: #808080;">IS</span></span> <span><span style="color: #808080;">NOT</span></span> </span><span><span style="color: #808080;">NULL</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">BEGIN</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span><span><span style="color: #0000ff;">ALTER</span></span> </span><span><span style="color: #0000ff;">DATABASE</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>SnapshotTest</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span><span style="color: #0000ff;">SET</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span><span style="color: #0000ff;">SINGLE_USER</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span><span style="color: #0000ff;">WITH</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span><span><span style="color: #0000ff;">ROLLBACK</span></span> <span><span style="color: #0000ff;">IMMEDIATE</span></span></span><span><span style="color: #808080;">;</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #808080;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span><span><span style="color: #0000ff;">DROP</span></span> </span><span><span style="color: #0000ff;">DATABASE</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>SnapshotTest</span><span><span style="color: #808080;">;</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #808080;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span style="font-family: Courier New;"><span><span style="color: #0000ff;"><span>END</span></span></span><span><span style="color: #808080;">;</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">GO</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span style="font-family: Courier New;"><span><span style="color: #0000ff;"><span>CREATE</span></span></span><span><span> </span><span><span style="color: #0000ff;">DATABASE</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>SnapshotTest</span><span><span style="color: #808080;">;</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">GO</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span style="font-family: Courier New;"><span><span style="color: #0000ff;"><span>ALTER</span></span></span><span><span> </span><span><span style="color: #0000ff;">DATABASE</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>SnapshotTest</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">SET</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span><span><span style="color: #0000ff;">ALLOW_SNAPSHOT_ISOLATION</span></span> <span><span style="color: #0000ff;">ON</span></span></span><span><span style="color: #808080;">;</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">GO</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #008000;">&#8211; Create the &quot;dbo.Test&quot; table</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #008000;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">USE</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>SnapshotTest</span><span><span style="color: #808080;">;</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">GO</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span style="font-family: Courier New;"><span><span style="color: #0000ff;"><span>CREATE</span></span></span><span><span> </span><span><span style="color: #0000ff;">TABLE</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>dbo<span><span style="color: #808080;">.</span></span>Test</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #808080;">(</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>Col1<span> </span><span><span style="color: #0000ff;">INT</span></span><span> </span><span><span style="color: #808080;">NOT</span></span> <span><span style="color: #808080;">NULL</span></span> </span><span><span style="color: #808080;">,</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>Col2<span> </span><span><span style="color: #0000ff;">CHAR</span></span><span><span style="color: #808080;">(</span></span>5<span><span style="color: #808080;">)</span></span><span> </span><span><span style="color: #808080;">NOT</span></span> </span><span><span style="color: #808080;">NULL</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #808080;">);</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">GO</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #008000;">&#8211; Populate the &quot;dbo.Test&quot; table with 10 consecutive rows</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #008000;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span style="font-family: Courier New;"><span><span style="color: #0000ff;"><span>INSERT</span></span></span><span><span> </span><span><span style="color: #0000ff;">INTO</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>dbo<span><span style="color: #808080;">.</span></span>Test <span><span style="color: #0000ff;">WITH </span></span><span><span style="color: #808080;">(</span></span><span><span style="color: #0000ff;">TABLOCK</span></span></span><span><span style="color: #808080;">)</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #808080;">(</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>Col1 </span><span><span style="color: #808080;">,</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>Col2</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #808080;">)</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span style="font-family: Courier New;"><span><span style="color: #0000ff;"><span>SELECT</span></span></span><span><span> <span><span style="color: #0000ff;">TOP </span></span><span><span style="color: #808080;">(</span></span>10</span><span><span style="color: #808080;">)</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>Col1<span> </span><span><span style="color: #808080;">=</span></span> <span><span style="color: #ff00ff;">ROW_NUMBER</span></span><span><span style="color: #0000ff;"> </span></span><span><span style="color: #808080;">()</span></span> <span><span style="color: #0000ff;">OVER </span></span><span><span style="color: #808080;">(</span></span><span><span style="color: #0000ff;">ORDER</span></span> <span><span style="color: #0000ff;">BY </span></span><span><span style="color: #808080;">(</span></span><span><span style="color: #0000ff;">SELECT</span></span> <span><span style="color: #808080;">NULL)</span></span> <span><span style="color: #0000ff;">ASC</span></span><span><span style="color: #808080;">)</span></span> </span><span><span style="color: #808080;">,</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>Col2<span> </span><span><span style="color: #808080;">=</span></span> </span><span><span style="color: #ff0000;">N'XXXXX'</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">FROM</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span><span><span style="color: #008000;">sys</span></span><span><span style="color: #808080;">.</span></span><span><span style="color: #008000;">all_columns</span></span></span><span><span style="color: #808080;">;</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">GO</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">SELECT</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>Col1 </span><span><span style="color: #808080;">,</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>Col2</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">FROM</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>dbo<span><span style="color: #808080;">.</span></span>Test</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span style="font-family: Courier New;"><span><span style="color: #0000ff;"><span>ORDER</span></span></span><span><span> </span><span><span style="color: #0000ff;">BY</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>Col1 <span><span style="color: #0000ff;">ASC</span></span></span><span><span style="color: #808080;">;</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">GO</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #008000;">&#8211; Create a non-clustered index on the &quot;Col1" column</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #008000;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span style="font-family: Courier New;"><span><span style="color: #0000ff;"><span>CREATE</span></span></span><span><span> <span><span style="color: #0000ff;">NONCLUSTERED</span></span> </span><span><span style="color: #0000ff;">INDEX</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>ix_Test_nc_nu_Col1</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">ON</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>dbo<span><span style="color: #808080;">.</span></span>Test<span><span style="color: #0000ff;"> </span></span><span><span style="color: #808080;">(</span></span>Col1 <span><span style="color: #0000ff;">ASC</span></span></span><span><span style="color: #808080;">);</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">GO</span></span></span></p>
<p style="direction: ltr;">Let's look at the index page before we update the table. First, we use DBCC IND in order to retrieve the index pages:</p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span style="font-family: Courier New;"><span><span style="color: #0000ff;"><span>DBCC</span></span></span><span><span> IND<span><span style="color: #0000ff;"> </span></span><span><span style="color: #808080;">(</span></span><span><span style="color: #ff0000;">N'SnapshotTest'</span></span> <span><span style="color: #808080;">,</span></span> <span><span style="color: #ff0000;">N'dbo.Test'</span></span> <span><span style="color: #808080;">,</span></span> 2</span><span><span style="color: #808080;">);</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">GO</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="color: #0000ff; font-family: Courier New; font-size: x-small;"> </span></span></p>
<p style="direction: ltr;">Here are the results (I left only the interesting columns):</p>
<table style="direction: ltr;" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="95"><strong>PageFID</strong></td>
<td width="95"><strong>PagePID</strong></td>
<td width="95"><strong>PageType</strong></td>
<td width="95"><strong>IndexLevel</strong></td>
</tr>
<tr>
<td width="95">1</td>
<td width="95">90</td>
<td width="95">10</td>
<td width="95">NULL</td>
</tr>
<tr>
<td width="95">1</td>
<td width="95">89</td>
<td width="95">2</td>
<td width="95">0</td>
</tr>
</tbody>
</table>
<p style="direction: ltr;">As we can see, there is a single index page (89) that contains the index data. Page #90 is the IAM page, so it's not relevant for our test. Let's look at the contents of page #89:</p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span style="font-family: Courier New;"><span><span style="color: #0000ff;"><span>DBCC</span></span></span><span><span> TRACEON<span><span style="color: #0000ff;"> </span></span><span><span style="color: #808080;">(</span></span>3604</span><span><span style="color: #808080;">);</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">GO</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span style="font-family: Courier New;"><span><span style="color: #0000ff;"><span>DBCC</span></span></span><span><span> PAGE<span><span style="color: #0000ff;"> </span></span><span><span style="color: #808080;">(</span></span><span><span style="color: #ff0000;">N'SnapshotTest'</span></span> <span><span style="color: #808080;">,</span></span> 1 <span><span style="color: #808080;">,</span></span> 89 <span><span style="color: #808080;">,</span></span> 1</span><span><span style="color: #808080;">);</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">GO</span></span></span></p>
<p style="direction: ltr;">Here are the results (only a partial view):</p>
<p style="direction: ltr;">Slot 0, Offset 0&#215;60, Length 13, DumpStyle BYTE</p>
<p style="direction: ltr;">Record Type = INDEX_RECORD Record Attributes = Record Size = 13</p>
<p style="direction: ltr;">Memory Dump @0&#215;000000000FAFA060</p>
<p style="direction: ltr;">0000000000000000: 06<strong><span style="color: #ff0000;">01</span></strong>0000 004f0000 00010000 00††††††††&#8230;..O&#8230;&#8230;.</p>
<p style="direction: ltr;">Slot 1, Offset 0&#215;6d, Length 13, DumpStyle BYTE</p>
<p style="direction: ltr;">Record Type = INDEX_RECORD Record Attributes = Record Size = 13</p>
<p style="direction: ltr;">Memory Dump @0&#215;000000000FAFA06D</p>
<p style="direction: ltr;">0000000000000000: 06<strong><span style="color: #ff0000;">02</span></strong>0000 004f0000 00010001 00††††††††&#8230;..O&#8230;&#8230;.</p>
<p style="direction: ltr;">Slot 2, Offset 0&#215;7a, Length 13, DumpStyle BYTE</p>
<p style="direction: ltr;">Record Type = INDEX_RECORD Record Attributes = Record Size = 13</p>
<p style="direction: ltr;">Memory Dump @0&#215;000000000FAFA07A</p>
<p style="direction: ltr;">0000000000000000: 06<strong><span style="color: #ff0000;">03</span></strong>0000 004f0000 00010002 00††††††††&#8230;..O&#8230;&#8230;.</p>
<p style="direction: ltr;">Slot 3, Offset 0&#215;87, Length 13, DumpStyle BYTE</p>
<p style="direction: ltr;">Record Type = INDEX_RECORD Record Attributes = Record Size = 13</p>
<p style="direction: ltr;">Memory Dump @0&#215;000000000FAFA087</p>
<p style="direction: ltr;">0000000000000000: 06<strong><span style="color: #ff0000;">04</span></strong>0000 004f0000 00010003 00††††††††&#8230;..O&#8230;&#8230;.</p>
<p style="direction: ltr;">Slot 4, Offset 0&#215;94, Length 13, DumpStyle BYTE</p>
<p style="direction: ltr;">Record Type = INDEX_RECORD Record Attributes = Record Size = 13</p>
<p style="direction: ltr;">Memory Dump @0&#215;000000000FAFA094</p>
<p style="direction: ltr;">0000000000000000: 06<strong><span style="color: #ff0000;">05</span></strong>0000 004f0000 00010004 00††††††††&#8230;..O&#8230;&#8230;.</p>
<p style="direction: ltr;">Slot 5, Offset 0xa1, Length 13, DumpStyle BYTE</p>
<p style="direction: ltr;">Record Type = INDEX_RECORD Record Attributes = Record Size = 13</p>
<p style="direction: ltr;">Memory Dump @0&#215;000000000FAFA0A1</p>
<p style="direction: ltr;">0000000000000000: 06<strong><span style="color: #ff0000;">06</span></strong>0000 004f0000 00010005 00††††††††&#8230;..O&#8230;&#8230;.</p>
<p style="direction: ltr;">We can see the first 6 slots in the index page containing the values 1 through 6. I marked the values in red. Each slot is an &quot;INDEX_RECORD&quot;, and the size of each record is 13 bytes. So far so good…</p>
<p style="direction: ltr;">Now, let's begin our experiment. In a new connection, we are going to start a snapshot transaction and retrieve the data from the &quot;dbo.Test&quot; table:</p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">USE</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>SnapshotTest</span><span><span style="color: #808080;">;</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">GO</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span style="font-family: Courier New;"><span><span style="color: #0000ff;"><span>SET</span></span></span><span><span> <span><span style="color: #0000ff;">TRANSACTION</span></span> <span><span style="color: #0000ff;">ISOLATION</span></span> <span><span style="color: #0000ff;">LEVEL</span></span> <span><span style="color: #0000ff;">SNAPSHOT</span></span></span><span><span style="color: #808080;">;</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">GO</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span style="font-family: Courier New;"><span><span style="color: #0000ff;"><span>BEGIN</span></span></span><span><span> </span><span><span style="color: #0000ff;">TRANSACTION</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>ReadData</span><span><span style="color: #808080;">;</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">GO</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;"> </span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">SELECT</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>Col1 </span><span><span style="color: #808080;">,</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>Col2</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">FROM</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>dbo<span><span style="color: #808080;">.</span></span>Test</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span style="font-family: Courier New;"><span><span style="color: #0000ff;"><span>ORDER</span></span></span><span><span> </span><span><span style="color: #0000ff;">BY</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>Col1 <span><span style="color: #0000ff;">ASC</span></span></span><span><span style="color: #808080;">;</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 12pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">GO</span></span></span></p>
<p style="direction: ltr;">And now, in the first connection, let's update Col1 from 2 to 5:</p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">UPDATE</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>dbo<span><span style="color: #808080;">.</span></span>Test</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">SET</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>Col1 <span><span style="color: #808080;">=</span></span> 5</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">WHERE</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>Col1 <span><span style="color: #808080;">=</span></span> 2</span><span><span style="color: #808080;">;</span></span></span></span></p>
<p style="direction: ltr;"><span style="line-height: 12pt;"><span style="font-family: Courier New;"><span style="color: #0000ff;">GO</span></span></span></p>
<p style="direction: ltr;">If we retrieve the data again inside the snapshot transaction (in the second connection), we will still see the previous value (2), as expected:</p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">SELECT</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>Col1 </span><span><span style="color: #808080;">,</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>Col2</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">FROM</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>dbo<span><span style="color: #808080;">.</span></span>Test</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span style="font-family: Courier New;"><span><span style="color: #0000ff;"><span>ORDER</span></span></span><span><span> </span><span><span style="color: #0000ff;">BY</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>Col1 <span><span style="color: #0000ff;">ASC</span></span></span><span><span style="color: #808080;">;</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 12pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">GO</span></span></span></p>
<table style="direction: ltr;" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="95"><strong>Col1</strong></td>
<td width="95"><strong>Col2</strong></td>
</tr>
<tr>
<td width="95">1</td>
<td width="95">XXXXX</td>
</tr>
<tr>
<td width="95">2</td>
<td width="95">XXXXX</td>
</tr>
<tr>
<td width="95">3</td>
<td width="95">XXXXX</td>
</tr>
<tr>
<td width="95">4</td>
<td width="95">XXXXX</td>
</tr>
<tr>
<td width="95">5</td>
<td width="95">XXXXX</td>
</tr>
<tr>
<td width="95">6</td>
<td width="95">XXXXX</td>
</tr>
<tr>
<td width="95">7</td>
<td width="95">XXXXX</td>
</tr>
<tr>
<td width="95">8</td>
<td width="95">XXXXX</td>
</tr>
<tr>
<td width="95">9</td>
<td width="95">XXXXX</td>
</tr>
<tr>
<td width="95">10</td>
<td width="95">XXXXX</td>
</tr>
</tbody>
</table>
<p style="direction: ltr;">If we try to retrieve only the rows with Col1 = 2, it also works:</p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">SELECT</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>Col1 </span><span><span style="color: #808080;">,</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>Col2</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">FROM</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>dbo<span><span style="color: #808080;">.</span></span>Test</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">WHERE</span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 0pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span><span> </span></span><span>Col1 <span><span style="color: #808080;">=</span></span> 2</span><span><span style="color: #808080;">;</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin: 0in 0in 12pt 14.2pt; direction: ltr;"><span><span style="font-family: Courier New;"><span style="color: #0000ff;">GO</span></span></span></p>
<table style="direction: ltr;" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="95"><strong>Col1</strong></td>
<td width="95"><strong>Col2</strong></td>
</tr>
<tr>
<td width="95">2</td>
<td width="95">XXXXX</td>
</tr>
</tbody>
</table>
<p style="direction: ltr;">How does it work?</p>
<p style="direction: ltr;">Let's look again at the contents of the index page:</p>
<p style="direction: ltr;">Slot 0, Offset 0&#215;60, Length 13, DumpStyle BYTE</p>
<p style="direction: ltr;">Record Type = INDEX_RECORD Record Attributes = Record Size = 13</p>
<p style="direction: ltr;">Memory Dump @0&#215;00000000111EA060</p>
<p style="direction: ltr;">0000000000000000: 06<strong><span style="color: #ff0000;">01</span></strong>0000 004f0000 00010000 00††††††††&#8230;..O&#8230;&#8230;.</p>
<p style="direction: ltr;">Slot 1, Offset 0xe2, Length 27, DumpStyle BYTE</p>
<p style="direction: ltr;"><span style="color: #ff0000;">Record Type = GHOST_INDEX_RECORD Record Attributes = VERSIONING_INFO Record Size = 27</span></p>
<p style="direction: ltr;">Memory Dump @0&#215;00000000111EA0E2</p>
<p style="direction: ltr;">0000000000000000: 4a<strong><span style="color: #ff0000;">02</span></strong>0000 004f0000 00010001 00602d00 †J&#8230;.O&#8230;&#8230;.`-.</p>
<p style="direction: ltr;">0000000000000010: 00010001 009e0100 000000†††††††††††††&#8230;&#8230;&#8230;..</p>
<p style="direction: ltr;">Slot 2, Offset 0&#215;7a, Length 13, DumpStyle BYTE</p>
<p style="direction: ltr;">Record Type = INDEX_RECORD Record Attributes = Record Size = 13</p>
<p style="direction: ltr;">Memory Dump @0&#215;00000000111EA07A</p>
<p style="direction: ltr;">0000000000000000: 06<strong><span style="color: #ff0000;">03</span></strong>0000 004f0000 00010002 00††††††††&#8230;..O&#8230;&#8230;.</p>
<p style="direction: ltr;">Slot 3, Offset 0&#215;87, Length 13, DumpStyle BYTE</p>
<p style="direction: ltr;">Record Type = INDEX_RECORD Record Attributes = Record Size = 13</p>
<p style="direction: ltr;">Memory Dump @0&#215;00000000111EA087</p>
<p style="direction: ltr;">0000000000000000: 06<strong><span style="color: #ff0000;">04</span></strong>0000 004f0000 00010003 00††††††††&#8230;..O&#8230;&#8230;.</p>
<p style="direction: ltr;">Slot 4, Offset 0xfd, Length 27, DumpStyle BYTE</p>
<p style="direction: ltr;"><span style="color: #ff0000;">Record Type = INDEX_RECORD Record Attributes = VERSIONING_INFO Record Size = 27</span></p>
<p style="direction: ltr;">Memory Dump @0&#215;00000000111EA0FD</p>
<p style="direction: ltr;">0000000000000000: 46<strong><span style="color: #ff0000;">05</span></strong>0000 004f0000 00010001 00000000 †F&#8230;.O&#8230;&#8230;&#8230;.</p>
<p style="direction: ltr;">0000000000000010: 00000000 009e0100 000000†††††††††††††&#8230;&#8230;&#8230;..</p>
<p style="direction: ltr;">Slot 5, Offset 0&#215;94, Length 13, DumpStyle BYTE</p>
<p style="direction: ltr;">Record Type = INDEX_RECORD Record Attributes = Record Size = 13</p>
<p style="direction: ltr;">Memory Dump @0&#215;00000000111EA094</p>
<p style="direction: ltr;">0000000000000000: 06<strong><span style="color: #ff0000;">05</span></strong>0000 004f0000 00010004 00††††††††&#8230;..O&#8230;&#8230;.</p>
<p style="direction: ltr;">Slot 6, Offset 0xa1, Length 13, DumpStyle BYTE</p>
<p style="direction: ltr;">Record Type = INDEX_RECORD Record Attributes = Record Size = 13</p>
<p style="direction: ltr;">Memory Dump @0&#215;00000000111EA0A1</p>
<p style="direction: ltr;">0000000000000000: 06<strong><span style="color: #ff0000;">06</span></strong>0000 004f0000 00010005 00††††††††&#8230;..O&#8230;&#8230;.</p>
<p style="direction: ltr;">First, we see that the value &quot;2" has been changed to &quot;5" and moved to its new position. We can also see that it now has the &quot;VERSIONING_INFO&quot; attribute, and it contains the additional 14 bytes for the pointer to the version store (27 bytes instead of 13). This is how SQL Server accesses the old value (2) when the snapshot transaction retrieves all the rows.</p>
<p style="direction: ltr;">But we can also see that there is a ghost index record where the old record was, with the value &quot;2". The ghost record always appears when an index record is deleted, but it is usually cleaned up by the ghost cleanup task. In our case, the ghost record won't be removed, because it also has the &quot;VERSIONING_INFO&quot; attribute, and there is an open snapshot transaction that requires that data. It points to the same entry in the version store. This record is used in order to retrieve the old value (2) when we try to retrieve only the rows with Col1 = 2. As soon as we commit or rollback the snapshot transaction, the ghost record will be cleaned up.</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/about-snapshots-and-ghosts/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>&#8235;AND vs. OR&#8236;</title>		<link>http://www.madeira.co.il/and-vs-or/</link>
		<comments>http://www.madeira.co.il/and-vs-or/#comments</comments>
		<pubDate>Sat, 24 Dec 2011 11:47:03 +0000</pubDate>
		<dc:creator>&#8235;גיא גלנצר&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[מקודמות]]></category>
		<category><![CDATA[Optimizer]]></category>
		<category><![CDATA[Performance Tuning]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[אינדקסים]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=2697</guid>
		<description><![CDATA[&#8235;Optimizing a query that contains OR operators is more challenging than doing the same for a query that uses the AND operator...&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p><img src='http://www.madeira.co.il/wp-content/plugins/simple-post-thumbnails/timthumb.php?src=/wp-content/thumbnails/2697.png&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<div style="direction: ltr; text-align: justify;"><span style="font-size: small;">Usually queries use the </span><strong>AND</strong><span style="font-size: small;"> operator between predicates. For example, a query that returns all the employees that are working more than 3 years in the company </span><strong>AND</strong><span style="font-size: small;"> have a yearly salary of less than $120,000. It is less common to see queries that use the </span><strong>OR</strong><span style="font-size: small;"> operator between predicates. </span></div>
<p><span style="font-size: small;"> </span></p>
<div style="direction: ltr;">But queries with <strong>OR</strong> do exist, and when I encounter such a query, it usually has a poor execution plan. Optimizing a query that contains <strong>OR</strong> operators is more challenging than doing the same for a query that uses the <strong>AND</strong> operator. There are all kinds of optimization techniques. In this post, I am going to write about creating the appropriate indexes for such queries.</div>
<p><span style="font-size: small;"> </span></p>
<p style="direction: ltr;"><span style="font-size: small;">Let's use an example. We'll create the &quot;Inventory.Products&quot; table and populate it with million rows of random data:</span></p>
<p><span style="font-size: small;"> </span></p>
<p style="direction: ltr;"><a href="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Code-Snippet-113.png" rel="wp-prettyPhoto[g2697]"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border-image: initial; border: 0px initial initial;" title="AND vs. OR - Code Snippet 1" src="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Code-Snippet-11_thumb2.png" border="0" alt="AND vs. OR - Code Snippet 11" width="420" height="293" /></a></p>
<p><span style="font-size: small;"> </span></p>
<p style="direction: ltr;"><span style="font-size: small;">Notice that this table has a clustered index on the &quot;Id&quot; column. Currently this is the only index we have. No non-clustered indexes at all.</span></p>
<p><span style="font-size: small;"> </span></p>
<p style="direction: ltr;"><span style="font-size: small;">Now, let's look at a query that uses the <strong>AND</strong> operator:</span></p>
<p><span style="font-size: small;"> </span></p>
<p style="direction: ltr;">
<p style="direction: ltr;"><a href="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Code-Snippet-24.png" rel="wp-prettyPhoto[g2697]"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="AND vs. OR - Code Snippet 2" src="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Code-Snippet-2_thumb4.png" border="0" alt="AND vs. OR - Code Snippet 2" width="420" height="186" /></a></p>
<p style="direction: ltr;">
<p style="direction: ltr;">If we run this query, we'll get a clustered index scan, as expected:</p>
<p style="direction: ltr;"><a href="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Execution-Plan-12.png" rel="wp-prettyPhoto[g2697]"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="AND vs. OR - Execution Plan 1" src="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Execution-Plan-1_thumb2.png" border="0" alt="AND vs. OR - Execution Plan 1" width="420" height="81" /></a></p>
<p style="direction: ltr;">
<p style="direction: ltr;">If you're using SQL Server 2008 or later, then you'll probably also see the optimizer's suggestion for a missing index. The optimizer suggests creating a non-clustered index on the &quot;Status&quot;, &quot;Weight&quot; and &quot;ListPrice&quot; columns, and also including the &quot;Id&quot; and &quot;Name&quot; columns. There are some problems with the optimizer's suggestion and with the missing indexes feature in general, but this is a subject for another post. For now, let's listen to the optimizer's suggestion and create the index:</p>
<p style="direction: ltr;"><a href="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Code-Snippet-33.png" rel="wp-prettyPhoto[g2697]"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="AND vs. OR - Code Snippet 3" src="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Code-Snippet-3_thumb3.png" border="0" alt="AND vs. OR - Code Snippet 3" width="420" height="187" /></a></p>
<p style="direction: ltr;">
<p style="direction: ltr;">Now the execution plan looks like this:</p>
<p style="direction: ltr;">
<p style="direction: ltr;"><a href="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Execution-Plan-22.png" rel="wp-prettyPhoto[g2697]"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="AND vs. OR - Execution Plan 2" src="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Execution-Plan-2_thumb2.png" border="0" alt="AND vs. OR - Execution Plan 2" width="420" height="136" /></a></p>
<div style="direction: ltr;">Yes, now we have an index seek, which is a much better plan. In my case, the number of logical reads dropped from 10,085 to 77. That's nice, isn't it? Notice that in your case the numbers might be different, because the data is random.</div>
<div style="direction: ltr;">In reality, we're not always interested in creating the optimal index for each query, because all those indexes consume a lot of space and maintenance overhead, and they also hurt performance of DML statements. So in many cases we will look for indexes that produce near-optimal results, but can be shared by many queries.</div>
<div style="direction: ltr;">When a query contains multiple predicates with the <strong>AND</strong> operator between them, there are many ways to come up with near-optimal indexes. Any single-column index on one of the columns filtered by the query can do the work.</div>
<div style="direction: ltr;">For example, let's create an index on the &quot;Status&quot; column:</div>
<p><span style="font-size: small;"> <span style="font-size: small;"> </span></span></p>
<p style="direction: ltr;"><span style="font-size: small;"><a href="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Code-Snippet-41.png" rel="wp-prettyPhoto[g2697]"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="AND vs. OR - Code Snippet 4" src="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Code-Snippet-4_thumb1.png" border="0" alt="AND vs. OR - Code Snippet 4" width="420" height="187" /></a></span></p>
<p><span style="font-size: small;"> </span></p>
<p style="direction: ltr;"><span style="font-size: small;">Here is the execution plan:</span></p>
<p><span style="font-size: small;"> </span></p>
<p style="direction: ltr;"><span style="font-size: small;"><a href="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Execution-Plan-13.png" rel="wp-prettyPhoto[g2697]"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="AND vs. OR - Execution Plan 1" src="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Execution-Plan-1_thumb3.png" border="0" alt="AND vs. OR - Execution Plan 1" width="420" height="81" /></a></span></p>
<div style="direction: ltr;">Oops! We've seen this plan already.</div>
<div style="direction: ltr;">Why didn't the optimizer choose to use the index on the &quot;Status&quot; column? Because it uses the index statistics on the &quot;Status&quot; column in order to estimate that the filter on the &quot;Status&quot; column alone will return 10% of the rows in the table (around 100,000 rows). Using the index also means performing 100,000 key lookups.</div>
<div style="direction: ltr;">Let's force the optimizer to use the index, and you'll see what I mean:</div>
<p style="direction: ltr;"><a href="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Code-Snippet-5.png" rel="wp-prettyPhoto[g2697]"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="AND vs. OR - Code Snippet 5" src="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Code-Snippet-5_thumb.png" border="0" alt="AND vs. OR - Code Snippet 5" width="420" height="187" /></a></p>
<p style="direction: ltr;">
<p style="direction: ltr;">Here is the execution plan:</p>
<p style="direction: ltr;">
<p style="direction: ltr;"><a href="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Execution-Plan-3.png" rel="wp-prettyPhoto[g2697]"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="AND vs. OR - Execution Plan 3" src="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Execution-Plan-3_thumb.png" border="0" alt="AND vs. OR - Execution Plan 3" width="420" height="116" /></a></p>
<div style="direction: ltr;">The number of logical reads in my case is 306,341, which is much worse than performing a clustered index scan (10,085 logical reads). Luckily, the optimizer is smart enough to realize that and ignore the index.</div>
<div style="direction: ltr;">One thing we can do is to add included columns to the index in order to cover the query and remove the need for the expensive key lookup operations:</div>
<p><span style="font-size: small;"> </span></p>
<p style="direction: ltr;"><span style="font-size: small;"><a href="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Code-Snippet-6.png" rel="wp-prettyPhoto[g2697]"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="AND vs. OR - Code Snippet 6" src="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Code-Snippet-6_thumb.png" border="0" alt="AND vs. OR - Code Snippet 6" width="420" height="269" /></a></span></p>
<p><span style="font-size: small;"> </span></p>
<div style="direction: ltr;"><span style="font-size: small;">Notice that I didn’t include the &quot;Id&quot; column in the index, because this is the clustered index key and it's already included in all the non-clustered indexes. </span></div>
<p><span style="font-size: small;"> </span></p>
<div style="direction: ltr;">So here is the plan now:</div>
<p><span style="font-size: small;"> </span></p>
<p style="direction: ltr;"><span style="font-size: small;"><a href="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Execution-Plan-4.png" rel="wp-prettyPhoto[g2697]"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="AND vs. OR - Execution Plan 4" src="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Execution-Plan-4_thumb.png" border="0" alt="AND vs. OR - Execution Plan 4" width="391" height="97" /></a></span></p>
<div style="direction: ltr;">So nice and simple, isn’t it? It uses now 735 logical reads. It's not the optimal 77 logical reads that we've already seen before by using the composite index, but it's still much better than the 10,085 logical reads when scanning the whole table.</div>
<div style="direction: ltr;">Another thing we can do is to create an index on a more selective column, for which the estimated number of rows returned will be significantly lower. For example, let's create an index on the &quot;ListPrice&quot; column:</div>
<p><span style="font-size: small;"> </span></p>
<p style="direction: ltr;"><span style="font-size: small;"><a href="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Code-Snippet-7.png" rel="wp-prettyPhoto[g2697]"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="AND vs. OR - Code Snippet 7" src="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Code-Snippet-7_thumb.png" border="0" alt="AND vs. OR - Code Snippet 7" width="420" height="187" /></a></span></p>
<p><span style="font-size: small;"> </span></p>
<p style="direction: ltr;"><span style="font-size: small;">Now the plan looks like that:</span></p>
<p><span style="font-size: small;"> </span></p>
<p style="direction: ltr;"><span style="font-size: small;"><a href="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Execution-Plan-5.png" rel="wp-prettyPhoto[g2697]"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="AND vs. OR - Execution Plan 5" src="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Execution-Plan-5_thumb.png" border="0" alt="AND vs. OR - Execution Plan 5" width="420" height="150" /></a></span></p>
<div style="direction: ltr;">This is not a covering index, and yet the optimizer chose to use the index and perform the key lookup operations. This is because the optimizer estimated that there are 1,976.27 rows with a list price less than $2.00, and in this case the cost of performing 1,976.27 key lookups is cheaper than performing a clustered index scan. In my case, the number of logical reads is 6,073, which is far from the optimal solution (77 logical reads), but it’s still better than a clustered index scan (10,085 logical reads).</div>
<div style="direction: ltr;">We can also create composite indexes on subsets of the columns filtered in the query. For example, we can create a composite index on the &quot;ListPrice&quot; column and &quot;Weight&quot; column. There are many combinations we can choose from, but let's stop here. You got the idea.</div>
<p style="direction: ltr;">Let's start all over again, this time with a query that uses the <strong>OR</strong> operator:</p>
<p style="direction: ltr;"><a href="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Code-Snippet-8.png" rel="wp-prettyPhoto[g2697]"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="AND vs. OR - Code Snippet 8" src="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Code-Snippet-8_thumb.png" border="0" alt="AND vs. OR - Code Snippet 8" width="420" height="187" /></a></p>
<p style="direction: ltr;">
<p style="direction: ltr;">We already have the index on the &quot;ListPrice&quot; column, but if we run this query, we get the following execution plan:</p>
<p style="direction: ltr;">
<p style="direction: ltr;"><a href="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Execution-Plan-6.png" rel="wp-prettyPhoto[g2697]"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="AND vs. OR - Execution Plan 6" src="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Execution-Plan-6_thumb.png" border="0" alt="AND vs. OR - Execution Plan 6" width="420" height="81" /></a></p>
<div style="direction: ltr;">The optimizer didn't use the non-clustered index, although we saw earlier that it was efficient as far as the &quot;ListPrice&quot; column in concerned. The problem is that in this case SQL Server has to return much more than the estimated 1,976.27 rows that match the criteria on the &quot;ListPrice&quot; column. In fact, this query returns 191,527 rows, compared to 15 rows returned by the query that uses the <strong>AND</strong> operator.</div>
<div style="direction: ltr;">Creating the composite index, which was the optimal index for the query with the <strong>AND</strong> operator is  also not going to work in this case, because what we need here is to retrieve all the rows that match each of the predicates separately and then perform a union. The composite index can only give us the intersection of the rows, but not the union.</div>
<div style="direction: ltr;">Instead, we can create a single-column index on each of the columns that is filtered in the query (&quot;Status&quot;, &quot;Weight&quot; and &quot;ListPrice&quot;). This way, the optimizer will be able to use each index separately and then combine the results with a union operation.</div>
<div style="direction: ltr;">Let's try that. We already have an index on the &quot;ListPrice&quot; column. Let's create another index on the &quot;Weight&quot; column and see what happens:</div>
<p style="direction: ltr;"><a href="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Code-Snippet-9.png" rel="wp-prettyPhoto[g2697]"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="AND vs. OR - Code Snippet 9" src="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Code-Snippet-9_thumb.png" border="0" alt="AND vs. OR - Code Snippet 9" width="420" height="91" /></a></p>
<p style="direction: ltr;">
<p style="direction: ltr;">The execution plan is still the same:</p>
<p style="direction: ltr;">
<p style="direction: ltr;"><a href="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Execution-Plan-61.png" rel="wp-prettyPhoto[g2697]"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="AND vs. OR - Execution Plan 6" src="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Execution-Plan-6_thumb1.png" border="0" alt="AND vs. OR - Execution Plan 6" width="420" height="81" /></a></p>
<div style="direction: ltr;">The optimizer didn't use the indexes, because it still needs to find all the rows that match the criteria on the &quot;Status&quot; column. We saw earlier that there are around 100,000 such rows, so it still needs to perform a clustered index scan in order to retrieve these rows. Since there is already a clustered index scan, the optimizer can simply grab all the rows it needs in that single scan, and the two non-clustered indexes become useless.</div>
<div style="direction: ltr;">Only when we have an index for each predicate in the query, the optimizer can use each index separately without the need for a clustered index scan at all.</div>
<div style="direction: ltr;">So let's create the missing index on the &quot;Status&quot; column:</div>
<p style="direction: ltr;"><a href="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Code-Snippet-10.png" rel="wp-prettyPhoto[g2697]"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="AND vs. OR - Code Snippet 10" src="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Code-Snippet-10_thumb.png" border="0" alt="AND vs. OR - Code Snippet 10" width="420" height="90" /></a></p>
<p style="direction: ltr;">
<p style="direction: ltr;">And now look at the execution plan:</p>
<p style="direction: ltr;">
<p style="direction: ltr;"><a href="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Execution-Plan-62.png" rel="wp-prettyPhoto[g2697]"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="AND vs. OR - Execution Plan 6" src="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Execution-Plan-6_thumb2.png" border="0" alt="AND vs. OR - Execution Plan 6" width="420" height="81" /></a></p>
<div style="direction: ltr;">Oh, no! What now?</div>
<div style="direction: ltr;">This time the optimizer didn’t use the indexes, because it still needs to retrieve the &quot;Name&quot; column for each one of the 191,527 rows. This will require a very expensive key lookup operation, so a clustered index scan is still the best option in this case.</div>
<div style="direction: ltr;">What we can do is add the &quot;Name&quot; column as an included column to each one of the 3 indexes:</div>
<p><span style="font-size: small;"> </span></p>
<p style="direction: ltr;"><span style="font-size: small;"><a href="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Code-Snippet-114.png" rel="wp-prettyPhoto[g2697]"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="AND vs. OR - Code Snippet 11" src="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Code-Snippet-11_thumb3.png" border="0" alt="AND vs. OR - Code Snippet 11" width="420" height="652" /></a></span></p>
<p><span style="font-size: small;"> </span></p>
<div style="direction: ltr;"><span style="font-size: small;">Notice that we don't need to make each index a covering index for the query. For example, we don't need to add the &quot;Status&quot; column as an included column in the index on the &quot;Weight&quot; column. This is because each index is used only to retrieve the rows that match the criteria on the index key. We only need to include the columns in the </span><span style="font-size: small; font-family: 'Courier New'; color: #0000ff;">SELECT</span><span style="font-size: small;"> clause. </span></div>
<p><span style="font-size: small;"> </span></p>
<div style="direction: ltr;">Here is the new execution plan:</div>
<p><span style="font-size: small;"> </span></p>
<p style="direction: ltr;"><span style="font-size: small;"><a href="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Execution-Plan-7.png" rel="wp-prettyPhoto[g2697]"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="AND vs. OR - Execution Plan 7" src="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR-Execution-Plan-7_thumb.png" border="0" alt="AND vs. OR - Execution Plan 7" width="420" height="135" /></a></span></p>
<p style="direction: ltr;">Look how the optimizer performs 3 different index seek operations, one for each index, and then combines the results using the &quot;Concatenation&quot; operator. It then performs an aggregate operation in order to remove duplicate rows. The number of logical reads is 1,213, which is around 12% of the 10,085 logical reads when scanning the whole table. This is very good, taking into account that the query returns 191,527 rows, which is around 19% of the million rows in the whole table.</p>
<div style="direction: ltr;">So, let's sum up.</div>
<div style="direction: ltr;">When the query uses the <strong>AND</strong> operator between predicates, there are a lot of combinations of indexes that you can create in order to satisfy the query. The optimal index would be a composite index on all the columns participating in the <span style="font-family: 'Courier New'; color: #0000ff;">WHERE</span> clause with all the rest of the columns in the query as included columns. But you might want to use a near-optimal index in order to reduce the overhead of your indexes.</div>
<div style="direction: ltr;">The case of the <strong>OR</strong> operator is different. The only way to satisfy the query is to have a separate covering index for each column participating in the <span style="font-family: 'Courier New'; color: #0000ff;">WHERE</span> clause. If even a single index is missing or is not efficient enough (for example, because it doesn’t cover all the relevant columns), there is no point in using the other indexes, and you'll get a table scan (or a clustered index scan) instead.</div>
<div style="direction: ltr;">But when you have an efficient index for each of the filtered columns, you'll get a very efficient execution plan that uses each index separately and then concatenates and aggregates the results in order to perform a union of the rows.</div>
<p style="direction: ltr; text-align: justify;">Here is the full script that I used in this post:</p>
<p style="direction: ltr; text-align: justify;"><a href="http://www.madeira.co.il/wp-content/uploads/2011/12/AND-vs.-OR.zip"><img class="alignleft size-full wp-image-2450" title="AND vs. OR" src="http://www.madeira.co.il/wp-content/uploads/2011/11/1322212224_application-x-sqlite2.png" alt="" width="128" height="128" /></a></p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/and-vs-or/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>&#8235;Monitoring Page Splits&#8236;</title>		<link>http://www.madeira.co.il/monitoring-page-splits/</link>
		<comments>http://www.madeira.co.il/monitoring-page-splits/#comments</comments>
		<pubDate>Fri, 16 Dec 2011 18:46:42 +0000</pubDate>
		<dc:creator>&#8235;גיא גלנצר&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[מקודמות]]></category>
		<category><![CDATA[Extended Events]]></category>
		<category><![CDATA[Monitoring]]></category>
		<category><![CDATA[Page Splits]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=2643</guid>
		<description><![CDATA[&#8235;קבלו את הפתרון האולטימטיבי לניטור Page Splits...&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p><img src='http://www.madeira.co.il/wp-content/plugins/simple-post-thumbnails/timthumb.php?src=/wp-content/thumbnails/2643.png&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p style="direction: ltr;"><span style="font-size: small;">3 months ago I presented a session in the </span><a href="http://www.madeira.co.il/rosh-hashana-user-group-meeting-summary-and-downloads/"><span style="color: #0000ff; font-size: small;">special Rosh HaShana user group meeting</span></a><span style="font-size: small;">. The session was about Monitoring Page Splits. I talked about the difference between a &quot;real&quot; page split (mid-page split) and a page allocation (end-page split). I demonstrated that all the tools available by SQL Server to monitor page splits actually monitor both types (mid-page and end-page), and there is no way to monitor only &quot;real&quot; page splits, which are a common concern for DBAs.</span></p>
<div style="direction: ltr;"><span style="font-size: small;">I then showed that there is one undocumented and unsupported way to achieve this goal, which is by looking at the transaction log using the &quot;sys.fn_dblog&quot; function and tracking the </span></div>
<p><span style="font-size: small;"> </span></p>
<div style="direction: ltr;">&quot;LOP_DELETE_SPLIT&quot; operation. This is the only indication in SQL Server for mid-page splits. I also showed that the same is true for SQL Server 2012.</div>
<p style="direction: ltr;"><span style="font-size: small;">After the presentation, </span><a href="http://sql-server-tuning.com/"><span style="color: #0000ff; font-size: small;">Ami Levin</span></a><span style="font-size: small;"> posted a question about it in the MVP forum and connected between me and </span><a href="http://sqlskills.com/blogs/jonathan/"><span style="color: #0000ff; font-size: small;">Jonathan Kehayias</span></a><span style="font-size: small;">. Jonathan suggested a much better solution, which is still based on the &quot;LOP_DELETE_SPLIT&quot; operation in the transaction log. But instead of searching for it in the log using &quot;sys.fn_dblog&quot;, he suggested to monitor it with Extended Events using the &quot;sqlserver.transaction_log&quot; event, which is a new event in SQL Server 2012.</span></p>
<p style="direction: ltr;"><span style="font-size: small;">There are several advantages to this approach over the &quot;sys.fn_dblog&quot; approach:</span></p>
<p style="direction: ltr;"><span style="font-size: small;">1. The &quot;sqlserver.transaction_log&quot; event is fired as the log record is created, and the relevant events are put in memory. This is much more efficient than reading the transaction log from the log file.</span></p>
<p style="direction: ltr;"><span style="font-size: small;">2. With Extended Events, you can control the timeframe in which you would like to monitor page splits by starting and stopping the event session. With &quot;sys.fn_dblog&quot;, the amount of log records you'll read is determined by the last time the log was truncated (for example, by a CHECKPOINT operation or by a log backup operation).</span></p>
<p style="direction: ltr;"><span style="font-size: small;">3. The Extended Events infrastructure lets you correlate between transaction log records and the operations that caused these records to be created. This is a very powerful tool, and I plan to write about it more in the near future.</span></p>
<p style="direction: ltr;"><span style="font-size: small;">4. While the content of the transaction log still remains undocumented, Extended Events is a well-documented and supported mechanism. The &quot;sys.fn_dblog&quot; function isn't.</span></p>
<p style="direction: ltr;"><span style="font-size: small;">Jonathan has just written a great post describing his solution. You can read it </span><a href="http://sqlskills.com/blogs/jonathan/post/Tracking-Problematic-Pages-Splits-in-SQL-Server-2012-Extended-Events-e28093-No-Really-This-Time%21.aspx"><span style="color: #0000ff; font-size: small;">here</span></a><span style="font-size: small;">.</span></p>
<p style="direction: ltr;"><span style="font-size: small;">I would like to thank Jonathan for sharing his solution with me (and with everyone, actually), and I would like to thank Ami for making the connection.</span></p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/monitoring-page-splits/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>&#8235;All You Can Print&#8236;</title>		<link>http://www.madeira.co.il/all-you-can-print/</link>
		<comments>http://www.madeira.co.il/all-you-can-print/#comments</comments>
		<pubDate>Sat, 10 Dec 2011 20:20:58 +0000</pubDate>
		<dc:creator>&#8235;גיא גלנצר&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[מקודמות]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=2632</guid>
		<description><![CDATA[&#8235;מנסה להדפיס טקסט ארוך, אבל הפקודה PRINT חותכת לך אותו באמצע? הנה הפתרון...&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p><img src='http://www.madeira.co.il/wp-content/plugins/simple-post-thumbnails/timthumb.php?src=/wp-content/thumbnails/2632.jpg&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p style="direction: ltr;"><span style="font-size: small;">In my previous post, </span><a title="Concatenating Strings" href="http://www.madeira.co.il/concatenating-strings-2/" target="_blank"><span style="font-size: small;"><span style="color: #0000ff;">Concatenating Strings</span></span></a><span style="font-size: small;">, I promised to post my own version of the <span style="font-family: 'Courier New'; color: #0000ff;">PRINT</span> statement. It's a stored procedure called &quot;PrintMax&quot;. The reason I use this stored procedure is because the <span style="font-family: 'Courier New'; color: #0000ff;">PRINT</span> statement can't handle a string with more than 4,000 bytes.</span></p>
<p style="direction: ltr;"><span style="font-size: small;">Here is a quote from </span><a title="PRINT" href="http://msdn.microsoft.com/en-us/library/ms176047.aspx" target="_blank"><span style="font-size: small;"><span style="color: #0000ff;">Books Online</span></span></a><span style="font-size: small;">:</span></p>
<blockquote>
<p style="direction: ltr;"><em><span style="font-size: small;">&quot;A message string can be up to 8,000 characters long if it is a non-Unicode string, and 4,000 characters long if it is a Unicode string. Longer strings are truncated. The varchar(max) and nvarchar(max) data types are truncated to data types that are no larger than varchar(8000) and nvarchar(4000).&quot;</span></em></p>
</blockquote>
<p><span style="font-size: small;"> </span></p>
<p style="direction: ltr;"><span style="font-size: small;">Sometimes, you want to print a long string. For example, you might want to print the definition of a long stored procedure. Or you might have a very long dynamic batch that you are going to execute, but you want to print it first for debug purposes.</span></p>
<p style="direction: ltr;"><span style="font-size: small;">The problem with the <span style="font-family: 'Courier New'; color: #0000ff;">PRINT</span> statement is not only that it prints up to the first 4,000 bytes. It also truncates your text without even generating a warning.</span></p>
<p style="direction: ltr;"><span style="font-size: small;">So I use my version of the <span style="font-family: 'Courier New'; color: #0000ff;">PRINT</span> statement – the &quot;PrintMax&quot; stored procedure – which does the following:</span></p>
<p style="direction: ltr;"><span style="font-size: small;">1. It accepts a <span style="font-family: 'Courier New';"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #666666;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #666666;">)</span></span> input parameter. So any text value you pass (Unicode or non-Unicode, fixed length or variable length) is converted to <span style="font-family: 'Courier New';"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #666666;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #666666;">)</span></span>.</span></p>
<p style="direction: ltr;"><span style="font-size: small;">2. It splits the large text into chunks of up to 4,000 characters.</span></p>
<p style="direction: ltr;"><span style="font-size: small;">3. It searches for the last line break within each chunk and splits the string there. This way, it doesn’t just cut sentences in the middle. It uses existing line breaks in the text in order to perform the split. The output is much more readable.</span></p>
<p style="direction: ltr;"><span style="font-size: small;">You can download the &quot;PrintMax&quot; stored procedure from <a title="PrintMax" href="http://www.madeira.co.il/printmax-10/" target="_blank"><span style="color: #0000ff;">here</span></a>.</span></p>
<p><span style="font-size: small;"> </span></p>
<p style="direction: ltr;"><span style="font-size: small;">Now, let's try it…</span></p>
<p style="direction: ltr;"><span style="font-size: small;">First, let's try to print a long string of X's with both methods – the regular <span style="font-family: 'Courier New'; color: #0000ff;">PRINT</span> statement and the &quot;PrintMax&quot; stored procedure:</span></p>
<blockquote>
<p style="direction: ltr;"><span style="font-family: 'Courier New'; font-size: small;"><span style="color: #0000ff;">DECLARE</span> @nvcLargeText <span style="color: #0000ff;">AS</span> <span style="font-family: 'Courier New';"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #666666;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #666666;">)</span></span><span style="color: #666666;">;</span></span></p>
<p style="direction: ltr;"><span style="font-family: 'Courier New'; font-size: small;"><span style="color: #0000ff;">SET</span> @nvcLargeText <span style="color: #666666;">=</span> <span style="color: #ff00ff;">REPLICATE</span> <span style="color: #666666;">(</span><span style="color: #ff00ff;">CAST</span> <span style="color: #666666;">(</span><span style="color: #ff0000;">N'X'</span> <span style="color: #0000ff;">AS</span> <span style="font-family: 'Courier New';"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #666666;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #666666;">)</span></span><span style="color: #666666;">) ,</span> 5000<span style="color: #666666;">);</span></span></p>
<p style="direction: ltr;"><span style="font-family: 'Courier New'; font-size: small;"><span style="color: #0000ff;">SELECT</span> LargeTextLength <span style="color: #666666;">=</span> <span style="color: #ff00ff;">LEN</span> <span style="color: #666666;">(</span>@nvcLargeText<span style="color: #666666;">);</span></span></p>
<p style="direction: ltr;"><span style="font-family: 'Courier New'; font-size: small;"><span style="color: #0000ff;">PRINT</span> @nvcLargeText<span style="color: #666666;">;</span></span></p>
<p style="direction: ltr;"><span style="font-family: 'Courier New'; font-size: small;"><span style="color: #0000ff;">EXECUTE</span> dbo.PrintMax @inLargeText <span style="color: #666666;">=</span> @nvcLargeText<span style="color: #666666;">;</span></span></p>
</blockquote>
<p style="direction: ltr;"><span style="font-size: small;">As you can see, the <span style="font-family: 'Courier New'; font-size: small;"><span style="color: #0000ff;">PRINT</span> </span>statement truncates the text at 4,000 characters. The &quot;PrintMax&quot; stored procedures splits the text into two parts. It prints the first 4,000 characters in the first line, and the additional 1,000 characters in the second line.</span></p>
<p><span style="font-size: small;"> </span></p>
<p style="direction: ltr;"><span style="font-size: small;">Now, let's try to print the largest SQL module in your database:</span></p>
<blockquote>
<p style="direction: ltr;"><span style="font-size: small;"><span style="font-family: 'Courier New';"><span style="color: #0000ff;">DECLARE</span> @nvcLargeText <span style="color: #0000ff;">AS</span> <span style="color: #0000ff;">NVARCHAR</span><span style="color: #666666;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #666666;">)</span><span style="color: #666666;">;</span></span></span></p>
<p style="direction: ltr;"><span style="font-size: small;"><span style="font-family: 'Courier New';"><span style="color: #0000ff;">SELECT</span> <span style="color: #0000ff;">TOP</span> <span style="color: #666666;">(</span>1<span style="color: #666666;">)</span> @nvcLargeText <span style="color: #666666;">=</span> <span style="color: #0000ff;">definition</span></span></span></p>
<p style="direction: ltr;"><span style="font-size: small;"><span style="font-family: 'Courier New';"><span style="color: #0000ff;">FROM</span> <span style="color: #008000;">sys.sql_modules</span></span></span></p>
<p style="direction: ltr;"><span style="font-size: small;"><span style="font-family: 'Courier New';"><span style="color: #0000ff;">ORDER BY</span> <span style="color: #ff00ff;">LEN</span> <span style="color: #666666;">(</span><span style="color: #0000ff;">definition</span><span style="color: #666666;">)</span> <span style="color: #0000ff;">DESC</span><span style="color: #666666;">;</span></span></span></p>
<p style="direction: ltr;"><span style="font-family: 'Courier New';"><span style="font-size: small;"><span style="color: #0000ff;">SELECT</span> LargeTextLength <span style="color: #666666;">=</span> <span style="color: #ff00ff;">LEN</span> <span style="color: #666666;">(</span>@nvcLargeText<span style="color: #666666;">);</span></span> </span></p>
<p style="direction: ltr;"><span style="font-family: 'Courier New';"><span style="font-size: small;"><span style="color: #0000ff;">PRINT</span> @nvcLargeText<span style="color: #666666;">;</span></span> </span></p>
<p style="direction: ltr;"><span style="font-size: small;"><span style="font-family: 'Courier New';"><span style="color: #0000ff;">EXECUTE</span> dbo.PrintMax @inLargeText <span style="color: #666666;">=</span> @nvcLargeText<span style="color: #666666;">;</span></span></span></p>
</blockquote>
<p style="direction: ltr;"><span style="font-size: small;">See how the <span style="font-family: 'Courier New';"><span style="font-size: small;"><span style="color: #0000ff;">PRINT</span> </span></span>statement truncates the code in the middle without even warning you about it? And did you see how the &quot;PrintMax&quot; stored procedure prints the whole code nicely?</span></p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/all-you-can-print/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8235;PrintMax&#8236;</title>		<link>http://www.madeira.co.il/printmax-10/</link>
		<comments>http://www.madeira.co.il/printmax-10/#comments</comments>
		<pubDate>Sat, 10 Dec 2011 19:25:57 +0000</pubDate>
		<dc:creator>&#8235;גיא גלנצר&#8236;</dc:creator>				<category><![CDATA[אל תציג בבלוגים]]></category>
		<category><![CDATA[הורדות]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=2610</guid>
		<description><![CDATA[&#8235;
This script creates the &#34;PrintMax&#34; stored procedure, which prints a text of any size. It's <a href="http://www.madeira.co.il/printmax-10/"><strong>לסיפור המלא...</strong></a>&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p><img src='http://www.madeira.co.il/wp-content/plugins/simple-post-thumbnails/timthumb.php?src=/wp-content/thumbnails/2610.png&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p style="direction: ltr;"><span style="font-size: small;">This script creates the &quot;PrintMax&quot; stored procedure, which prints a text of any size. It's intended to replace the <span style="font-family: 'Courier New'; color: #0000ff;">PRINT</span> statement, which is limited to a text size of up to 4,000 bytes.</span></p>
<p style="direction: ltr;"><span style="font-size: small;">The &quot;PrintMax&quot; stored procedure does the following:</span></p>
<p style="direction: ltr;"><span style="font-size: small;">1. It accepts a <span style="font-family: 'Courier New';"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #666666;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #666666;">)</span></span> input parameter. So any text value you pass (Unicode or non-Unicode, fixed length or variable length) is converted to <span style="font-family: 'Courier New';"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #666666;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #666666;">)</span></span>.</span></p>
<p style="direction: ltr;"><span style="font-size: small;">2. It splits the large text into chunks of up to 4,000 characters.</span></p>
<p style="direction: ltr;"><span style="font-size: small;">3. It searches for the last line break within each chunk and splits the string there. This way, it doesn’t just cut sentences in the middle. It uses existing line breaks in the text in order to perform the split. The output is much more readable.</span></p>
<p style="direction: ltr;"><a style="font-size: small;" href="http://www.madeira.co.il/wp-content/uploads/2011/12/PrintMax1.zip"><img class="alignleft size-full wp-image-2450" title="PrintMax.zip" src="http://www.madeira.co.il/wp-content/uploads/2011/11/1322212224_application-x-sqlite2.png" alt="" width="128" height="128" /></a></p>
<p style="direction: ltr;">For more information about this stored procedure: <a title="All You Can Print" href="http://www.madeira.co.il/all-you-can-print/" target="_blank"><span style="color: #0000ff;">http://www.madeira.co.il/all-you-can-print/</span></a>.</p>
<p><span style="font-size: small;"> </span></p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/printmax-10/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>&#8235;Concatenating Strings&#8236;</title>		<link>http://www.madeira.co.il/concatenating-strings-2/</link>
		<comments>http://www.madeira.co.il/concatenating-strings-2/#comments</comments>
		<pubDate>Wed, 30 Nov 2011 19:29:07 +0000</pubDate>
		<dc:creator>&#8235;גיא גלנצר&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[מקודמות]]></category>
		<category><![CDATA[Data Types]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=2485</guid>
		<description><![CDATA[&#8235;כמה תגליות מעניינות לגבי שרשור מחרוזות ו-(NVARCHAR(MAX...&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p><img src='http://www.madeira.co.il/wp-content/plugins/simple-post-thumbnails/timthumb.php?src=/wp-content/thumbnails/2485.png&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p dir="ltr"><span style="font-size: small;">Yesterday I needed to run a long batch inside a string using </span><a href="http://msdn.microsoft.com/en-us/library/ms188001.aspx"><span style="color: #0000ff; font-size: small;">sys.sp_executesql</span></a><span style="font-size: small;">. So I declared a variable with the <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #a5a5a5;">)</span></span> data type, assigned the text of the batch into it, and tried to execute it. When I assigned the text into the variable, I used concatenation of a few strings. Something like that:</span></p>
<p dir="ltr"><span style="font-family: Courier New;"><span style="color: #0000ff; font-size: small;"> </span></span></p>
<p dir="ltr"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: #0000ff;">DECLARE</span> @Statement <span style="color: #0000ff;">AS NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #a5a5a5;">)</span><span style="color: #a5a5a5;">;</span></span></span></p>
<p dir="ltr"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: #0000ff;">SET</span> @Statement <span style="color: #a5a5a5;">=</span> <span style="color: #ff0000;">N'Long String&#8230;'</span> <span style="color: #a5a5a5;">+</span> @Variable <span style="color: #a5a5a5;">+</span> <span style="color: #ff0000;">N'Another Long String&#8230;'</span> <span style="color: #a5a5a5;">+ &#8230;</span></span></span></p>
<p dir="ltr"><span style="font-size: small;"> </span></p>
<p dir="ltr"><span style="font-size: small;">I have probably done that a million times before. But this time I received an error. The error resulted from the fact that my string was truncated. The length of my string should have been more than 10,000 bytes. I checked the length of the <span style="font-family: Courier New;">@Statement</span> variable using the <span style="color: #ff00ff; font-family: Courier New;">DATALENGTH</span> function, and it turned out to be only 8,000 bytes.</span></p>
<p dir="ltr"><span style="font-size: small;">I guess it's the first time that I'm trying to assign such a long string to a <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #a5a5a5;">)</span></span> variable. At first, I couldn't figure it out. What's the whole point of the <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #a5a5a5;">)</span></span> data type, if such a variable cannot contain more than 8,000 bytes? It doesn't make sense…</span></p>
<p dir="ltr"><span style="font-size: small;">I thought that maybe there was something about my text that caused this strange behavior, so I decided to try something much simpler:</span></p>
<p dir="ltr"><span style="font-family: Courier New; font-size: small;"> </span></p>
<p dir="ltr"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: #0000ff;">DECLARE</span> @String <span style="color: #0000ff;">AS</span> <span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #a5a5a5;">)</span>;</span></span></p>
<p dir="ltr"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: #0000ff;">SET</span> @String <span style="color: #a5a5a5;">=</span> <span style="color: #ff00ff;">REPLICATE</span> <span style="color: #a5a5a5;">(</span><span style="color: #ff0000;">N'X'</span> <span style="color: #a5a5a5;">,</span> 5000<span style="color: #a5a5a5;">);</span></span></span></p>
<p dir="ltr"><span style="font-family: Courier New;"><span style="font-size: small;"><span style="color: #0000ff;">SELECT</span> <span style="color: #ff00ff;">DATALENGTH</span> <span style="color: #a5a5a5;">(</span>@String<span style="color: #a5a5a5;">);</span></span></span></p>
<p dir="ltr"><span style="font-size: small;"> </span></p>
<p dir="ltr"><span style="font-size: small;">I expected the length to be 10,000 bytes, but it was 8,000 bytes again. At this point I started to get nervous. It says &quot;MAX&quot;, doesn't it?</span></p>
<p dir="ltr"><span style="font-size: small;">I checked the value of <span style="color: #ff00ff; font-family: Courier New;">@@TEXTSIZE</span>, but it was set to the maximum value (2147483647). I played with it for a while, and after some (wasted) time, I finally understood what's going on. So here it is…</span></p>
<p dir="ltr"><span style="font-size: small;"> </span></p>
<p dir="ltr"><span style="font-size: small;">Every expression in SQL Server has a data type. This includes a column in a table, a variable, a returned value from a scalar function, and even a literal, such as the expression <span style="color: #ff0000; font-family: Courier New;">N'ABCD'</span>. In the case of the first 3 types of expressions, the data type is well defined. But in the case of a literal, it's a little less clear what the data type should be. SQL Server has its own rules as of what data type to assign to a literal. So what's the data type of <span style="color: #ff0000; font-family: Courier New;">N'ABCD'</span>?</span></p>
<p dir="ltr"><span style="font-size: small;">I can think of several options that would make sense, but according to SQL Server's rules, the data type of <span style="color: #ff0000; font-family: Courier New;">N'ABCD'</span> is <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #000000;">4</span><span style="color: #a5a5a5;">)</span></span>. As long as the number of characters (n) in the string literal is less or equal to 4,000, the data type would be <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #000000;">n</span><span style="color: #a5a5a5;">)</span></span>. If the number of characters is greater than 4,000, the data type would be <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #a5a5a5;">)</span></span>. So far it makes sense, doesn't it?</span></p>
<p dir="ltr"><span style="font-size: small;">Now, let's talk about concatenation. What do you think would be the data type of the following expression?</span></p>
<p dir="ltr"><span style="color: #ff0000; font-family: Courier New; font-size: small;">N'ABCD' <span style="color: #a5a5a5;">+</span> N'EFG'</span></p>
<p dir="ltr"><span style="font-size: small;"> </span></p>
<p dir="ltr"><span style="font-size: small;">Correct! It's <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #000000;">7</span><span style="color: #a5a5a5;">)</span></span>. Everything is making sense. Isn't it great?</span></p>
<p dir="ltr"><span style="font-size: small;">Now, let's complicate things just a little bit. Suppose you have an expression that is a concatenation of two string literals – one of them contains 3,000 characters and the other contains 2,000 characters. Something like that:</span></p>
<p dir="ltr"><a href="http://www.madeira.co.il/wp-content/uploads/2011/11/image1.png" rel="wp-prettyPhoto[g2485]"><img title="image" alt="image" width="240" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" src="http://www.madeira.co.il/wp-content/uploads/2011/11/image_thumb1.png" border="0" height="80" /></a></p>
<p dir="ltr"><span style="font-size: small;"> </span></p>
<p dir="ltr"><span style="font-size: small;">What would be the data type in this case? Well, it can't be <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #000000;">5000</span><span style="color: #a5a5a5;">)</span></span>, so it should be <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #a5a5a5;">)</span></span>, right? Wrong! The data type of this expression is <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #000000;">4000</span><span style="color: #a5a5a5;">)</span></span>, and the last 1,000 characters simply get truncated. So if you assign this expression to a <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #000000;">4000</span><span style="color: #a5a5a5;">)</span></span> variable, the value of the variable would be 3,000 X's and 1,000 Y's. No errors, no warnings.</span></p>
<p dir="ltr"><span style="font-size: small;">If you assign it to a <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #a5a5a5;">)</span></span> variable, then the following things will happen. First, SQL Server will perform the concatenation and assign the data type <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #000000;">4000</span><span style="color: #a5a5a5;">)</span></span> to the expression (and truncate the last 1,000 characters). Then, SQL Server will implicitly convert the <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #000000;">4000</span><span style="color: #a5a5a5;">)</span></span> expression to <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #a5a5a5;">)</span></span> and assign it to the variable. So the type of the variable would be <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #a5a5a5;">)</span></span>, but its value would still be 3,000 X's and 1,000 Y's.</span></p>
<p dir="ltr"><span style="font-size: small;">This explains the behavior that I originally experienced. Since I concatenated a few long strings, the result was truncated to accommodate 4,000 characters and then assigned to my <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #a5a5a5;">)</span></span> variable. That's great, isn't it?</span></p>
<p dir="ltr"><span style="font-size: small;"> </span></p>
<p dir="ltr"><span style="font-size: small;">So how do we solve this problem? If we concatenate a <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #a5a5a5;">)</span></span> expression with any other string expression, the result would be <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #a5a5a5;">)</span></span>. It doesn't matter how many expressions we want to concatenate. As long as at least one of them would be <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #a5a5a5;">)</span></span>, the result would be <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #a5a5a5;">)</span></span> as well. So all we need to do is to explicitly convert one of the literals to <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #a5a5a5;">)</span></span>, like this:</span></p>
<p dir="ltr"><a href="http://www.madeira.co.il/wp-content/uploads/2011/11/image3.png" rel="wp-prettyPhoto[g2485]"><img title="image" alt="image" width="428" style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" src="http://www.madeira.co.il/wp-content/uploads/2011/11/image_thumb3.png" border="0" height="78" /></a></p>
<p dir="ltr"><span style="font-size: small;"> </span></p>
<p dir="ltr"><span style="font-size: small;">The data type of this expression is <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #a5a5a5;">)</span></span>, and its length is 5,000 characters, as expected. Not so pretty, but it works.</span></p>
<p dir="ltr"><span style="font-size: small;">Remember the <span style="font-family: Courier New;"><span style="color: #ff00ff;">REPLICATE</span> <span style="color: #a5a5a5;">(</span><span style="color: #ff0000;">N'X'</span> <span style="color: #a5a5a5;">,</span> 5000<span style="color: #a5a5a5;">)</span></span> example? Why is the data type of this expression <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #000000;">4000</span><span style="color: #a5a5a5;">)</span></span> and not <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #a5a5a5;">)</span></span>? Well, the answer is simple – The <span style="font-family: Courier New;"><span style="color: #ff00ff;">REPLICATE</span> </span>function simply concatenates the same expression the requested number of times. It's just like the following expression:</span></p>
<p dir="ltr"><span style="color: #ff0000; font-family: Courier New; font-size: small;">N'X' <span style="color: #a5a5a5;">+</span> N'X' <span style="color: #a5a5a5;">+</span> N'X' <span style="color: #a5a5a5;">+ &#8230;</span></span></p>
<p dir="ltr"><span style="font-size: small;"> </span></p>
<p dir="ltr"><span style="font-size: small;">And we already know what happens here…</span></p>
<p dir="ltr"><span style="font-size: small;"> </span></p>
<p dir="ltr"><span style="font-size: small;">By the way, if you try to print a <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #a5a5a5;">)</span></span> expression using the <span style="color: #0000ff; font-family: Courier New;">PRINT</span> statement, it will also truncate the value to 4,000 characters, even if the expression is a true <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #a5a5a5;">)</span></span> with more than 4,000 characters. This has nothing to do with the explanation given above. It happens because the <span style="color: #0000ff; font-family: Courier New;">PRINT</span> statement can't handle more than 4,000 Unicode characters or 8,000 non-Unicode characters. So if it's given a <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #ff00ff;">MAX</span><span style="color: #a5a5a5;">)</span></span> value, it truncates it to <span style="font-family: Courier New;"><span style="color: #0000ff;">NVARCHAR</span><span style="color: #a5a5a5;">(</span><span style="color: #000000;">4000</span><span style="color: #a5a5a5;">)</span></span>. Again, no errors, no warnings.</span></p>
<p dir="ltr"><span style="font-size: small;">This is why I use my own version of <span style="color: #0000ff; font-family: Courier New;">PRINT</span>, which is a stored procedure called &quot;PrintMax&quot;. I'll talk about it next time…</span></p>
<p><span style="font-size: small;"> </span></p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/concatenating-strings-2/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

