Click here to Skip to main content
15,908,674 members
Home / Discussions / Database
   

Database

 
GeneralRe: Get hirarchy query Pin
ScottM118-Aug-10 2:10
ScottM118-Aug-10 2:10 
GeneralRe: Get hirarchy query Pin
SatyaKeerthi1518-Aug-10 18:52
SatyaKeerthi1518-Aug-10 18:52 
GeneralRe: Get hirarchy query Pin
ScottM118-Aug-10 20:56
ScottM118-Aug-10 20:56 
GeneralRe: Get hirarchy query Pin
SatyaKeerthi1518-Aug-10 21:11
SatyaKeerthi1518-Aug-10 21:11 
GeneralRe: Get hirarchy query Pin
ScottM118-Aug-10 21:17
ScottM118-Aug-10 21:17 
GeneralRe: Get hirarchy query Pin
SatyaKeerthi1518-Aug-10 19:01
SatyaKeerthi1518-Aug-10 19:01 
QuestionGet hirarchy query Pin
SatyaKeerthi1518-Aug-10 19:02
SatyaKeerthi1518-Aug-10 19:02 
AnswerRe: Get hirarchy query Pin
ps_prakash0219-Aug-10 21:31
ps_prakash0219-Aug-10 21:31 
Have a look to this code and see whether it solves your problem,

CREATE TABLE TempTree (Id int IDENTITY, Id_Project VARCHAR(100), Id_Parent VARCHAR(100))
INSERT INTO TempTree (Id_Project, Id_Parent) VALUES ('Root','Root')
INSERT INTO TempTree (Id_Project, Id_Parent) VALUES ('Level - 1 1', 'Root')
INSERT INTO TempTree (Id_Project, Id_Parent) VALUES ('Level - 1 2', 'Root')
INSERT INTO TempTree (Id_Project, Id_Parent) VALUES ('Level - 1 3', 'Root')
INSERT INTO TempTree (Id_Project, Id_Parent) VALUES ('Level - 2 1', 'Level - 1 1')
INSERT INTO TempTree (Id_Project, Id_Parent) VALUES ('Level - 2 2', 'Level - 1 1')
INSERT INTO TempTree (Id_Project, Id_Parent) VALUES ('Level - 2 3', 'Level - 1 2')
INSERT INTO TempTree (Id_Project, Id_Parent) VALUES ('Level - 2 4', 'Level - 1 2')
INSERT INTO TempTree (Id_Project, Id_Parent) VALUES ('Level - 2 5', 'Level - 1 3')
INSERT INTO TempTree (Id_Project, Id_Parent) VALUES ('Level - 2 6', 'Level - 1 3')

INSERT INTO TempTree (Id_Project, Id_Parent) VALUES ('Level - 3 1', 'Level - 2 1')
INSERT INTO TempTree (Id_Project, Id_Parent) VALUES ('Level - 3 2', 'Level - 2 1')
INSERT INTO TempTree (Id_Project, Id_Parent) VALUES ('Level - 3 3', 'Level - 2 2')
INSERT INTO TempTree (Id_Project, Id_Parent) VALUES ('Level - 3 4', 'Level - 2 2')
INSERT INTO TempTree (Id_Project, Id_Parent) VALUES ('Level - 3 5', 'Level - 2 3')
INSERT INTO TempTree (Id_Project, Id_Parent) VALUES ('Level - 3 6', 'Level - 2 3')
INSERT INTO TempTree (Id_Project, Id_Parent) VALUES ('Level - 3 7', 'Level - 2 4')
INSERT INTO TempTree (Id_Project, Id_Parent) VALUES ('Level - 3 8', 'Level - 2 4')
INSERT INTO TempTree (Id_Project, Id_Parent) VALUES ('Level - 3 9', 'Level - 2 5')
INSERT INTO TempTree (Id_Project, Id_Parent) VALUES ('Level - 3 10', 'Level - 2 5')
INSERT INTO TempTree (Id_Project, Id_Parent) VALUES ('Level - 3 11', 'Level - 2 6')
INSERT INTO TempTree (Id_Project, Id_Parent) VALUES ('Level - 3 12', 'Level - 2 6')

CREATE PROC Dbo.Proc_TheTree (@parent VARCHAR(100))
AS

CREATE TABLE #TheList (RootId int, RootName VARCHAR(100), ChildName VARCHAR(100))
CREATE TABLE #TheSearch (SLNO INT IDENTITY, ParentName VARCHAR(100), IsSearchCompleted BIT)

