我有一个嵌套的子查询来生成XML文件输出结果。
在最终的XML结果中,我需要根据条件跳过几个元素。
请帮助我在下面的场景中如何处理?
查询:
SELECT InterfaceName,[Version],CreationSource,CreationTimestamp,
(SELECT TOP 1 NULL AS BusinessUnitList,
(SELECT BusinessUnit.[BusinessUnit_id] AS id,
(SELECT [User_id]AS id,[User_loginName] AS loginName ,[User_password] AS [password] ,[User_statusCode] AS statusCode,[User_firstName] AS firstName,[User_lastName] AS lastName ,[User_defaultLanguageName] AS defaultLanguageName,[User_forcePasswordChange] AS forcePasswordChange,
--RoleList
(SELECT TOP 1 NULL AS RoleList,
(SELECT Role_id AS id FROM #UserRole AS [Role] WHERE [Role].User_id= [User].[User_id] FOR XML AUTO, TYPE)
FROM [dbo].[EmployeeFeedStage] AS RoleList FOR XML AUTO, TYPE
),
--OrgList
(SELECT TOP 1 NULL AS OrgList,
(SELECT [Organization_id] AS id FROM [dbo].[EmployeeFeedStage] AS Organization WHERE Organization.[User_id]= [User].[User_id] FOR XML AUTO, TYPE)
FROM [dbo].[EmployeeFeedStage] AS OrgList FOR XML AUTO, TYPE
),
--Address
(SELECT [Address_country] AS country ,[Address_email] AS email FROM [dbo].[EmployeeFeedStage] AS [Address] WHERE [Address].[User_id]= [User].[User_id] FOR XML AUTO, TYPE) ,
--Employee
(SELECT [Employee_badgeNumber] AS badgeNumber,[Employee_birthDate] AS birthDate,[Employee_hireDate] AS hireDate,[Employee_seniorityDate] AS seniorityDate,[Employee_managerLevelFlag] AS managerLevelFlag,[Employee_managerClockOverridePassword] AS managerClockOverridePassword,[Employee_punchValidation] AS punchValidation,[Employee_employeeType] AS employeeType,[Employee_schedulingTypeCode] AS schedulingTypeCode, [Employee_employeeGeneratesExceptions] AS employeeGeneratesExceptions ,[Employee_employeeGeneratesAlerts] AS employeeGeneratesAlerts ,[Employee_payrollSystemNumber] AS payrollSystemNumber ,
--AccrualConfigList
--Here need to add condition to skip this element if AccrualConfig_id is NULL
(SELECT TOP 1 NULL AS AccrualConfigList,
(SELECT [AccrualConfig_id] AS id, [AccrualConfig_start] AS start FROM [dbo].[EmployeeFeedStage] AS AccrualConfig WHERE AccrualConfig.[User_id]= [User].[User_id] FOR XML AUTO, TYPE)
FROM [dbo].[EmployeeFeedStage] AS AccrualConfigList FOR XML AUTO, TYPE
) ,
--Availability
(SELECT [Availability_minHoursPerWeek] AS minHoursPerWeek,[Availability_maxHoursPerWeek] AS maxHoursPerWeek,[Availability_maxDaysPerWeek]AS maxDaysPerWeek,[Availability_maxConsecutiveDays]AS maxConsecutiveDays,[Availability_maxConsecutiveDaysAcrossWeeks]AS maxConsecutiveDaysAcrossWeeks,[Availability_minHoursBetweenShifts]AS minHoursBetweenShifts, [Availability_allowSplitShifts]AS allowSplitShifts FROM [dbo].[EmployeeFeedStage] AS [Availability] WHERE [Availability].[User_id]= [User].[User_id] FOR XML AUTO, TYPE),
--Job List--
(SELECT TOP 1 NULL AS JobList,
(SELECT [Job_id] AS id, [Job_start] AS start,
(SELECT [PrimaryJobInfo_payPolicyId] AS payPolicyId,[PrimaryJobInfo_punchRuleId] AS punchRuleId,[PrimaryJobInfo_shiftStrategyId] AS shiftStrategyId,[PrimaryJobInfo_exemptFlag] AS exemptFlag,[PrimaryJobInfo_salariedFlag] AS salariedFlag,[PrimaryJobInfo_fullTimeFlag] AS fullTimeFlag FROM [dbo].[EmployeeFeedStage] AS PrimaryJobInfo WHERE PrimaryJobInfo.[User_id]= [User].[User_id] FOR XML AUTO, TYPE ) ,
(SELECT [JobRate_start] AS start,[JobRate_rate] AS rate FROM [dbo].[EmployeeFeedStage] AS JobRate WHERE JobRate.[User_id]= [User].[User_id] FOR XML AUTO, TYPE)
FROM [dbo].[EmployeeFeedStage] AS Job WHERE Job.[User_id]= [User].[User_id] FOR XML AUTO, TYPE)
FROM [dbo].[EmployeeFeedStage] AS JobList WHERE JobList.[User_id]= [User].[User_id] FOR XML AUTO, TYPE),
--WorkStatusList
(SELECT TOP 1 NULL AS WorkStatusList,
(SELECT [WorkStatus_action] AS action,[WorkStatus_status] AS status,[WorkStatus_start] AS start FROM [dbo].[EmployeeFeedStage] AS WorkStatus WHERE WorkStatus.[User_id]= [User].[User_id] FOR XML AUTO, TYPE)
FROM [dbo].[EmployeeFeedStage] AS WorkStatusList WHERE WorkStatusList.[User_id]= [User].[User_id] FOR XML AUTO, TYPE
)
--(SELECT TOP 1 NULL AS TimeoffAccrualList,
--(SELECT [TimeoffAccrual_PayAdjustmentID] AS payAdjustmentId,[TimeOffAccrual_EffectiveDate] AS effectiveDate, [TimeOffAccrual_Accrued] AS accrued FROM [dbo].[EmployeeFeedStage] AS TimeoffAccrual WHERE TimeoffAccrual.[User_id]= [User].[User_id] FOR XML AUTO, TYPE)
-- FROM [dbo].[EmployeeFeedStage] AS TimeoffAccrualList WHERE TimeoffAccrualList.[User_id]= [User].[User_id] FOR XML AUTO, TYPE
--)
FROM [dbo].[EmployeeFeedStage] AS Employee WHERE Employee.[User_id]= [User].[User_id] FOR XML AUTO, TYPE
)
FROM [dbo].[EmployeeFeedStage] AS [User] WHERE [User].[BusinessUnit_id] = BusinessUnit.[BusinessUnit_id] FOR XML AUTO, TYPE)
FROM [dbo].[EmployeeFeedStage] AS BusinessUnit WHERE BusinessUnit.[BusinessUnit_id]='10027760' GROUP BY BusinessUnit.[BusinessUnit_id] FOR XML AUTO, TYPE)
FROM [dbo].[EmployeeFeedStage] AS BusinessUnitList FOR XML AUTO, TYPE)
FROM #Document AS EnterpriseDocument FOR XML AUTO, TYPE
答案 0 :(得分:0)
快速演示,for xml auto, type
with t as (
select 1 c1, 2 c2
union all
select 3, 4
union all
select 5, 6
)
select c1, (
select c1, case when c2 < 6 then c2 end as c2
from t t2
where t1.c1=t2.c1
for xml auto, type) xml
from t t1 ;