Click here to Skip to main content
15,892,298 members
Home / Discussions / Database
   

Database

 
GeneralRe: Question about using conditional SQL statement Pin
Ashfield15-Dec-09 20:57
Ashfield15-Dec-09 20:57 
AnswerCROSS POST Pin
dan!sh 15-Dec-09 5:08
professional dan!sh 15-Dec-09 5:08 
GeneralRe: CROSS POST Pin
James Shao15-Dec-09 12:46
James Shao15-Dec-09 12:46 
QuestionSql Doubt Pin
rajanandal15-Dec-09 2:26
rajanandal15-Dec-09 2:26 
AnswerRe: Sql Doubt Pin
Richard MacCutchan15-Dec-09 2:39
mveRichard MacCutchan15-Dec-09 2:39 
GeneralRe: Sql Doubt Pin
rajanandal15-Dec-09 2:53
rajanandal15-Dec-09 2:53 
GeneralRe: Sql Doubt Pin
dxlee15-Dec-09 4:27
dxlee15-Dec-09 4:27 
AnswerRe: Sql Doubt Pin
Niladri_Biswas15-Dec-09 15:18
Niladri_Biswas15-Dec-09 15:18 
Try this.

Inputs:

declare @t table(childid int,name varchar(20),code int,parentid int)
insert into @t values (6219,'HBO',02,0)
insert into @t values(6220,'HBO1', 0201,6219)
insert into @t values(6221,'HEAO',0202,6219)
insert into @t values(6222,'HTS',0203,6219)
insert into @t values(6231,'MEAO',0403,6228)
insert into @t values(6232,'MTS',0404,6228)
insert into @t values(6234,'Midde',05,0)
insert into @t values(6235,'Overi',06,0)
insert into @t values(6228,'MBO',04,0)
insert into @t values(6236,'WO',10,0)
insert into @t values(6237,'Post',07,0)
insert into @t values(6239,'Basi',NULL,NULL)
insert into @t values(6448,'Athe',05001,6234)
select * from @t


Query:

;with cte as
(
	select 
		cast(t1.childid as varchar(1000)) [path]
		, t1.childid
		,t1.name
		,t1.code
		,t1.parentid 
		,0 AS [Level] from @t t1 where parentid =  0 or parentid is null

	union all
	select 
		cast([path] + '/' + cast(t1.childid as varchar(1000)) as varchar(1000)) [path]
		,t1.childid
		,t1.name
		,t1.code
		,t1.parentid 
		,c.[Level]+1 AS [Level]from @t t1
	join cte c
	on c.childid = t1.parentid
)
select childid,name,code,parentid
from cte order by [path]


Output:

childid	name	code	parentid
6219	HBO	2	0
6220	HBO1	201	6219
6221	HEAO	202	6219
6222	HTS	203	6219
6228	MBO	4	0
6231	MEAO	403	6228
6232	MTS	404	6228
6234	Midde	5	0
6448	Athe	5001	6234
6235	Overi	6	0
6236	WO	10	0
6237	Post	7	0
6239	Basi	NULL	NULL

Smile | :)

Niladri Biswas

GeneralRe: Sql Doubt Pin
rajanandal15-Dec-09 23:28
rajanandal15-Dec-09 23:28 
QuestionSQL Between Stored Proc Pin
Illegal Operation14-Dec-09 18:08
Illegal Operation14-Dec-09 18:08 
AnswerRe: SQL Between Stored Proc Pin
Niladri_Biswas14-Dec-09 18:15
Niladri_Biswas14-Dec-09 18:15 
AnswerRe: SQL Between Stored Proc Pin
Andy_L_J14-Dec-09 18:36
Andy_L_J14-Dec-09 18:36 
Questionssis script component Pin
Ebube14-Dec-09 4:24
Ebube14-Dec-09 4:24 
QuestionRe: ssis script component Pin
Unforgiv3n17-Dec-09 20:50
Unforgiv3n17-Dec-09 20:50 
QuestionCan VS support Ruby on Rails -like database migrations? Pin
GarethHowell14-Dec-09 2:40
GarethHowell14-Dec-09 2:40 
AnswerRe: Can VS support Ruby on Rails -like database migrations? Pin
i.j.russell14-Dec-09 9:07
i.j.russell14-Dec-09 9:07 
Questionwhere clause in sql [modified] Pin
AndyInUK14-Dec-09 0:22
AndyInUK14-Dec-09 0:22 
AnswerRe: where clause in sql Pin
David Skelly14-Dec-09 0:34
David Skelly14-Dec-09 0:34 
GeneralRe: where clause in sql Pin
AndyInUK14-Dec-09 0:39
AndyInUK14-Dec-09 0:39 
AnswerRe: where clause in sql Pin
Luc Pattyn14-Dec-09 1:29
sitebuilderLuc Pattyn14-Dec-09 1:29 
GeneralRe: where clause in sql Pin
AndyInUK14-Dec-09 1:31
AndyInUK14-Dec-09 1:31 
GeneralRe: where clause in sql Pin
Eddy Vluggen14-Dec-09 1:40
professionalEddy Vluggen14-Dec-09 1:40 
GeneralRe: where clause in sql Pin
Luc Pattyn14-Dec-09 1:59
sitebuilderLuc Pattyn14-Dec-09 1:59 
GeneralRe: where clause in sql Pin
Niladri_Biswas14-Dec-09 18:12
Niladri_Biswas14-Dec-09 18:12 
QuestionDSn Creation through SQLConfigDataSource() funciton Pin
Durga_Devi14-Dec-09 0:19
Durga_Devi14-Dec-09 0:19 

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.