<?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&#8236;</title>	<atom:link href="http://www.madeira.co.il/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;Israeli SQL Server User Group Meeting #119&#8236;</title>		<link>http://www.madeira.co.il/israeli-sql-server-user-group-meeting-119-2/</link>
		<comments>http://www.madeira.co.il/israeli-sql-server-user-group-meeting-119-2/#comments</comments>
		<pubDate>Sat, 19 May 2012 09:02:28 +0000</pubDate>
		<dc:creator>&#8235;Madeira&#8236;</dc:creator>				<category><![CDATA[אל תציג בבלוגים]]></category>
		<category><![CDATA[חדשות ואירועים]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=5015</guid>
		<description><![CDATA[&#8235;When: Monday, June 4th, 2012
Where: Dekel Hall, Microsoft Offices, Hapnina 2 St., Raa’nana
Schedule:

17:30 – 17:45 Gathering
17:45 – 18:45 First <a href="http://www.madeira.co.il/israeli-sql-server-user-group-meeting-119-2/"><strong>לסיפור המלא...</strong></a>&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p style="direction: ltr;">When: Monday, June 4th, 2012</p>
<p style="direction: ltr;">Where: Dekel Hall, Microsoft Offices, Hapnina 2 St., Raa’nana</p>
<p style="direction: ltr;">Schedule:</p>
<ul>
<li style="direction: ltr;">17:30 – 17:45 Gathering</li>
<li style="direction: ltr;">17:45 – 18:45 First Session</li>
<li style="direction: ltr;">18:45 – 19:00 Q&amp;A and Job Search</li>
<li style="direction: ltr;">19:00 – 19:15 Break</li>
<li style="direction: ltr;">19:15 – 20:15 Second Session</li>
</ul>
<p style="direction: ltr;">Agenda:</p>
<p style="direction: ltr;">
<div id="_mcePaste" style="direction: ltr;"><span style="text-decoration: underline;">Parallel Query Processing for Pros</span> (Noam Brezis, <a href="http://www.madeira.co.il/" target="_blank">Madeira</a>)</div>
<div id="_mcePaste" style="direction: ltr;">SQL Server has the ability to spread query processing across several parallel threads. This ability can dramatically improve query performance. However, in some cases, this ability is devastating for our system. In this session we will dig deep into understanding parallel processing and the different parameters that affect it. We will also understand how we should configure our system in order to perform optimally.</div>
<div id="_mcePaste" style="direction: ltr;"><span style="text-decoration: underline;">Big Data and New Challenges for DBAs</span> (Michael Naumov, <a href="http://www.liveperson.com/" target="_blank">LivePerson</a>)</div>
<div id="_mcePaste" style="direction: ltr;">Hadoop has become a popular platform for managing large datasets of structured and unstructured data. It does not replace existing infrastructures, but instead augments them. Most companies will still use relational databases for transactional processing and low-latency queries, but can benefit from Hadoop for reporting, machine learning or ETL. This session will cover:</div>
<div id="_mcePaste" style="direction: ltr;">· What is Hadoop and why do I care?</div>
<div id="_mcePaste" style="direction: ltr;">· What do people do with Hadoop?</div>
<div id="_mcePaste" style="direction: ltr;">· How can SQL Server DBAs add Hadoop to their architecture?</div>
<p style="direction: ltr;">To register for the event in order for Microsoft to arrange parking and food:<a href="http://isug119.eventbrite.com/">http://isug119.eventbrite.com</a></p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/israeli-sql-server-user-group-meeting-119-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8235;Suggestion: &quot;USE&quot; keyword with Linked Servers&#8236;</title>		<link>http://www.madeira.co.il/suggestion-use-keyword-with-linked-servers/</link>
		<comments>http://www.madeira.co.il/suggestion-use-keyword-with-linked-servers/#comments</comments>
		<pubDate>Wed, 16 May 2012 12:40:50 +0000</pubDate>
		<dc:creator>&#8235;איתן בלומין&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[כללי]]></category>
		<category><![CDATA[Linked Server]]></category>
		<category><![CDATA[remote queries]]></category>
		<category><![CDATA[sp_executesql]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/suggestion-use-keyword-with-linked-servers/</guid>
		<description><![CDATA[&#8235;I’ve posted a suggestion on Microsoft Connect for a feature allowing you to connect to databases on linked servers with the "USE" keyword. Technically, it would be like "opening a new session" from one SQL Server to another.&#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/5011.png&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p style="text-align: left;" dir="ltr">Using special functions for querying and executing commands at linked servers is not very convenient, and sometimes even very problematic.<br />
For example, I need to execute some code on several linked servers but the linked server names are dynamic (queried from a table for example).<br />
On each such linked server I need to execute a dynamic query.<br />
However because I must use the <strong>EXECUTE() AT [...]</strong> command I'm exposing a big hole here for SQL Injection.<br />
I found myself wishing I could use<strong> sp_executesql</strong> with linked servers but there's no such way as far as I know.</p>
<p style="text-align: left;" dir="ltr">For example, this is what I’m forced to do:</p>
<div id="codeSnippetWrapper" style="line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; text-align: left; padding: 4px; border: 1px solid silver;">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">CREATE</span> <span style="color: #0000ff;">PROCEDURE</span> BackupOnRemoteServer</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    @<span style="color: #0000ff;">Path</span> NVARCHAR(4000)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">AS</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">DECLARE</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        @<span style="color: #0000ff;">Statement</span>    NVARCHAR(<span style="color: #0000ff;">MAX</span>)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">SET</span> @<span style="color: #0000ff;">Statement</span> = N<span style="color: #006080;">'    </span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    BACKUP DATABASE [MyDB]</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    TO DISK = '<span style="color: #006080;">''</span> + @<span style="color: #0000ff;">Path</span> + <span style="color: #006080;">''</span><span style="color: #006080;">''</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">EXECUTE</span> (@<span style="color: #0000ff;">Statement</span>) <span style="color: #0000ff;">AT</span> [SQLINS1]</pre>
<p><!--CRLF--></p>
</div>
</div>
<p style="text-align: left;" dir="ltr">I’ve <a href="https://connect.microsoft.com/SQLServer/feedback/details/742327/use-keyword-with-linked-servers#details" target="_blank">posted a suggestion on Microsoft Connect</a> for the following feature:</p>
<p style="text-align: left;" dir="ltr">It would be very helpful if we were able to connect to databases on linked servers with the &quot;<strong>USE</strong>&quot; keyword.</p>
<p style="text-align: left;">Technically, it would be like &quot;opening a new session&quot; from one SQL Server to another.</p>
<p style="text-align: left;">The sample I’ve posted above could be executed like this:</p>
<div id="codeSnippetWrapper" style="line-height: 12pt; background-color: #f4f4f4; margin-top: 20px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow-x: auto; overflow-y: auto; cursor: text; text-align: left; padding: 4px; border: 1px solid silver;">
<div id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">CREATE</span> <span style="color: #0000ff;">PROCEDURE</span> BackupOnRemoteServer</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    @<span style="color: #0000ff;">Path</span> NVARCHAR(4000)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">AS</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">DECLARE</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        @ServerName <span style="color: #0000ff;">VARCHAR</span>(200),</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        @<span style="color: #0000ff;">Statement</span>    NVARCHAR(<span style="color: #0000ff;">MAX</span>),</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">        @<span style="color: #0000ff;">Parameters</span> NVARCHAR(<span style="color: #0000ff;">MAX</span>)</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">SET</span> @ServerName = <span style="color: #006080;">'SQLINS1'</span>;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">SET</span> @<span style="color: #0000ff;">Parameters</span> = N<span style="color: #006080;">'@Path NVARCHAR(4000)'</span>;</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">SET</span> @<span style="color: #0000ff;">Statement</span> = N<span style="color: #006080;">'</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    USE [' + @ServerName + N<span style="color: #006080;">'].[MyDB]</span></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    BACKUP DATABASE [MyDB]</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    TO DISK = @Path'</pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"></pre>
<p><!--CRLF--></p>
<pre style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;">    <span style="color: #0000ff;">EXEC</span> sp_executesql @<span style="color: #0000ff;">Statement</span>, @<span style="color: #0000ff;">Parameters</span>, @Path</pre>
<p><!--CRLF--></p>
</div>
</div>
<p style="text-align: left;" dir="ltr">Once the USE command was executed, theoretically it's like I'm connected directly to the linked server using a query window.</p>
<p style="text-align: left;" dir="ltr">Security could be improved versus SQL Injection (use <strong>sp_executesql </strong>with parameters instead of <strong>EXECUTE() AT</strong>) like the example above.</p>
<p style="text-align: left;">Usability could be improved due to cleaner and more readable code.</p>
<p style="text-align: left;" dir="ltr">If you like this idea, please visit this page and vote for it so it’ll get higher priority on Microsoft’s to-do list:</p>
<p style="text-align: left;" dir="ltr"><a href="https://connect.microsoft.com/SQLServer/feedback/details/742327/use-keyword-with-linked-servers#details">https://connect.microsoft.com/SQLServer/feedback/details/742327/use-keyword-with-linked-servers#details</a></p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/suggestion-use-keyword-with-linked-servers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8235;SQL Server 2012 Team vs 100&#8236;</title>		<link>http://www.madeira.co.il/sql-server-2012-team-vs-100/</link>
		<comments>http://www.madeira.co.il/sql-server-2012-team-vs-100/#comments</comments>
		<pubDate>Wed, 16 May 2012 10:11:30 +0000</pubDate>
		<dc:creator>&#8235;Madeira&#8236;</dc:creator>				<category><![CDATA[אל תציג בבלוגים]]></category>
		<category><![CDATA[חדשות ואירועים]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=5003</guid>
		<description><![CDATA[&#8235;If you love SQL Server 2012, if you already know a thing or two about <a href="http://www.madeira.co.il/sql-server-2012-team-vs-100/"><strong>לסיפור המלא...</strong></a>&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p style="direction: ltr;">If you love SQL Server 2012, if you already know a thing or two about it and you want to learn more, this is for you!</p>
<p style="direction: ltr;">Press <a href="http://view.email.microsoftemail.com/?j=fe9315787160017d73&amp;m=fecb157674650c7e&amp;ls=fe4b1d79726d017c761d&amp;l=fec21c767365017e&amp;s=fe25177776650d7c7d1274&amp;jb=ff991575&amp;ju=fe581773706c027f7d10&amp;r=0" target="_blank">here</a> to learn more about NAYA Technologies and Microsoft's SQL Server 2012 Team vs 100.</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/sql-server-2012-team-vs-100/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8235;!Madeira is celebrating 5 years&#8236;</title>		<link>http://www.madeira.co.il/madeira-is-celebrating-5-years/</link>
		<comments>http://www.madeira.co.il/madeira-is-celebrating-5-years/#comments</comments>
		<pubDate>Wed, 16 May 2012 08:47:15 +0000</pubDate>
		<dc:creator>&#8235;Madeira&#8236;</dc:creator>				<category><![CDATA[אל תציג בבלוגים]]></category>
		<category><![CDATA[חדשות ואירועים]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=4997</guid>
		<description><![CDATA[&#8235;Madeira is celebrating 5 years and the best way to party was a 2 days, <a href="http://www.madeira.co.il/madeira-is-celebrating-5-years/"><strong>לסיפור המלא...</strong></a>&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p style="direction: ltr;">Madeira is celebrating 5 years and the best way to party was a 2 days, better-than-ever, company trip.</p>
<p style="direction: ltr;">You can watch selected pictures from our trip at our Facebook company page <a href="http://www.facebook.com/media/set/?set=a.10151636710225302.851519.298955530301&amp;type=3" target="_blank">here</a>.</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/madeira-is-celebrating-5-years/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>&#8235;Service Broker Sample: Multi-Threading&#8236;</title>		<link>http://www.madeira.co.il/service-broker-sample-multi-threading/</link>
		<comments>http://www.madeira.co.il/service-broker-sample-multi-threading/#comments</comments>
		<pubDate>Mon, 07 May 2012 09:01:00 +0000</pubDate>
		<dc:creator>&#8235;איתן בלומין&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[כללי]]></category>
		<category><![CDATA[מקודמות]]></category>
		<category><![CDATA[Fast]]></category>
		<category><![CDATA[Multi-Threading]]></category>
		<category><![CDATA[Parallel Queries]]></category>
		<category><![CDATA[Parallel Querying]]></category>
		<category><![CDATA[Samples]]></category>
		<category><![CDATA[Service Broker]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=4961</guid>
		<description><![CDATA[&#8235;Here's a cool sample of how you can use Service Broker to your benefit by implementing multi-threading in SQL Server&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p><img src='http://www.madeira.co.il/wp-content/plugins/simple-post-thumbnails/timthumb.php?src=/wp-content/thumbnails/4961.jpg&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<h2 dir="ltr">Introduction</h2>
<p dir="ltr">Service Broker is a cool new feature since SQL Server 2005. Yes, SQL 2012 just came out so it’s hardly “new” anymore. But in reality, many DBAs and DB users are still not aware of this feature, or don’t know how to utilize it, or are afraid to do so because they’re not familiar with it.</p>
<p dir="ltr">Using this short series of posts, I’m hoping to ease the minds of such people</p>
<p dir="ltr">In short, Service Broker is an asynchronous queuing engine working inside SQL Server. Using this engine we can send and receive binary or XML messages between queues. When a message is inserted in a queue, Service Broker executes an “activation procedure” that you specified, which is supposed to handle messages from that queue.</p>
<p dir="ltr">Since we have a lot to cover in our sample, I won’t go into too much detail introducing Service Broker. But Microsoft has an excellent and detailed chapter about Service Broker available right here: <a href="http://msdn.microsoft.com/en-us/library/bb522893.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/bb522893.aspx</a></p>
<h2 dir="ltr">Sample 1: Multi-Threading</h2>
<p dir="ltr">I managed to encounter several times a scenario where an application needed to execute several queries independent of each other, and then group them together somehow. However, each of these queries takes a while to execute.   <br />One might wonder: If only we could use some kind of multi-threading mechanism to execute them in parallel, and then the total running time will be of the slowest query only and not the total of them.</p>
<p dir="ltr">Well, it just so happens that our first sample will show how you can implement “multi-threading” in SQL Server using Service Broker queues!</p>
<h3 dir="ltr">The Design</h3>
<p dir="ltr">First, I’ll explain the general idea of the design:</p>
<p dir="ltr">Since Service Broker only works with binary or XML messages, we’ll use XML to send a “<strong>query request</strong>”, and wait for a “<strong>query response</strong>”.    <br />Specifically, we’ll send a message containing some kind of a dynamic query. But that dynamic query must return its result in XML form using an output parameter. Then, Service Broker will execute the query, retrieve the result from the output parameter, and send the result back as the response.    <br />After that, it’s up to us to gather all the responses together and do whatever it is we wanted to do with it.</p>
<p dir="ltr">Also, for the sake of our sample, we’ll need to simulate long execution times. We’ll do this simply by adding a WAITFOR DELAY command into our dynamic queries which will cause the query to wait for a random number of seconds (between 10 and 30).</p>
<h3 dir="ltr">The Scripts</h3>
<p dir="ltr">Available below is a ZIP file with the two scripts we’ll use:   <br />One is the installation script, responsible for creating all the objects we require – the database, the service broker procedures, endpoints and queues. Everything you need to implement multi-threading in SQL Server.    <br />The other is a sample of how to use this mechanism, using simple queries and a WAITFOR DELAY command to simulate long execution times.</p>
<p dir="ltr"><a href="http://www.madeira.co.il/?dl_name=Service_Broker_Multi-Threading_v3.zip" target="_blank"><img style="margin: 0px 0px 10px" class="alignnone" title="Download" alt="" src="http://www.madeira.co.il/wp-content/uploads/2009/12/zip.png" width="128" height="128" /></a></p>
<p dir="ltr">The scripts are commented to help you understand what’s going on. But we’ll go over them bit by bit just in case.</p>
<h3 dir="ltr">The Installation Script</h3>
<p dir="ltr">In order to explain the installation script, I’ll list all the objects that we’re creating and explain the purpose of each object. More detailed information about the implementation can be found in the script itself.</p>
<p dir="ltr">
<ul dir="ltr">
<li>
<div><strong>SB_PQ_Test</strong>:          <br />That’s the database we’ll be using to create all the necessary objects.</div>
</li>
<li>
<div><strong>SB_PQ_ServiceBrokerLogs</strong>:          <br />This is a logging table which will be used to log the start and end of each query execution, and any errors that may occur.</div>
</li>
<li>
<div><strong>SB_PQ_ExecuteDynamicQuery</strong>:          <br />This is the procedure which will actually execute the dynamic queries. It builds the dynamic command, uses <a href="http://msdn.microsoft.com/en-us/library/ms188001.aspx" target="_blank">sp_executesql</a> to execute it, and returns the result as an output parameter. The name of the output parameter itself can be dynamic.</div>
</li>
<li>
<div><strong>SB_PQ_HandleQueue</strong>:          <br />This is the activation procedure that Service Broker executes when it creates a “reader instance”. Or in other words, when there’s something in the request queue.          <br />This procedure is the most important part because it’s responsible for retrieving messages from the request queue, execute the query (using SB_PQ_ExecuteDynamicQuery), and handle any errors that may occur in-between.</div>
</li>
<li>
<div><strong>SB_PQ_Request_Queue</strong>:</div>
</li>
<li>
<div>This is the queue where all query requests will be waiting to be processed. Its activation procedure is SB_PQ_HandleQueue.</div>
</li>
<li>
<div><strong>SB_PQ_Response_Queue</strong>:          <br />This is the queue into which query responses will be sent. Its doesn't have an activation procedure because query response extraction is done manually.</div>
</li>
<li>
<div><strong>[//SB_PQ/Message]</strong>:          <br />This is a simple message type of “Well Formed XML”. We’ll be using it to verify our messages.</div>
</li>
<li>
<div><strong>[//SB_PQ/Contract]</strong>:          <br />This is a contract defining that both target and initiator must send messages of type [//SB_PQ/Message].</div>
</li>
<li>
<div><strong>[//SB_PQ/ProcessReceivingService]</strong>:          <br />This is the service endpoint working as the “address” of request queries. We will be sending messages <em>into </em>this service. The queue it’ll be using to receive messages is SB_PQ_Request_Queue.</div>
</li>
<li>
<div><strong>[//SB_PQ/ProcessStartingService]</strong>:          <br />This is the service endpoint working as the “address” of response queries.          <br />We will be sending messages <em>from</em> this service. The queue it’ll be using to receive messages is SB_PQ_Response_Queue.</div>
</li>
<li>
<div><strong>SB_PQ_Start_Query</strong>:          <br />This is the public procedure that will used for starting an execution thread, by sending a request query to [//SB_PQ/ProcessStartingService].          <br />You can think of it as an API simplifying our use of all the above objects.          <br />It receives as parameters the SQL command that should be executed, the name of the output parameter (without the @), and the conversation group ID.</div>
</li>
<li>
<div><strong>SB_PQ_Get_Response_One</strong>:          <br />This is the public procedure that will be used to retrieve the results of our queries. It receives as a parameter the conversation group ID to be used, and returns the first result it finds as an output parameter. You may also think of it as part of the API simplifying our use of the above objects.</div>
</li>
</ul>
<p dir="ltr">Whew, that was a long list! Please note that since Service Broker works asynchronously in the background, error handling is extremely important. Note all the error handling, especially in the <strong>SB_PQ_HandleQueue</strong> procedure.</p>
<h3 dir="ltr">The Sample Script</h3>
<p dir="ltr">Now we can really get down to business. I’ll go over the important parts in the script and explain each of them:</p>
<p dir="ltr">
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #008000">-- random workload simulation settings</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">SET</span> @MinDelay = 10</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">SET</span> @MaxDelay = 30</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">SET</span> @ConvGroup = NEWID();    <span style="color: #008000">-- the messages will be grouped in a specific conversation group</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">SET</span> @NumOfSegments = 3;        <span style="color: #008000">-- number of &quot;threads&quot; to use</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #008000">-- create several segments</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">SET</span> @Segment = 1;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">WHILE</span> @Segment &lt;= @NumOfSegments</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">BEGIN</pre>
<p><!--CRLF--></div>
</p></div>
<p>In our sample, we create several “segments” (or threads) where each of them will have a random delay between 10 and 30 seconds, and execute a simple query returning its details. In the above script we define the minimum and maximum delay in seconds, set the conversation group ID by which our threads will be grouped, total number of threads (3 in our case), and we start looping per each of the threads.</p>
<p dir="ltr">
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #008000">-- random delay between @MinDelay and @MaxDelay seconds to simulate long execution time</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">SET</span> @Delay = <span style="color: #006080">'00:00:'</span> + <span style="color: #0000ff">CONVERT</span>(<span style="color: #0000ff">varchar</span>(8), ROUND(RAND() * (@MaxDelay - @MinDelay),0) + @MinDelay)</pre>
<p><!--CRLF--></div>
</p></div>
<p>The above script simply prepares the delay representation by generating a random number of seconds as we defined earlier (between 10 and 30).</p>
<p dir="ltr">
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #008000">-- build our dynamic SQL command. note the use of XML as the result.</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">SET</span> @<span style="color: #0000ff">SQL</span> = N<span style="color: #006080">'</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">WAITFOR DELAY '</span><span style="color: #006080">''</span> + @Delay + <span style="color: #006080">''</span><span style="color: #006080">';</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">SET @SB_PQ_Result = </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">(</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    SELECT</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        Segment = '</span> + <span style="color: #0000ff">CONVERT</span>(nvarchar(<span style="color: #0000ff">max</span>), @Segment) + N<span style="color: #006080">',</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        Delay = '</span><span style="color: #006080">''</span> + @Delay + N<span style="color: #006080">''</span><span style="color: #006080">',</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        StartDate = GETDATE(),</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        Name = QUOTENAME(name),</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        object_id, type, modify_date</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    FROM</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        SB_PQ_Test.sys.tables AS Tab</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    FOR XML AUTO, ELEMENTS</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">'</span>;</pre>
<p><!--CRLF--></div>
</p></div>
<p>In the script above we create the dynamic SQL command which will be executed by the current thread. Note the use of <strong>XML AUTO, ELEMENTS </strong>to format the results in XML and put them in the output parameter.</p>
<p dir="ltr">Also note the use of <strong>WAITFOR DELAY </strong>at the start to simulate long execution time.</p>
<p dir="ltr">
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #008000">-- Send request to queue</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">EXEC</span> SB_PQ_Start_Query @<span style="color: #0000ff">SQL</span>, @OutputParam, @ConvGroup;</pre>
<p><!--CRLF--></div>
</p></div>
<p>The above script executes the API procedure <strong>SB_PQ_Start_Query</strong> with the SQL command we prepared, the name of the output parameter, and the conversation group ID. This will put the message in the request queue and continue executing without waiting for a response (the magic of asynchronous work).</p>
<p dir="ltr">
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #008000">-- increment segment index</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">SET</span> @Segment = @Segment + 1;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">END</span></pre>
<p><!--CRLF--></div>
</p></div>
<p>The above script increases the thread index so we can continue to the next one.</p>
<p dir="ltr">
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #008000">-- init final result</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">DECLARE</span> @TotalResult XML;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">SET</span> @TotalResult = <span style="color: #006080">'&lt;Tables&gt; &lt;/Tables&gt;'</span></pre>
<p><!--CRLF--></div>
</p></div>
<p>The above script initializes an XML variable called @TotalResult which will contain all the results we’re about to receive.</p>
<p dir="ltr">
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #008000">-- count based on number of segments that we created earlier</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">SET</span> @Segment = 1;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">WHILE</span> @Segment &lt;= @NumOfSegments</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">BEGIN</pre>
<p><!--CRLF--></div>
</p></div>
<p>In this part of the script we’re resetting the @Segment index and looping again based on the number of threads in @NumOfSegments.</p>
<p dir="ltr">
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #008000">-- Get segment from response queue</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">EXEC</span> SB_PQ_Get_Response_One @ConvGroup, @CurrentResult <span style="color: #0000ff">OUTPUT</span></pre>
<p><!--CRLF--></div>
</p></div>
<p>In the above script, we use the API procedure SB_PQ_Get_Response_One to retrieve a single result from any of the threads (whichever finished executing first). The result will be returned in the output variable @CurrentResult.</p>
<p dir="ltr">
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #008000">-- insert into TotalResults using XML DML (syntax for SQL2008 and newer)</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">SET</span> @TotalResult.<span style="color: #0000ff">modify</span>(<span style="color: #006080">'</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">insert sql:variable(&quot;@CurrentResult&quot;)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">into (/Tables)[1] '</span>);</pre>
<p><!--CRLF--></div>
</p></div>
<p>The above script uses XML DML language with reference to the @CurrentResult variable to insert its contents into the @TotalResult variable.</p>
<p dir="ltr">Please note that this method of “inserting” a SQL variable into an XML document is <strong>SQL 2008 </strong>syntax and won’t work in older versions.</p>
<p dir="ltr">If you have SQL Server 2005, you will need to work a little harder to collect the results (for example, declaring @TotalResult as nvarchar(4000) without a closing tag, concatenating the results one-by-one, and only at the end concatenate the closing tag as well).</p>
<p dir="ltr">
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #008000">-- increment segment index</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">SET</span> @Segment = @Segment + 1;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">END</pre>
<p><!--CRLF--></div>
</p></div>
<p>The above script increases the thread index so we can continue to the next one.</p>
<p dir="ltr">
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #008000">-- return final result (as XML)</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">SELECT</span> @TotalResult.query(<span style="color: #006080">'.'</span>) <span style="color: #0000ff">AS</span> FinalResult</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #008000">-- return final result (as relational table)</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">SELECT</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    Segment        = T.XRecord.query(<span style="color: #006080">'.'</span>).<span style="color: #0000ff">value</span>(<span style="color: #006080">'(/Tab/Segment)[1]'</span>,<span style="color: #006080">'varchar(100)'</span>),</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    Delay        = T.XRecord.query(<span style="color: #006080">'.'</span>).<span style="color: #0000ff">value</span>(<span style="color: #006080">'(/Tab/Delay)[1]'</span>,<span style="color: #006080">'varchar(8)'</span>),</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    StartDate    = T.XRecord.query(<span style="color: #006080">'.'</span>).<span style="color: #0000ff">value</span>(<span style="color: #006080">'(/Tab/StartDate)[1]'</span>,<span style="color: #006080">'datetime'</span>)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">FROM</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    @TotalResult.nodes(<span style="color: #006080">'/Tables/Tab'</span>) <span style="color: #0000ff">AS</span> T(XRecord)</pre>
<p><!--CRLF--></div>
</p></div>
<p>The above script shows two methods of how you can return your final data.</p>
<p dir="ltr">The first method returns the simple XML representation of @TotalResult.</p>
<p dir="ltr">The second method shows how you can break down each element in @TotalResult (using the <em>nodes()</em> method) and display the values of each element (using the <em>value()</em> method).</p>
<p dir="ltr">
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #008000">-- check the SB logs to see how many unique sessions executed our requests</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">DECLARE</span> @NumOfSessions <span style="color: #0000ff">INT</span>;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">SELECT</span> @NumOfSessions = <span style="color: #0000ff">COUNT</span>(<span style="color: #0000ff">DISTINCT</span> SPID)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">FROM</span> SB_PQ_ServiceBrokerLogs</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">WHERE</span> LogDate &gt;= @StartTime</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">PRINT</span> <span style="color: #0000ff">CONVERT</span>(nvarchar(100),@NumOfSessions) + <span style="color: #006080">' unique sessions participated in execution'</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">&#160;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">SELECT</span> *</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">FROM</span> SB_PQ_ServiceBrokerLogs</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">WHERE</span> LogDate &gt;= @StartTime</pre>
<p><!--CRLF--></div>
</p></div>
<p>The above script is a sort of “debug” script which will display information from our Service Broker logging table, specifically showing how many unique sessions participated in execution since the start of the script, and displays the contents of the logging table from that period of time.</p>
<h3 dir="ltr">Execution Delay</h3>
<p dir="ltr">It takes Service Broker several seconds before activating more than one reader instance. However, because our activation procedure continues waiting for new requests even after finishing an execution, once a reader instance is activated it’ll stay active until it’s manually shut down.</p>
<p dir="ltr">As a result of this behavior, your first execution in multi-threading may not be as fast as you’d expect. But from the second try onwards it shouldn’t take longer than the longest running query.</p>
<h3 dir="ltr">Customizing for Your Version</h3>
<p dir="ltr">The above sample script is a very crude sample of how to use the API we created using the installation script. But this architecture can be used for just about anything you want to execute multi-threaded.</p>
<p dir="ltr">Your own implementation will probably be much different.</p>
<p dir="ltr">You may want to perform a query on a large range of records, and prefer to break down that range to several sub-ranges, where each sub-range will be a separate thread / segment.</p>
<p dir="ltr">Maybe you have several different queries and you need to do different things with them, so you’ll have several different executions of the API procedures (you’d need to think how to identify each query though, perhaps by adding that information in the XML somewhere).</p>
<p dir="ltr">Maybe you’re not even performing queries. Maybe it’s some maintenance tasks you wish to perform simultaneously (you still should expect a response of some kind, for example a message notifying of success or failure).</p>
<p dir="ltr">In any case, you’ll need to follow these rules:</p>
<p dir="ltr">
<ul dir="ltr">
<li>
<div>Your SQL command must make use of an output parameter of type XML. You don’t need to declare it inside your command, but simply put into it the XML result you want to expect.</div>
</li>
<li>
<div>Take note of the name of the output parameter you’re using and provide it during the execution of the <strong>SB_PQ_Start_Query </strong>procedure.</div>
</li>
<li>
<div>If there’s a chance other sessions will be using this API at the same time, it’s extremely important to make use of the Conversation Group ID parameter so you’d have a way of separating between the data of different sessions.</div>
</li>
<li>
<div>In any case, the thread will try to return a response even if there was an error in your query (NULL will be returned). So in order to prevent clogging on the response queue, don’t forget to use the <strong>SB_PQ_Get_Response_One </strong>procedure to take them out of the queue.</div>
</li>
</ul>
<h3 dir="ltr">Conclusion</h3>
<p dir="ltr">This was but one example of how you can use Service Broker to improve your work with SQL Server. I’ll be working on new examples so check back soon!</p>
<p dir="ltr">I hope my post wasn’t too complicated or confusing. But if you have any questions, please post them in the comment section below, and I’d be happy to help!</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/service-broker-sample-multi-threading/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8235;SQL Explore&#8236;</title>		<link>http://www.madeira.co.il/sql-explore-2/</link>
		<comments>http://www.madeira.co.il/sql-explore-2/#comments</comments>
		<pubDate>Wed, 02 May 2012 13:10:32 +0000</pubDate>
		<dc:creator>&#8235;Madeira&#8236;</dc:creator>				<category><![CDATA[אל תציג בבלוגים]]></category>
		<category><![CDATA[חדשות ואירועים]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=4956</guid>
		<description><![CDATA[&#8235;Get ready for the biggest SQL Server event of the year!

Microsoft in cooperation with John <a href="http://www.madeira.co.il/sql-explore-2/"><strong>לסיפור המלא...</strong></a>&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p style="direction: ltr;">Get ready for the <a href="http://www.microsoft.com/israel/sql-explore/content/Lectures.aspx">biggest SQL Server event of the year</a>!</p>
<p style="direction: ltr;">
<p style="direction: ltr;">Microsoft in cooperation with John Bryce – Hi-Tech College are conducting a two-day advanced professional training event with lots of professional seminars for DBAs, programmers and BI professionals. This is the place and time to take your skills to a higher level and learn about all the new features in SQL Server 2012.</p>
<p style="direction: ltr;">
<p style="direction: ltr;">When: Sunday-Monday, 10-11/06/2012, 09:00-16:30</p>
<p style="direction: ltr;">Where: Daniel Hotel, Herzelia</p>
<p style="direction: ltr;">
<p style="direction: ltr;"><a href="http://www.madeira.co.il/author/guyglantser/">Guy Glantser</a> is presenting a seminar on 10/06 – <a href="http://www.microsoft.com/israel/sql-explore/content/Seminars/11063.aspx">SQL Server for Developers</a>.</p>
<p style="direction: ltr;">This seminar is intended for development DBAs as well as application developers, and it covers some of the more interesting and practical aspects of development in SQL Server, including some of the new features in SQL Server 2012.</p>
<p style="direction: ltr;">
<p style="direction: ltr;">If you want to register for Guy's seminar or for any other seminar, contact us for a lowered price.</p>
<p style="direction: ltr;">Don’t wait for the last minute. Reserve your place now!</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/sql-explore-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>&#8235;Be What&#039;s Next&#8236;</title>		<link>http://www.madeira.co.il/be-whats-next/</link>
		<comments>http://www.madeira.co.il/be-whats-next/#comments</comments>
		<pubDate>Wed, 02 May 2012 12:56:21 +0000</pubDate>
		<dc:creator>&#8235;Madeira&#8236;</dc:creator>				<category><![CDATA[אל תציג בבלוגים]]></category>
		<category><![CDATA[חדשות ואירועים]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=4948</guid>
		<description><![CDATA[&#8235;Microsoft’s annual event in the IT field was held last Monday (23.4.2012) at Ganei Hata’arukha, <a href="http://www.madeira.co.il/be-whats-next/"><strong>לסיפור המלא...</strong></a>&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p style="direction: ltr;">Microsoft’s annual event in the IT field was held last Monday (23.4.2012) at Ganei Hata’arukha, Tel Aviv.</p>
<p style="direction: ltr;">The event title was &quot;In order to lead you don't need just to stay up to date &#8211; you need to be what's next&quot;</p>
<p style="direction: ltr;">Details about the event at Microsoft site: <a href="http://www.microsoft.com/israel/benext/">http://www.microsoft.com/israel/benext/</a></p>
<p style="direction: ltr;">Lectures about SQL Server 2012 from &quot;Be what's next&quot; event: <a href="http://www.youtube.com/user/MicrosoftIsrael?feature=watch">http://www.youtube.com/user/MicrosoftIsrael?feature=watch</a></p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/be-whats-next/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8235;Israeli SQL Server User Group Meeting #118&#8236;</title>		<link>http://www.madeira.co.il/israeli-sql-server-user-group-meeting-118-3/</link>
		<comments>http://www.madeira.co.il/israeli-sql-server-user-group-meeting-118-3/#comments</comments>
		<pubDate>Wed, 02 May 2012 10:55:30 +0000</pubDate>
		<dc:creator>&#8235;Madeira&#8236;</dc:creator>				<category><![CDATA[אל תציג בבלוגים]]></category>
		<category><![CDATA[חדשות ואירועים]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=4945</guid>
		<description><![CDATA[&#8235;When: Monday, May 7th, 2012
Where: Dekel Hall, Microsoft Offices, Hapnina 2 St., Raa’nana
Schedule: 

17:30 &#8211; <a href="http://www.madeira.co.il/israeli-sql-server-user-group-meeting-118-3/"><strong>לסיפור המלא...</strong></a>&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p style="direction: ltr"><strong>When:</strong> Monday, May 7<sup>th</sup>, 2012</p>
<p style="direction: ltr"><strong>Where:</strong> Dekel Hall, Microsoft Offices, Hapnina 2 St., Raa’nana</p>
<p style="direction: ltr"><strong>Schedule: </strong></p>
<ul>
<li><strong>17:30 &#8211;      17:45 </strong><strong> Gathering</strong></li>
<li><strong>17:45 &#8211;      18:45 </strong><strong> First      Session</strong></li>
<li><strong>18:45 &#8211; 19:00 </strong><strong> Q</strong><strong>&amp;A      and Job Search</strong></li>
<li><strong>19:00 &#8211; 19:15 </strong><strong> Break</strong></li>
<li><strong>19:15 &#8211; 20:15 </strong><strong> Second      Session</strong></li>
</ul>
<p style="direction: ltr"><strong>Agenda:</strong></p>
<p style="direction: ltr">Common Errors That Effect Performance (Adi Cohen, <a href="http://www.naya-tech.co.il/">Naya-Tech</a>)</p>
<p style="direction: ltr">There are quite a few commonly used techniques that have a negative impact on your database perfromance. In this session we will review some of these errors, see why and how they impact performance and also provide some better performing alternative solutions. Among others, we will cover:</p>
<ul>
<li>Misunderstanding      of the query plan when using procedures</li>
<li>Query      plan differences between procedures and ad-hoc batches</li>
<li>The      differences between a temporary table and a table variable</li>
<li>And      many more…</li>
</ul>
<p style="direction: ltr">Who is afraid of Columnstore Indexes? (Michael Zilberstein, <a href="http://dbart.co.il/english/">DB-Art</a>)</p>
<p style="direction: ltr">This talk describes new SQL Server 2012 feature called &quot;columnstore index&quot;. In this session we will learn about the differences between columnstore indexes and B-Tree indexes we are used to work with. We will see when it is best to use and when not to use this new index. We will cover limitations that columnstore index imposes on the tables that use it and how to live with those limitations. Like in all my sessions, I won't let you go without some internals &#8211; how columnstore index is organized on a physical level and how Query Processor works this new type of index. And of course demos, demos, and more demos&#8230;</p>
<p style="direction: ltr"><strong>To register for the event in order for Microsoft to arrange parking and food: </strong><a href="http://isug118.eventbrite.com/">http://isug118.eventbrite.com</a></p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/israeli-sql-server-user-group-meeting-118-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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;מיקומה הפיזי של רשומה בטבלה&#8236;</title>		<link>http://www.madeira.co.il/%d7%9e%d7%99%d7%a7%d7%95%d7%9e%d7%94-%d7%94%d7%a4%d7%99%d7%96%d7%99-%d7%a9%d7%9c-%d7%a8%d7%a9%d7%95%d7%9e%d7%94-%d7%91%d7%98%d7%91%d7%9c%d7%94/</link>
		<comments>http://www.madeira.co.il/%d7%9e%d7%99%d7%a7%d7%95%d7%9e%d7%94-%d7%94%d7%a4%d7%99%d7%96%d7%99-%d7%a9%d7%9c-%d7%a8%d7%a9%d7%95%d7%9e%d7%94-%d7%91%d7%98%d7%91%d7%9c%d7%94/#comments</comments>
		<pubDate>Fri, 27 Apr 2012 12:30:24 +0000</pubDate>
		<dc:creator>&#8235;חיים פישנר&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[כללי]]></category>
		<category><![CDATA[מקודמות]]></category>
		<category><![CDATA[Internals]]></category>
		<category><![CDATA[PhysLoc]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Undocumented]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=4809</guid>
		<description><![CDATA[&#8235;כל רשומה בטבלה שמורה במקום פיזי כלשהו, המיקום הפיזי של הרשומה מורכב מ3 דברים:
הקובץ הפיזי עליו בו היא מצוייה, המספר Page אליו היא משוייכת, ובאיזה מיקום בתוך אותו הPage.
תמיד רציתם לדעת כיצד ניתן לגלות זאת? התשובה כבר כאן!&#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/4809.gif&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p>בפוסט הקודם כתבתי בנושא המבנה של הPage ועל מה המקום שבתוכו &quot;מתבזבז&quot; הן עם זה עבור ה- Page Header או בעבור ה- Slot Array, בנוסף בסוף הפוסט גם נתתי איזכור לגבי עמודה חבוייה בשם PhysLoc שקיימת עבור כל שורה בכל טבלה בDB של SQL SERVER.</p>
<p>ניתן לעבור ולקרוא שוב או בפעם הראשונה את הפוסט הראשון בקישור הבא :<span style="color: #0000ff"> <span style="text-decoration: underline"><a href="http://www.madeira.co.il/%D7%94%D7%99%D7%9B%D7%9F-%D7%94%D7%A8%D7%A9%D7%95%D7%9E%D7%95%D7%AA-%D7%A9%D7%9C%D7%99/"><span style="color: #0000ff">&quot;היכן הרשומות שלי?!&quot;</span></a></span></span></p>
<p>וכעת נדבר על העמודה<span style="text-decoration: underline"> (או יותר נכון הפונקציה)</span> <strong>PhysLoc</strong> (קיצור של Physical location) ולשם מה היא קיימת ומה ניתן לעשות איתה.</p>
<p>נשים לב שגם בOracle קיימת עמודה (מזהה) דומה והיא מוכרת בשם ROWID.</p>
<p>ניצור טבלה פשוט בכדי להמחיש את השימוש בעמודה:</p>
<div style="text-align: left;padding: 4px;line-height: 12pt;background-color: #f4f4f4;margin: 20px 0px 10px;width: 97.5%;font-family: 'Courier New',courier,monospace;direction: ltr;font-size: 8pt;overflow: auto;cursor: text;border: 1px solid silver">
<pre style="border-style: none;text-align: left;padding: 0px;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New',courier,monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible"><span style="color: #0000ff">USE</span> MASTER;
<span style="color: #0000ff">GO</span>
<span style="color: #0000ff">IF</span> DATABASEPROPERTY (N<span style="color: #006080">'CheckPhysLoc'</span>, <span style="color: #006080">'Version'</span>) &gt; 0
    <span style="color: #0000ff">DROP</span> <span style="color: #0000ff">DATABASE</span> CheckPhysLoc;
<span style="color: #0000ff">GO</span>
<span style="color: #0000ff">CREATE</span> <span style="color: #0000ff">DATABASE</span> [CheckPhysLoc] <span style="color: #0000ff">ON</span>  <span style="color: #0000ff">PRIMARY</span>
( NAME = N<span style="color: #006080">'a'</span>, FILENAME = N<span style="color: #006080">'C:\CheckPhysLoc.mdf'</span> , <span style="color: #0000ff">SIZE</span> = 2304KB ,  FILEGROWTH = 1024KB ),
( NAME = N<span style="color: #006080">'b'</span>, FILENAME = N<span style="color: #006080">'C:\b.ndf'</span> , <span style="color: #0000ff">SIZE</span> = 3072KB , FILEGROWTH = 1024KB ),
( NAME = N<span style="color: #006080">'c'</span>, FILENAME = N<span style="color: #006080">'C:\c.ndf'</span> , <span style="color: #0000ff">SIZE</span> = 3072KB , FILEGROWTH = 1024KB ),
( NAME = N<span style="color: #006080">'d'</span>, FILENAME = N<span style="color: #006080">'C:\d.ndf'</span> , <span style="color: #0000ff">SIZE</span> = 3072KB , FILEGROWTH = 1024KB ),
( NAME = N<span style="color: #006080">'e'</span>, FILENAME = N<span style="color: #006080">'C:\e.ndf'</span> , <span style="color: #0000ff">SIZE</span> = 3072KB , FILEGROWTH = 1024KB )
 LOG <span style="color: #0000ff">ON</span>
( NAME = N<span style="color: #006080">'CheckPhysLoc_log'</span>, FILENAME = N<span style="color: #006080">'C:\CheckPhysLoc_log.LDF'</span> ,
    <span style="color: #0000ff">SIZE</span> = 10240KB , MAXSIZE = 2048GB , FILEGROWTH = 131072KB )
<span style="color: #0000ff">GO</span>
<span style="color: #0000ff">USE</span> CheckPhysLoc;
<span style="color: #0000ff">GO</span>
<span style="color: #0000ff">create</span> <span style="color: #0000ff">table</span> RowLocation (Id tinyint, Name <span style="color: #0000ff">varchar</span>(1000));
<span style="color: #0000ff">GO</span></pre>
</div>
<p>בכוונה יצרתי את הDB עם 5 קבצי DATA תחת אותו הFile group שהוא הPRIMARY אני אגיע לשימוש בכך בהמשך.</p>
<p>כעת נדאג להכניס רשומות אל הטבלה שיצרנו:</p>
<div style="text-align: left;padding: 4px;line-height: 12pt;background-color: #f4f4f4;margin: 20px 0px 10px;width: 97.5%;font-family: 'Courier New',courier,monospace;direction: ltr;font-size: 8pt;overflow: auto;cursor: text;border: 1px solid silver">
<pre style="border-style: none;text-align: left;padding: 0px;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New',courier,monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible">insert <span style="color: #0000ff">into</span> RowLocation
<span style="color: #0000ff">select</span> <span style="color: #0000ff">top</span> 1000 ABS(CHECKSUM(NEWID()))%255, REPLICATE(<span style="color: #006080">'X'</span>, 500)
<span style="color: #0000ff">from</span> sys.all_objects</pre>
</div>
<p>עכשיו נשלוף את הרשומות ואת הPhysLoc של כל רשומה בנוסף נשתמש בפונקציה ה-<a href="http://sqlblog.com/blogs/paul_white/archive/tags/Undocumented/default.aspx">Undocumented</a>: fn_PhysLocFormatter</p>
<div style="text-align: left;padding: 4px;line-height: 12pt;background-color: #f4f4f4;margin: 20px 0px 10px;width: 97.5%;font-family: 'Courier New',courier,monospace;direction: ltr;font-size: 8pt;overflow: auto;cursor: text;border: 1px solid silver">
<pre style="border-style: none;text-align: left;padding: 0px;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New',courier,monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible"><span style="color: #0000ff">select</span>     %%physloc%% <span style="color: #0000ff">as</span> PhysLoc,
         sys.fn_PhysLocFormatter(%%physloc%%) <span style="color: #0000ff">as</span> PhysLocaion,
         r.*
<span style="color: #0000ff">from</span>     RowLocation r</pre>
</div>
<p>ונקבל את התוצאה הבאה :</p>
<p><a href="http://www.madeira.co.il/wp-content/uploads/2012/04/physloc_1_thumb3.jpg" rel="wp-prettyPhoto[g4809]"><img style="margin: 0px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0pt none" src="http://www.madeira.co.il/wp-content/uploads/2012/04/physloc_1_thumb3_thumb.jpg" border="0" alt="physloc_1_thumb[3]" width="453" height="164" /></a></p>
<p>ובעצם קיבלנו את העמודות הנסתרות, נסתכל על העמודה שנתתי לה את השם &quot;PhysLocaion&quot; ונראה שהיא בנויה במבנה של (<span style="color: #ff0000">XX</span>:<span style="color: #9b00d3">YYYY</span>:<span style="color: #0000ff">ZZ</span>) ככה ש:</p>
<p><span style="color: #ff0000">הXX</span>- מתאר את <strong>ה</strong><strong>FileID</strong> שבו ממוקמת הרשומה.</p>
<p><span style="color: #9b00d3">הYY</span>- מתאר את <strong>ה</strong><strong>PageID</strong> שבו ממוקמת הרשומה.</p>
<p><span style="color: #0000ff">הZZ</span>- מתאר את <strong>ה</strong><strong>SlotID</strong> המיקום בתוך הPage ששייך לאותה הרשומה.</p>
<p>בקיצור זהו נתון מאוד חשוב והכרחי לשם העבודה התקינה של המנוע של SQL SERVER שידע כל רשומה ורשומה היכן היא נמצאת בדיוק.</p>
<p>ישנו הבדל קטן בין הגרסאות של SQL SERVER 2008/5, ההבדל הוא בשם העמודה ובפונקציות הקיימות עבורה.</p>
<p>לדגומא <strong>ב2008</strong> – העמודה נקראת באמת <strong>%%physloc%%</strong></p>
<p><strong> </strong></p>
<p><strong> </strong>בעוד <strong>שב2005</strong>- העמודה נקראת <code><strong>%%lockres%%</strong></code><code><strong> </strong></code><code>(קיים גם ב2008 מקביל ל</code>%%physloc%% לאחר הפרסור)</p>
<p>כעת נדבר על פונקציה הקיימת בגרסאת 2008 ומעלה ונקראת : sys.fn_PhysLocCracker  הפונקציה מפרקת את הPhysLoc ל3 העמודות (<strong>file_id, page_id, slot_id</strong>)  בצורה הבאה:</p>
<div style="text-align: left;padding: 4px;line-height: 12pt;background-color: #f4f4f4;margin: 20px 0px 10px;width: 97.5%;font-family: 'Courier New',courier,monospace;direction: ltr;font-size: 8pt;overflow: auto;cursor: text;border: 1px solid silver">
<pre style="border-style: none;text-align: left;padding: 0px;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New',courier,monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible"><span style="color: #0000ff">select</span>     %%physloc%% <span style="color: #0000ff">as</span> PhysLoc,
         sys.fn_PhysLocFormatter(%%physloc%%) <span style="color: #0000ff">as</span> PhysLocaion,
         physLocator.file_id <span style="color: #0000ff">as</span> FileId,
         physLocator.page_id <span style="color: #0000ff">as</span> PageId,
         physLocator.slot_id <span style="color: #0000ff">as</span> SlotId,
         r.*
<span style="color: #0000ff">from</span>     RowLocation r
<span style="color: #0000ff">cross</span> apply sys.fn_PhysLocCracker(%%physloc%%) physLocator</pre>
</div>
<p>נקבל את התוצאה הבאה :</p>
<p><a href="http://www.madeira.co.il/wp-content/uploads/2012/04/physloc_2_thumb2.jpg" rel="wp-prettyPhoto[g4809]"><img style="margin-right: 0px;margin-left: 0px;padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0pt none" src="http://www.madeira.co.il/wp-content/uploads/2012/04/physloc_2_thumb2_thumb.jpg" border="0" alt="physloc_2_thumb[2]" width="453" height="164" /></a></p>
<p>בגרסאת SQL SERVER 2005 הפונקציה הנ&quot;ל איננה קיימת אבל אין זה אומר שלא ניתן לממש אותה, עשיתי את זה והיא נראת כך למי שמעוניין.</p>
<div style="text-align: left;padding: 4px;line-height: 12pt;background-color: #f4f4f4;margin: 20px 0px 10px;width: 97.5%;font-family: 'Courier New',courier,monospace;direction: ltr;font-size: 8pt;overflow: auto;cursor: text;border: 1px solid silver">
<pre style="border-style: none;text-align: left;padding: 0px;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New',courier,monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible"><span style="color: #0000ff">CREATE</span> <span style="color: #0000ff">FUNCTION</span> [dbo].[hf_PhysLocformatter]
(
    @v <span style="color: #0000ff">varchar</span>(100)
)
<span style="color: #0000ff">RETURNS</span> <span style="color: #0000ff">TABLE</span>
<span style="color: #0000ff">AS</span>
<span style="color: #0000ff">RETURN</span>
(
    <span style="color: #0000ff">select</span>  <span style="color: #0000ff">SUBSTRING</span>(@v, PATINDEX(<span style="color: #006080">'(_%'</span>, @v) + 1, PATINDEX(<span style="color: #006080">'%:%'</span>, @v) - (PATINDEX(<span style="color: #006080">'(%:%'</span>, @v) + 1)) <span style="color: #0000ff">AS</span> FileID,
            <span style="color: #0000ff">SUBSTRING</span>(<span style="color: #0000ff">SUBSTRING</span>(@v, PATINDEX(<span style="color: #006080">'%:%'</span>, @v)+1, LEN(@v) - PATINDEX(<span style="color: #006080">'%:%'</span>, @v)), 1, PATINDEX(<span style="color: #006080">'%:%'</span>, <span style="color: #0000ff">SUBSTRING</span>(@v, PATINDEX(<span style="color: #006080">'%:%'</span>, @v)+1, LEN(@v) - PATINDEX(<span style="color: #006080">'%:%'</span>, @v)))-1) <span style="color: #0000ff">AS</span> PageID,
            <span style="color: #0000ff">SUBSTRING</span>(<span style="color: #0000ff">SUBSTRING</span>(@v, PATINDEX(<span style="color: #006080">'%:%'</span>, @v)+1, LEN(@v) - PATINDEX(<span style="color: #006080">'%:%'</span>, @v)), PATINDEX(<span style="color: #006080">'%:%'</span>, <span style="color: #0000ff">SUBSTRING</span>(@v, PATINDEX(<span style="color: #006080">'%:%'</span>, @v)+1, LEN(@v) - PATINDEX(<span style="color: #006080">'%:%'</span>, @v)))+1, LEN(<span style="color: #0000ff">SUBSTRING</span>(@v, PATINDEX(<span style="color: #006080">'%:%'</span>, @v)+1, LEN(@v) - PATINDEX(<span style="color: #006080">'%:%'</span>, @v))) - 1 - PATINDEX(<span style="color: #006080">'%:%'</span>, <span style="color: #0000ff">SUBSTRING</span>(@v, PATINDEX(<span style="color: #006080">'%:%'</span>, @v)+1, LEN(@v) - PATINDEX(<span style="color: #006080">'%:%'</span>, @v)))) <span style="color: #0000ff">AS</span> SlotID
)</pre>
</div>
<p>והשימוש בה זהה לשימוש בפונקציה הקיימת ב2008.</p>
<p>אתם שואלים מה זה עוזר לנו ?</p>
<p>נסתכל על הדוגמא הבאה, בתחילת הפוסט יצרנו DB עם מספר DATA FILES המשוייכים לו, לאחר מכן, הכנסנו חומר לטבלה שהתפרש על כל חמשת הקבצים שהגדרנו ובגלל שהתחלנו מאותה &quot;נקודת פתיחה&quot; בכל הקבצים (כולם היו ריקים).</p>
<p>עכשיו, במידה ונרצה לדעת עבור כל רשומה על איזה קובץ היא &quot;יושבת&quot;, נוכל להשתמש בשליפה שהצגנו למעלה.</p>
<p>ובמידה ונרצה לדעת כמה רשומות “יושבות” לנו על כל קובץ פיזי מאותה הטבלה,  איך נדע ? נריץ את השליפה הבאה :</p>
<div style="text-align: left;padding: 4px;line-height: 12pt;background-color: #f4f4f4;margin: 20px 0px 10px;width: 97.5%;font-family: 'Courier New',courier,monospace;direction: ltr;font-size: 8pt;overflow: auto;cursor: text;border: 1px solid silver">
<pre style="border-style: none;text-align: left;padding: 0px;line-height: 12pt;background-color: #f4f4f4;margin: 0em;width: 100%;font-family: 'Courier New',courier,monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible"><span style="color: #0000ff">select</span>     physLoc.file_id <span style="color: #0000ff">as</span> FileId,
         <span style="color: #0000ff">COUNT</span>(*) <span style="color: #0000ff">as</span> [<span style="color: #0000ff">RowCount</span>]
<span style="color: #0000ff">from</span>     RowLocation r
<span style="color: #0000ff">cross</span> apply sys.fn_PhysLocCracker(%%physloc%%) physLoc
<span style="color: #0000ff">group</span> <span style="color: #0000ff">by</span> physLoc.file_id
<span style="color: #0000ff">order</span> <span style="color: #0000ff">by</span> FileId</pre>
</div>
<p>והתוצאה תחזיר לנו את הפלט הבא:</p>
<p><a href="http://www.madeira.co.il/wp-content/uploads/2012/04/physloc_3_thumb2.jpg" rel="wp-prettyPhoto[g4809]"><img style="padding-left: 0px;padding-right: 0px;padding-top: 0px;border: 0pt none" src="http://www.madeira.co.il/wp-content/uploads/2012/04/physloc_3_thumb2_thumb.jpg" border="0" alt="physloc_3_thumb[2]" width="161" height="113" /></a></p>
<p>כעת שאנחנו יודעים על הקיום של הדבר המופלא הזה שנקרא <strong>PhysLoc </strong>אנחנו יכולים לעשות איתו כל מה שברצוננו ובנוסף זהו מזהה <strong>חד חד ערכי בטבלה</strong> הרי ששתי רשומות לא יכולות לשבת באותו המיקום הפיזי בעת ובעונה אחת.</p>
<p>ומכאן תוכלו להשתמש בידע על ה<strong>PhysLoc </strong>גם עבור מציאת מה ממוצע של מספר השורות בכל PAGE בטבלה ועוד, השתמשו ביצירתיות שלכם ובידע לפי צרכיכם.</p>
<p><em><span style="color: #333366"><span style="text-decoration: underline"><strong>ספויילרים קטנים</strong></span> </span>לשימושים נחוצים שניתן להתחיל לבדוק כיצד הדברים כאן עוזרים לנו הם :</em></p>
<ul>
<li><span style="font-style: italic">כאשר מנטרים </span><em>LOCKS / DEADLOCKS</em><span style="font-style: italic"> המשאבים שננעלים כנראה מתוארים בעזרת הייצוג של הפונקציות הנ&quot;ל.</span></li>
<li><span style="font-style: italic">ובכדי למצוא את הרשומות כאשר מתמודדים עם </span><em>CORRUPTION</em><span style="font-style: italic">.</span></li>
</ul>
<p>ולבסוף אני רוצה להגיד תודה לרוני ורד שבזכותה שמתי לב לפונקציות האלו וזאת בזכות חידה שהיא שאלה אותי, אז תודה!</p>
<p>מקווה שנהנתם,</p>
<p>חיים פישנר.</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/%d7%9e%d7%99%d7%a7%d7%95%d7%9e%d7%94-%d7%94%d7%a4%d7%99%d7%96%d7%99-%d7%a9%d7%9c-%d7%a8%d7%a9%d7%95%d7%9e%d7%94-%d7%91%d7%98%d7%91%d7%9c%d7%94/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

