在xml sql列上使用xquery过滤结果集

时间:2011-05-27 16:26:39

标签: xml xquery-sql

我无法弄清楚如何过滤这段xml。我希望获得svcProvNbr

所在的userServiceCde = OASV
<dataExtract xmlns="http://dtd-sirvahub.sirva.com/xml/SCHEMA/ATS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://dtd-sirvahub.sirva.com/xml/SCHEMA/ATS http://dtd-sirvahub.sirva.com/xml/SCHEMA/ATS/dataExtract.xsd">
   <shipment>
      <location lctnSeqNbr="001">
         <lctnServiceProvider>
            <userServiceCde>EASV</userServiceCde>
            <svcProvNbr>1470-000</svcProvNbr>
            <svcProvTypeCde>A</svcProvTypeCde>
         </lctnServiceProvider>
         <lctnServiceProvider>
            <userServiceCde>OASV</userServiceCde>
            <svcProvNbr>1470-000</svcProvNbr>
            <svcProvTypeCde>A</svcProvTypeCde>
         </lctnServiceProvider>
      </location>
   </shipment>
</dataExtract>

我尝试了几种不同的方法,但没有运气

xmlalliedxml.query
    ('declare namespace r = "http://dtd-sirvahub.sirva.com/xml/SCHEMA/ATS";
     data(/r:dataExtract/r:shipment/r:location[@lctnSeqNbr=sql:variable("@vcSegmentNo")]/r:lctnServiceProvider.userServiceCde[.="OASV"]/r:svcProvNbr)')

xmlalliedxml.query
    ('declare namespace r = "http://dtd-sirvahub.sirva.com/xml/SCHEMA/ATS";
     for $sp in /r:dataExtract/r:shipment/r:location[@lctnSeqNbr=sql:variable("@vcSegmentNo")]/r:lctnServiceProvider/r:svcProvNbr
 where $sp/r:dataExtract/r:shipment/r:location[@lctnSeqNbr=sql:variable("@vcSegmentNo")]/r:lctnServiceProvider/r:userServiceCde[.="OASV"]
 return
 $sp')

2 个答案:

答案 0 :(得分:1)

试试这个:

DECLARE @input XML = '<dataExtract xmlns="http://dtd-sirvahub.sirva.com/xml/SCHEMA/ATS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://dtd-sirvahub.sirva.com/xml/SCHEMA/ATS http://dtd-sirvahub.sirva.com/xml/SCHEMA/ATS/dataExtract.xsd">
 <shipment>
   <location lctnSeqNbr="001">
     <lctnServiceProvider>
       <userServiceCde>EASV</userServiceCde>
       <svcProvNbr>1470-000</svcProvNbr>
       <svcProvTypeCde>A</svcProvTypeCde>
     </lctnServiceProvider>
     <lctnServiceProvider>
       <userServiceCde>OASV</userServiceCde>
       <svcProvNbr>1470-001</svcProvNbr>
       <svcProvTypeCde>A</svcProvTypeCde>
     </lctnServiceProvider>
   </location>
 </shipment>
</dataExtract>'

;WITH XMLNAMESPACES('http://dtd-sirvahub.sirva.com/xml/SCHEMA/ATS' AS ns)
SELECT
    @input.value('(ns:dataExtract/ns:shipment/ns:location/ns:lctnServiceProvider[ns:userServiceCde="OASV"]/ns:svcProvNbr)[1]', 
                 'varchar(50)') AS 'Service Provider Number"

我修改了你的XML,所以我可以将这两个条目分开,这个SELECT语句确实会返回1470-001作为值。

更新:要从表格中的列执行此操作,请使用以下代码:

;WITH XMLNAMESPACES('http://dtd-sirvahub.sirva.com/xml/SCHEMA/ATS' AS ns)
SELECT
    XmlAllied.value('(ns:dataExtract/ns:shipment/ns:location/ns:lctnServiceProvider[ns:userServiceCde="OASV"]/ns:svcProvNbr)[1]', 
                 'varchar(50)') AS 'Service Provider Number"
FROM 
    dbo.AutoExtract
WHERE
    (some condition here)

答案 1 :(得分:0)

除了marc_s提供的内容之外,您可能还需要考虑在FROM和WHERE之间使用CROSS APPLY TableColumn.nodes('/') as t(c)TableColumn是包含XML的列的名称,然后是括号内的路径)。< / p>