以XML格式生成输出的SQL Server代码我遇到的问题是没有按要求输出
select Section.Text,
Section.ImageUrl,
(select Item.Text "Item/@Text",
Item.Url "Item/@Url",
Item.ImageUrl "Item/@ImageUrl"
for xml path(''),type) as Items
from tblATNOT_MenuSections Section
join (select mit.Text,
mit.Url,
mit.ImageUrl,
mit.Section,
mit.ItemOrder
from tblATNOT_MenuItems mit
join tblATNOT_RoleMenuAccess rma on (rma.Item = mit.Item and rma.Valid = 1)
where (mit.Valid = 1 or mit.Valid is null)
and rma.Role in (select distinct rum.RoleId
from tblSSAppsRoleUserMap rum
join viwSSAppsEmpMasterExtended vem on (vem.PersonId = rum.PersonId)
where vem.IsEmployeeActive = 'Y'
and vem.PersonId = 86
and rum.RoleId = rma.Role)
or rma.Role is null
)Item
on (Item.Section = Section.Section)
order by Section.SectionOrder,
Item.ItemOrder
for xml auto, root('Menu')
当前输出:
<Menu>
<Section Text="Home" ImageUrl="/ATNOT/Include/Images/home.gif">
<Items>
<Item Text="Home" Url="/ATNOT/Pages/Home.aspx" ImageUrl="/ATNOT/Include/Images/page_add.png" />
</Items>
</Section>
<Section Text="Entry" ImageUrl="/ATNOT/Include/Images/page_go.png">
<Items>
<Item Text="Attendance Entry" Url="/ATNOT/Pages/AttendanceEntry.aspx" ImageUrl="/ATNOT/Include/Images/page_add.png" />
</Items>
</Section>
<Section Text="Entry" ImageUrl="/ATNOT/Include/Images/page_go.png">
<Items>
<Item Text="My Attendance Requests" Url="/ATNOT/Pages/AttendanceRequests.aspx" ImageUrl="/ATNOT/Include/Images/page_add.png" />
</Items>
</Section>
<Section Text="Claim" ImageUrl="/ATNOT/Include/Images/new_tour.gif">
<Items>
<Item Text="Back Date OT Claim" Url="/ATNOT/Pages/BackDateClaim.aspx" ImageUrl="/ATNOT/Include/Images/page_add.png" />
</Items>
</Section>
<Section Text="Claim" ImageUrl="/ATNOT/Include/Images/new_tour.gif">
<Items>
<Item Text="Overtime Claim" Url="/ATNOT/Pages/OvertimeClaim.aspx" ImageUrl="/ATNOT/Include/Images/page_add.png" />
</Items>
</Section>
<Section Text="Claim" ImageUrl="/ATNOT/Include/Images/new_tour.gif">
<Items>
<Item Text="My Overtime Requests" Url="/ATNOT/Pages/OvertimeRequests.aspx" ImageUrl="/ATNOT/Include/Images/page_add.png" />
</Items>
</Section>
</Menu>
但我的要求如下.....请告诉我剩下的......
此处子节点项每次重复
期望的输出:
<menu>
<section text="Home" image_url="/ATNOT/Include/Images/home.gif">
<items>
<item text="Home" url="/ATNOT/Pages/Home.aspx" image_url="/ATNOT/Include/Images/page_add.png" />
</items>
</section>
<section text="Entry" image_url="/ATNOT/Include/Images/page_go.png">
<items>
<item text="Attendance Entry" url="/ATNOT/Pages/AttendanceEntry.aspx" image_url="/ATNOT/Include/Images/page_add.png" />
<item text="My Attendance Requests" url="/ATNOT/Pages/AttendanceRequests.aspx" image_url="/ATNOT/Include/Images/page_add.png" />
</items>
</section>
<section text="Claim" image_url="/ATNOT/Include/Images/new_tour.gif">
<items>
<item text="Back Date OT Claim" url="/ATNOT/Pages/BackDateClaim.aspx" image_url="/ATNOT/Include/Images/page_add.png" />
<item text="Overtime Claim" url="/ATNOT/Pages/OvertimeClaim.aspx" image_url="/ATNOT/Include/Images/page_add.png" />
<item text="My Overtime Requests" url="/ATNOT/Pages/OvertimeRequests.aspx" image_url="/ATNOT/Include/Images/page_add.png" />
</items>
</section>
</menu>
DDL脚本:
CREATE TABLE [dbo].[tblSSAppsRoleUserMap]
(
[Id] [int] IDENTITY(1,1) NOT NULL,
[RoleId] [int] NOT NULL,
[PersonId] [int] NOT NULL,
[ProfitCenterCode] [varchar](25) NULL,
[CostCenterCode] [varchar](25) NULL,
[LocationCode] [int] NULL,
[OrgEntityCode] [varchar](10) NULL,
[Active] [int] NOT NULL,
[CreatedBy] [int] NOT NULL,
[CreatedOn] [datetime] NOT NULL,
CONSTRAINT [pk_rum_Id] PRIMARY KEY CLUSTERED ([Id] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[tblSSAppsRoleUserMap] ON
INSERT [dbo].[tblSSAppsRoleUserMap] ([Id], [RoleId], [PersonId], [ProfitCenterCode], [CostCenterCode], [LocationCode], [OrgEntityCode], [Active], [CreatedBy], [CreatedOn]) VALUES (1, 1, 37306, N'100', N'100', 100, N'3736', 1, 38331, CAST(0x0000A307012DD394 AS DateTime))
INSERT [dbo].[tblSSAppsRoleUserMap] ([Id], [RoleId], [PersonId], [ProfitCenterCode], [CostCenterCode], [LocationCode], [OrgEntityCode], [Active], [CreatedBy], [CreatedOn]) VALUES (2, 1, 38440, N'100', N'100', 100, N'3482', 1, 36540, CAST(0x0000A2AE01043994 AS DateTime))
INSERT [dbo].[tblSSAppsRoleUserMap] ([Id], [RoleId], [PersonId], [ProfitCenterCode], [CostCenterCode], [LocationCode], [OrgEntityCode], [Active], [CreatedBy], [CreatedOn]) VALUES (2797, 184, 194, N'100', N'100', 100, NULL, 1, 1, CAST(0x0000A3F000BED520 AS DateTime))
INSERT [dbo].[tblSSAppsRoleUserMap] ([Id], [RoleId], [PersonId], [ProfitCenterCode], [CostCenterCode], [LocationCode], [OrgEntityCode], [Active], [CreatedBy], [CreatedOn]) VALUES (2798, 184, 449, N'100', N'100', 100, NULL, 1, 1, CAST(0x0000A3F000BED520 AS DateTime))
SET IDENTITY_INSERT [dbo].[tblSSAppsRoleUserMap] OFF
CREATE TABLE [dbo].[tblATNOT_MenuSections]
(
[Section] [int] NOT NULL,
[Text] [varchar](500) NULL,
[Url] [varchar](500) NULL,
[ImageUrl] [varchar](500) NULL,
[SectionOrder] [int] NULL,
[Valid] [bit] NULL,
[Active] [bit] NULL,
[CreatedBy] [int] NULL,
[CreatedOn] [datetime] NULL,
[ModifiedBy] [int] NULL,
[ModifiedOn] [datetime] NULL,
CONSTRAINT [pk_mes_Section] PRIMARY KEY CLUSTERED ([Section] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[tblATNOT_MenuSections] ([Section], [Text], [Url], [ImageUrl], [SectionOrder], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (1, N'Home', NULL, N'/ATNOT/Include/Images/home.gif', 1, 1, 1, 3591, CAST(0x0000A3A400000000 AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_MenuSections] ([Section], [Text], [Url], [ImageUrl], [SectionOrder], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (2, N'Entry', NULL, N'/ATNOT/Include/Images/page_go.png', 2, 1, 1, 3591, CAST(0x0000A3A400000000 AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_MenuSections] ([Section], [Text], [Url], [ImageUrl], [SectionOrder], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (3, N'Claim', NULL, N'/ATNOT/Include/Images/new_tour.gif', 3, 1, 1, 3591, CAST(0x0000A3A400000000 AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_MenuSections] ([Section], [Text], [Url], [ImageUrl], [SectionOrder], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (4, N'Report', NULL, N'/ATNOT/Include/Images/reportExcel.gif', 4, 1, 1, 3591, CAST(0x0000A3A400000000 AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_MenuSections] ([Section], [Text], [Url], [ImageUrl], [SectionOrder], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (5, N'Administrator', NULL, N'/ATNOT/Include/Images/admin1.png', 5, 1, 1, 3591, CAST(0x0000A3A400000000 AS DateTime), NULL, NULL)
/****** Object: Table [dbo].[tblATNOT_MenuItems] Script Date: 12/16/2014 14:43:27 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tblATNOT_MenuItems](
[Item] [int] NOT NULL,
[Section] [int] NULL,
[Text] [varchar](500) NULL,
[Url] [varchar](500) NULL,
[ImageUrl] [varchar](500) NULL,
[ItemOrder] [int] NULL,
[Valid] [bit] NULL,
[Active] [bit] NULL,
[CreatedBy] [int] NULL,
[CreatedOn] [datetime] NULL,
[ModifiedBy] [int] NULL,
[ModifiedOn] [datetime] NULL,
CONSTRAINT [pk_meit_Item] PRIMARY KEY CLUSTERED
(
[Item] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[tblATNOT_MenuItems] ([Item], [Section], [Text], [Url], [ImageUrl], [ItemOrder], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (1, 1, N'Home', N'/ATNOT/Pages/Home.aspx', N'/ATNOT/Include/Images/page_add.png', 1, 1, 1, 3591, CAST(0x0000A3A400000000 AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_MenuItems] ([Item], [Section], [Text], [Url], [ImageUrl], [ItemOrder], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (2, 2, N'Attendance Entry', N'/ATNOT/Pages/AttendanceEntry.aspx', N'/ATNOT/Include/Images/page_add.png', 1, 1, 1, 3591, CAST(0x0000A3A400000000 AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_MenuItems] ([Item], [Section], [Text], [Url], [ImageUrl], [ItemOrder], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (3, 2, N'My Attendance Requests', N'/ATNOT/Pages/AttendanceRequests.aspx', N'/ATNOT/Include/Images/page_add.png', 2, 1, 1, 3591, CAST(0x0000A3A400000000 AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_MenuItems] ([Item], [Section], [Text], [Url], [ImageUrl], [ItemOrder], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (4, 3, N'Overtime Claim', N'/ATNOT/Pages/OvertimeClaim.aspx', N'/ATNOT/Include/Images/page_add.png', 2, 1, 1, 3591, CAST(0x0000A3FC011E85EA AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_MenuItems] ([Item], [Section], [Text], [Url], [ImageUrl], [ItemOrder], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (5, 3, N'Back Date OT Claim', N'/ATNOT/Pages/BackDateClaim.aspx', N'/ATNOT/Include/Images/page_add.png', 1, 1, 1, 3591, CAST(0x0000A3A400000000 AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_MenuItems] ([Item], [Section], [Text], [Url], [ImageUrl], [ItemOrder], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (6, 3, N'My Overtime Requests', N'/ATNOT/Pages/OvertimeRequests.aspx', N'/ATNOT/Include/Images/page_add.png', 3, 1, 1, 3591, CAST(0x0000A3A400000000 AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_MenuItems] ([Item], [Section], [Text], [Url], [ImageUrl], [ItemOrder], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (7, 4, N'Daily Overtime ', N'/ATNOT/Reports/DailyOvertimeReport.aspx', N'/ATNOT/Include/Images/page_add.png', 1, 1, 1, 3591, CAST(0x0000A3A400000000 AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_MenuItems] ([Item], [Section], [Text], [Url], [ImageUrl], [ItemOrder], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (8, 4, N'Monthly Overtime ', N'/ATNOT/Reports/MonthlyOvertimeReport.aspx', N'/ATNOT/Include/Images/page_add.png', 2, 1, 1, 3591, CAST(0x0000A3A400000000 AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_MenuItems] ([Item], [Section], [Text], [Url], [ImageUrl], [ItemOrder], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (9, 4, N'Monthly Shift Allowance ', N'/ATNOT/Reports/MonthlyShiftAllowanceReport.aspx', N'/ATNOT/Include/Images/page_add.png', 3, 1, 1, 3591, CAST(0x0000A3A400000000 AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_MenuItems] ([Item], [Section], [Text], [Url], [ImageUrl], [ItemOrder], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (10, 4, N'Detailed Overtime Report', N'/ATNOT/Reports/DetailedOvertimeReport.aspx', N'/ATNOT/Include/Images/page_add.png', 4, 1, 1, 3591, CAST(0x0000A3A400000000 AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_MenuItems] ([Item], [Section], [Text], [Url], [ImageUrl], [ItemOrder], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (11, 5, N'Back Date Entry', N'/ATNOT/Admin/BackDateEntry.aspx', N'/ATNOT/Include/Images/page_add.png', 1, 1, 1, 3591, CAST(0x0000A3A400000000 AS DateTime), NULL, NULL)
/****** Object: Table [dbo].[tblATNOT_RoleMenuAccess] Script Date: 12/16/2014 14:43:27 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[tblATNOT_RoleMenuAccess](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Role] [int] NULL,
[Item] [int] NULL,
[Valid] [bit] NULL,
[Active] [bit] NULL,
[CreatedBy] [int] NULL,
[CreatedOn] [datetime] NULL,
[ModifiedBy] [int] NULL,
[ModifiedOn] [datetime] NULL,
CONSTRAINT [pk_rma_Id] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[tblATNOT_RoleMenuAccess] ON
INSERT [dbo].[tblATNOT_RoleMenuAccess] ([Id], [Role], [Item], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (1, NULL, 1, 1, 1, 3591, CAST(0x0000A3C200000000 AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_RoleMenuAccess] ([Id], [Role], [Item], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (2, NULL, 2, 1, 1, 3591, CAST(0x0000A3C200000000 AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_RoleMenuAccess] ([Id], [Role], [Item], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (3, NULL, 3, 1, 1, 3591, CAST(0x0000A3C200000000 AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_RoleMenuAccess] ([Id], [Role], [Item], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (4, NULL, 4, 1, 1, 3591, CAST(0x0000A3C200000000 AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_RoleMenuAccess] ([Id], [Role], [Item], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (5, NULL, 5, 1, 1, 3591, CAST(0x0000A3C200000000 AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_RoleMenuAccess] ([Id], [Role], [Item], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (6, NULL, 6, 1, 1, 3591, CAST(0x0000A3C200000000 AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_RoleMenuAccess] ([Id], [Role], [Item], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (7, 161, 7, 1, 1, 3591, CAST(0x0000A3C200000000 AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_RoleMenuAccess] ([Id], [Role], [Item], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (8, 161, 8, 1, 1, 3591, CAST(0x0000A3C200000000 AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_RoleMenuAccess] ([Id], [Role], [Item], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (9, 161, 9, 1, 1, 3591, CAST(0x0000A3C200000000 AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_RoleMenuAccess] ([Id], [Role], [Item], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (10, 161, 10, 1, 1, 3591, CAST(0x0000A3C200000000 AS DateTime), NULL, NULL)
INSERT [dbo].[tblATNOT_RoleMenuAccess] ([Id], [Role], [Item], [Valid], [Active], [CreatedBy], [CreatedOn], [ModifiedBy], [ModifiedOn]) VALUES (11, 172, 11, 1, 1, 3591, CAST(0x0000A3C200000000 AS DateTime), NULL, NULL)
SET IDENTITY_INSERT [dbo].[tblATNOT_RoleMenuAccess] OFF
/****** Object: Default [DF__tblATNOT___Valid__7F16D496] Script Date: 12/16/2014 14:43:27 ******/
ALTER TABLE [dbo].[tblATNOT_RoleMenuAccess] ADD DEFAULT ((1)) FOR [Valid]
GO
/****** Object: Default [DF__tblATNOT___Activ__000AF8CF] Script Date: 12/16/2014 14:43:27 ******/
ALTER TABLE [dbo].[tblATNOT_RoleMenuAccess] ADD DEFAULT ((1)) FOR [Active]
GO
/****** Object: ForeignKey [fk_mit_mse_Section_Section] Script Date: 12/16/2014 14:43:27 ******/
ALTER TABLE [dbo].[tblATNOT_MenuItems] WITH CHECK ADD CONSTRAINT [fk_mit_mse_Section_Section] FOREIGN KEY([Section])
REFERENCES [dbo].[tblATNOT_MenuSections] ([Section])
GO
ALTER TABLE [dbo].[tblATNOT_MenuItems] CHECK CONSTRAINT [fk_mit_mse_Section_Section]
GO
/****** Object: ForeignKey [fk_rac_mit_Item_Item] Script Date: 12/16/2014 14:43:27 ******/
ALTER TABLE [dbo].[tblATNOT_RoleMenuAccess] WITH CHECK ADD CONSTRAINT [fk_rac_mit_Item_Item] FOREIGN KEY([Item])
REFERENCES [dbo].[tblATNOT_MenuItems] ([Item])
GO
ALTER TABLE [dbo].[tblATNOT_RoleMenuAccess] CHECK CONSTRAINT [fk_rac_mit_Item_Item]
GO
/****** Object: ForeignKey [fk_rac_rma_Role_RoleId] Script Date: 12/16/2014 14:43:27 ******/
ALTER TABLE [dbo].[tblATNOT_RoleMenuAccess] WITH CHECK ADD CONSTRAINT [fk_rac_rma_Role_RoleId] FOREIGN KEY([Role])
REFERENCES [dbo].[tblSSAppsRoleMaster] ([RoleId])
GO
ALTER TABLE [dbo].[tblATNOT_RoleMenuAccess] CHECK CONSTRAINT [fk_rac_rma_Role_RoleId]
GO
答案 0 :(得分:1)
您可以重写查询以使用for xml path
并使用查询来获取列列表中相关子查询中的项目。
这是一个简化的例子来说明我的意思。
select S.Text,
S.ImageUrl,
(
select I.Text as '@Text',
I.Url as '@Url',
I.ImageUrl as '@ImageUrl'
from Item as I -- Replace with your Items query
where S.Section = I.Section -- The correlation to Section
order by I.ItemOrder
for xml path('Item'), type
) as Items
from tblATNOT_MenuSections as S
order by S.SectionOrder
for xml path('section'), root('Menu')
使用除viwSSAppsEmpMasterExtended
select S.Text,
S.ImageUrl,
(
select I.Text as '@Text',
I.Url as '@Url',
I.ImageUrl as '@ImageUrl'
from tblATNOT_MenuItems as I
inner join tblATNOT_RoleMenuAccess as R
on I.Item = R.Item and
R.Valid = 1
where S.Section = I.Section and
(I.Valid = 1 or I.Valid is null) and
(R.Role in (
select M.RoleId
from tblSSAppsRoleUserMap M
) or R.Role is null)
order by I.ItemOrder
for xml path('Item'), type
) as Items
from tblATNOT_MenuSections as S
order by S.SectionOrder
for xml path('section'), root('Menu')
要过滤子查询中不匹配的行,您必须复制主查询中exists子句中的逻辑。
select S.Text,
S.ImageUrl,
(
select I.Text as '@Text',
I.Url as '@Url',
I.ImageUrl as '@ImageUrl'
from tblATNOT_MenuItems as I
inner join tblATNOT_RoleMenuAccess as R
on I.Item = R.Item and
R.Valid = 1
where S.Section = I.Section and
(I.Valid = 1 or I.Valid is null) and
(R.Role in (
select M.RoleId
from tblSSAppsRoleUserMap M
) or R.Role is null)
order by I.ItemOrder
for xml path('Item'), type
) as Items
from tblATNOT_MenuSections as S
where exists (
select *
from tblATNOT_MenuItems as I
inner join tblATNOT_RoleMenuAccess as R
on I.Item = R.Item and
R.Valid = 1
where S.Section = I.Section and
(I.Valid = 1 or I.Valid is null) and
(R.Role in (
select M.RoleId
from tblSSAppsRoleUserMap M
) or R.Role is null)
)
order by S.SectionOrder
for xml path('section'), root('Menu')
答案 1 :(得分:0)
提供所需输出的代码:
select Section.Text,
Section.ImageUrl,
(
select Item.Text "Item/@Text",
Item.Url "Item/@Url",
Item.ImageUrl "Item/@ImageUrl"
from tblATNOT_MenuItems as Item
inner join tblATNOT_RoleMenuAccess as rma on Item.Item = rma.Item and rma.Valid = 1
where Section.Section = Item.Section
and (Item.Valid = 1 or Item.Valid is null)
and (rma.Role in (select distinct rum.RoleId
from tblSSAppsRoleUserMap rum
join viwSSAppsEmpMasterExtended vem on (vem.PersonId = rum.PersonId)
where vem.IsEmployeeActive = 'Y'
and vem.PersonId = @p_PersonId
and rum.RoleId = rma.Role)
or rma.Role is null)
order by Item.ItemOrder
for xml path(''), type
) as Items
from tblATNOT_MenuSections as Section
where exists (
select *
from tblATNOT_MenuItems as Item
inner join tblATNOT_RoleMenuAccess as rma on Item.Item = rma.Item and rma.Valid = 1
where Section.Section = Item.Section
and (Item.Valid = 1 or Item.Valid is null)
and (rma.Role in (select distinct rum.RoleId
from tblSSAppsRoleUserMap rum
join viwSSAppsEmpMasterExtended vem on (vem.PersonId = rum.PersonId)
where vem.IsEmployeeActive = 'Y'
and vem.PersonId = @p_PersonId
and rum.RoleId = rma.Role)
or rma.Role is null)
)
order by Section.SectionOrder
for xml auto, root('Menu')