How to query values from xml nodes?

后端 未结 4 1963
既然无缘
既然无缘 2020-11-28 11:57

i have a table that contains an XML column:

CREATE TABLE Batches( 
   BatchID int,
   RawXml xml 
)

The xml contains items such as:

相关标签:
4条回答
  • 2020-11-28 12:24
    SELECT  b.BatchID,
            x.XmlCol.value('(ReportHeader/OrganizationReportReferenceIdentifier)[1]','VARCHAR(100)') AS OrganizationReportReferenceIdentifier,
            x.XmlCol.value('(ReportHeader/OrganizationNumber)[1]','VARCHAR(100)') AS OrganizationNumber
    FROM    Batches b
    CROSS APPLY b.RawXml.nodes('/CasinoDisbursementReportXmlFile/CasinoDisbursementReport') x(XmlCol);
    

    Demo: SQLFiddle

    0 讨论(0)
  • 2020-11-28 12:35

    Try this:

    SELECT RawXML.value('(/GrobXmlFile//Grob//ReportHeader//OrganizationReportReferenceIdentifier/node())[1]','varchar(50)') AS ReportIdentifierNumber,
           RawXML.value('(/GrobXmlFile//Grob//ReportHeader//OrganizationNumber/node())[1]','int') AS OrginazationNumber
    FROM Batches
    
    0 讨论(0)
  • 2020-11-28 12:40

    if you have only one xml in your table, you can convert it in 2 steps:

    CREATE TABLE Batches( 
       BatchID int,
       RawXml xml 
    )
    
    declare @xml xml=(select top 1 RawXml from @Batches)
    
    SELECT  --b.BatchID,
            x.XmlCol.value('(ReportHeader/OrganizationReportReferenceIdentifier)[1]','VARCHAR(100)') AS OrganizationReportReferenceIdentifier,
            x.XmlCol.value('(ReportHeader/OrganizationNumber)[1]','VARCHAR(100)') AS OrganizationNumber
    FROM    @xml.nodes('/CasinoDisbursementReportXmlFile/CasinoDisbursementReport') x(XmlCol)
    
    0 讨论(0)
  • 2020-11-28 12:48

    This works, been tested...

    SELECT  n.c.value('OrganizationReportReferenceIdentifier[1]','varchar(128)') AS 'OrganizationReportReferenceNumber',  
            n.c.value('(OrganizationNumber)[1]','varchar(128)') AS 'OrganizationNumber'
    FROM    Batches t
    Cross   Apply RawXML.nodes('/GrobXmlFile/Grob/ReportHeader') n(c)  
    
    0 讨论(0)
提交回复
热议问题