我有以下代码:
SELECT distinct
msi.SupplierNumber
,Province.[Name] [ProvinceName]
,District.[Code] [DistrictCode]
,District.[Name] [DistrictName]
,Municipality.[Code] [MunicipalityCode]
,Municipality.[Name] [MunicipalityName]
,City.[Code] [CityCode]
,City.[Name] [CityName]
,Suburb.[Code] [SuburbCode]
,Suburb.[Name] [SuburbName]
,Ward.[Code] [WardCode]
FROM
dbo.MasterSupplierIdentification msi WITH (NOLOCK)
INNER JOIN dbo.[LinkMasterSupplierCommodity] lmsc WITH (NOLOCK) ON lmsc.MasterSupplierIdentificationID = msi.MasterSupplierIdentificationID
INNER JOIN dbo.[MasterCommodityGroup] mcg WITH (NOLOCK) ON mcg.[MasterCommodityGroupID] = lmsc.MasterCommodityID
INNER JOIN dbo.[MasterCommodityLocation] mcl WITH (NOLOCK) ON mcl.[MasterCommodityID] = mcg.[MasterCommodityID]
INNER JOIN dbo.[MDWard] Ward WITH (NOLOCK) ON Ward.[WardID] = mcl.[WardID]
INNER JOIN dbo.[MDSuburb] Suburb WITH (NOLOCK) ON Suburb.[SuburbID] = Ward.[SuburbID]
INNER JOIN dbo.[MDCity] City WITH (NOLOCK) ON City.[CityID] = Suburb.[CityID]
INNER JOIN dbo.[MDMunicipality] Municipality WITH (NOLOCK) ON Municipality.[MunicipalityID] = City.[MunicipalityID]
INNER JOIN dbo.[MDDistrict] District WITH (NOLOCK) ON District.[DistrictID] = Municipality.[DistrictID]
INNER JOIN dbo.[MDProvince] Province WITH (NOLOCK) ON Province.[ProvinceID] = District.[ProvinceID]
WHERE
msi.SupplierNumber = 'MAAA0000002'
and Suburb.Code = '181001001'
for xml auto, Root('Suppliers'), elements xsinil
返回:
<Suppliers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<msi>
<SupplierNumber>MAAA0000002</SupplierNumber>
<Province>
<ProvinceName>Western Cape</ProvinceName>
<District>
<DistrictCode>105</DistrictCode>
<DistrictName>Central Karoo</DistrictName>
<Municipality>
<MunicipalityCode>181</MunicipalityCode>
<MunicipalityName>Laingsburg</MunicipalityName>
<City>
<CityCode>181001</CityCode>
<CityName>Laingsburg NU</CityName>
<Suburb>
<SuburbCode>181001001</SuburbCode>
<SuburbName>Laingsburg NU</SuburbName>
<Ward>
<WardCode>1</WardCode>
</Ward>
<Ward>
<WardCode>12</WardCode>
</Ward>
<Ward>
<WardCode>2</WardCode>
</Ward>
</Suburb>
</City>
</Municipality>
</District>
</Province>
</msi>
在此特定示例中,病房重复。我想让它包裹在一个父节点中。似乎做到这一点的唯一方法是使用子查询,我在子查询中指定的不是AUTO,而是PATH和ROOT,并将其作为TYPE返回。