![]() |
| ||
| Depth Travesal Hi, I am a newbie for MSSQL. Previously, I was given a question regarding MSSQL. Although the interview had over, but I am curious to find out on how to solve the question below: There are 2 tables, NAMES (ID INT IDENTITY(1,1), NAME SYSNAME) and RELATIONSHIPS (NAMEID INT, PARENT_NAMEID INT) linked via NAMES.ID= RELANTIONSHIP.NAMEID and where top-most name has a PARENT_NAMEID=0. Show a nested list of names including LEVEL, NAMEID and NAME, where LEVEL indicates the nest level (or depth) from top. You may use functions, stored procedures,views and any other Transact SQL commands compliant with Microsoft SQL 2000. Sample Data: NAMES table content ID NAME 1 Frank 2 Jo 3 Mary 4 Peter 5 Amy RELATIONSHIPS table content NAMEID PARENT_NAMEID 1 0 2 1 3 2 4 1 5 2 Expected Output LEVEL ID NAME 0 1 Frank 1 2 Jo 2 5 Amy 2 3 Mary 1 4 Peter Thanks in advanced |
| ||
| Re: Depth Travesal Try this code in Query Analyser. It enough for small data. If you want to use for big data, modify the code. set nocount on |
| ||
| Re: Depth Travesal Thanks Huangzi, will test it later. Thanks again for the solution you offer. |
| All times are GMT -4. The time now is 2:00 am. |
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC