<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-321205227862496353</id><updated>2024-08-28T20:37:25.534-07:00</updated><category term="T-SQL"/><category term="recursive query"/><title type='text'>.Net Programming</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://code-cs.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/321205227862496353/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://code-cs.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>lt</name><uri>http://www.blogger.com/profile/17164667078965864307</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-321205227862496353.post-4843494774103563300</id><published>2011-04-01T10:37:00.000-07:00</published><updated>2011-04-01T11:08:41.589-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="T-SQL"/><title type='text'>SQL limit</title><content type='html'>A lot of you are missing &lt;strong&gt;LIMIT&lt;/strong&gt; of MySql, but with some minor changes you can do this.&lt;br /&gt;So asuming we have a tables called &lt;a href=&quot;http://code-cs.blogspot.com/2011/03/sql-20052008-recursive-query.html&quot;&gt;TreeEmployees&lt;/a&gt; we will use &lt;strong&gt;ROW_NUMBER()&lt;/strong&gt; sql function as an order index to the where clause.&lt;br /&gt;Let&#39;s say we have a simle query&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;SELECT&lt;/span&gt; [Id]&lt;br /&gt;,[ParentId]&lt;br /&gt;,[Name]&lt;br /&gt;,[Description]&lt;br /&gt;,[&lt;span class=&quot;kwrd&quot;&gt;Position&lt;/span&gt;]&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;FROM&lt;/span&gt; [Sample].[dbo].[TreeEmployees]&lt;/pre&gt;but we want to paginate this&lt;br /&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;SELECT&lt;/span&gt; * &lt;span class=&quot;kwrd&quot;&gt;FROM&lt;/span&gt; (&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;SELECT&lt;/span&gt; [Id]&lt;br /&gt;,[ParentId]&lt;br /&gt;,[Name]&lt;br /&gt;,[Description]&lt;br /&gt;,[&lt;span class=&quot;kwrd&quot;&gt;Position&lt;/span&gt;]&lt;br /&gt;, ROW_NUMBER() &lt;span class=&quot;kwrd&quot;&gt;OVER&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;ORDER&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;BY&lt;/span&gt; Name) &lt;span class=&quot;kwrd&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;row&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;FROM&lt;/span&gt; TreeEmployees ) tree&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;row&lt;/span&gt; &amp;gt;= 1 &lt;span class=&quot;kwrd&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;row&lt;/span&gt; &amp;lt;= 10&lt;/pre&gt;This query will just take the first 10 records. Notice that first row will just have index 1 not 0.&lt;br /&gt;&lt;br /&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Id&lt;/td&gt;&lt;td&gt;ParentId&lt;/td&gt;&lt;td&gt;Name&lt;/td&gt;&lt;td&gt;Description&lt;/td&gt;&lt;td&gt;Position&lt;/td&gt;&lt;td&gt;row&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;7&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;Anna Mendez&lt;/td&gt;&lt;td&gt;Member of Joyce&#39;s team&lt;/td&gt;&lt;td&gt;Worker&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;9&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;Carter Bowls&lt;/td&gt;&lt;td&gt;Jan&#39;s team 1&lt;/td&gt;&lt;td&gt;Team Leader&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;NULL&lt;/td&gt;&lt;td&gt;Jan Right&lt;/td&gt;&lt;td&gt;Jan folowers&lt;/td&gt;&lt;td&gt;Chief Account manager&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;Jimmy Clarck&lt;/td&gt;&lt;td&gt;Jan&#39;s team 2&lt;/td&gt;&lt;td&gt;Team Leader&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;NULL&lt;/td&gt;&lt;td&gt;John Doe&lt;/td&gt;&lt;td&gt;John folowers&lt;/td&gt;&lt;td&gt;Technical Manager&lt;/td&gt;&lt;td&gt;5&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;Joyce May Ann&lt;/td&gt;&lt;td&gt;Joyce team&lt;/td&gt;&lt;td&gt;Team Leader&lt;/td&gt;&lt;td&gt;6&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;6&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;Laurel Bean&lt;/td&gt;&lt;td&gt;Member of Flynn&#39;s team&lt;/td&gt;&lt;td&gt;Worker&lt;/td&gt;&lt;td&gt;7&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;13&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;Lilly Ann Parker&lt;/td&gt;&lt;td&gt;Jimmy&#39;s team&lt;/td&gt;&lt;td&gt;Worker&lt;/td&gt;&lt;td&gt;8&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;12&lt;/td&gt;&lt;td&gt;9&lt;/td&gt;&lt;td&gt;Louise Senna&lt;/td&gt;&lt;td&gt;Carter&#39;s team&lt;/td&gt;&lt;td&gt;Worker&lt;/td&gt;&lt;td&gt;9&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;8&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;Maria Fernandez&lt;/td&gt;&lt;td&gt;Member of Joyce&#39;s team&lt;/td&gt;&lt;td&gt;Worker&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;a href=&quot;http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fcode-cs.blogspot.com%2f2011%2f04%2fsql-limit.html&quot;&gt;&lt;img src=&quot;http://www.blogger.com/http%3A%2F%2Fwww.dotnetkicks.com%2FServices%2FImages%2FKickItImageGenerator.ashx%3Furl%3Dhttp%253a%252f%252fcode-cs.blogspot.com%252f2011%252f04%252fsql-limit.html%26fgcolor%3D00CCFF&quot; border=&quot;0&quot; alt=&quot;kick it on DotNetKicks.com&quot; /&gt;&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://code-cs.blogspot.com/feeds/4843494774103563300/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-cs.blogspot.com/2011/04/sql-limit.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/321205227862496353/posts/default/4843494774103563300'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/321205227862496353/posts/default/4843494774103563300'/><link rel='alternate' type='text/html' href='http://code-cs.blogspot.com/2011/04/sql-limit.html' title='SQL limit'/><author><name>lt</name><uri>http://www.blogger.com/profile/17164667078965864307</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-321205227862496353.post-2734592253585814494</id><published>2011-03-26T02:13:00.000-07:00</published><updated>2011-04-01T11:05:20.154-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="recursive query"/><category scheme="http://www.blogger.com/atom/ns#" term="T-SQL"/><title type='text'>Sql 2005/2008 recursive query</title><content type='html'>&lt;div&gt;Let&#39;s have the following example:&lt;br /&gt;We have a table with and Id and ParentId hold relations between employees and we want to see all employees that are under a manager or team leader.&lt;br /&gt;First the sql table and data&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;CREATE&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;TABLE&lt;/span&gt; [dbo].[TreeEmployees](&lt;br /&gt;  [Id] [bigint] &lt;span class=&quot;kwrd&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;NULL&lt;/span&gt;,&lt;br /&gt;  [ParentId] [bigint] &lt;span class=&quot;kwrd&quot;&gt;NULL&lt;/span&gt;,&lt;br /&gt;  [Name] [&lt;span class=&quot;kwrd&quot;&gt;varchar&lt;/span&gt;](50) &lt;span class=&quot;kwrd&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;NULL&lt;/span&gt;,&lt;br /&gt;  [Description] [&lt;span class=&quot;kwrd&quot;&gt;varchar&lt;/span&gt;](200) &lt;span class=&quot;kwrd&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;NULL&lt;/span&gt;,&lt;br /&gt;  [&lt;span class=&quot;kwrd&quot;&gt;Position&lt;/span&gt;] [&lt;span class=&quot;kwrd&quot;&gt;varchar&lt;/span&gt;](50) &lt;span class=&quot;kwrd&quot;&gt;NOT&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;NULL&lt;/span&gt;,&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;CONSTRAINT&lt;/span&gt; [PK_Tree] &lt;span class=&quot;kwrd&quot;&gt;PRIMARY&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;KEY&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;CLUSTERED&lt;/span&gt;&lt;br /&gt;(&lt;br /&gt;  [Id] &lt;span class=&quot;kwrd&quot;&gt;ASC&lt;/span&gt;&lt;br /&gt;)&lt;span class=&quot;kwrd&quot;&gt;WITH&lt;/span&gt; (PAD_INDEX  = &lt;span class=&quot;kwrd&quot;&gt;OFF&lt;/span&gt;, STATISTICS_NORECOMPUTE  = &lt;span class=&quot;kwrd&quot;&gt;OFF&lt;/span&gt;, IGNORE_DUP_KEY = &lt;span class=&quot;kwrd&quot;&gt;OFF&lt;/span&gt;, ALLOW_ROW_LOCKS  = &lt;span class=&quot;kwrd&quot;&gt;ON&lt;/span&gt;, ALLOW_PAGE_LOCKS  = &lt;span class=&quot;kwrd&quot;&gt;ON&lt;/span&gt;) &lt;span class=&quot;kwrd&quot;&gt;ON&lt;/span&gt; [&lt;span class=&quot;kwrd&quot;&gt;PRIMARY&lt;/span&gt;]&lt;br /&gt;) &lt;span class=&quot;kwrd&quot;&gt;ON&lt;/span&gt; [&lt;span class=&quot;kwrd&quot;&gt;PRIMARY&lt;/span&gt;]&lt;br /&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;SET&lt;/span&gt; ANSI_PADDING &lt;span class=&quot;kwrd&quot;&gt;OFF&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;ALTER&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;TABLE&lt;/span&gt; [dbo].[TreeEmployees] &lt;span class=&quot;kwrd&quot;&gt;ADD&lt;/span&gt;  &lt;span class=&quot;kwrd&quot;&gt;CONSTRAINT&lt;/span&gt; [DF_Tree_Name]  &lt;span class=&quot;kwrd&quot;&gt;DEFAULT&lt;/span&gt; (&lt;span class=&quot;str&quot;&gt;&#39;&#39;&lt;/span&gt;) &lt;span class=&quot;kwrd&quot;&gt;FOR&lt;/span&gt; [Name]&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;ALTER&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;TABLE&lt;/span&gt; [dbo].[TreeEmployees] &lt;span class=&quot;kwrd&quot;&gt;ADD&lt;/span&gt;  &lt;span class=&quot;kwrd&quot;&gt;CONSTRAINT&lt;/span&gt; [DF_Tree_Description]  &lt;span class=&quot;kwrd&quot;&gt;DEFAULT&lt;/span&gt; (&lt;span class=&quot;str&quot;&gt;&#39;&#39;&lt;/span&gt;) &lt;span class=&quot;kwrd&quot;&gt;FOR&lt;/span&gt; [Description]&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;ALTER&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;TABLE&lt;/span&gt; [dbo].[TreeEmployees] &lt;span class=&quot;kwrd&quot;&gt;ADD&lt;/span&gt;  &lt;span class=&quot;kwrd&quot;&gt;CONSTRAINT&lt;/span&gt; [DF_Tree_Position]  &lt;span class=&quot;kwrd&quot;&gt;DEFAULT&lt;/span&gt; (&lt;span class=&quot;str&quot;&gt;&#39;&#39;&lt;/span&gt;) &lt;span class=&quot;kwrd&quot;&gt;FOR&lt;/span&gt; [&lt;span class=&quot;kwrd&quot;&gt;Position&lt;/span&gt;]&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;GO&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Add sql data&lt;br /&gt;&lt;/pre&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;INSERT&lt;/span&gt; [dbo].[TreeEmployees] ([Id], [ParentId], [Name], [Description], [&lt;span class=&quot;kwrd&quot;&gt;Position&lt;/span&gt;]) &lt;span class=&quot;kwrd&quot;&gt;VALUES&lt;/span&gt; (1, &lt;span class=&quot;kwrd&quot;&gt;NULL&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;John Doe&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;John folowers&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Technical Manager&#39;&lt;/span&gt;)&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;INSERT&lt;/span&gt; [dbo].[TreeEmployees] ([Id], [ParentId], [Name], [Description], [&lt;span class=&quot;kwrd&quot;&gt;Position&lt;/span&gt;]) &lt;span class=&quot;kwrd&quot;&gt;VALUES&lt;/span&gt; (2, &lt;span class=&quot;kwrd&quot;&gt;NULL&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Jan Right&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Jan folowers&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Chief Account manager&#39;&lt;/span&gt;)&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;INSERT&lt;/span&gt; [dbo].[TreeEmployees] ([Id], [ParentId], [Name], [Description], [&lt;span class=&quot;kwrd&quot;&gt;Position&lt;/span&gt;]) &lt;span class=&quot;kwrd&quot;&gt;VALUES&lt;/span&gt; (3, 1, N&lt;span class=&quot;str&quot;&gt;&#39;Martin Flynn&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Flyn team&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Team Leader&#39;&lt;/span&gt;)&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;INSERT&lt;/span&gt; [dbo].[TreeEmployees] ([Id], [ParentId], [Name], [Description], [&lt;span class=&quot;kwrd&quot;&gt;Position&lt;/span&gt;]) &lt;span class=&quot;kwrd&quot;&gt;VALUES&lt;/span&gt; (4, 1, N&lt;span class=&quot;str&quot;&gt;&#39;Joyce May Ann&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Joyce team&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Team Leader&#39;&lt;/span&gt;)&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;INSERT&lt;/span&gt; [dbo].[TreeEmployees] ([Id], [ParentId], [Name], [Description], [&lt;span class=&quot;kwrd&quot;&gt;Position&lt;/span&gt;]) &lt;span class=&quot;kwrd&quot;&gt;VALUES&lt;/span&gt; (5, 3, N&lt;span class=&quot;str&quot;&gt;&#39;Mia Donovan&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Meber of Fylnn&#39;&lt;/span&gt;&lt;span class=&quot;str&quot;&gt;&#39;s team&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Worker&#39;&lt;/span&gt;)&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;INSERT&lt;/span&gt; [dbo].[TreeEmployees] ([Id], [ParentId], [Name], [Description], [&lt;span class=&quot;kwrd&quot;&gt;Position&lt;/span&gt;]) &lt;span class=&quot;kwrd&quot;&gt;VALUES&lt;/span&gt; (6, 3, N&lt;span class=&quot;str&quot;&gt;&#39;Laurel Bean&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Member of Flynn&#39;&lt;/span&gt;&lt;span class=&quot;str&quot;&gt;&#39;s team&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Worker&#39;&lt;/span&gt;)&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;INSERT&lt;/span&gt; [dbo].[TreeEmployees] ([Id], [ParentId], [Name], [Description], [&lt;span class=&quot;kwrd&quot;&gt;Position&lt;/span&gt;]) &lt;span class=&quot;kwrd&quot;&gt;VALUES&lt;/span&gt; (7, 4, N&lt;span class=&quot;str&quot;&gt;&#39;Anna Mendez&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Member of Joyce&#39;&lt;/span&gt;&lt;span class=&quot;str&quot;&gt;&#39;s team&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Worker&#39;&lt;/span&gt;)&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;INSERT&lt;/span&gt; [dbo].[TreeEmployees] ([Id], [ParentId], [Name], [Description], [&lt;span class=&quot;kwrd&quot;&gt;Position&lt;/span&gt;]) &lt;span class=&quot;kwrd&quot;&gt;VALUES&lt;/span&gt; (8, 4, N&lt;span class=&quot;str&quot;&gt;&#39;Maria Fernandez&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Member of Joyce&#39;&lt;/span&gt;&lt;span class=&quot;str&quot;&gt;&#39;s team&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Worker&#39;&lt;/span&gt;)&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;INSERT&lt;/span&gt; [dbo].[TreeEmployees] ([Id], [ParentId], [Name], [Description], [&lt;span class=&quot;kwrd&quot;&gt;Position&lt;/span&gt;]) &lt;span class=&quot;kwrd&quot;&gt;VALUES&lt;/span&gt; (9, 2, N&lt;span class=&quot;str&quot;&gt;&#39;Carter Bowls&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Jan&#39;&lt;/span&gt;&lt;span class=&quot;str&quot;&gt;&#39;s team 1&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Team Leader&#39;&lt;/span&gt;)&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;INSERT&lt;/span&gt; [dbo].[TreeEmployees] ([Id], [ParentId], [Name], [Description], [&lt;span class=&quot;kwrd&quot;&gt;Position&lt;/span&gt;]) &lt;span class=&quot;kwrd&quot;&gt;VALUES&lt;/span&gt; (10, 2, N&lt;span class=&quot;str&quot;&gt;&#39;Jimmy Clarck&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Jan&#39;&lt;/span&gt;&lt;span class=&quot;str&quot;&gt;&#39;s team 2&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Team Leader&#39;&lt;/span&gt;)&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;INSERT&lt;/span&gt; [dbo].[TreeEmployees] ([Id], [ParentId], [Name], [Description], [&lt;span class=&quot;kwrd&quot;&gt;Position&lt;/span&gt;]) &lt;span class=&quot;kwrd&quot;&gt;VALUES&lt;/span&gt; (11, 9, N&lt;span class=&quot;str&quot;&gt;&#39;Sully Westwood&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Carter&#39;&lt;/span&gt;&lt;span class=&quot;str&quot;&gt;&#39;s team&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Worker&#39;&lt;/span&gt;)&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;INSERT&lt;/span&gt; [dbo].[TreeEmployees] ([Id], [ParentId], [Name], [Description], [&lt;span class=&quot;kwrd&quot;&gt;Position&lt;/span&gt;]) &lt;span class=&quot;kwrd&quot;&gt;VALUES&lt;/span&gt; (12, 9, N&lt;span class=&quot;str&quot;&gt;&#39;Louise Senna&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Carter&#39;&lt;/span&gt;&lt;span class=&quot;str&quot;&gt;&#39;s team&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Worker&#39;&lt;/span&gt;)&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;INSERT&lt;/span&gt; [dbo].[TreeEmployees] ([Id], [ParentId], [Name], [Description], [&lt;span class=&quot;kwrd&quot;&gt;Position&lt;/span&gt;]) &lt;span class=&quot;kwrd&quot;&gt;VALUES&lt;/span&gt; (13, 10, N&lt;span class=&quot;str&quot;&gt;&#39;Lilly Ann Parker&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Jimmy&#39;&lt;/span&gt;&lt;span class=&quot;str&quot;&gt;&#39;s team&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Worker&#39;&lt;/span&gt;)&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;INSERT&lt;/span&gt; [dbo].[TreeEmployees] ([Id], [ParentId], [Name], [Description], [&lt;span class=&quot;kwrd&quot;&gt;Position&lt;/span&gt;]) &lt;span class=&quot;kwrd&quot;&gt;VALUES&lt;/span&gt; (14, 10, N&lt;span class=&quot;str&quot;&gt;&#39;Tina Riggs&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Jimmy&#39;&lt;/span&gt;&lt;span class=&quot;str&quot;&gt;&#39;s team&#39;&lt;/span&gt;, N&lt;span class=&quot;str&quot;&gt;&#39;Worker&#39;&lt;/span&gt;)&lt;br /&gt;&lt;br /&gt;And here is the recursive query:&lt;/pre&gt;&lt;pre class=&quot;csharpcode&quot;&gt;&lt;span class=&quot;kwrd&quot;&gt;declare&lt;/span&gt; @rootId bigint&lt;br /&gt;--&lt;span class=&quot;kwrd&quot;&gt;set&lt;/span&gt; this &lt;span class=&quot;kwrd&quot;&gt;to&lt;/span&gt; 1 &lt;span class=&quot;kwrd&quot;&gt;or&lt;/span&gt; 2&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;set&lt;/span&gt; @rootId = 2;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;WITH&lt;/span&gt;  TeamMembers  &lt;span class=&quot;kwrd&quot;&gt;as&lt;/span&gt;&lt;br /&gt;(&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;select&lt;/span&gt; Id, ParentId, Name, [Description],[&lt;span class=&quot;kwrd&quot;&gt;Position&lt;/span&gt;], 0 &lt;span class=&quot;kwrd&quot;&gt;as&lt;/span&gt; [&lt;span class=&quot;kwrd&quot;&gt;level&lt;/span&gt;] &lt;span class=&quot;kwrd&quot;&gt;from&lt;/span&gt; TreeEmployees&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;where&lt;/span&gt; Id=@rootId&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;union&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;all&lt;/span&gt;&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;select&lt;/span&gt; tc.id, tc.ParentId, tc.Name, tc.[Description],tc.[&lt;span class=&quot;kwrd&quot;&gt;Position&lt;/span&gt;],[&lt;span class=&quot;kwrd&quot;&gt;level&lt;/span&gt;] +1 &lt;span class=&quot;kwrd&quot;&gt;as&lt;/span&gt; [&lt;span class=&quot;kwrd&quot;&gt;level&lt;/span&gt;] &lt;span class=&quot;kwrd&quot;&gt;from&lt;/span&gt; TreeEmployees &lt;span class=&quot;kwrd&quot;&gt;as&lt;/span&gt; tc&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;inner&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;join&lt;/span&gt; TeamMembers &lt;span class=&quot;kwrd&quot;&gt;on&lt;/span&gt; tc.ParentId = TeamMembers.Id&lt;br /&gt;)&lt;br /&gt;&lt;span class=&quot;kwrd&quot;&gt;select&lt;/span&gt; * &lt;span class=&quot;kwrd&quot;&gt;from&lt;/span&gt; TeamMembers&lt;/pre&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt;And here are the results:&lt;/p&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Id&lt;/td&gt;&lt;td&gt;ParentId&lt;/td&gt;&lt;td&gt;Name&lt;/td&gt;&lt;td&gt;Description&lt;/td&gt;&lt;td&gt;Position&lt;/td&gt;&lt;td&gt;level&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;NULL&lt;/td&gt;&lt;td&gt;Jan Right&lt;/td&gt;&lt;td&gt;Jan folowers&lt;/td&gt;&lt;td&gt;Chief Account manager&lt;/td&gt;&lt;td&gt;0&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;9&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;Carter Bowls&lt;/td&gt;&lt;td&gt;Jan&#39;s team 1&lt;/td&gt;&lt;td&gt;Team Leader&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;Jimmy Clarck&lt;/td&gt;&lt;td&gt;Jan&#39;s team 2&lt;/td&gt;&lt;td&gt;Team Leader&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;13&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;Lilly Ann Parker&lt;/td&gt;&lt;td&gt;Jimmy&#39;s team&lt;/td&gt;&lt;td&gt;Worker&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;14&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;td&gt;Tina Riggs&lt;/td&gt;&lt;td&gt;Jimmy&#39;s team&lt;/td&gt;&lt;td&gt;Worker&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;11&lt;/td&gt;&lt;td&gt;9&lt;/td&gt;&lt;td&gt;Sully Westwood&lt;/td&gt;&lt;td&gt;Carter&#39;s team&lt;/td&gt;&lt;td&gt;Worker&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;12&lt;/td&gt;&lt;td&gt;9&lt;/td&gt;&lt;td&gt;Louise Senna&lt;/td&gt;&lt;td&gt;Carter&#39;s team&lt;/td&gt;&lt;td&gt;Worker&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;a href=&quot;http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fcode-cs.blogspot.com%2f2011%2f03%2fsql-20052008-recursive-query.html&quot;&gt;&lt;img src=&quot;http://www.blogger.com/http%3A%2F%2Fwww.dotnetkicks.com%2FServices%2FImages%2FKickItImageGenerator.ashx%3Furl%3Dhttp%253a%252f%252fcode-cs.blogspot.com%252f2011%252f03%252fsql-20052008-recursive-query.html&quot; border=&quot;0&quot; alt=&quot;kick it on DotNetKicks.com&quot; /&gt;&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://code-cs.blogspot.com/feeds/2734592253585814494/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://code-cs.blogspot.com/2011/03/sql-20052008-recursive-query.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/321205227862496353/posts/default/2734592253585814494'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/321205227862496353/posts/default/2734592253585814494'/><link rel='alternate' type='text/html' href='http://code-cs.blogspot.com/2011/03/sql-20052008-recursive-query.html' title='Sql 2005/2008 recursive query'/><author><name>lt</name><uri>http://www.blogger.com/profile/17164667078965864307</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>