Dear Team,
I have a data in SQL Server Table with below format;
Create Table Menus( id int identity(1,1), menuID int , menuname varchar(50), ParentID int )
Insert into Menus(menuID,menuname,ParentID) values (1,'File',-1)
Insert into Menus(menuID,menuname,ParentID) values (2,'New',1)
Insert into Menus(menuID,menuname,ParentID) values (3,'Project',2)
Insert into Menus(menuID,menuname,ParentID) values (4,'File Query',2)
Insert into Menus(menuID,menuname,ParentID) values (5,'Close',1)
Insert into Menus(menuID,menuname,ParentID) values (6,'Edit',-1)
Insert into Menus(menuID,menuname,ParentID) values (7,'Cut',6)
Insert into Menus(menuID,menuname,ParentID) values (8,'Copy',6)
Insert into Menus(menuID,menuname,ParentID) values (9,'Paste',6)
Insert into Menus(menuID,menuname,ParentID) values (10,'Paste Special Values',9)
Insert into Menus(menuID,menuname,ParentID) values (11,'Paste Special Formulas',9)
Insert into Menus(menuID,menuname,ParentID) values (12,'Paste Special Formulas',11)
Create Table Menus_rights ( rid int ,menuid int,rights_code varchar(7),user_id varchar(8))
Insert into Menus_rights(rid,menuid,rights_code,user_id) values (1,2,'CMD','lingai')
Insert into Menus_rights(rid,menuid,rights_code,user_id) values (2,11,'P','lingai')
With Cte as
(
Select h1.menuid,h1.parentid,h1.menuname, menuname self_description, cast(id as varbinary(max)) [level],cast(h1.id as varchar(max)) [levelid]
from menus h1
where h1.ParentID <=0
Union all
Select h2.menuid,h2.parentid,c.self_description,h2.menuname,c.[level]+cast(h2.id as varbinary(max)) as [level],
c.[levelid] + '>' + cast (h2.id as varchar(10)) [levelid]
from menus h2
inner join cte c on h2.ParentID = c.menuid
)
Select * from
cte
cross apply (select parsename(REPLACE(REVERSE(levelid), '>', '.'), 1))c1(lvl1)
cross apply (select parsename(REPLACE(REVERSE(levelid), '>', '.'), 2))c2(lvl2)
cross apply (select parsename(REPLACE(REVERSE(levelid), '>', '.'), 3))c3(lvl3)
cross apply (select parsename(REPLACE(REVERSE(levelid), '>', '.'), 4))c4(lvl4)
/*
cross apply (select substring(levelid,1,charindex('>',levelid+'>')-1))c1 (rootlevelid1)
cross apply (select substring(levelid,1,charindex('>',levelid+'>')-1))c2 (rootlevelid2)
cross apply (select substring(levelid,1,charindex('>',levelid+'>')-1))c3 (rootlevelid3)
cross apply (select substring(levelid,1,charindex('>',levelid+'>')-1))c4 (rootlevelid4)
*/
Left join Menus_rights on Menus_rights.menuid = cte.menuID
order by [level] option (maxrecursion 1000)
I want Generate RIBONBAR from above Result set
-----------------------------------------
File
New
Edit
paste
paste special formulas
-------------------------------------------------------------------------------------