![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
#3
| |||
| |||
|
|
How to achieve tree structure in MSSQL I have two tables as follows. I need a hierarchical structure Test category id Demo 1 Windows Hosting 2 Linux Hosting 3 General 4 Test111 subcatid subcatname parentid parentsubcatid 1 ademo 1 NULL 2 bdemo 1 NULL 3 cdemo 1 NULL 4 awh 2 NULL 5 bwh 2 NULL 6 cwh 2 NULL 7 aademo 1 1 8 a1demo 1 1 9 a2demo 1 1 10 a3demo 1 1 11 b1demo 1 2 12 b2demo 1 2 13 b3demo 1 2 14 c1demo 1 3 15 c2demo 1 3 16 c3demo 1 3 parentid in the Test111 has a foreign key relation with the Test id and parentsubcatid has a foreign key relationship with the subcatid The required output is Demo -- ademo -- -- aademo -- -- a1demo -- -- a2demo ... -- bdemo -- -- b1demo -- -- b2.demo There is no b2.demo in yoursample data, only b2data .... -- cdemo ... If any body have idea then please let me know. Thnaks in advance Regards NItin Roman |
#4
| |||
| |||
|
|
On Jun 27, 8:38 am, Nitin <nitinro... (AT) gmail (DOT) com> wrote: How to achieve tree structure in MSSQL I have two tables as follows. I need a hierarchical structure Test category id Demo 1 Windows Hosting 2 Linux Hosting 3 General 4 Test111 subcatid subcatname parentid parentsubcatid 1 ademo 1 NULL 2 bdemo 1 NULL 3 cdemo 1 NULL 4 awh 2 NULL 5 bwh 2 NULL 6 cwh 2 NULL 7 aademo 1 1 8 a1demo 1 1 9 a2demo 1 1 10 a3demo 1 1 11 b1demo 1 2 12 b2demo 1 2 13 b3demo 1 2 14 c1demo 1 3 15 c2demo 1 3 16 c3demo 1 3 parentid in the Test111 has a foreign key relation with the Test id and parentsubcatid has a foreign key relationship with the subcatid The required output is Demo -- ademo -- -- aademo -- -- a1demo -- -- a2demo ... -- bdemo -- -- b1demo -- -- b2.demo There is no b2.demo in yoursample data, only b2data .... -- cdemo ... If any body have idea then please let me know. Thnaks in advance Regards NItin Roman help us help you. What DBMS are you using? What version? First idea: your tables are not normalized. Second idea: what did you try? TO get the output you describe may require a report writer tool. With straight SQL your output likely looks like this: Demo -- ademo -- Demo -- ademo -- aademo ... Demo -- bdemo -- Demo -- bdemo -- b2demo and you will need report writer features like COLUMN BREAK to get the format you asked for. |
#5
| |||
| |||
|
|
Hi Ed Prochak, It is typing mistake, It is actually b2demo. I am using postgres database and Java as a server side language. I am implementing tree like structure so that it will get displayed in the table. Whenever i add any category or subcategory it will list in the table in a nested pattern On the other hand i am binding this result to the listbox. I need such a query to get this kind of output. I could be able to get this kind of result in sql2005 with stored procedure. But it is quite difficult to do it postgres. Here is the procedure for that in SQL CREATE PROC dbo.ShowHierarchy10 ( @Root int ) AS BEGIN SET NOCOUNT ON DECLARE @catid int, @catname varchar(30),@catnametoprint varchar(50),@catidtoprint int SET @catname = (SELECT catname FROM Test111 where catid = @Root) SET @catid = (SELECT MIN(catid) FROM dbo.Test111 WHERE parentcatid = @Root) SET @catnametoprint = REPLICATE('-', @@NESTLEVEL * 2) + @catname SET @catidtoprint = (SELECT catid from Test111 where catname = @catname) INSERT INTO Test112(catid,catname) values(@catidtoprint,@catnametoprint) WHILE @catid IS NOT NULL BEGIN EXEC dbo.ShowHierarchy10 @catid SET @catid = (SELECT MIN(catid) FROM dbo.Test111 WHERE parentcatid = @Root AND catid > @catid) END END GO EXEC dbo.ShowHierarchy10 1 GO I modified the table structure for that. catid catname 1 Category NULL 2 Demo 1 3 General 1 4 Windows Hosting 1 5 Linux Hosting 1 6 Demo 1 2 7 Demo 2 2 8 Demo 3 2 9 General 1 3 10 General 2 3 11 General 3 3 can u suggest any other idea to resolve this issue. Regards Nitin Roman. |
#6
| |||
| |||
|
#7
| |||
| |||
|
|
| new.subcatname , new.subcatid |
![]() |
| Thread Tools | |
| Display Modes | |
| |