我有一张合同表。有几个列,最后一个 XML 列代表整个合同。合同内部是项目(1 个或更多),每个项目内部有 1 行或更多行。我需要能够选择所有行和它们所在的项目,但是我现在使用示例查询得到了奇怪的结果。
这是 XML 的示例:
<ZEstimateContract xmlns="http://schemas.datacontract.org/2004/07/Zeller.Gp" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" z:Id="i1">
<Name xmlns="http://schemas.datacontract.org/2004/07/Zynergy">QGPET0000000218</Name>
<_projects>
<ZEstimateProject z:Id="i9">
<Name xmlns="http://schemas.datacontract.org/2004/07/Zynergy"><BOM1></Name>
<Parent i:nil="true" />
<Quantity>1</Quantity>
<_lines>
<ZEstimateLine z:Id="i40">
<Name xmlns="http://schemas.datacontract.org/2004/07/Zynergy">0.080 Aluminum 2ft x 4ft</Name>
<_lines />
</ZEstimateLine>
</_lines>
<_projects />
</ZEstimateProject>
<ZEstimateProject z:Id="i97">
<Name xmlns="http://schemas.datacontract.org/2004/07/Zynergy"><BOM2></Name>
<Parent i:nil="true" />
<Quantity>1</Quantity>
<_lines>
<ZEstimateLine z:Id="i113">
<Name xmlns="http://schemas.datacontract.org/2004/07/Zynergy">#8X8SPOOL</Name>
</ZEstimateLine>
<_projects />
</ZEstimateProject>
</_projects>
</ZEstimateContract>
这是我的示例查询:
WITH XMLNAMESPACES('http://schemas.datacontract.org/2004/07/Zeller.Gp' AS ZC,
'http://schemas.datacontract.org/2004/07/Zynergy' AS ZYN)
SELECT Contract, LineName.value('.', 'varchar(50)') as LineItemName, ProjName.value('.', 'varchar(50)') as ProjectName
FROM dbo.tblContractMaster AS CM
CROSS APPLY CM.FullContract.nodes('/ZC:ZEstimateContract/ZC:_projects/ZC:ZEstimateProject/ZYN:Name') as Proj(ProjName)
CROSS APPLY CM.FullContract.nodes('/ZC:ZEstimateContract/ZC:_projects/ZC:ZEstimateProject/ZC:_lines/ZC:ZEstimateLine/ZYN:Name') as Line(LineName)
结果是:
QGPET0000000218 0.080 Aluminum 2ft x 4ft <BOM1>
QGPET0000000218 #8X8SPOOL <BOM1>
QGPET0000000218 0.080 Aluminum 2ft x 4ft <BOM2>
QGPET0000000218 #8X8SPOOL <BOM2>
问题是“0.080 Aluminum 2ft x 4ft”仅在“BOM1”上,“#8X8SPOOL”仅在“BOM2”上,因此结果中应该只有 2 行。请帮忙,谢谢!