<?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/dudi/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;Subqueries For Begginers&#8236;</title>		<link>http://www.madeira.co.il/subqueries-for-begginers-2/</link>
		<comments>http://www.madeira.co.il/subqueries-for-begginers-2/#comments</comments>
		<pubDate>Sat, 28 Apr 2012 16:26:51 +0000</pubDate>
		<dc:creator>&#8235;דודי רוזנטל&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[כללי]]></category>
		<category><![CDATA[מקודמות]]></category>
		<category><![CDATA[SQL Server 2008 R2]]></category>
		<category><![CDATA[SUBQUERIES]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=4913</guid>
		<description><![CDATA[&#8235;How simple are subqueries, or better said – how simple is understanding them? Let’s see!&#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/4913.jpg&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">Don’t know what’s with you guys, but when I first bumped into subqueries I could have understood their importance and efficiency, but for some reason, it wasn’t as intuitive for me as I hoped to. That means, I already know joins, right? And if, as claimed, subquries are only another way of writing it, and the SQL’s optimizer uses joins in any way, why bother? </font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">Well, because it’s worthy and well, because it’s much more simple than it might look when dealing with it for the first time.</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">How simple are subqueries, or better said – how simple is understanding them? Let’s see:</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">First, a subquery is basically a query inside a query. That’s all there is to it. It might also become handy to understand that it’s basically, as mentioned before, another way of writing joins.</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">Now, let’s divide subqueries into 3 types in order to understand which type is to be used where:</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">Let’s think of a result set of a query, any result set, as the purpose of the inner query of ours (inner query, needless to say, is the one who’s inside the brackets, as opposed to the outer query who’s, well, you get the picture, ain’t ya?) : One type is a table – a data set with columns and rows. Other type is a data set that contains only one column and couple of rows, and the third type is a case when we get only one column and only one row.</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">Now, let’s call the first type “Derived Table”, the second type “List” and the third type “Scalar value”. Now and in general, derived tables will be placed after the “FROM” part of the outer query, lists will be placed after the “IN” part of the “WHERE” part of the outer query and scalar value will be put wherever we can use an expression in the outer query.</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">Examples? Why not?</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><b><u><span><font face="Times New Roman"><font color="#2f2f2f">Scalar values subqueries:</font></font></span></u></b><span></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">&#8211; Retrieve the orders with total amount greater than the average total amount</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">SELECT</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>Id ,</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>CustomerId ,</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>TotalAmount ,</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>DateAndTime</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">FROM</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>Sales.Orders</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">WHERE</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>TotalAmount &gt;</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>(</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>SELECT</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>AVG (TotalAmount)</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>FROM</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>Sales.Orders</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>)</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">ORDER BY</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>TotalAmount ASC;</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">GO</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">&#160;</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">&#8211; Retrieve the English customers along with the number of orders</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">&#8211; and the number of support calls of each customer</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">&#160;</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">SELECT</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>CustomerId<span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>= Customers.Id ,</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>CustomerName<span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span><span>&#160;</span>= Customers.FirstName + N' ' + Customers.LastName ,</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>NumberOfOrders<span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>=</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>(</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>SELECT</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>COUNT (*)</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>FROM</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>Sales.Orders AS Orders</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>WHERE</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>Orders.CustomerId = Customers.Id</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>) ,</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>NumberOfSupportCalls =</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>(</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>SELECT</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>COUNT (*)</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>FROM</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160; </font></span><font><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>Support.Calls AS Calls</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>WHERE</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>Calls.CustomerId = Customers.Id</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>)</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">FROM</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>Marketing.Customers AS Customers</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">WHERE</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>Customers.CountryId = 7<span>&#160;&#160;&#160; </span>&#8211; England</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">ORDER BY</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>CustomerId ASC;</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">GO</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">&#160;</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">SELECT         <br />Id,          <br />FirstName,          <br />LastName,          <br />(SELECT Name FROM Lists.Countries WHERE Id = Operation.Members.CountryId)          <br />FROM          <br />Operation.Members;          <br />GO</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">Notice the difference between the first 2 examples and the third one: The last depends on a parameter from the outer query while the first two stand on their own. That’s the difference between a correlated subquery (the last one) and non-correlated one (first two).</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><b><u><span><font face="Times New Roman"><font color="#2f2f2f">List subquery:</font></font></span></u></b><span></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">SELECT p.Name </font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">FROM Production.Product p WHERE ProductSubcategoryID IN </font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">(SELECT ProductSubcategoryID FROM Production.ProductSubcategory WHERE </font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">Name = 'Wheels') </font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><b><u><span><font face="Times New Roman"><font color="#2f2f2f">Derived Table subquery (as another way of writing a join query, for example):</font></font></span></u></b><span></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">The join way:</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">SELECT p.Name </font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">FROM Production.Product p </font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">INNER JOIN Production.ProductSubcategory psc </font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">ON p.ProductSubcategoryID = psc.ProductSubcategoryID </font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">WHERE psc.Name = 'Wheels'</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">And the subquery way:</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">SELECT p.Name </font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">FROM Production.Product p </font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">INNER JOIN (SELECT * FROM Production.ProductSubcategory </font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">WHERE Name = 'Wheels') psc </font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">ON p.ProductSubcategoryID = psc.ProductSubcategoryID</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">&#160;</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">A derived table can also be correlated, but in that case we will need to use a CROSS/OUTER APPLY. Lets take a look at an example:</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">&#8211; Retrieve all the customers along with the last 3 orders of each customer</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">&#160;</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">SELECT</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>CustomerId<span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>= Customers.Id ,</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>CustomerName<span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>= Customers.FirstName + N' ' + Customers.LastName ,</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>OrderId<span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>= LastThreeOrders.Id ,</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>OrderAmount<span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>= LastThreeOrders.TotalAmount ,</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>OrderDateTime<span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>= LastThreeOrders.DateAndTime</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">FROM</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>Marketing.Customers AS Customers</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">OUTER APPLY</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>(</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>SELECT TOP (3)</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>Orders.Id ,</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>Orders.TotalAmount ,</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>Orders.DateAndTime</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>FROM</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>Sales.Orders AS Orders</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>WHERE</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font><span>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </span>Orders.CustomerId = Customers.Id</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>ORDER BY</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>Orders.DateAndTime DESC</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>)</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>AS</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>LastThreeOrders</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">ORDER BY</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>CustomerId<span>&#160;&#160;&#160; </span>ASC ,</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>OrderDateTime DESC;</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">GO</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">&#160;</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">Some things to remember about subqueries, and you are free to start working with them (Of course, it’s only the basics of it, but hey, as long as you understand the way of using it and where to use it, the rest is simply a select query in which you can do whatever you already know about select queries):</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><b><u><span><font face="Times New Roman"><font color="#2f2f2f">Derived tables:</font></font></span></u></b><span></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">· Can return any number of rows.</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">· Can return any number of columns.</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">· Must follow rules for being relational:</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">o Set must have a name (alias. Write “AS &lt;query_name&gt; outside of it).</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">o All columns must have unique names (aliases).</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">o Rows and columns have no order. Order (by) is done at the outer query anyway.</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><b><u><span><font face="Times New Roman"><font color="#2f2f2f">Scalar value subquery:</font></font></span></u></b><span></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">· Must return only 1 row and only 1 column.</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">· Does not require column or set alias.</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">· Can be correlated.</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><b><u><span><font face="Times New Roman"><font color="#2f2f2f">List subquey:</font></font></span></u></b><span></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">· Must return only 1 column</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">· Can return any number of rows</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">· Does not require column or set alias</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">· Can be correlated.</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">Let’s finish this post by saying, that subqueries can also be used for an EXIST check. In most cases, it is a very efficient way for such a check.</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">You might want to remember that:</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">· Subquery can and should be correlated.</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">· It does not return any output.</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">· Number of rows and columns is irrelevant, because SQL Server only checks for the existence of rows and doesn’t really need to retrieve any data, so specifying a long list of columns or a single constant in the column set is identical in this case. Here is an example:</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">&#8211; Retrieve the customers who have at least one order in the last 3 days</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">SELECT</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>Customers.Id ,</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>Customers.FirstName ,</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>Customers.LastName</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">FROM</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>Marketing.Customers AS Customers</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">WHERE</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>EXISTS</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>(</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>SELECT</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>NULL</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>FROM</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>Sales.Orders AS Orders</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>WHERE</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>Orders.CustomerId = Customers.Id</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>AND</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>Orders.DateAndTime &gt; DATEADD (DAY , -3 , SYSDATETIME ())</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>)</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">ORDER BY</font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f"><span><font>&#160;&#160;&#160;&#160;&#160;&#160; </font></span><font>Customers.Id ASC;</font></font></font></span></p>
<p style="line-height: 13pt;margin: 0in 0in 0pt;direction: ltr;background: white" class="MsoNormal"><span><font face="Times New Roman"><font color="#2f2f2f">GO</font></font></span></p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/subqueries-for-begginers-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8235;Spatial Data&#8236;</title>		<link>http://www.madeira.co.il/spatial-data/</link>
		<comments>http://www.madeira.co.il/spatial-data/#comments</comments>
		<pubDate>Sun, 04 Mar 2012 11:41:01 +0000</pubDate>
		<dc:creator>&#8235;דודי רוזנטל&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[כללי]]></category>
		<category><![CDATA[מקודמות]]></category>
		<category><![CDATA[Spatial Data]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=4520</guid>
		<description><![CDATA[&#8235;Spatial data is defined as data that is represented by 2D or 3D images. Lets take a look at this interesting, valuable tool in SQL Server 2008. &#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/4520.jpg&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">Spatial data is defined as data that is represented by 2D or 3D images. 2D images are supported in SQL Server 2008 and 3D images may be added in future releases.</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">SQLCLR UDT (SQL Common Language Runtime User Defined Types) defines Spatial Data as “technology for hosting of the Microsoft .NET common language runtime engine (such as C# or VB.NET) within SQL Server”.</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">Spatial data is subdivided into geometric data (Euclidian geometry, aka “Flat Earth”) and geographic data (locations and boundaries on the earth, aka “Round Earth”).</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong><span style="text-decoration: underline">Geography</span></strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">To explain the uses of Geography Spatial Data Type, let’s think of someone’s address. This someone might be a customer of ours, for that example.</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">Let’s think of this someone’s address as a point of longitude and latitude, which sets the exact location of him on Earth. Knowing where a customer lives as latitude/longitude allows you to answer questions such as:</p>
<p class="MsoListParagraphCxSpFirst" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span>1.<span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>What is the closest company branch for him?</p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span>2.<span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Which salesperson lives closest to him?</p>
<p class="MsoListParagraphCxSpLast" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span>3.<span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>How many customers live more than 2 Kilometers from your nearest branch location?</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">Etc.</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">Defining the Geography Spatial Data Type will lead us to these points:</p>
<p class="MsoListParagraphCxSpFirst" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Geodetic, Ellipsoidal (round-earth) data, such as GPS latitude and longitude coordinates.</p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Different Types of Projections.</p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Enables sharing information with other people who might be using a different projection.</p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Uses Latitude and Longitude angles to identify points on the Earth.<span> </span>Latitude measures how far North (or South) of the Equator a point is, while Longitude measures how far East (or West) of a Prime Meridian a point is.</p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>SQL Server 2008's Spatial data types are based on the OGC's &quot;Simple Feature Access&quot; standards.Open Geospatial Consortium, Inc. (OGC) is described as &quot;a non-profit, international, voluntary consensus standards organization that is leading the<span> </span>development of standards for geospatial and location based services&quot;.</p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>It is important to know which Spatial Reference System is used, so accuracy will not be defected.</p>
<p class="MsoListParagraphCxSpLast" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Commonly Used Reference System Today: Global Positioning System (<strong>GPS</strong>) Data &#8211; WGS84.</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong><span style="text-decoration: underline">Geometry</span></strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">The Geometry spatial data type is used to represent information in a uniform 2-dimensional plane (like graph paper).<span> </span>Units are user-defined.</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">Geometry data type will be used in conjunction with some existing planar projection of the real world that becomes the underlying reference.<span> </span>For example, coordinate space is being used to represent a warehouse facility.<span> </span>You can define polygons that represent storage bays within the warehouse etc.<span> </span>You can then determine where the closest item to each and every bay is.</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">Defining the Geography Spatial Data Type will lead us to these points:</p>
<p class="MsoListParagraphCxSpFirst" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Uses Cartesian coordinate system.</p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Used to represent information in a uniform 2-dimensional plane</p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Units are completely user-defined, and could be inches, miles, pixels etc.<span> </span>Actually, SQL Server doesn't care &#8211; It can perform calculations just the same.</p>
<p class="MsoListParagraphCxSpLast" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Present Storage Bays in Warehouses, Linkages between areas etc.</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">Now, after understanding what Spatial Data is and what Geometry and Geography Data Types are, let’s examine the Specific Types of Spatial Data:</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong> </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong>POINT</strong>:</p>
<p class="MsoListParagraphCxSpFirst" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>An exact location.</p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Defined in terms of an X and Y pair of coordinates (Z (elevation) and M (measure) are optional too).<span> </span></p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Does not have a length or any area associated with it.</p>
<p class="MsoListParagraphCxSpLast" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Used as the fundamental building blocks of more complex spatial types.</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong> </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong>LINESTRING: </strong></p>
<p class="MsoListParagraphCxSpFirst" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Defined as the path between a sequence of points</p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Considered simple if it does not cross over itself, and is considered a ring if the starting point is the same as the ending point.</p>
<p class="MsoListParagraphCxSpLast" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>A LineString is always considered to be a one dimensional object; it has length, but does not have area (even if it is a ring).</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong> </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong>POLYGON</strong>:</p>
<p class="MsoListParagraphCxSpFirst" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>A closed two-dimensional shape defined by a ring.</p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Has both length and area.<span> </span></p>
<p class="MsoListParagraphCxSpLast" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>May also have holes in its interior.</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong> </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong>COLLECTIONS:</strong></p>
<p class="MsoListParagraphCxSpFirst" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Type that can hold a collection of instances.<span> </span>This is similar to a list or an array in most programming languages.</p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>The most generic type of collection is the GeomCollection, whose members can be of any type.<span> </span>Deriving from GeomCollection are MultiPolygon, MultiLineString, and MultiPoint.</p>
<p class="MsoListParagraphCxSpLast" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span style="font-family: Symbol">·</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>As their names suggest, the members of these collection types must be of the single instance type (MultiPoint can only contain Points etc.).</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong><span style="line-height: 21pt"><span>Representing Spatial Data</span></span></strong></p>
<p class="MsoNormal" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span style="font-family: Arial">•</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>The OGC defines different ways to represent geospatial information as bytes of data that can then be interpreted by the Geometry or Geography types.</p>
<p class="MsoNormal" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span style="font-family: Arial">•</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>SQL Server 2008 supports three such formats: Well-Known Text (WKT), Well-Known Binary (WKB), and Geography Markup Language (GML).</p>
<p class="MsoNormal" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span style="font-family: Arial">•</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>WKT is commonly used since it is both concise and [relatively] human-readable.</p>
<p class="MsoNormal" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span style="font-family: Arial">•</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span><span lang="FR">POINT(10 10) / POINT(10 10 10 1)<span> </span>// X Y Z M. </span></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><a href="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image002.png" rel="wp-prettyPhoto[g4520]"><img style="margin: 0px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px" src="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image002_thumb.png" border="0" alt="clip_image002" width="75" height="75" /></a></span></p>
<p class="MsoNormal" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span style="font-family: Arial">•</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>LINESTRING(0 0, 10 10).</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><a href="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image004.jpg" rel="wp-prettyPhoto[g4520]"><img style="margin: 0px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px" src="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image004_thumb.jpg" border="0" alt="clip_image004" width="395" height="204" /></a></span></p>
<p class="MsoNormal" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span style="font-family: Arial">•</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span><span lang="SV">POLYGON((0 0, 0 10, 10 10, 10 0, 0 0)).</span></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><a href="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image006.png" rel="wp-prettyPhoto[g4520]"><img style="margin: 0px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px" src="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image006_thumb.png" border="0" alt="clip_image006" width="314" height="190" /></a></span></p>
<p class="MsoNormal" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span style="font-family: Arial">•</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span><span lang="SV">POLYGON WITH TRIANGLE IN IT</span>:<span lang="SV"> ((0 0, 0 10, 10 10, 10 0, 0 0), (1 1, 4 5, 4 1, 1 1)).</span></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><span><a href="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image008.png" rel="wp-prettyPhoto[g4520]"><img style="margin: 0px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px" src="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image008_thumb.png" border="0" alt="clip_image008" width="136" height="136" /></a></span><strong> </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong> </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong><span style="line-height: 21pt"><span>Instantiating the UDT</span></span></strong></p>
<p class="MsoNormal" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span style="font-family: Arial">•</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Any variable, parameter, or table column can be declared as type Geometry or Geography:</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt 0.25in;direction: ltr"><strong><span> </span>DECLARE @g Geometry </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt 0.25in;direction: ltr"><strong><span> </span>DECLARE @h Geography</strong></p>
<p class="MsoNormal" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span style="font-family: Arial">•</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Use the STGeomFromText() static method to parse data provided in valid WKT syntax:</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt 0.25in;direction: ltr"><strong><span> </span>DECLARE @g Geometry</strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt 0.25in;direction: ltr"><strong><span> </span>SET @g = Geometry::STGeomFromText('LINESTRING(0 0, 10 10, 21 2)',0)</strong></p>
<p class="MsoNormal" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span style="font-family: Arial">•</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>In SQL syntax, CLR method names are case sensitive!</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt 0.25in;direction: ltr"><strong><span> </span>DECLARE @g Geometry</strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt 0.25in;direction: ltr"><strong><span> </span>SET @g = Geometry::STLineFromText('LINESTRING(0 0, 10 10, 21 2)')</strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong> </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong><span style="line-height: 21pt"><span>Working with a UDT Instance</span></span></strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong>DECLARE @g GEOMETRY</strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong>SET @g = 'LINESTRING (<span> </span>69 26, 69 23, 69 21, 67 20, 65 20, </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong><span> </span>63 18, 58 17, 52 17, 51 17, 49 17, 45 18, 44 20, </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong><span> </span>44 21, 42 26, 42 29, 42 32, 42 35, 43 35, 47 38, </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong><span> </span>50 41, 55 42, 58 42, 65 44, 66 44, 67 44, 68 45, </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong><span> </span>69 47, 70 48, 70 50, 71 51, 70 56, 68 60, 68 63, </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong><span> </span>66 65, 65 66, 63 68, 60 71, 59 71, 57 71, 55 71, </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong><span> </span>51 69, 45 65, 44 63, 42 62, 41 59, 41 57, 41 56, </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong><span> </span>41 54, 42 53 )'</strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><span><a href="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image010.png" rel="wp-prettyPhoto[g4520]"><img style="margin: 0px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px" src="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image010_thumb.png" border="0" alt="clip_image010" width="194" height="140" /></a></span><strong> </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong> </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong>STEnvelope:</strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">A rectangle that is defined by the combination of minimum and maximum X and Y values found in a given Geometry instance.<span> </span>The OGC standard method STEnvelope() returns the bounding box (a Polygon) for the instance on which it is invoked.<span> </span>All points of the original instance lie within the new Polygon.</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><span><a href="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image012.png" rel="wp-prettyPhoto[g4520]"><img style="margin: 0px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px" src="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image012_thumb.png" border="0" alt="clip_image012" width="194" height="154" /></a></span><strong> </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong> </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong>STConvexHull:</strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">If something is convex, then it is thought of as having a surface that is curved or rounded outward from the center. For example, the side of an apple is convex.</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><span><a href="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image014.png" rel="wp-prettyPhoto[g4520]"><img style="margin: 0px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px" src="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image014_thumb.png" border="0" alt="clip_image014" width="190" height="148" /></a></span><strong> </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong> </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong>Reduce(tolerance):</strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">A method that will simplify a given instance using the Douglas-Peucker algorithm.</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">The result is a an approximation of the original instance containing a fewer number of points. The accuracy of the new shape improves as the provided tolerance value approaches zero, but more points are necessary to provide that accuracy.</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><span><a href="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image016.png" rel="wp-prettyPhoto[g4520]"><img style="margin: 0px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px" src="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image016_thumb.png" border="0" alt="clip_image016" width="202" height="147" /></a></span></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong><span style="line-height: 21pt"><span>Instances to interact with one another</span></span></strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong>DECLARE @g geometry </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong><span lang="SV"><span> </span>= 'POLYGON((10 10, 40 10, 40 40, 10 40, 10 10))'</span></strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong>DECLARE @h geometry </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong><span lang="SV"><span> </span>= 'POLYGON((30 30, 50 30, 50 50, 30 50, 30 30))'</span></strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><span><a href="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image018.png" rel="wp-prettyPhoto[g4520]"><img style="margin: 0px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px" src="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image018_thumb.png" border="0" alt="clip_image018" width="171" height="158" /></a></span><strong> </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong> </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong>STDifference():</strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">Returns a new instance consisting of all points from the base instance that do not contain points from the parameter instance.</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><span><a href="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image020.png" rel="wp-prettyPhoto[g4520]"><img style="margin: 0px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px" src="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image020_thumb.png" border="0" alt="clip_image020" width="173" height="160" /></a></span><strong> </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong> </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong>STIntersection(): </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">Returns a new instance containing only the points that are in common between the base instance and the parameter instance.</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><span><a href="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image022.png" rel="wp-prettyPhoto[g4520]"><img style="margin: 0px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px" src="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image022_thumb.png" border="0" alt="clip_image022" width="180" height="167" /></a></span><strong> </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong> </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong>STUnion(): </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">Returns a new instance containing all of the points of the base instance and the parameter instance merged together.</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><span><a href="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image024.png" rel="wp-prettyPhoto[g4520]"><img style="padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0px" src="http://www.madeira.co.il/wp-content/uploads/2012/03/clip_image024_thumb.png" border="0" alt="clip_image024" width="189" height="175" /></a></span><strong> </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong> </strong></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong><span style="line-height: 21pt"><span>Working With Geography</span></span></strong></p>
<p class="MsoNormal" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span style="font-family: Arial">•</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>The Open Geospatial Consortium's Simple Features specification, which SQL Server 2008's Geometry data type is based upon, defines standards for working with spatial data using a flat-earth (projected planar) model.</p>
<p class="MsoNormal" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span style="font-family: Arial">•</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>It defines how to work with paper maps of the world, but not globes.</p>
<p class="MsoNormal" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span style="font-family: Arial">•</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>SQL Server team recognized that and implemented a second data type just for representing geospatial data using a true ellipsoidal model: Geography.</p>
<p class="MsoNormal" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span style="font-family: Arial">•</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Longitude is the horizontal angle (how far East or West from a Prime Meridian) and ranges from -180 degrees to 180 degrees (with -180 and 180 being the same).<span> </span>Latitude is the vertical angle (how far North or South from the Equator) and ranges from -90 degrees to 90 degrees (with -90 representing the South Pole and 90 representing the North Pole).</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/spatial-data/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>&#8235;Changing location of tempdb files&#8236;</title>		<link>http://www.madeira.co.il/changing-location-of-tempdb-files/</link>
		<comments>http://www.madeira.co.il/changing-location-of-tempdb-files/#comments</comments>
		<pubDate>Tue, 07 Feb 2012 08:46:20 +0000</pubDate>
		<dc:creator>&#8235;דודי רוזנטל&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[כללי]]></category>
		<category><![CDATA[מקודמות]]></category>
		<category><![CDATA[SQL Server 2008 R2]]></category>
		<category><![CDATA[tempdb]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=4174</guid>
		<description><![CDATA[&#8235;Imagine a situation in which your tempdb database (data and log files) needs to be relocated from its current location to a new one as part of a planned relocation or due to data files gone large. Lets see how its done.&#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/4174.jpg&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">Imagine a situation (and if you already faced that problem, you don’t even need to imagine) in which your tempdb database (data and log files) needs to be relocated from its current location to a new one as part of a planned relocation or due to data files gone large. Current location, usually the default one, might just not be enough sometimes.</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">So is there a way to change the location of tempdb database files?</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">Well, just imagine how short and irrelevant this post would have been if the answer to this question was “Nope, there isn’t”, but since there is, and quite a simple one as a matter of fact, let’s see how it’s done (based on <a href="http://msdn.microsoft.com/en-us/library/ms345408.aspx"><span style="color: #0000ff">BOL</span></a>):</p>
<p class="MsoNormal" style="line-height: normal;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span style="font-family: Segoe UI"><span>1.</span></span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span><span><span style="font-family: Segoe UI"><span>Determine the logical file names of the </span></span></span><span><span style="font-family: Courier New"><span style="color: #000066">tempdb</span></span></span><span><span style="font-family: Segoe UI"><span> database and their current location on the disk. </span></span><a name="CodeSnippetCopyLink"></a></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Consolas"><span>SELECT name, physical_name AS CurrentLocation</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Consolas"><span>FROM sys.master_files</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Consolas"><span>WHERE database_id = DB_ID(N'tempdb');</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 9pt 0.5in;direction: ltr"><span><span><span style="font-family: Consolas"><span>GO</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 9pt 0.5in;direction: ltr"><span><span><span style="font-family: Consolas"><span>(</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 9pt 0.5in;direction: ltr"><span><span><span style="font-family: Consolas"><span><span> </span></span><span>Or use </span></span></span></span><span><span><span><span style="color: #800000;font-family: Courier New">sp_helpfile</span></span></span></span><span><span><span style="font-family: Consolas"><span>:</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 0pt;direction: ltr"><span><span><span><span style="font-family: Consolas"><span> </span></span></span></span></span><span style="font-family: Courier New"><span><span><span><span style="color: #0000ff">USE</span></span></span></span><span><span><span> tempdb</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 0pt;direction: ltr"><span><span><span style="font-family: Courier New"><span style="color: #0000ff"><span><span> </span></span><span>GO</span></span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 0pt;direction: ltr"><span><span><span style="font-family: Courier New"><span style="color: #800000"><span><span> </span></span><span>sp_helpfile</span></span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 0pt;direction: ltr"><span><span><span style="font-family: Courier New"><span style="color: #0000ff"><span><span> </span></span><span>GO</span></span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 0pt;direction: ltr"><span style="font-family: Courier New"><span><span><span><span style="color: #0000ff"><span> </span></span></span></span></span><span><span><span>)</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 0pt;direction: ltr"><span><span><span style="font-family: Courier New"><span style="color: #0000ff"> </span></span></span></span></p>
<p class="MsoListParagraphCxSpFirst" style="line-height: normal;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span><span style="font-family: Segoe UI"><span>2.</span></span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span><span><span style="font-family: Segoe UI"><span>Change the location of the files:</span></span></span></span></p>
<p class="MsoListParagraphCxSpLast" style="line-height: normal;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Segoe UI"><span> </span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Consolas"><span>USE master;</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Consolas"><span>GO</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Consolas"><span>ALTER DATABASE tempdb </span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Consolas"><span>MODIFY FILE (NAME = tempdev, FILENAME = 'E:\SQLData\tempdb.mdf');</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Consolas"><span>GO</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Consolas"><span>ALTER DATABASE tempdb </span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span><span style="font-family: Consolas"><span>MODIFY FILE (NAME = templog, FILENAME = 'F:\SQLLog\templog.ldf');</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 9pt 0.5in;direction: ltr"><span><span><span style="font-family: Consolas"><span>GO</span></span></span></span></p>
<p class="MsoNormal" style="line-height: 13.5pt;margin: 0in 0in 0pt;direction: ltr;background: white"><span><span><span style="font-family: Courier New"><span style="color: #2f2f2f"><span><span> </span></span><span>You will get this message:</span></span></span></span></span></p>
<p class="MsoNormal" style="line-height: 13.5pt;margin: 0in 0in 0pt 45.8pt;direction: ltr;background: white"><span><span><span style="font-family: Courier New"><span style="color: #2f2f2f"> </span></span></span></span></p>
<p class="MsoNormal" style="line-height: 13.5pt;margin: 0in 0in 0pt 45.8pt;direction: ltr;background: white"><span><span><span style="font-family: Courier New"><span style="color: #2f2f2f">The file &quot;tempdev&quot; has been modified in the system catalog. The</span></span></span></span></p>
<p class="MsoNormal" style="line-height: 13.5pt;margin: 0in 0in 0pt;direction: ltr;background: white"><span><span><span style="font-family: Courier New"><span style="color: #2f2f2f"><span><span> </span></span><span>new path will be used the next time the database is started.</span></span></span></span></span></p>
<p class="MsoNormal" style="line-height: 13.5pt;margin: 0in 0in 0pt;direction: ltr;background: white"><span><span><span style="font-family: Courier New"><span style="color: #2f2f2f"><span><span> </span></span><span>The file &quot;templog&quot; has been modified in the system catalog. The</span></span></span></span></span></p>
<p class="MsoNormal" style="line-height: 13.5pt;margin: 0in 0in 0pt;direction: ltr;background: white"><span><span><span style="font-family: Courier New"><span style="color: #2f2f2f"><span><span> </span></span><span>new path will be used the next time the database is started.</span></span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span><span style="font-family: Segoe UI"><span>3.</span></span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span><span><span style="font-family: Segoe UI"><span>Stop and restart the instance of SQL Server. </span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span><span style="font-family: Segoe UI"><span>4.</span></span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span><span><span style="font-family: Segoe UI"><span>Verify the file change.</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span style="font-family: Consolas"><span>SELECT name, physical_name AS CurrentLocation, state_desc</span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span style="font-family: Consolas"><span>FROM sys.master_files</span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 9pt 0.5in;direction: ltr"><span><span style="font-family: Consolas"><span>WHERE database_id = DB_ID(N'tempdb');</span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 9pt 0.5in;direction: ltr"><span><span style="font-family: Consolas"><span>(</span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 9pt 0.5in;direction: ltr"><span><span style="font-family: Consolas"><span><span> </span></span><span>Or use </span></span></span><span><span><span style="color: #800000;font-family: Courier New">sp_helpfile</span></span></span><span><span style="font-family: Consolas"><span>:</span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 0pt;direction: ltr"><span><span><span style="font-family: Consolas"><span> </span></span></span></span><span style="font-family: Courier New"><span><span><span style="color: #0000ff">USE</span></span></span><span><span> tempdb</span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 0pt;direction: ltr"><span><span style="font-family: Courier New"><span style="color: #0000ff"><span><span> </span></span><span>GO</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 0pt;direction: ltr"><span><span style="font-family: Courier New"><span style="color: #800000"><span><span> </span></span><span>sp_helpfile</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 0pt;direction: ltr"><span><span style="font-family: Courier New"><span style="color: #0000ff"><span><span> </span></span><span>GO</span></span></span></span></p>
<p class="MsoNormal" style="line-height: normal;margin: 0in 0in 9pt 0.5in;direction: ltr"><span><span style="font-family: Courier New"><span>)</span></span></span></p>
<p class="MsoNormal" style="line-height: normal;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span><span style="font-family: Segoe UI"><span>5.</span></span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span><span><span style="font-family: Segoe UI"><span>Delete the </span></span></span><span><span style="font-family: Courier New"><span style="color: #000066">tempdb.mdf</span></span></span><span><span style="font-family: Segoe UI"><span> and </span></span></span><span><span style="font-family: Courier New"><span style="color: #000066">templog.ldf</span></span></span><span><span style="font-family: Segoe UI"><span> files from the original location (See section 1).</span></span></span></p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">It’s known that tempdb database is deleted and recreated every time we restart the SQL Server service (when the instance of SQL Server is started), so we don’t need to physically move the data and log files, but only change file configuration in the database. The files are then created in the new location and until then uses data and log files in the existing location.</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><strong>Important</strong>: If you are moving databases to a new drive/volume or another server (different drive letter), change the SQL Server Agent log path as well. If not, SQL Agent will fail to start. If the Database Default Locations path is not modified, creating a database may fail.</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr"><span> </span>To change the SQL Agent log path:</p>
<p class="MsoListParagraphCxSpFirst" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span>1.<span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>In SQL Server Management Studio expand SQL Server Agent.</p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span>2.<span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Right-click on Errorlogs.</p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span>3.<span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Click on Configure.</p>
<p class="MsoListParagraphCxSpLast" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span>4.<span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Change the Error Log File path to the new desired location.</p>
<p class="MsoNormal" style="line-height: 13pt;margin: 0in 0in 10pt;direction: ltr">To change the Database Default Locations path:</p>
<p class="MsoListParagraphCxSpFirst" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span>1.<span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>In SQL Server Management Studio right-click on the SQL Server name.</p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span>2.<span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Choose Properties.</p>
<p class="MsoListParagraphCxSpMiddle" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 0pt 0.5in;direction: ltr"><span><span>3.<span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Choose Database Settings.</p>
<p class="MsoListParagraphCxSpLast" style="line-height: 13pt;text-indent: -0.25in;margin: 0in 0in 10pt 0.5in;direction: ltr"><span><span>4.<span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span>Under &quot;Database Default Locations&quot; modify the Data and Log paths to the new, desired location.</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/changing-location-of-tempdb-files/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>&#8235;Help The Optimizer&#8236;</title>		<link>http://www.madeira.co.il/help-the-optimizer-2/</link>
		<comments>http://www.madeira.co.il/help-the-optimizer-2/#comments</comments>
		<pubDate>Tue, 24 Jan 2012 10:05:42 +0000</pubDate>
		<dc:creator>&#8235;דודי רוזנטל&#8236;</dc:creator>				<category><![CDATA[כללי]]></category>
		<category><![CDATA[מאמרים]]></category>
		<category><![CDATA[מקודמות]]></category>
		<category><![CDATA[Execution Plan]]></category>
		<category><![CDATA[Optimizer]]></category>
		<category><![CDATA[SQL Server 2008 R2]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=3833</guid>
		<description><![CDATA[&#8235;Even a smart tool with a complicated algorithm such as the optimizer might sometimes need our help. Help it and get helped even more!&#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/3833.jpg&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p dir="ltr">Reading this posts title might make us smile: Me helping the optimizer? Isn’t it the one who supposes to help me?</p>
<p dir="ltr">Well, yes and no. The optimizer might help me getting the best execution pal for my queries (as long as I keep performing the right maintenance issues), but is some cases I can hint it to perform even better, and the way to do it is by re-scripting, re-quering and using nothing but some basic TSQL.</p>
<p dir="ltr">As always, an example shown might be worth a thousand words, and my purpose is to make my point with less than a half.</p>
<p dir="ltr">Lets take a look at a script of a procedure which job is to receive data of workers entering and exiting a factory and in addition to inserting it into a table, presenting it on screen for boarder guards:</p>
<p dir="ltr">
<p dir="ltr"><strong>SET ANSI_NULLS ON<br />
SET QUOTED_IDENTIFIER OFF<br />
GO</strong></p>
<p dir="ltr"><strong>CREATE PROCEDURE [dbo].[sp_TLA_Monitor]</strong></p>
<p dir="ltr"><strong>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
@pUserNo      int      ,<br />
@pSort        smallint ,<br />
@pDoor        int      ,<br />
@pFilterP     smallint = 0 ,<br />
@pFilterV     smallint = 0<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</strong></p>
<p dir="ltr"><strong>AS<br />
DECLARE @ActLinkString varchar(8000)<br />
SET nocount on </strong></p>
<p dir="ltr"><strong>SET @ActLinkString = &quot;</strong></p>
<p dir="ltr"><strong>IF @pFilterP &gt; 0 AND @pFilterP &lt; 4<br />
BEGIN<br />
SET @ActLinkString =<br />
'DECLARE @NUMERATOR int '+<br />
'SET @NUMERATOR = ( SELECT MIN( MA.Numerator ) '+<br />
' FROM TLA_OpenEvents MA  '+</strong></p>
<p dir="ltr"><strong>CASE WHEN ( @pFilterP = 1 ) THEN 'WHERE DATEDIFF( day   ,  EventTime , GETDATE() ) &lt; 1 ) '<br />
WHEN ( @pFilterP = 2 ) THEN 'WHERE DATEDIFF( Hour  ,  EventTime , GETDATE() ) &lt; '+CAST( @pFilterV  AS VARCHAR) +' ) '<br />
WHEN ( @pFilterP = 3 ) THEN 'WHERE DATEDIFF( day   ,  EventTime , GETDATE() ) &lt; '+CAST( @pFilterV  AS VARCHAR) +' ) '<br />
ELSE ' ' END<br />
END</strong></p>
<p dir="ltr"><strong>SET @ActLinkString = @ActLinkString +<br />
'SELECT C.Descript Controller , DE.Descript Device , '+<br />
'cObjCode =  CASE WHEN ISNULL( DO.Code , 0 ) &lt;&gt; 0 THEN DO.Code ELSE CASE WHEN ( MA.Type &gt;= 810 AND MA.Type &lt;= 817 )  THEN MA.ControllerPhisicalID  ELSE MA.Device END END , '+<br />
'cObjType =  CASE WHEN ISNULL( DO.Code , 0 ) &lt;&gt; 0 THEN 1100    ELSE CASE WHEN ( MA.Type &gt;= 810 AND MA.Type &lt;= 817 )  THEN 1200 ELSE 1300 END  END , DO.Descript Door , '+ </strong></p>
<p dir="ltr"><strong> 'MA.Badge Card , W.No WCNo, W.Name WC , DP.No DepNo, DP.Name Dep ,'+<br />
'OE.EventTime , ED.Descript Type ,'+<br />
'OE.FirstOperator ,'+<br />
'FirstOperatorDescr = (SELECT Emp_Name From Emp Where Emp_no = OE.FirstOperator ),'+<br />
'OE.FirstTreatEventTime , '+<br />
'OE.LastOperator  ,'+<br />
'LastOperatorDescr  = (SELECT Emp_Name From Emp Where Emp_no = OE.LastOperator  ),'+<br />
'OE.EndTime , '+<br />
'OE.Numerator , OE.Code , ED.Color , '+<br />
'OE.Severity , OE.Sensitivty ,OE.Priority , OE.Status , '+<br />
'UserType = ISNULL( vwObj.ObjType , 0 ) , '+<br />
'CarNo    = vwObj.ObjCarNo , '+<br />
'EmpNo    = vwObj.ObjNo    ,  Emp_Name = vwObj.ObjName , '+<br />
'xFunction = ( SELECT Descript FROM dvc_def WHERE code = MA.FunctionCode ) , '+<br />
'Pic =  vwObj.ObjPic '+</strong></p>
<p dir="ltr"><strong>'FROM TLA_Mov_Access MA '+<br />
'left join vw_ObjectType   vwObj  on vwObj.ObjCode  =  MA.Emp_no AND MA.Tag_Type = vwObj.ObjType '+<br />
'left join Work_c          W      on W.No           =  vwObj.ObjStation '+<br />
'left join Dep             DP     on DP.No          =  vwObj.ObjDep '+<br />
'join TLA_OpenEvents       OE     on OE.Numerator   =  MA.Numerator '+<br />
'left join TLA_Devices     DE     on DE.Code        =  MA.Device '+<br />
'join TLA_EventsDef        ED     on ED.Code        =  MA.Event '+<br />
'left join TLA_Doors       DO     on DO.Code        =  DE.Door '+<br />
'left join TLA_Controllers C      on C.Code         =  MA.ControllerPhisicalID '+<br />
' join dbo.fnc_GetMapPerm('+CAST( @pUserNo AS VARCHAR )+', 12 )  GMP on GMP.Control = ISNULL(DO.Code,-1) '+ &#8211;210609</strong></p>
<p dir="ltr"><strong> CASE WHEN ( @pFilterP = 0 ) THEN 'WHERE 1 = 1 '<br />
WHEN ( @pFilterP = 1 ) THEN 'WHERE MA.Numerator &gt; @NUMERATOR  '<br />
WHEN ( @pFilterP = 2 ) THEN 'WHERE MA.Numerator &gt; @NUMERATOR  '<br />
WHEN ( @pFilterP = 3 ) THEN 'WHERE MA.Numerator &gt; @NUMERATOR  '<br />
WHEN ( @pFilterP = 4 ) THEN 'WHERE MA.Numerator &gt; (SELECT MAX(Numerator) &#8211; '+CAST( @pFilterV  AS VARCHAR)+' FROM TLA_Mov_Access) ' ELSE 'WHERE 1 = 1' END +</strong></p>
<p dir="ltr"><strong> CASE WHEN ( @pDoor &gt;= 0 ) THEN ' AND DO.Code = '+CAST( @pDoor AS VARCHAR) ELSE ' ' END + </strong></p>
<p dir="ltr"><strong> CASE @pSort<br />
WHEN  0 THEN  ' ORDER BY OE.Numerator DESC '<br />
WHEN  1 THEN  ' ORDER BY OE.Priority DESC '<br />
WHEN  2 THEN  ' ORDER BY OE.Severity DESC '<br />
WHEN  3 THEN  ' ORDER BY OE.Sensitivty DESC ' END</strong></p>
<p dir="ltr"><strong>EXEC @ActLinkString</strong></p>
<p dir="ltr"><strong>GO<br />
SET QUOTED_IDENTIFIER OFF<br />
GO<br />
SET ANSI_NULLS ON<br />
GO</strong></p>
<p dir="ltr">
<p dir="ltr">As you can see, the procedure creates a string to be executed.</p>
<p dir="ltr">We noticed that recently query time of running this procedure is much longer than it used to be – a growth of hundreds of percents. Checking all maintenance issues (indexes, fragmentations etc.) didn’t help the query to run faster like it used to.</p>
<p dir="ltr">Then we ran the Execution Plan of the query and got this:</p>
<p dir="ltr"><a href="http://www.madeira.co.il/wp-content/uploads/2012/01/sp_TLA_Monitor_Before_Execution_Plan.png" rel="wp-prettyPhoto[g3833]"><img style="padding-left: 0px;padding-right: 0px;padding-top: 0px;border-width: 0px" src="http://www.madeira.co.il/wp-content/uploads/2012/01/sp_TLA_Monitor_Before_Execution_Plan_thumb.png" border="0" alt="sp_TLA_Monitor_Before_Execution_Plan" width="244" height="184" /></a></p>
<p dir="ltr">
<p dir="ltr">A kind of a complex one, as a query that uses lots of LEFT JOINs should look like.</p>
<p dir="ltr">Funny thing about it that when we looked at the details we saw in more than one place a huge difference between the estimated and the actual number of rows and between the estimated and the actual number of executions.</p>
<p dir="ltr"><a href="http://www.madeira.co.il/wp-content/uploads/2012/01/Key-Lookup.png" rel="wp-prettyPhoto[g3833]"><img style="padding-left: 0px;padding-right: 0px;padding-top: 0px;border-width: 0px" src="http://www.madeira.co.il/wp-content/uploads/2012/01/Key-Lookup_thumb.png" border="0" alt="Key Lookup" width="114" height="244" /></a></p>
<p dir="ltr">
<p dir="ltr">Checked again if statistics are updated. Well, they were.</p>
<p dir="ltr">So what is it? What makes the optimizer get the wrong impression? We assumed that if the estimated numbers were closer, much closer in fact, to the actual numbers, optimizer could have probably chosen a second way, different one and perhaps much better one, to execute the query.</p>
<p dir="ltr">Answer is simple: The more complex a query is, the more stages it goes along the way and the more JOINs, mostly of course LEFT JOINs it uses, makes the algorithm become much less accurate. Take for an example a 10% of a standard deviation of the optimizer in estimation at each level. and you got yourself one irrelevant Execution Plan.</p>
<p dir="ltr">So what’s the solution for this problem, you may ask.</p>
<p dir="ltr">A good thing to do would be trying to rescript the query in a way that the optimizer will go step-by-step and perform less complex simultaneous processes.</p>
<p dir="ltr">This query might work better if instead of using one SELECT Statement will all these JOINs, we will try to use temporary tables and devide the SELECT vertically instead of horizontally. In that way, optimizer will use much less JOINs per INSERT INTO and Execution Plan might be devided to more levels, but much more simple in each level.</p>
<p dir="ltr">It should be looking something like this:</p>
<p dir="ltr">
<p dir="ltr"><strong>SET ANSI_NULLS ON<br />
SET QUOTED_IDENTIFIER OFF<br />
GO</strong></p>
<p dir="ltr"><strong>CREATE PROCEDURE [dbo].[sp_TLA_Monitor_Test]</strong></p>
<p dir="ltr"><strong>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
@pUserNo      int      ,<br />
@pSort        smallint ,<br />
@pDoor        int      ,<br />
@pFilterP     smallint = 0 ,<br />
@pFilterV     smallint = 0<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</strong></p>
<p dir="ltr"><strong>/*<br />
DOOR       = 1100;<br />
CONTROLLER = 1200;<br />
DEVICE     = 1300;<br />
BUILDING   = 2000;<br />
IMAGE      = 3000;<br />
BUZZER     = 4000;<br />
*/</strong></p>
<p dir="ltr"><strong>AS<br />
DECLARE @ActLinkString varchar(8000)<br />
SET nocount on </strong></p>
<p dir="ltr"><strong>SET @ActLinkString = &quot;</strong></p>
<p dir="ltr"><strong>IF @pFilterP &gt; 0 AND @pFilterP &lt; 4<br />
BEGIN<br />
SET @ActLinkString =<br />
'DECLARE @NUMERATOR int '+<br />
'SET @NUMERATOR = ( SELECT MIN( MA.Numerator ) '+<br />
' FROM TLA_OpenEvents MA  '+</strong></p>
<p dir="ltr"><strong> CASE WHEN ( @pFilterP = 1 ) THEN 'WHERE DATEDIFF( day   ,  EventTime , GETDATE() ) &lt; 1 ) '<br />
WHEN ( @pFilterP = 2 ) THEN 'WHERE DATEDIFF( Hour  ,  EventTime , GETDATE() ) &lt; '+CAST( @pFilterV  AS VARCHAR) +' ) '<br />
WHEN ( @pFilterP = 3 ) THEN 'WHERE DATEDIFF( day   ,  EventTime , GETDATE() ) &lt; '+CAST( @pFilterV  AS VARCHAR) +' ) '<br />
ELSE ' ' END<br />
END</strong></p>
<p dir="ltr"><strong>SET @ActLinkString = @ActLinkString +<br />
'SELECT '+<br />
'MA.Badge Card, '+<br />
'MA.Tag_Type, '+<br />
'MA.Emp_no, '+<br />
'MA.Numerator Num1, '+<br />
'MA.Event, '+<br />
'MA.Device Dev1, '+<br />
'MA.ControllerPhisicalID, '+<br />
'MA.FunctionCode, '+<br />
'MA.Type Type1, '+<br />
'C.Descript Controller '+<br />
'INTO '+<br />
'#Tmp1 '+<br />
'FROM '+<br />
'TLA_Mov_Access MA '+<br />
'LEFT JOIN TLA_Controllers C on MA.ControllerPhisicalID=C.Code '+</strong></p>
<p><strong> </strong></p>
<div style="direction: ltr"><span style="font-weight: bold">'SELECT '+</span></div>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong></p>
<div style="direction: ltr">'T.Card, T.Tag_Type, T.Emp_no, T.Num1, T.Event, T.Dev1, T.Controller, T.ControllerPhisicalID, T.FunctionCode, T.Type1, '+</div>
<div style="direction: ltr">'UserType = ISNULL(vwObj.ObjType, 0), '+</div>
<div style="direction: ltr">'CarNo    = vwObj.ObjCarNo, '+</div>
<div style="direction: ltr">'EmpNo    = vwObj.ObjNo, '+</div>
<div style="direction: ltr">'Emp_Name = vwObj.ObjName, '+</div>
<div style="direction: ltr">'Pic =  vwObj.ObjPic, '+</div>
<div style="direction: ltr">'vwObj.ObjStation, '+</div>
<div style="direction: ltr">'vwObj.ObjDep '+</div>
<div style="direction: ltr">'INTO '+</div>
<div style="direction: ltr">'#Tmp2 '+</div>
<div style="direction: ltr">'FROM '+</div>
<div style="direction: ltr">'#Tmp1 T '+</div>
<div style="direction: ltr">'LEFT JOIN vw_ObjectType vwObj on vwObj.ObjCode=T.Emp_no AND T.Tag_Type=vwObj.ObjType '+</div>
<p></strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p dir="ltr"><strong>'SELECT '+<br />
'T.Card, T.Tag_Type, T.Emp_no, T.Num1, T.Event, T.Dev1, T.Controller, T.ControllerPhisicalID, T.FunctionCode, T.Type1, '+<br />
'T.UserType, T.CarNo, T.EmpNo, T.Emp_Name, T.Pic, T.ObjStation, T.ObjDep, '+<br />
'OE.EventTime, '+<br />
'OE.FirstOperator,'+<br />
'FirstOperatorDescr = (SELECT Emp_Name From Emp Where T.Emp_no = OE.FirstOperator), '+<br />
'OE.FirstTreatEventTime, '+<br />
'OE.LastOperator, '+<br />
'LastOperatorDescr = (SELECT Emp_Name From Emp Where T.Emp_no = OE.LastOperator), '+<br />
'OE.EndTime, '+<br />
'OE.Numerator, '+<br />
'OE.Code, '+<br />
'OE.Severity, '+<br />
'OE.Sensitivty, '+<br />
'OE.Priority, '+<br />
'OE.Status, '+<br />
'ED.Descript Type, '+<br />
'ED.Color '+<br />
'INTO '+<br />
'#Tmp3 '+<br />
'FROM '+<br />
'#Tmp2 T '+<br />
'JOIN TLA_OpenEvents OE on OE.Numerator=T.Num1 '+<br />
'JOIN TLA_EventsDef ED on ED.Code=T.Event '+</strong></p>
<div style="direction: ltr"><span style="font-weight: bold">'SELECT '+</span></div>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong></p>
<div style="direction: ltr">'T.Card, T.Tag_Type, T.Emp_no, T.Num1, T.Event, T.Dev1, T.Controller, T.ControllerPhisicalID, T.FunctionCode, T.Type1, '+</div>
<div style="direction: ltr">'T.UserType, T.CarNo, T.EmpNo, T.Emp_Name, T.Pic, T.ObjStation, T.ObjDep, '+</div>
<div style="direction: ltr">'T.EventTime, T.FirstOperator, T.FirstOperatorDescr, T.FirstTreatEventTime, T.LastOperator, '+</div>
<div style="direction: ltr">'T.LastOperatorDescr, T.EndTime, T.Numerator, T.Code, T.Severity, T.Sensitivty, '+</div>
<div style="direction: ltr">'T.Priority, T.Status, T.Type, T.Color, '+</div>
<div style="direction: ltr">'DE.Descript Device, '+</div>
<div style="direction: ltr">'xFunction = (SELECT Descript FROM dvc_def WHERE code = T.FunctionCode), '+</div>
<div style="direction: ltr">'DO.Descript Door, '+</div>
<div style="direction: ltr">'cObjCode =  CASE WHEN ISNULL( DO.Code , 0 ) &lt;&gt; 0 THEN DO.Code ELSE CASE WHEN ( T.Type1 &gt;= 810 AND T.Type1 &lt;= 817 )  THEN T.ControllerPhisicalID  ELSE T.Dev1 END END , '+</div>
<div style="direction: ltr">'cObjType =  CASE WHEN ISNULL( DO.Code , 0 ) &lt;&gt; 0 THEN 1100    ELSE CASE WHEN ( T.Type1 &gt;= 810 AND T.Type1 &lt;= 817 )  THEN 1200 ELSE 1300 END  END '+</div>
<div style="direction: ltr">'INTO '+</div>
<div style="direction: ltr">'#Tmp4 '+</div>
<div style="direction: ltr">'FROM '+</div>
<div style="direction: ltr">'#Tmp3 T '+</div>
<div style="direction: ltr">'LEFT JOIN TLA_Devices DE on T.Dev1=DE.Code '+</div>
<div style="direction: ltr">'LEFT JOIN TLA_Doors DO on DE.Door=DO.Code '+</div>
<div style="direction: ltr">'JOIN dbo.fnc_GetMapPerm('+CAST( @pUserNo AS VARCHAR )+', 12 )  GMP on GMP.Control = ISNULL(DO.Code,-1) '+</div>
<p></strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<div style="direction: ltr"><span style="font-weight: bold">'SELECT '+</span></div>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p><strong></p>
<div style="direction: ltr">'T.Controller, '+</div>
<div style="direction: ltr">'T.Device, '+</div>
<div style="direction: ltr">'T.cObjCode, '+</div>
<div style="direction: ltr">'T.cObjType, '+</div>
<div style="direction: ltr">'T.Door, '+</div>
<div style="direction: ltr">'T.Card, '+</div>
<div style="direction: ltr">'W.No WCNo, '+</div>
<div style="direction: ltr">'W.Name WC, '+</div>
<div style="direction: ltr">'DP.No DepNo, '+</div>
<div style="direction: ltr">'DP.Name Dep, '+</div>
<div style="direction: ltr">'T.EventTime, '+</div>
<div style="direction: ltr">'T.Type, '+</div>
<div style="direction: ltr">'T.FirstOperator, '+</div>
<div style="direction: ltr">'T.FirstOperatorDescr, '+</div>
<div style="direction: ltr">'T.FirstTreatEventTime, '+</div>
<div style="direction: ltr">'T.LastOperator, '+</div>
<div style="direction: ltr">'T.LastOperatorDescr, '+</div>
<div style="direction: ltr">'T.EndTime, '+</div>
<div style="direction: ltr">'T.Numerator, '+</div>
<div style="direction: ltr">'T.Code, '+</div>
<div style="direction: ltr">'T.Color, '+</div>
<div style="direction: ltr">'T.Severity, '+</div>
<div style="direction: ltr">'T.Sensitivty, '+</div>
<div style="direction: ltr">'T.Priority, '+</div>
<div style="direction: ltr">'T.Status, '+</div>
<div style="direction: ltr">'T.UserType, '+</div>
<div style="direction: ltr">'T.CarNo, '+</div>
<div style="direction: ltr">'T.EmpNo, '+</div>
<div style="direction: ltr">'T.Emp_Name, '+</div>
<div style="direction: ltr">'T.xFunction, '+</div>
<div style="direction: ltr">'T.Pic '+</div>
<div style="direction: ltr">'FROM '+</div>
<div style="direction: ltr">'#Tmp4 T '+</div>
<div style="direction: ltr">'LEFT JOIN Work_c W on T.ObjStation=W.No '+</div>
<div style="direction: ltr">'LEFT JOIN Dep DP on T.ObjDep=DP.No '+</div>
<p></strong></p>
<p><strong> </strong></p>
<p><strong> </strong></p>
<p dir="ltr"><strong> CASE WHEN ( @pFilterP = 0 ) THEN 'WHERE 1 = 1 '<br />
WHEN ( @pFilterP = 1 ) THEN 'WHERE T.Numerator &gt; @NUMERATOR  '<br />
WHEN ( @pFilterP = 2 ) THEN 'WHERE T.Numerator &gt; @NUMERATOR  '<br />
WHEN ( @pFilterP = 3 ) THEN 'WHERE T.Numerator &gt; @NUMERATOR  '<br />
WHEN ( @pFilterP = 4 ) THEN 'WHERE T.Numerator &gt; (SELECT MAX(Numerator) &#8211; '+CAST( @pFilterV  AS VARCHAR)+' FROM TLA_Mov_Access) ' ELSE 'WHERE 1 = 1' END +</strong></p>
<p dir="ltr"><strong> CASE WHEN ( @pDoor &gt;= 0 ) THEN ' AND T.Code = '+CAST( @pDoor AS VARCHAR) ELSE ' ' END + </strong></p>
<p dir="ltr"><strong> CASE @pSort<br />
WHEN  0 THEN  ' ORDER BY T.Numerator DESC '<br />
WHEN  1 THEN  ' ORDER BY T.Priority DESC '<br />
WHEN  2 THEN  ' ORDER BY T.Severity DESC '<br />
WHEN  3 THEN  ' ORDER BY T.Sensitivty DESC ' END</strong></p>
<p dir="ltr"><strong>EXECUTE(@ActLinkString)</strong></p>
<p dir="ltr"><strong>GO<br />
SET QUOTED_IDENTIFIER OFF<br />
GO<br />
SET ANSI_NULLS ON<br />
GO</strong></p>
<p dir="ltr">
<p dir="ltr">And the Execution Plan of it will look something like this:</p>
<p dir="ltr"><a href="http://www.madeira.co.il/wp-content/uploads/2012/01/sp_TLA_Monitor_After_Execution_Plan.png" rel="wp-prettyPhoto[g3833]"><img style="padding-left: 0px;padding-right: 0px;padding-top: 0px;border-width: 0px" src="http://www.madeira.co.il/wp-content/uploads/2012/01/sp_TLA_Monitor_After_Execution_Plan_thumb.png" border="0" alt="sp_TLA_Monitor_After_Execution_Plan" width="244" height="184" /></a></p>
<p dir="ltr">
<p dir="ltr">Try it. You will be surprised to find out how many times it helps queries to be executed much faster. Where it looks like there’s no hope, this solution just might be the one!</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/help-the-optimizer-2/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>&#8235;Trace Flag 4135&#8236;</title>		<link>http://www.madeira.co.il/trace-flag-4135-2/</link>
		<comments>http://www.madeira.co.il/trace-flag-4135-2/#comments</comments>
		<pubDate>Wed, 04 Jan 2012 08:24:52 +0000</pubDate>
		<dc:creator>&#8235;דודי רוזנטל&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[כללי]]></category>
		<category><![CDATA[מקודמות]]></category>
		<category><![CDATA[SQL Server 2008 R2]]></category>
		<category><![CDATA[Trace Flags]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=3093</guid>
		<description><![CDATA[&#8235;What are Trace Flags, What's the purpose of Trace Flag 4135, when will we use it and how?&#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/3093.jpg&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<div style="margin: 0in 0in 10pt;line-height: 13pt" dir="ltr" class="MsoNormal">Before we start discussing Trace Flag 4135 and its affect and behavior, let’s try to make the definition of a Trace Flag clear:</div>
<div style="margin: 0in 0in 10pt;line-height: 13pt" dir="ltr" class="MsoNormal">A Trace Flag, according to Microsoft official definition at <a href="http://www.msdn.microsoft.com"><span>www.msdn.microsoft.com</span></a>, is used to temporarily set specific server characteristics or to switch off a particular behavior. Trace flags are frequently used to diagnose performance issues or to debug stored procedures or complex computer systems.</div>
<div style="margin: 0in 0in 10pt;line-height: 13pt" dir="ltr" class="MsoNormal">Flags can be set by using the DBCC TRACEON command or by using the “-T” option with the sqlservr command-line executable (later to be shown). After activated, trace flags will be in effect until you restart the server, or until you deactivate it by using the DBCC TRACEOFF command.</div>
<div style="margin: 0in 0in 10pt;line-height: 13pt" dir="ltr" class="MsoNormal">Before we get to the point of Trace Flag 4135 itself, two important things are to be noted:</div>
<div style="margin: 0in 0in 0pt 0.5in;line-height: 13pt;text-indent: -0.25in" dir="ltr" class="MsoListParagraphCxSpFirst"><span><span>1.<span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span><span dir="ltr"> </span>Some of the Trace Flags are undocumented. <span> </span>Nevertheless, they may be not less useful for administering and monitoring.</div>
<div style="margin: 0in 0in 10pt 0.5in;line-height: 13pt;text-indent: -0.25in" dir="ltr" class="MsoListParagraphCxSpLast"><span><span>2.<span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span><span dir="ltr"> </span>Trace flags behavior may not be supported in future releases of SQL Server.</div>
<div style="margin: 0in 0in 10pt;line-height: 13pt" dir="ltr" class="MsoNormal">So why worry, you ask? Well, let me answer you that by giving an example of a situation I was having. The days are now, no future releases of SQL Server are yet to be out there, but the problem sure existed:</div>
<div style="margin: 0in 0in 10pt;line-height: 13pt" dir="ltr" class="MsoNormal">While working on Microsoft SQL Server 2008 R2 and when trying to run a store procedure that inserts data into lots of temporary tables by using INSERT statement containing subqueries that referenced to the same temporary tables (WOW, that was a long one…),<span> </span>we received this error message:</div>
<div style="margin: 0in 0in 10pt;line-height: 13pt" dir="ltr" class="MsoNormal"><span style="line-height: 9pt"><span style="font-family: Courier New"><span>Msg 605, Level 21, State 3, Line 1 </span></span></span></div>
<div style="margin: 0in 0in 10pt;line-height: 13pt" dir="ltr" class="MsoNormal"><span style="line-height: 9pt"><span style="font-family: Courier New"><span>Attempt to fetch logical page (1:225) in database 2 failed. It belongs to allocation unit 281474980315136 not to 504403158513025024.</span></span></span></div>
<div style="margin: 0in 0in 10pt;line-height: 13pt" dir="ltr" class="MsoNormal">Executed <strong>DBCC CHECKDB</strong> and got the following results:</div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal"><span><span style="font-family: Courier New"><span>DBCC results for 'tempdb'.</span></span></span></div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal"><span><span style="font-family: Courier New"><span> </span></span></span></div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal"><span><span style="font-family: Courier New"><span>DBCC CHECKDB will not check SQL Server catalog or Service Broker consistency because a database snapshot could not be created or because WITH TABLOCK was specified.</span></span></span></div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal"><span><span style="font-family: Courier New"><span>CHECKDB found 0 allocation errors and 0 consistency errors in database 'tempdb'.</span></span></span></div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal"><span><span style="font-family: Courier New"><span>DBCC execution completed. If DBCC printed error messages, contact your system administrator.</span></span></span></div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal"><span><span style="font-family: Courier New"><span> </span></span></span></div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal"><span><span style="font-family: Courier New"> </span></span></div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal">It turns out that the corruption occurred in tempdb. We tried to restart the server in order to recreate it but got the same error when tried to run the sp. We also eliminated the thought that there’s not enough disk space for tempdb to grow by checking the amount of free space on the disk where it is located. That was also not the problem.</div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal">So what is the problem? Could it be a bug on SQL Server? Were we checking all that’s needed to eliminate the idea of a hardware failure?</div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal">Well, I’m not entirely sure that at my current level of experience I can answer the second question, but a quick search on the web introduced me with Trace Flag 4135.</div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal">We found that if the application/query makes frequent use of #temp tables in stored procedures, we may see errors 605 (like the one presented above) or 824 when trying to rerun the query:</div>
<div style="background: white;margin: 0in 0in 0pt;line-height: 18pt" dir="ltr" class="MsoNormal"><span><span style="font-family: Segoe UI"><span>Msg 824, Level 24, State 2, Line 1 </span></span></span></div>
<div style="background: white;margin: 0in 0in 10pt;line-height: 18pt" dir="ltr" class="MsoNormal"><span><span style="font-family: Segoe UI"><span>SQL Server detected a logical consistency-based I/O error: incorrect checksum (expected: 0&#215;50758180; actual: 0&#215;15658bfc). It occurred during a read of page (1:336) in database ID 2 at offset 0&#215;000000002a0000 in file 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\tempdb.mdf'. Additional messages in the SQL Server error log or system event log may provide more detail. This is a severe error condition that threatens database integrity and must be corrected immediately. Complete a full database consistency check (DBCC CHECKDB). This error can be caused by many factors; for more information, see SQL Server Books Online.</span></span></span></div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal"><span><span style="font-family: Segoe UI"><span> </span></span></span></div>
<div style="margin: 0in 0in 0pt;line-height: 18pt" dir="ltr" class="MsoNormal"><span>In some cases, you may also receive an error message that resembles the following: </span></div>
<div style="margin: 0in 0in 0pt;line-height: 18pt" dir="ltr" class="MsoNormal"><span> </span></div>
<div style="background: white;margin: 0in 0in 10pt;line-height: 18pt" dir="ltr" class="MsoNormal"><span><span style="font-family: Segoe UI"><span>Msg 601, Level 12, State 3, Procedure p<em>rocedure name</em>, Line <em>line number</em><br />
Could not continue scan with NOLOCK due to data movement.</span></span></span></div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal"><span><span style="font-family: Segoe UI"><span> </span></span></span></div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal">The cause might sound complicated, but it can be as simple as this query:</div>
<div dir="ltr">
<div>
<div>
<pre>INSERT #table1</pre>
<div><!--CRLF--></div>
<pre><span>SELECT</span> s.col1, s.col2</pre>
<div><!--CRLF--></div>
<pre><span>FROM</span> #table2 s <span>INNER</span> LOOP <span>JOIN</span> #table1 t</pre>
<div><!--CRLF--></div>
<pre><span>ON</span> s.col3 = t.col3</pre>
<div><!--CRLF--></div>
</div>
</div>
</div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal"><a href="http://www.support.microsoft.com"><span>www.support.microsoft.com</span></a> offers this solution to the problem:</div>
<div style="margin: 0in 0in 0pt;line-height: 18pt" dir="ltr" class="MsoNormal"><span><span style="font-family: Segoe UI"><span>The fix for this issue was first released in Cumulative Update 3. For more information about how to obtain this cumulative update package for SQL Server 2008, click the following article number to view the article in the Microsoft Knowledge Base:</span></span></span></div>
<div style="margin: 0in 0in 0pt;line-height: 18pt" dir="ltr" class="MsoNormal"><a href="http://support.microsoft.com/kb/960484"><span><span><span><span style="font-family: Segoe UI">960484</span></span></span></span></a><span style="font-family: Segoe UI"><span><span> </span></span><span><span>(http://support.microsoft.com/kb/960484/ ) </span></span><span><span>Cumulative update package 3 for SQL Server 2008</span></span></span></div>
<div style="margin: 0in 0in 0pt;line-height: 18pt" dir="ltr" class="MsoNormal"><span style="font-family: Segoe UI"><strong><span><span>Note</span></span></strong><span><span> Because the builds are cumulative, each new fix release contains all the hotfixes and all the security fixes that were included with the previous SQL Server 2008 fix release. We recommend that you consider applying the most recent fix release that contains this hotfix. For more information, click the following article number to view the article in the Microsoft Knowledge Base:</span></span></span></div>
<div style="margin: 0in 0in 10pt;line-height: 18pt" dir="ltr" class="MsoNormal"><a href="http://support.microsoft.com/kb/956909"><span><span><span><span style="font-family: Segoe UI">956909</span></span></span></span></a><span style="font-family: Segoe UI"><span><span> </span></span><span><span>(http://support.microsoft.com/kb/956909/ ) </span></span><span><span>The SQL Server 2008 builds that were released after SQL Server 2008 was released</span></span></span></div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal"><span><span style="font-family: Segoe UI"><span>After you install this cumulative update package, you have to enable trace flag 4135. To do this, you can add the <strong>-T4135</strong> startup parameter. Or, you can use the <strong>dbcc traceon(4135)</strong> statement for a specific session.</span></span></span></div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal">Understanding this bug also helped us understand why we didn’t get any corruption indication from DBCC CHECKDB &#8211; The objects that were used by the stored procedure were temporary.</div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal">So now we know the problem, we know the cause and we know the solution.</div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal">Enabling (and disabling) a Trace Flag is an easy thing to do:</div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal">You can use the DBCC TRACEON command to turn on the trace flag’ like this: DBCC TRACEON (4135).</div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal">You can turn off the trace flag by using the DBCC TRACEOFF command: DBCC TRACEOFF (4135).</div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal">(Note that you can also use the DBCC TRACESTATUS command to get the status information for the particular trace flag currently turned on. In our case, DBCC TRACESTATUS (4135))</div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal">To get the status information for all trace flags currently turned on, you can use -1 for trace# (DBCC TRACESTATUS (-1))</div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal">There’s another way of setting Trace Flags which is done via SQL Server Configuration Manager. In order to do it this way, simply follow these quick steps:</div>
<div style="margin: 0in 0in 0pt 0.5in;line-height: 13pt;text-indent: -0.25in" dir="ltr" class="MsoListParagraphCxSpFirst"><span style="line-height: 14pt"><span><span>1)</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span><span dir="ltr"> </span><span style="line-height: 14pt"><span>Open SQL Server Configuration Manager from Microsoft SQL Server 2008 R2-&gt; Configuration Tools</span></span></div>
<div style="margin: 0in 0in 0pt 0.5in;line-height: 13pt;text-indent: -0.25in" dir="ltr" class="MsoListParagraphCxSpMiddle"><span style="line-height: 14pt"><span><span>2)</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span><span dir="ltr"> </span><span style="line-height: 14pt"><span>Right click to open properties on SQL Server you are working on</span></span></div>
<div style="margin: 0in 0in 10pt 0.5in;line-height: 13pt;text-indent: -0.25in" dir="ltr" class="MsoListParagraphCxSpLast"><span style="line-height: 14pt"><span><span>3)</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span><span style="line-height: 14pt"><span>In Advanced panel in Startup Parameters, add <strong>;-T1435 </strong>in the end of parameter.</span></span></div>
<div style="margin: 0in 0in 10pt 0.5in;line-height: 13pt;text-indent: -0.25in" dir="ltr" class="MsoListParagraphCxSpLast"><span style="line-height: 14pt"><a href="http://www.madeira.co.il/wp-content/uploads/2012/01/22222223.jpg" rel="wp-prettyPhoto[g3093]"><img style="border-width: 0px;padding-top: 0px;padding-right: 0px;padding-left: 0px" border="0" alt="2222222" src="http://www.madeira.co.il/wp-content/uploads/2012/01/2222222_thumb3.jpg" width="475" height="300"></a></span></div>
<div style="margin: 0in 0in 0pt 0.5in;line-height: 13pt;text-indent: -0.25in" dir="ltr" class="MsoListParagraphCxSpFirst"><span style="line-height: 14pt"><span><span>4)</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span><span style="line-height: 14pt"><span>Click Apply and Ok. </span></span></div>
<div style="margin: 0in 0in 10pt 0.5in;line-height: 13pt;text-indent: -0.25in" dir="ltr" class="MsoListParagraphCxSpLast"><span style="line-height: 14pt"><span><span>5)</span><span style="line-height: normal"><span style="font-family: Times New Roman"><span> </span></span></span></span></span><span style="line-height: 14pt"><span>Right click to restart SQL Server.</span></span></div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal">And you’re good to go…</div>
<div style="margin: 0in 0in 0pt;line-height: normal" dir="ltr" class="MsoNormal">(Special thanks to Guy Glantser and Eitan Blumin for their professional help)</div>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/trace-flag-4135-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

