<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>&#8235;Madeira &#187; אסף אביב&#8236;</title>	<atom:link href="http://www.madeira.co.il/author/asafaviv/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;Difference between ROW_NUMBER, RANK, DENSE_RANK, NTILE&#8236;</title>		<link>http://www.madeira.co.il/difference-between-row_number-rank-dense_rank-ntile/</link>
		<comments>http://www.madeira.co.il/difference-between-row_number-rank-dense_rank-ntile/#comments</comments>
		<pubDate>Mon, 16 Apr 2012 08:05:56 +0000</pubDate>
		<dc:creator>&#8235;אסף אביב&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[כללי]]></category>
		<category><![CDATA[מקודמות]]></category>
		<category><![CDATA[asaf aviv]]></category>
		<category><![CDATA[DENSE_RANK]]></category>
		<category><![CDATA[NTILE]]></category>
		<category><![CDATA[Rank]]></category>
		<category><![CDATA[ROW_NUMBER]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server 2008 R2]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=4835</guid>
		<description><![CDATA[&#8235;Ranking functions returns a ranking value for each row either in a partition, or not. We can produce the very same result for all functions, but they have different purpose.&#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/4835.jpg&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p dir="ltr">What is the Difference between <a href="http://msdn.microsoft.com/en-us/library/ms186734.aspx">ROW_NUMBER</a>, <a href="http://msdn.microsoft.com/en-us/library/ms176102.aspx">RANK</a> and <a href="http://msdn.microsoft.com/en-us/library/ms173825.aspx">DENSE_RANK</a>? Which one to use?</p>
<p dir="ltr">Ranking functions returns a ranking value for each row either in a partition, or not. We can produce the very same result for all functions, but they have different purpose.</p>
<p dir="ltr">In SQL Server there are 4 ranking functions:</p>
<p dir="ltr">ROW_NUMBER &#8211; Returns the sequential number of a row within a partition of a result set, without any gaps in the ranking. The rank of a row is one plus the number of distinct ranks that come before the row in question.</p>
<p dir="ltr">RANK &#8211; Returns the rank of each row within the partition of a result set. The rank of a row is one plus the number of ranks that come before the row in question.</p>
<p dir="ltr">DENSE_RANK &#8211; Returns the rank of rows within the partition of a result set, without any gaps in the ranking. The rank of a row is one plus the number of distinct ranks that come before the row in question.</p>
<p dir="ltr">NTILE &#8211; Distributes the rows in an ordered partition into a specified number of groups. The groups are numbered, starting at one. For each row, NTILE returns the number of the group to which the row belongs.</p>
<p dir="ltr">Let's see a simple example:</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">
<div style="border-style: none;text-align: left;padding: 0px;line-height: 12pt;background-color: #f4f4f4;width: 100%;font-family: 'Courier New',courier,monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible">
<pre style="border-style: none;text-align: left;padding: 0px;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"><span style="color: #008000">-- create table</span></pre>
<p><!--CRLF--></p>
<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">TABLE</span> dbo.Grades</pre>
<p><!--CRLF--></p>
<pre style="border-style: none;text-align: left;padding: 0px;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">(</pre>
<p><!--CRLF--></p>
<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">Names</span> <span style="color: #0000ff">VARCHAR</span>(1),</pre>
<p><!--CRLF--></p>
<pre style="border-style: none;text-align: left;padding: 0px;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">Grade <span style="color: #0000ff">INT</span></pre>
<p><!--CRLF--></p>
<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">)</pre>
<p><!--CRLF--></p>
<pre style="border-style: none;text-align: left;padding: 0px;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"><span style="color: #0000ff">GO</span></pre>
<p><!--CRLF--></p>
<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: #008000">-- insert data</span></pre>
<p><!--CRLF--></p>
<pre style="border-style: none;text-align: left;padding: 0px;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">INSERT <span style="color: #0000ff">INTO</span> dbo.Grades</pre>
<p><!--CRLF--></p>
<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">VALUES</span> (<span style="color: #006080">'A'</span>,100),(<span style="color: #006080">'B'</span>,90),(<span style="color: #006080">'C'</span>,80),(<span style="color: #006080">'D'</span>,70),(<span style="color: #006080">'E'</span>,70),(<span style="color: #006080">'F'</span>,60)</pre>
<p><!--CRLF--></p>
<pre style="border-style: none;text-align: left;padding: 0px;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"><span style="color: #0000ff">GO</span></pre>
<p><!--CRLF--></p>
</div>
</div>
<p dir="ltr">This query explains usage of all the four function together in one query.</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">
<div style="border-style: none;text-align: left;padding: 0px;line-height: 12pt;background-color: #f4f4f4;width: 100%;font-family: 'Courier New',courier,monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible">
<pre style="border-style: none;text-align: left;padding: 0px;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"><span style="color: #008000">-- Test the data</span></pre>
<p><!--CRLF--></p>
<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> <span style="color: #0000ff">Names</span>,</pre>
<p><!--CRLF--></p>
<pre style="border-style: none;text-align: left;padding: 0px;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">     Grade,</pre>
<p><!--CRLF--></p>
<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">     ROW_NUMBER () <span style="color: #0000ff">OVER</span> (<span style="color: #0000ff">ORDER</span> <span style="color: #0000ff">BY</span> Grade <span style="color: #0000ff">DESC</span>) <span style="color: #0000ff">as</span> ROW_NUMBER,</pre>
<p><!--CRLF--></p>
<pre style="border-style: none;text-align: left;padding: 0px;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">       RANK () <span style="color: #0000ff">OVER</span> (<span style="color: #0000ff">ORDER</span> <span style="color: #0000ff">BY</span> Grade <span style="color: #0000ff">DESC</span>) <span style="color: #0000ff">as</span> RANK,</pre>
<p><!--CRLF--></p>
<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">       DENSE_RANK () <span style="color: #0000ff">OVER</span> (<span style="color: #0000ff">ORDER</span> <span style="color: #0000ff">BY</span> Grade <span style="color: #0000ff">DESC</span>) <span style="color: #0000ff">as</span> DENSE_RANK,</pre>
<p><!--CRLF--></p>
<pre style="border-style: none;text-align: left;padding: 0px;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">       NTILE(3) <span style="color: #0000ff">OVER</span>(<span style="color: #0000ff">ORDER</span> <span style="color: #0000ff">BY</span> Grade <span style="color: #0000ff">desc</span>) <span style="color: #0000ff">AS</span> NTILE</pre>
<p><!--CRLF--></p>
<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">FROM</span> dbo.Grades</pre>
<p><!--CRLF--></p>
</div>
</div>
<p dir="ltr">The results:</p>
<p dir="ltr"><a href="$image[5].png"></a><a href="http://www.madeira.co.il/wp-content/uploads/2012/04/Result.png" rel="wp-prettyPhoto[g4835]"><img class="alignnone size-medium wp-image-4840" src="http://www.madeira.co.il/wp-content/uploads/2012/04/Result-300x107.png" alt="" width="300" height="107" /></a></p>
<p dir="ltr">As you can see, ROW_NUMBER returns a serial number for each row, without any gaps. The numbers returned by the DENSE_RANK function do not have gaps and always have consecutive ranks. The RANK function does not always return consecutive integer. The NTILE, since I've specified 2 in the parameter, it divided all data into 2 groups.</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/difference-between-row_number-rank-dense_rank-ntile/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>&#8235;Use of Proxy for Running Jobs&#8236;</title>		<link>http://www.madeira.co.il/use-of-proxy-for-running-jobs/</link>
		<comments>http://www.madeira.co.il/use-of-proxy-for-running-jobs/#comments</comments>
		<pubDate>Thu, 08 Mar 2012 13:30:07 +0000</pubDate>
		<dc:creator>&#8235;אסף אביב&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[כללי]]></category>
		<category><![CDATA[מקודמות]]></category>
		<category><![CDATA[asaf aviv]]></category>
		<category><![CDATA[SQL Server 2008 R2]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server Agent]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=4547</guid>
		<description><![CDATA[&#8235;With a proxy account, you can assign a job to use an account other than the SQL Server Agent account.&#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/4547.jpg&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p style="text-align: left" dir="ltr">Problem:</p>
<p dir="ltr">A classic problem when SQL Server Agent runs tasks like SSIS, DTS etc&#8230;, it's uses the agent service account by default. This account may not have access to a file share or database server that is necessary to successfully run the task. Proxy accounts in SQL Server 2008 allow you to circumvent this problem.</p>
<p dir="ltr">Solution:</p>
<p dir="ltr">With a proxy account, you can assign a job to use an account other than the SQL Server Agent account. Creating a proxy account is a two &#8211; step process. First, you must create a credential that will allow a user to use an Active Directory account that is not their own, Then, you specify how that account may be used.</p>
<p dir="ltr">A proxy can be defined with one or more sub-systems. The list of sub-systems is as follows:</p>
<p dir="ltr">@subsystem_id = 2 &#8211; ActiveX Script</p>
<p dir="ltr">@subsystem_id = 3 &#8211; Operating System (CMD Exec)</p>
<p dir="ltr">@subsystem_id = 4 &#8211; Replication Snapshot</p>
<p dir="ltr">@subsystem_id = 5 &#8211; Replication Distributor</p>
<p dir="ltr">@subsystem_id = 6 &#8211; Replication Transaction-Log Reader</p>
<p dir="ltr">@subsystem_id = 7 &#8211; Replication Merge</p>
<p dir="ltr">@subsystem_id = 8 &#8211; Replication Queue Reader</p>
<p dir="ltr">@subsystem_id = 9 &#8211; SQL Server Analysis Services Query</p>
<p dir="ltr">@subsystem_id = 10 &#8211; SQL Server Analysis Services Command</p>
<p dir="ltr">@subsystem_id = 11 &#8211; SQL Server Integration Services Package</p>
<p dir="ltr">@subsystem_id = 12 &#8211; Unassigned Proxy</p>
<p dir="ltr">As a first step for setting up a proxy account, we need to create a credential.</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">
<div style="border-style: none;text-align: left;padding: 0px;line-height: 12pt;background-color: #f4f4f4;width: 100%;font-family: 'Courier New',courier,monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible">
<pre style="border-style: none;text-align: left;padding: 0px;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"><span style="color: #0000ff">USE</span> [master]</pre>
<p><!--CRLF--></p>
<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">GO</span></pre>
<p><!--CRLF--></p>
<pre style="border-style: none;text-align: left;padding: 0px;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"><span style="color: #0000ff">CREATE</span> CREDENTIAL [MyCredential] <span style="color: #0000ff">WITH</span> <span style="color: #0000ff">IDENTITY</span> = N<span style="color: #006080">'Domain\asaf'</span>, SECRET = N<span style="color: #006080">'Password'</span></pre>
<p><!--CRLF--></p>
<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">GO</span></pre>
<p><!--CRLF--></p>
</div>
</div>
<p dir="ltr">Now, let's create a proxy account.</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">
<div style="border-style: none;text-align: left;padding: 0px;line-height: 12pt;background-color: #f4f4f4;width: 100%;font-family: 'Courier New',courier,monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible">
<pre style="border-style: none;text-align: left;padding: 0px;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"><span style="color: #0000ff">USE</span> [msdb]</pre>
<p><!--CRLF--></p>
<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">GO</span></pre>
<p><!--CRLF--></p>
<pre style="border-style: none;text-align: left;padding: 0px;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"><span style="color: #0000ff">EXEC</span> msdb.dbo.sp_add_proxy @proxy_name=N<span style="color: #006080">'MyProxy'</span>,</pre>
<p><!--CRLF--></p>
<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">   @credential_name=N<span style="color: #006080">'MyCredential'</span>,</pre>
<p><!--CRLF--></p>
<pre style="border-style: none;text-align: left;padding: 0px;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">         @enabled=1</pre>
<p><!--CRLF--></p>
<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">GO</span></pre>
<p><!--CRLF--></p>
</div>
</div>
<p dir="ltr">This proxy will be used to run Operating System commands (@subsystem_id=11).</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">
<div style="border-style: none;text-align: left;padding: 0px;line-height: 12pt;background-color: #f4f4f4;width: 100%;font-family: 'Courier New',courier,monospace;direction: ltr;color: black;font-size: 8pt;overflow: visible">
<pre style="border-style: none;text-align: left;padding: 0px;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"><span style="color: #0000ff">EXEC</span> msdb.dbo.sp_grant_proxy_to_subsystem @proxy_name=N<span style="color: #006080">'MyProxy'</span>,                                   @subsystem_id=11</pre>
<p><!--CRLF--></p>
<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">GO</span></pre>
<p><!--CRLF--></p>
<pre style="border-style: none;text-align: left;padding: 0px;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"><span style="color: #0000ff">EXEC</span> msdb.dbo.sp_grant_login_to_proxy @proxy_name=N<span style="color: #006080">'MyProxy'</span>,                                 @msdb_role=N<span style="color: #006080">'SQLAgentUserRole'</span></pre>
<p><!--CRLF--></p>
<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">GO</span></pre>
<p><!--CRLF--></p>
</div>
</div>
<p dir="ltr"><a href="http://www.madeira.co.il/wp-content/uploads/2012/03/1.jpg" rel="wp-prettyPhoto[g4547]"><img class="alignnone size-full wp-image-4551" src="http://www.madeira.co.il/wp-content/uploads/2012/03/1.jpg" alt="" /></a></p>
<p dir="ltr">Once the proxy exists it can be selected in setting up a job task by specifying the new proxy in the Run As dropdown in the task dialog.</p>
<p dir="ltr"><a href="http://www.madeira.co.il/wp-content/uploads/2012/03/2.jpg" rel="wp-prettyPhoto[g4547]"><img class="alignnone size-full wp-image-4552" src="http://www.madeira.co.il/wp-content/uploads/2012/03/2.jpg" alt="" /></a></p>
<p dir="ltr">Notes</p>
<p dir="ltr">-SQL Server Agent impersonates the credentials associated with the proxy to run the job step if it has been defined to use the proxy instead of using the default security context of SQL Server Agent Service account.</p>
<p dir="ltr">-When you create a job step to execute T-SQL, you cannot define a proxy because T-SQL does not use proxies but rather it runs under the security context of the job owner. If you want to change the security context for Transact-SQL job step, you can use the database_user_name parameter of sp_add_jobstep system stored procedure.</p>
<p dir="ltr">-You need to be sysadmin to create, modify or delete proxy accounts. However to use it, you need to be sysadmin or a member of either of these SQL Server Agent fixed database roles: SQLAgentUserRole, SQLAgentReaderRole, or SQLAgentOperatorRole.</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/use-of-proxy-for-running-jobs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8235;Working with Synonyms in SQL Server&#8236;</title>		<link>http://www.madeira.co.il/working-with-synonyms-in-sql-server/</link>
		<comments>http://www.madeira.co.il/working-with-synonyms-in-sql-server/#comments</comments>
		<pubDate>Sat, 11 Feb 2012 16:24:09 +0000</pubDate>
		<dc:creator>&#8235;אסף אביב&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[כללי]]></category>
		<category><![CDATA[מקודמות]]></category>
		<category><![CDATA[asaf aviv]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server 2008 R2]]></category>
		<category><![CDATA[SYNONYM]]></category>
		<category><![CDATA[Synonyms]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=4201</guid>
		<description><![CDATA[&#8235;Synonyms were first introduced in SQL Server 2005 and prove to be a great feature so far.&#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/4201.jpg&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p dir="ltr">Synonyms were first introduced in SQL 2005 and prove to be a great feature so far. A Synonym is a database object that serves the following purposes:</p>
<p dir="ltr">-Synonyms provide a layer of abstraction over the referenced object</p>
<p dir="ltr">-Allow changes to complicated (multi part) and lengthy names with a simplified  alias as a same server resident object.</p>
<p dir="ltr">-Provides flexibility for changing the location of objects without changing existing code.</p>
<p dir="ltr">-Synonyms can be created in the same database to provide backward compatibility for older applications in case of drop or rename of objects.</p>
<p dir="ltr">-Synonyms can be useful if you give the front-end query tools like spreadsheets and Access linked tables direct links in to the tables.</p>
<p dir="ltr">This helps because it allows you to move objects like tables, procedures to other locations without having to change the existing objects that reference them.</p>
<p dir="ltr">For example, consider the Employee table of the AdventureWorks sample database, located on a server named Server1. To reference this table from another server, Server2, a client application would have to use the four-part name Server1.AdventureWorks.HumanResources.Employee. Also, if the location of the table were to change, for example, to another server, the client application would have to be modified to reflect that change.</p>
<p dir="ltr">To address both these issues, you can create a synonym, EmpTable, on Server2 for the Employee table on Server1.</p>
<div style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 20px 0px 10px;width: 97.5%;font-family: 'Courier New',courier,monospace;direction: ltr;font-size: 8pt;overflow: auto;cursor: text;border: silver 1px solid;padding: 4px">
<div 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">USE</span> AdventureWorks;</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">GO</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"><span style="color: #0000ff">CREATE</span> SYNONYM EmpTable</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">FOR</span> Server1.AdventureWorks.HumanResources.Employee;</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">GO</span></pre>
<p><!--CRLF--></p>
<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">USE</span> AdventureWorks;</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">GO</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"><span style="color: #0000ff">SELECT</span> <span style="color: #0000ff">TOP</span> 5 *</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">FROM</span> EmpTable;</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">GO</pre>
<p><!--CRLF--></p>
</div>
</div>
<p dir="ltr"><a href="http://www.madeira.co.il/wp-content/uploads/2012/02/Result.jpg" rel="wp-prettyPhoto[g4201]"><img class="alignnone size-full wp-image-4207" src="http://www.madeira.co.il/wp-content/uploads/2012/02/Result.jpg" alt="" width="514" height="144" /></a></p>
<p dir="ltr">Now, the client application only has to use the single-part name, EmpTable, to reference the Employee table. Also, if the location of the Employee table changes, you will have to modify the synonym, EmpTable, to point to the new location of the Employee table. Because there is no ALTER synonym statement, you first have to drop the synonym, EmpTable, and then re-create the synonym with the same name, but point the synonym to the new location of Employee.</p>
<div style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 20px 0px 10px;width: 97.5%;font-family: 'Courier New',courier,monospace;direction: ltr;font-size: 8pt;overflow: auto;cursor: text;border: silver 1px solid;padding: 4px">
<div 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">USE</span> AdventureWorks;</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">GO</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"><span style="color: #0000ff">DROP</span> SYNONYM EmpTable;</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">GO</span></pre>
<p><!--CRLF--></p>
<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">CREATE</span> SYNONYM EmpTable</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">FOR</span> Server3.AdventureWorks.HumanResources.Employee;</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">GO</span></pre>
<p><!--CRLF--></p>
</div>
</div>
<p dir="ltr">A synonym belongs to a schema, and like other objects in a schema, the name of a Synonym must be unique. You can create synonyms for the following database objects:</p>
<p dir="ltr">
<p dir="ltr">-Tables</p>
<p dir="ltr">-Views</p>
<p dir="ltr">-Assembly Stored Procedures, Table Valued Functions, Aggregations</p>
<p dir="ltr">-SQL Scalar Functions</p>
<p dir="ltr">-SQL Stored Procedures</p>
<p dir="ltr">-SQL Table Valued Functions</p>
<p dir="ltr">-SQL Inline-Table-Valued Functions</p>
<p dir="ltr">-Local and Global Temporary Tables</p>
<p dir="ltr">-Replication-filter-procedures</p>
<p dir="ltr">-Extended Stored Procedures</p>
<p dir="ltr">Additionally synonyms can be used only to change data of object not the schema of the object. Synonyms can be used with only SELECT, UPDATE, INSERT, DELETE, EXECUTE commands.</p>
<p dir="ltr">Limitations:</p>
<p dir="ltr">-You can delete a synonym without getting any warning that it is being referenced by any other database object.</p>
<p dir="ltr">-The object for which the synonym is being created is checked at run time. It is not checked at creation time. So this means that if you make any related error e.g. spelling error, the synonym will created, but you will get an error while accessing the object.</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/working-with-synonyms-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>&#8235;SQL Server 2008 new data type HierarchyID&#8236;</title>		<link>http://www.madeira.co.il/the-new-data-type-hierarchyid-is-one-of-the-coolest-and-fantastic-new-features-of-sql-server-2008/</link>
		<comments>http://www.madeira.co.il/the-new-data-type-hierarchyid-is-one-of-the-coolest-and-fantastic-new-features-of-sql-server-2008/#comments</comments>
		<pubDate>Sun, 15 Jan 2012 16:47:32 +0000</pubDate>
		<dc:creator>&#8235;אסף אביב&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[כללי]]></category>
		<category><![CDATA[מקודמות]]></category>
		<category><![CDATA[features]]></category>
		<category><![CDATA[HierarchyID]]></category>
		<category><![CDATA[Madeira]]></category>
		<category><![CDATA[SQL Server 2008 R2]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[What's New]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=3601</guid>
		<description><![CDATA[&#8235;
One of the coolest and fantastic new features of SQL Server 2008 is the new <a href="http://www.madeira.co.il/the-new-data-type-hierarchyid-is-one-of-the-coolest-and-fantastic-new-features-of-sql-server-2008/"><strong>לסיפור המלא...</strong></a>&#8236;]]></description>			<content:encoded><![CDATA[<div dir="rtl"><p><img src='http://www.madeira.co.il/wp-content/plugins/simple-post-thumbnails/timthumb.php?src=/wp-content/thumbnails/3601.jpg&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p dir="rtl">One of the coolest and fantastic new features of SQL Server 2008 is the new data type HierarchyID. You’ve probably been confronted with a scenario in which you needed to data tree of some sort, and you’ve probably struggled with joins, recursive operations, and other strategies in the past. With this new type you can implement tree structures with ease, making use of the best indexing strategy.</p>
<p dir="ltr">The HierarchyID data type allows you to construct relationships among data elements within a table, specifically to represent a position in a hierarchy.</p>
<p dir="ltr">Some Facts:</p>
<p dir="ltr">-New system CLR type supporting trees.</p>
<p dir="ltr">-Extremely compact &#8211; Internally stored as varbinary &lt;= 900 bytes.</p>
<p dir="ltr">-Easy to understand &#8211; Holds a path that provides a topological sort of a tree.</p>
<p dir="ltr">-Has a set of methods that provide tree functionality: GetAncestor, GetDescendant,  GetLevel, GetRoot, IsDescendant, Parse, Read, Reparent, ToString, Write.</p>
<p dir="ltr">-Efficient querying that can utilize depth-first and breadth-first indexes.</p>
<p dir="ltr">The hierarchyid data type has the following limitations:</p>
<p dir="ltr">
<blockquote>
<p dir="ltr">-A column of type hierarchyid does not automatically represent a  tree. It is up to the application to generate and assign hierarchyid values in such a way that the desired relationship between rows is reflected in the values. Some applications might not even want to have a column of type hierarchyid represent a tree. Perhaps the values are references to location in a hierarchy defined in another table.</p>
<p dir="ltr">-It is up to the application to manage concurrency in generating and assigning hierarchyid values. There is no guarantee that hierarchyid values in a column are unique unless the application uses a unique key constraint or enforces uniqueness itself through its own logic.</p>
<p dir="ltr">-Hierarchical relationships represented by hierarchyid values are not enforced like a foreign key relationship. It is possible and sometimes appropriate to have a hierarchical relationship where A has a child B, and then A is deleted leaving B with a relationship to a nonexistent record. If this behavior is unacceptable, the application must query for descendants before deleting parents.</p>
</blockquote>
<p dir="ltr">Methods provided are as follows:</p>
<p dir="ltr">
<p dir="ltr"><strong>GetAncestor</strong>: Returns a HierarchyID that represents the n<sup>th</sup> ancestor of this HierarchyID node.</p>
<p dir="ltr"><strong>GetDescendant</strong>: Returns a child node of this HierarchyID node.</p>
<p dir="ltr"><strong>GetLevel</strong>: Returns an integer that represents the depth of this HierarchyID node in the overall hierarchy.</p>
<p dir="ltr"><strong>GetRoot</strong>: Returns the root HierarchyID node of this hierarchy tree. Static.</p>
<p dir="ltr"><strong>IsDescendant</strong>: Returns true if the passed-in child node is a descendant of this HierarchyID node.</p>
<p dir="ltr"><strong>Parse</strong>: Converts a string representation of a hierarchy to a HierarchyID value. Static.</p>
<p dir="ltr"><strong>Reparent</strong>: Moves a node of a hierarchy to a new location within the hierarchy.</p>
<p dir="ltr"><strong>ToString</strong>: Returns a string that contains the logical representation of this HierarchyID.</p>
<p dir="ltr"><strong><em>Indexing Strategies :</em></strong></p>
<p dir="ltr">There are two strategies for indexing hierarchical data:</p>
<p dir="ltr">
<p dir="ltr">-Depth-first</p>
<p dir="ltr">A depth-first index, rows in a subtree are stored near each other. For example, all employees that report through a manager are stored near their managers' record- Useful for querying subtrees.</p>
<p dir="ltr"><a href="$clip_image00142.gif"></a><a href="http://www.madeira.co.il/wp-content/uploads/2012/01/1.jpg" rel="wp-prettyPhoto[g3601]"><img class="alignnone size-full wp-image-3695" src="http://www.madeira.co.il/wp-content/uploads/2012/01/1.jpg" alt="" /></a></p>
<p dir="ltr">
<p dir="ltr">-Breadth-first</p>
<p dir="ltr">A breadth-first stores the rows each level of the hierarchy together. For example, the records of employees who directly report to the same manager are stored near each other – Useful for querying immediate children.</p>
<p dir="ltr"><a href="$clip_image00162.gif"></a><a href="http://www.madeira.co.il/wp-content/uploads/2012/01/2.jpg" rel="wp-prettyPhoto[g3601]"><img class="alignnone size-full wp-image-3696" src="http://www.madeira.co.il/wp-content/uploads/2012/01/2.jpg" alt="" /></a></p>
<p dir="ltr">
<pre style="text-align: left"><span style="font-family: Arial">Everyone found that genius but a lot broke their teeth using it in real life so let's </span></pre>
<pre style="text-align: left"><span style="font-family: Arial">Try it:</span></pre>
<pre style="text-align: left"><span style="font-family: Calibri"><span style="font-family: Arial">Let's assume we already have the following products</span> </span></pre>
<pre><a href="$image2.png"></a><a href="http://www.madeira.co.il/wp-content/uploads/2012/01/3.jpg" rel="wp-prettyPhoto[g3601]"><img class="alignnone size-full wp-image-3697" src="http://www.madeira.co.il/wp-content/uploads/2012/01/3.jpg" alt="" /></a></pre>
<pre style="text-align: left"><span style="font-family: Arial">The Idea is to represent a product tree:</span></pre>
<pre><a href="$image8.png"></a><a href="http://www.madeira.co.il/wp-content/uploads/2012/01/4.jpg" rel="wp-prettyPhoto[g3601]"><img class="alignnone size-full wp-image-3698" src="http://www.madeira.co.il/wp-content/uploads/2012/01/4.jpg" alt="" /></a></pre>
<pre style="text-align: left"><span style="font-family: Arial">First, we need to create a table to store the product hierarchy:</span></pre>
<div style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 20px 0px 10px;width: 97.5%;font-family: 'Courier New',courier,monospace;direction: ltr;font-size: 8pt;overflow: auto;cursor: text;border: 1px solid silver;padding: 4px">
<div 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: #606060">   1:</span> <span style="color: #0000ff">CREATE</span> <span style="color: #0000ff">TABLE</span> dbo.ProductTree</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: #606060">   2:</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"><span style="color: #606060">   3:</span> [hierarchyid] [hierarchyid] <span style="color: #0000ff">NOT</span> <span style="color: #0000ff">NULL</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: #606060">   4:</span> [Node] [nvarchar](4000) <span style="color: #0000ff">NOT</span> <span style="color: #0000ff">NULL</span>, --String representation <span style="color: #0000ff">of</span> the HierarchyID.</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: #606060">   5:</span> [ProductID] [<span style="color: #0000ff">int</span>] <span style="color: #0000ff">NOT</span> <span style="color: #0000ff">NULL</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: #606060">   6:</span> )<span style="color: #0000ff">ON</span> [<span style="color: #0000ff">PRIMARY</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"><span style="color: #606060">   7:</span> <span style="color: #0000ff">GO</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: #606060">   8:</span> <span style="color: #0000ff">CREATE</span> <span style="color: #0000ff">UNIQUE</span> <span style="color: #0000ff">CLUSTERED</span> <span style="color: #0000ff">INDEX</span> [IX_ProductTree] <span style="color: #0000ff">ON</span> dbo.ProductTree</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: #606060">   9:</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: #606060">  10:</span>  [hierarchyid],</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: #606060">  11:</span>  [ProductID]</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: #606060">  12:</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"><span style="color: #606060">  13:</span> <span style="color: #0000ff">GO</span></pre>
<p><!--CRLF--></p>
</div>
</div>
<pre style="text-align: left"><span style="font-family: Arial">Let's start building the tree! First, the root: </span></pre>
<div style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 20px 0px 10px;width: 97.5%;font-family: 'Courier New',courier,monospace;direction: ltr;font-size: 8pt;overflow: auto;cursor: text;border: 1px solid silver;padding: 4px">
<div 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: #606060">   1:</span> --Insert The Root -&gt; Product</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: #606060">   2:</span> INSERT <span style="color: #0000ff">INTO</span> dbo.ProductTree</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: #606060">   3:</span> (hierarchyid, Node, ProductID)</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: #606060">   4:</span> <span style="color: #0000ff">VALUES</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"><span style="color: #606060">   5:</span> (hierarchyid::GetRoot(), hierarchyid::GetRoot().ToString(), 1)</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: #606060">   6:</span> <span style="color: #0000ff">GO</span></pre>
<p><!--CRLF--></p>
</div>
</div>
<pre style="text-align: left"><span style="font-family: Arial">Notice the call GetRoot() to get the root node reference.</span></pre>
<pre style="text-align: left"><span style="font-family: Arial">So now we have the root node, now we need to add the first child:</span></pre>
<div style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 20px 0px 10px;width: 97.5%;font-family: 'Courier New',courier,monospace;direction: ltr;font-size: 8pt;overflow: auto;cursor: text;border: 1px solid silver;padding: 4px">
<div 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: #606060">   1:</span> --Insert <span style="color: #0000ff">First</span> Child(IPhone)</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: #606060">   2:</span> <span style="color: #0000ff">DECLARE</span> @ProductID <span style="color: #0000ff">INT</span> = 2 <span style="color: #008000">-- The First Child  = iPhone</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"><span style="color: #606060">   3:</span> <span style="color: #0000ff">DECLARE</span> @hierarchyid HIERARCHYID</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: #606060">   4:</span> <span style="color: #0000ff">DECLARE</span> @Parenthierarchyid HIERARCHYID</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: #606060">   5:</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: #606060">   6:</span> --<span style="color: #0000ff">Get</span> Parent hierarchyid</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: #606060">   7:</span> <span style="color: #0000ff">SELECT</span> @Parenthierarchyid = hierarchyid</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: #606060">   8:</span> <span style="color: #0000ff">FROM</span> dbo.ProductTree</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: #606060">   9:</span> <span style="color: #0000ff">WHERE</span> ProductID = 1</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: #606060">  10:</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"><span style="color: #606060">  11:</span> --<span style="color: #0000ff">Get</span> the hierarchyid</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: #606060">  12:</span> <span style="color: #0000ff">SELECT</span> @hierarchyid = @Parenthierarchyid.GetDescendant(<span style="color: #0000ff">NULL</span>,<span style="color: #0000ff">NULL</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"><span style="color: #606060">  13:</span> <span style="color: #0000ff">FROM</span> dbo.ProductTree</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: #606060">  14:</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"><span style="color: #606060">  15:</span> --Insert <span style="color: #0000ff">First</span> Child(IPhone)</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: #606060">  16:</span> INSERT <span style="color: #0000ff">INTO</span> dbo.ProductTree</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: #606060">  17:</span> (hierarchyid, Node, ProductID)</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: #606060">  18:</span> <span style="color: #0000ff">VALUES</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"><span style="color: #606060">  19:</span> (@hierarchyid, @hierarchyid.ToString(), @ProductID)</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: #606060">  20:</span>  <span style="color: #0000ff">Go</span></pre>
<p><!--CRLF--></p>
</div>
</div>
<pre style="text-align: left"><span style="font-family: Arial">Notice the call GetDescendant() to get the descendant of a given node. </span></pre>
<pre style="text-align: left"><span style="font-family: Arial">It has a great significance in terms of finding the new descendant position </span></pre>
<pre style="text-align: left"><span style="font-family: Arial">Get the descendants etc.</span></pre>
<pre style="text-align: left"><span style="font-family: Arial">We'll be inserting the "Mac", in the same level, below "Product" </span></pre>
<pre style="text-align: left"><span style="font-family: Arial">And next to "iPhone".</span></pre>
<div style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 20px 0px 10px;width: 97.5%;font-family: 'Courier New',courier,monospace;direction: ltr;font-size: 8pt;overflow: auto;cursor: text;border: 1px solid silver;padding: 4px">
<div 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: #606060">   1:</span> --Insert <span style="color: #0000ff">Second</span> Child(Mac)</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: #606060">   2:</span> <span style="color: #0000ff">DECLARE</span> @ProductID <span style="color: #0000ff">INT</span> = 3 <span style="color: #008000">-- The Second Child = Mac</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"><span style="color: #606060">   3:</span> <span style="color: #0000ff">DECLARE</span> @FirstChildhierarchyid HIERARCHYID</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: #606060">   4:</span> <span style="color: #0000ff">DECLARE</span> @hierarchyid HIERARCHYID</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: #606060">   5:</span> <span style="color: #0000ff">DECLARE</span> @Parenthierarchyid HIERARCHYID</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: #606060">   6:</span> --<span style="color: #0000ff">Get</span> <span style="color: #0000ff">First</span> Child hierarchyid <span style="color: #0000ff">and</span> Parent hierarchyid</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: #606060">   7:</span> <span style="color: #0000ff">SELECT</span> @FirstChildhierarchyid = hierarchyid,</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: #606060">   8:</span>        @Parenthierarchyid = hierarchyid.GetAncestor(hierarchyid.GetLevel())</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: #606060">   9:</span> <span style="color: #0000ff">FROM</span> dbo.ProductTree</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: #606060">  10:</span> <span style="color: #0000ff">WHERE</span> ProductID = 2 <span style="color: #008000">-- iPhone</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"><span style="color: #606060">  11:</span> --<span style="color: #0000ff">Get</span> the hierarchyid</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: #606060">  12:</span> <span style="color: #0000ff">SELECT</span> @hierarchyid = @Parenthierarchyid.GetDescendant(@FirstChildhierarchyid,<span style="color: #0000ff">NULL</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"><span style="color: #606060">  13:</span> <span style="color: #0000ff">FROM</span> dbo.ProductTree</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: #606060">  14:</span> --Insert <span style="color: #0000ff">Second</span> Child(Mac)</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: #606060">  15:</span> INSERT <span style="color: #0000ff">INTO</span> dbo.ProductTree</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: #606060">  16:</span> (hierarchyid, Node, ProductID)</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: #606060">  17:</span> <span style="color: #0000ff">VALUES</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: #606060">  18:</span> (@hierarchyid, @hierarchyid.ToString(), @ProductID)</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: #606060">  19:</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: #606060">  20:</span> <span style="color: #0000ff">Go</span></pre>
<p><!--CRLF--></p>
</div>
</div>
<pre style="text-align: left"><span style="font-family: Arial">Notice the call GetAncestor() to get the ancestor </span></pre>
<pre style="text-align: left"><span style="font-family: Arial">Of a given child node by the level (int).</span></pre>
<p style="text-align: left" dir="ltr">Since we needed to add a new product,</p>
<p style="text-align: left" dir="ltr">At the same level of the &quot;iPhone&quot; product,</p>
<p style="text-align: left" dir="ltr">we had to get a node reference next to it, hence the @Parenthierarchyid.GetDescendant(@FirstChildhierarchyid,NULL).</p>
<p style="text-align: left" dir="ltr">Now let's add a second level child to &quot;iPhone&quot;:</p>
<p dir="ltr">
<div style="text-align: left;line-height: 12pt;background-color: #f4f4f4;margin: 20px 0px 10px;width: 97.5%;font-family: 'Courier New',courier,monospace;direction: ltr;font-size: 8pt;overflow: auto;cursor: text;border: 1px solid silver;padding: 4px">
<div 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: #606060">   1:</span> --Insert <span style="color: #0000ff">Second</span> <span style="color: #0000ff">Level</span> Child(iPhone 4S)</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: #606060">   2:</span> <span style="color: #0000ff">DECLARE</span> @ProductID <span style="color: #0000ff">INT</span> = 4 <span style="color: #008000">-- The Second Level Child = iPhone 4S</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"><span style="color: #606060">   3:</span> <span style="color: #0000ff">DECLARE</span> @hierarchyid HIERARCHYID</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: #606060">   4:</span> <span style="color: #0000ff">DECLARE</span> @Parenthierarchyid HIERARCHYID</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: #606060">   5:</span> --<span style="color: #0000ff">Get</span> the Parent hierarchyid</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: #606060">   6:</span> <span style="color: #0000ff">SELECT</span> @Parenthierarchyid = hierarchyid</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: #606060">   7:</span> <span style="color: #0000ff">FROM</span> dbo.ProductTree</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: #606060">   8:</span> <span style="color: #0000ff">WHERE</span> ProductID = 2 <span style="color: #008000">-- iPhone</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"><span style="color: #606060">   9:</span> --<span style="color: #0000ff">Get</span> the hierarchyid</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: #606060">  10:</span> <span style="color: #0000ff">SELECT</span> @hierarchyid = @Parenthierarchyid.GetDescendant(<span style="color: #0000ff">NULL</span>,<span style="color: #0000ff">NULL</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"><span style="color: #606060">  11:</span> <span style="color: #0000ff">FROM</span> dbo.ProductTree</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: #606060">  12:</span> --Insert <span style="color: #0000ff">Second</span> <span style="color: #0000ff">Level</span> Child(iPhone 4S)</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: #606060">  13:</span> INSERT <span style="color: #0000ff">INTO</span> dbo.ProductTree</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: #606060">  14:</span> (hierarchyid, Node, ProductID)</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: #606060">  15:</span> <span style="color: #0000ff">VALUES</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: #606060">  16:</span> (@hierarchyid, @hierarchyid.ToString(), @ProductID)</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: #606060">  17:</span> <span style="color: #0000ff">Go</span></pre>
<p><!--CRLF--></p>
</div>
</div>
<p dir="ltr"><span style="font-family: Calibri"><strong><span style="font-family: Arial">Here is the result</span>:</strong></span></p>
<pre><a href="$clip_image0023.jpg"></a><a href="http://www.madeira.co.il/wp-content/uploads/2012/01/5.jpg" rel="wp-prettyPhoto[g3601]"><img class="alignnone size-full wp-image-3699" src="http://www.madeira.co.il/wp-content/uploads/2012/01/5.jpg" alt="" /></a></pre>
<div>
<pre style="text-align: left"><span style="font-family: Arial">?Notice the path representation and how it all fits in. Pretty neat huh</span><span style="font-family: Times New Roman;font-size: large">
Conclusion</span></pre>
</div>
<p style="text-align: left" dir="ltr">According performances, you can use the HierarchyID type without hesitation when modeling tree structures in a relational database. That new type fulfills its goal although it's not simple to implement…</p>
<pre style="text-align: left"><span style="font-family: Arial">...Hope you like this article I will write more about HierarchyID very soon</span></pre>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/the-new-data-type-hierarchyid-is-one-of-the-coolest-and-fantastic-new-features-of-sql-server-2008/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>&#8235;נעים מאוד&#8236;</title>		<link>http://www.madeira.co.il/%d7%a0%d7%a2%d7%99%d7%9d-%d7%9e%d7%90%d7%95%d7%93/</link>
		<comments>http://www.madeira.co.il/%d7%a0%d7%a2%d7%99%d7%9d-%d7%9e%d7%90%d7%95%d7%93/#comments</comments>
		<pubDate>Sun, 21 Feb 2010 14:41:37 +0000</pubDate>
		<dc:creator>&#8235;אסף אביב&#8236;</dc:creator>				<category><![CDATA[בלוגים]]></category>
		<category><![CDATA[מקודמות]]></category>
		<category><![CDATA[asaf aviv]]></category>
		<category><![CDATA[Madeira]]></category>
		<category><![CDATA[אסף אביב]]></category>
		<category><![CDATA[קריירה]]></category>

		<guid isPermaLink="false">http://www.madeira.co.il/?p=1113</guid>
		<description><![CDATA[&#8235;לאחר לאחר שנתיים של ניסיון בתמיכת רשתות, מגוון לקוחות ומנעד עיסוקים רחב, אסף אביב מצטרף באופן רשמי לצוות היועצים של Madeira.&#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/1113.jpg&amp;w=214&amp;h=129&amp;zc=1&amp;ft=jpg' alt='post thumbnail' /></p>
<p dir="rtl">שלום לכולם, אני אסף אביב.</p>
<p dir="rtl">נכנסתי לעולם בסיסי הנתונים לפני 4 שנים ופשוט התאהבתי. במסגרת תפקידי כ-DBA, הספקתי לספק שירותי ייעוץ למספר חברות מובילות בארץ ובעולם, במספר תחומים שונים ומגוונים של מוצרי SQL Server.</p>
<p dir="rtl">אשמח לענות לשאלות בפורום וכמובן לנסות ולחדש באמצעות מאמרים.</p>
<p dir="rtl">אז נשתמע,<br />
אסף</p>
</div>]]></content:encoded>			<wfw:commentRss>http://www.madeira.co.il/%d7%a0%d7%a2%d7%99%d7%9d-%d7%9e%d7%90%d7%95%d7%93/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

