May 23, 2011 at 7:37 am
My sql as following,
declare @tMenuCollection table
(idx int,parentID int,menuNme varchar(50),navigateUrl varchar(50));
insert into @tMenuCollection values(1,0,'Bayar Zakat','void(0)');
insert into @tMenuCollection values(2,0,'Amil','void(0)');
insert into @tMenuCollection values(3,0,'Kira Zakat & Simpan','void(0)');
insert into @tMenuCollection values(4,3,'Tambah','void(0)');
insert into @tMenuCollection values(5,3,'Papar','void(0)');
insert into @tMenuCollection values(6,3,'Carian','void(0)');
insert into @tMenuCollection values(7,2,'Urus Cek','void(0)');
insert into @tMenuCollection values(8,7,'Debit','void(0)');
insert into @tMenuCollection values(9,0,'Surat Majikan','void(0)');
I want to generate output as following,
idx | parentID | menuNme | navigateUrl
----------------------------------------------------------------------------
1 | 0 | [Bayar Zakat] | void(0)
2 | 0 | [Amil] | void(0)
3 | 0 | [Kira Zakat & Simpan] | void(0)
4 | 3 | [Kira Zakat & Simpan] - Tambah | void(0)
5 | 3 | [Kira Zakat & Simpan] - Papar | void(0)
6 | 3 | [Kira Zakat & Simpan] - Carian | void(0)
7 | 2 | [Amil] - Urus Cek | void(0)
8 | 7 | [Amil] - Urus Cek - Debit | void(0)
9 | 0 | [Surat Majikan] | void(0)
*above output is main menu, menu and sub menu
How my SQL statement looks like?
May 23, 2011 at 8:19 am
How does this work out? Check out this BOL link for how it this recursive common-table-expression works.
;
WITH cte AS
(
SELECT t1.idx,
t1.parentID,
menuNme = CONVERT(VARCHAR(1000), '[' + t1.menuNme + ']'),
t1.navigateUrl
FROM @tMenuCollection t1
WHERE t1.parentID = 0
UNION ALL
SELECT t1.idx,
t1.parentID,
menuNme = CONVERT(VARCHAR(1000), cte.menuNme + ' - [' + t1.menuNme + ']'),
t1.navigateUrl
FROM @tMenuCollection t1
JOIN cte
ON t1.parentID = cte.idx
WHERE t1.parentID > 0
)
SELECT *
FROM cte
ORDER BY idx;
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
May 23, 2011 at 8:48 am
WayneS (5/23/2011)
How does this work out? Check out this BOL link for how it this recursive common-table-expression works.
I can't get your solution to work Wayne (which is irritating because mine doesn't get the correct result! :-P)
So far, I got to this point: -
;WITH CTE AS (
SELECT DISTINCT
ROW_NUMBER () OVER(order by main.idx, main.parentid) AS idx,
main.parentid,
'[' + CASE WHEN parent.menuNme IS NOT NULL
THEN parent.menuNme + ']' + ISNULL(' - ' + main.menuNme,'')
ELSE main.menuNme + ']' END AS menuNme,
navigateUrl
FROM (SELECT
idx, parentID, menuNme, navigateUrl
FROM @tMenuCollection) main
--Get parent
OUTER APPLY (SELECT
idx AS menuid, menuNme
FROM @tMenuCollection
WHERE idx = main.parentID) parent
--Get child
OUTER APPLY (SELECT
idx AS menuid, menuNme
FROM @tMenuCollection
WHERE main.idx = parentID) child
)
SELECT * FROM CTE
Which fails to get idx 7 correctly. When I run yours, I get a max recursion error.
May 23, 2011 at 10:22 am
My mistake. My table as following,
declare @tMenuCollection table(idx int,parentID int,menuNme varchar(50),navigateUrl varchar(50));
insert into @tMenuCollection values(1,0,'Bayar Zakat','void(0)');
insert into @tMenuCollection values(2,0,'Amil','void(0)');
insert into @tMenuCollection values(3,0,'Kira Zakat & Simpan','void(0)');
insert into @tMenuCollection values(4,3,'Tambah','void(0)');
insert into @tMenuCollection values(5,3,'Papar','void(0)');
insert into @tMenuCollection values(6,3,'Carian','void(0)');
insert into @tMenuCollection values(7,2,'Urus Cek','void(0)');
insert into @tMenuCollection values(8,7,'Debit','void(0)');
insert into @tMenuCollection values(9,0,'Surat Majikan','void(0)');
Thanks to both of you
May 23, 2011 at 10:31 am
miss.delinda (5/23/2011)
My mistake. My table as following,
declare @tMenuCollection table(idx int,parentID int,menuNme varchar(50),navigateUrl varchar(50));
insert into @tMenuCollection values(1,0,'Bayar Zakat','void(0)');
insert into @tMenuCollection values(2,0,'Amil','void(0)');
insert into @tMenuCollection values(3,0,'Kira Zakat & Simpan','void(0)');
insert into @tMenuCollection values(4,3,'Tambah','void(0)');
insert into @tMenuCollection values(5,3,'Papar','void(0)');
insert into @tMenuCollection values(6,3,'Carian','void(0)');
insert into @tMenuCollection values(7,2,'Urus Cek','void(0)');
insert into @tMenuCollection values(8,7,'Debit','void(0)');
insert into @tMenuCollection values(9,0,'Surat Majikan','void(0)');
Thanks to both of you
:blush: Oh yeah... I forgot to mention this...
miss.delinda, is my query working properly for you?
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
May 23, 2011 at 11:52 pm
Sir Waynes,
Your query working awesome. tq sir
May 24, 2011 at 2:05 am
miss.delinda (5/23/2011)
My mistake. My table as following,
declare @tMenuCollection table(idx int,parentID int,menuNme varchar(50),navigateUrl varchar(50));
insert into @tMenuCollection values(1,0,'Bayar Zakat','void(0)');
insert into @tMenuCollection values(2,0,'Amil','void(0)');
insert into @tMenuCollection values(3,0,'Kira Zakat & Simpan','void(0)');
insert into @tMenuCollection values(4,3,'Tambah','void(0)');
insert into @tMenuCollection values(5,3,'Papar','void(0)');
insert into @tMenuCollection values(6,3,'Carian','void(0)');
insert into @tMenuCollection values(7,2,'Urus Cek','void(0)');
insert into @tMenuCollection values(8,7,'Debit','void(0)');
insert into @tMenuCollection values(9,0,'Surat Majikan','void(0)');
Thanks to both of you
D'Oh!
I had struggled to understand your ordering before that correction. Glad Wayne's query worked. This is how I'd have looked at it.
SELECT DISTINCT
main.idx,
main.parentid,
'[' + CASE WHEN parent.menuNme IS NOT NULL
THEN parent.menuNme + ']' + ISNULL(' - ' + main.menuNme,'')
ELSE main.menuNme + ']' END AS menuNme,
navigateUrl
FROM (SELECT
idx, parentID, menuNme, navigateUrl
FROM @tMenuCollection) main
--Get parent
OUTER APPLY (SELECT
idx AS menuid, menuNme
FROM @tMenuCollection
WHERE idx = main.parentID) parent
--Get child
OUTER APPLY (SELECT
idx AS menuid, menuNme
FROM @tMenuCollection
WHERE main.idx = parentID) child
May 24, 2011 at 10:05 am
tq skcadavre for your effort
Viewing 8 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy