我有以下代码:
allKeys
返回:
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
在这个具体的例子中,Wards重复。我想将它包装在父节点中。似乎这样做的唯一方法是使用子查询,其中我指定不是AUTO而是在子查询中指定PATH和ROOT并将其作为TYPE返回。
答案 0 :(得分:0)
你的最后一句是正确的。如果要查看父节点中包含的嵌套数据(1:多关系),则必须将它们包含在一个块中。
一个提示:你到处都使用WITH (NOLOCK)
。除非你真的知道你在做什么,除非你有充分的理由,这是一件危险的事情。很常见,但不是很好的做法...
尝试这样(未经测试):
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]
,WardXml.XmlAsBlock
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]
CROSS APPLY
(
SELECT WardCode FROM dbo.[MDWard] Ward WITH (NOLOCK)
WHERE Ward.[WardID] = mcl.[WardID]
FOR XML PATH('Ward'),TYPE
) AS WardXml(XmlAsBlock)
WHERE
msi.SupplierNumber = 'MAAA0000002'
and Suburb.Code = '181001001'
for xml auto, Root('Suppliers'), elements xsinil