Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
samadhi
Starting Member
1 Post |
Posted - 2003-07-22 : 00:27:18
|
| Hi all,I've got a table named Users having three columns...Name,UserId and ManagerId.Each Manager can have one or more users working under him and then each user might be manager to one or more users.I need to fire an sql query to display a tree like structure of manager,his subordinates,the subordinate's subordinates and so on.I'm using .Net and sql server 2000.I need to populate the treeview web control in asp.net with the data as above(preferably i need to generate the data from the database in xml format so that i can give that xml as datasource to the Treeview web control in the front-end.). Could anyone please help me out with this...???Thanks in advance...Rahul Kumar Saxena |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2003-07-22 : 10:08:21
|
| Ahhh...the hierarchy question againHow many levels deep do you have?There are several ways to do this...But check out Rob's article:http://www.sqlteam.com/item.asp?ItemID=8866Ahh, where's part 2 again?I couldn't crack it...Just used my old refresh everything model...Brett8-) |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-07-23 : 10:54:48
|
(a profane drool after reading rob's article (Part I))CREATE TABLE [a] ( [lev] [int] NULL , [p] [varchar] (5) NOT NULL , [c] [varchar] (5) NOT NULL , CONSTRAINT [PK_a] PRIMARY KEY NONCLUSTERED ( [p], [c] ) ON [PRIMARY] ) ON [PRIMARY]GOCREATE TABLE [aa] ( [p] [varchar] (5) NOT NULL , [nm] [varchar] (5) NULL , CONSTRAINT [PK_aa] PRIMARY KEY NONCLUSTERED ( [p] ) ON [PRIMARY]) ON [PRIMARY]GOp - parent c - child--- a ------------ -- aa ------lev p c p nm------------------ ------------0 g b g john0 g d b paul 0 g e d nick1 b y e don 1 b k y mario 1 b z k rok 1 d r z floyd 1 e q r steve 1 e m q kit 2 q o m zak 2 q w o osuo------------------ w wess -------------- =declare @i int, @p varchar(5), @c varchar(5), @j varchar(8000)set @i=0 set @j=(select top 1 p from a where lev=0)set @j='<'+@j+'></'+@j+'>'while @i<=(select max(lev) from a)begindeclare abc cursor for select p, c from a where lev=@iopen abcfetch next from abc into @p, @cwhile @@fetch_status=0beginset @j=stuff(@j,charindex(@p,@j)+2,0,'<'+@c+'></'+@c+'>')fetch next from abc into @p, @cendclose abcdeallocate abcset @i=@i+1enddeclare abc cursor for select p, nm from aaopen abcfetch next from abc into @p, @cwhile @@fetch_status=0beginset @j=stuff(@j,charindex('<'+@p,@j)+3,0,@c)fetch next from abc into @p, @cendclose abcdeallocate abcprint @j==================================================Output:<g>john<e>don<m>zak</m><q>kit<w>wess</w><o>osuo</o></q></e><d>nick<r>steve</r></d><b>paul<z>floyd</z><k>rok</k><y>mario</y></b></g>- <g> john - <e> don <m>zak</m> - <q> kit <w>wess</w> <o>osuo</o> </q> </e> - <d> nick <r>steve</r> </d> - <b> paul <z>floyd</z> <k>rok</k> <y>mario</y> </b> </g> - Vit |
 |
|
|
Stoad
Freaky Yak Linguist
1983 Posts |
Posted - 2003-07-24 : 04:36:15
|
| ... or on client's VBA code (from ADP in this case) and avoiding 8000 maxsizelimitation (tables are the same as in my previous post):Sub myXML()Dim rs As New ADODB.RecordsetDim i, maxLEV, k, s, ssrs.CursorLocation = adUseClientrs.Open "select * from a order by lev", CurrentProject.Connection, adOpenStatics = "<" & rs(1) & "></" & rs(1) & ">"rs.MoveLastmaxLEV = rs(0)While i <= maxLEVrs.MoveFirstWhile Not rs.EOFIf rs(0) = i Thenss = Split(s, "<" & rs(1) & ">", 2, 1)s = ss(0) & "<" & rs(1) & "><" & rs(2) & "></" & rs(2) & ">" & ss(1)End Ifrs.MoveNextWendi = i + 1Wendrs.CloseDebug.Print s' <g><e><m></m><q><w></w><o></o></q></e><d><r></r></d>' <b><z></z><k></k><y></y></b></g>rs.Open "select * from aa", CurrentProject.Connection, adOpenStaticWhile Not rs.EOFss = Split(s, "<" & rs(0) & ">", 2, 1)s = ss(0) & "<" & rs(0) & ">" & rs(1) & ss(1)rs.MoveNextWendDebug.Print s<g>john<e>don<m>zak</m><q>kit<w>wess</w><o>osuo</o></q></e><d>nick<r>steve</r></d><b>paul<z>floyd</z><k>rok</k><y>mario</y></b></g>rs.CloseSet rs = NothingEnd Sub- Vit |
 |
|
|
|
|
|
|
|