IF NOT EXISTS (SELECT * FROM TempTree WHERE id_parent = @parent)
BEGIN
SELECT * FROM TempTree WHERE id_project = @parent
END
ELSE
BEGIN
INSERT INTO #TheSearch (ParentName, IsSearchCompleted)
SELECT id_project, 0 FROM TempTree WHERE id_parent = @parent

INSERT INTO #TheList (RootId, RootName, ChildName)
SELECT (SELECT Id FROM TempTree WHERE Id_Project= @parent), Id_Parent, Id_Project
FROM TempTree
WHERE id_parent = @parent

DECLARE @MINSLNO INT
DECLARE @PARENTNAME VARCHAR(100)

SELECT @MINSLNO = MIN(SLNO) FROM #TheSearch
WHILE ISNULL(@MINSLNO,0) > 0
BEGIN

SELECT @PARENTNAME = ParentName FROM #TheSearch
WHERE SLNO = @MINSLNO AND ParentName <> @parent

IF EXISTS (SELECT * FROM TempTree WHERE id_parent = @PARENTNAME)
BEGIN
INSERT INTO #TheList (RootId, RootName, ChildName)
SELECT (SELECT Id FROM TempTree WHERE Id_Project= @PARENTNAME), Id_Parent, Id_Project
FROM TempTree
WHERE id_parent = @PARENTNAME

INSERT INTO #TheSearch (ParentName, IsSearchCompleted)
SELECT id_project, 0 FROM TempTree WHERE id_parent = @PARENTNAME
END

SELECT @MINSLNO = MIN(SLNO) FROM #TheSearch WHERE SLNO > @MINSLNO
END

SELECT * FROM #TheList
END

EXEC Proc_TheTree 'Level - 1 1'
QuestionHow to query exclusion? SQL Help needed Pin
Kyudos16-Aug-10 17:36
Kyudos16-Aug-10 17:36 
AnswerRe: How to query exclusion? SQL Help needed Pin
Kyudos16-Aug-10 17:44
Kyudos16-Aug-10 17:44 
GeneralRe: How to query exclusion? SQL Help needed Pin
Mycroft Holmes16-Aug-10 18:22
professionalMycroft Holmes16-Aug-10 18:22 
GeneralRe: How to query exclusion? SQL Help needed Pin
Kyudos17-Aug-10 10:46
Kyudos17-Aug-10 10:46 
GeneralRe: How to query exclusion? SQL Help needed Pin
Kyudos17-Aug-10 13:18
Kyudos17-Aug-10 13:18 
GeneralRe: How to query exclusion? SQL Help needed Pin
Mycroft Holmes17-Aug-10 13:31
professionalMycroft Holmes17-Aug-10 13:31 
GeneralRe: How to query exclusion? SQL Help needed Pin
Kyudos18-Aug-10 11:10
Kyudos18-Aug-10 11:10 
GeneralRe: How to query exclusion? SQL Help needed Pin
Kyudos17-Aug-10 13:30
Kyudos17-Aug-10 13:30 
QuestionSelect and Update Pin
T.RATHA KRISHNAN15-Aug-10 20:53
T.RATHA KRISHNAN15-Aug-10 20:53 
AnswerRe: Select and Update Pin
Luc Pattyn16-Aug-10 0:22
sitebuilderLuc Pattyn16-Aug-10 0:22 
Questionaccess database Pin
sharmakamal14-Aug-10 7:45
sharmakamal14-Aug-10 7:45 
AnswerRe: access database Pin
Luc Pattyn14-Aug-10 8:59
sitebuilderLuc Pattyn14-Aug-10 8:59 
GeneralRe: access database Pin
PIEBALDconsult14-Aug-10 15:19
mvePIEBALDconsult14-Aug-10 15:19 
GeneralRe: access database Pin
Luc Pattyn14-Aug-10 15:24
sitebuilderLuc Pattyn14-Aug-10 15:24 
GeneralRe: access database Pin
PIEBALDconsult14-Aug-10 16:13
mvePIEBALDconsult14-Aug-10 16:13 
GeneralRe: access database Pin
Corporal Agarn17-Aug-10 1:10
professionalCorporal Agarn17-Aug-10 1:10 
AnswerRe: access database Pin
Dr.Walt Fair, PE14-Aug-10 10:15
professionalDr.Walt Fair, PE14-Aug-10 10:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.