Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When querying XML in SQL Server 2005, multiple tags of the document are in the same row

Update to provide clarification

Here's a full copy of one of the xml files I"m having trouble with.

<Grower_Run xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Puller xsi:type="Puller">
    <Puller_Number xsi:type="xsd:int">16</Puller_Number>
  </Puller>
  <Run_ID xsi:type="xsd:string">SA1611030B</Run_ID>
  <Crucible xsi:type="Crucible">
    <Crucible_Type xsi:type="xsd:string">RWNTYPE</Crucible_Type>
    <Section>
      <Grower_Run_Section>
        <SectionID xsi:type="xsd:string">SA1611030B1</SectionID>
        <Crystal_Growth>
          <Growth_StartTime xsi:type="xsd:dateTime">2011-03-01T12:59:30</Growth_StartTime>
          <Growth_Process>
            <Growth_Process>
              <Process_Name xsi:type="xsd:string">BODY</Process_Name>
              <Single_Moment_Snapshot>
                <Single_Moment_Snapshot>
                  <Snapshot_Name xsi:type="xsd:string">Body Start</Snapshot_Name>
                  <Snapshot_Datetime xsi:type="xsd:dateTime">2011-03-01T17:11:30</Snapshot_Datetime>
                  <Ingot_Length xsi:type="xsd:decimal">228.19</Ingot_Length>
                  <Heater_Temp xsi:type="xsd:decimal">1337.09</Heater_Temp>
                  <LS_Temp xsi:type="xsd:decimal">1243.00</LS_Temp>
                  <Heater_Power xsi:type="xsd:decimal">56.10</Heater_Power>
                </Single_Moment_Snapshot>
                <Single_Moment_Snapshot>
                  <Snapshot_Name xsi:type="xsd:string">Mid Body</Snapshot_Name>
                  <Snapshot_Datetime xsi:type="xsd:dateTime">2011-03-01T17:11:30</Snapshot_Datetime>
                  <Ingot_Length xsi:type="xsd:decimal">228.19</Ingot_Length>
                  <Heater_Temp xsi:type="xsd:decimal">1337.09</Heater_Temp>
                  <LS_Temp xsi:type="xsd:decimal">1243.00</LS_Temp>
                  <Heater_Power xsi:type="xsd:decimal">56.10</Heater_Power>
                </Single_Moment_Snapshot>
                <Single_Moment_Snapshot>
                  <Snapshot_Name xsi:type="xsd:string">Tail Start</Snapshot_Name>
                  <Snapshot_Datetime xsi:type="xsd:dateTime">2011-03-02T01:34:24</Snapshot_Datetime>
                  <Ingot_Length xsi:type="xsd:decimal">230.40</Ingot_Length>
                  <Heater_Temp xsi:type="xsd:decimal">1338.20</Heater_Temp>
                  <LS_Temp xsi:type="xsd:decimal">1243.40</LS_Temp>
                  <Heater_Power xsi:type="xsd:decimal">56.39</Heater_Power>
                </Single_Moment_Snapshot>
              </Single_Moment_Snapshot>
            </Growth_Process>
          </Growth_Process>
          <Growth_FinishTime xsi:type="xsd:dateTime">2011-03-02T01:35:24</Growth_FinishTime>
        </Crystal_Growth>
      </Grower_Run_Section>
      <Grower_Run_Section>
        <SectionID xsi:type="xsd:string">SA1611030B9</SectionID>
        <Crystal_Growth>
          <Growth_StartTime xsi:type="xsd:dateTime">2011-03-02T04:02:37</Growth_StartTime>
          <Growth_Process>
            <Growth_Process>
              <Process_Name xsi:type="xsd:string">BODY</Process_Name>
              <Single_Moment_Snapshot>
                <Single_Moment_Snapshot>
                  <Snapshot_Name xsi:type="xsd:string">Body Start</Snapshot_Name>
                  <Snapshot_Datetime xsi:type="xsd:dateTime">2011-03-02T07:54:39</Snapshot_Datetime>
                  <Ingot_Length xsi:type="xsd:decimal">231.80</Ingot_Length>
                  <Heater_Temp xsi:type="xsd:decimal">1340.00</Heater_Temp>
                  <LS_Temp xsi:type="xsd:decimal">1246.70</LS_Temp>
                  <Heater_Power xsi:type="xsd:decimal">56.60</Heater_Power>
                </Single_Moment_Snapshot>
                <Single_Moment_Snapshot>
                  <Snapshot_Name xsi:type="xsd:string">Mid Body</Snapshot_Name>
                  <Snapshot_Datetime xsi:type="xsd:dateTime">2011-03-02T07:54:39</Snapshot_Datetime>
                  <Ingot_Length xsi:type="xsd:decimal">231.80</Ingot_Length>
                  <Heater_Temp xsi:type="xsd:decimal">1340.00</Heater_Temp>
                  <LS_Temp xsi:type="xsd:decimal">1246.70</LS_Temp>
                  <Heater_Power xsi:type="xsd:decimal">56.60</Heater_Power>
                </Single_Moment_Snapshot>
                <Single_Moment_Snapshot>
                  <Snapshot_Name xsi:type="xsd:string">Tail Start</Snapshot_Name>
                  <Snapshot_Datetime xsi:type="xsd:dateTime">2011-03-03T06:47:19</Snapshot_Datetime>
                  <Ingot_Length xsi:type="xsd:decimal">1778.00</Ingot_Length>
                  <Heater_Temp xsi:type="xsd:decimal">1388.80</Heater_Temp>
                  <LS_Temp xsi:type="xsd:decimal">1330.70</LS_Temp>
                  <Heater_Power xsi:type="xsd:decimal">63.50</Heater_Power>
                </Single_Moment_Snapshot>
              </Single_Moment_Snapshot>
            </Growth_Process>
          </Growth_Process>
          <Growth_FinishTime xsi:type="xsd:dateTime">2011-03-03T06:48:19</Growth_FinishTime>
        </Crystal_Growth>
      </Grower_Run_Section>
    </Section>
  </Crucible>
</Grower_Run>

Ok so for clarification here is my full query.

DECLARE @FILES TABLE(FILENAME VARCHAR(20),DEPTH INT,FILE_FLAG INT)
DECLARE @XML_TABLE TABLE(
    xmlFileName VARCHAR(300),
    xml_data xml
)
DECLARE @xmlFileName VARCHAR(300)
DECLARE @FILENAME VARCHAR(20)

INSERT INTO @FILES
EXEC Master.dbo.xp_DirTree '\\SASSOAPPSRV\Grower XML Files\',1,1

DECLARE XML_CURSOR CURSOR FOR SELECT FILENAME FROM @FILES

OPEN XML_CURSOR
FETCH NEXT FROM XML_CURSOR 
INTO @FILENAME

WHILE @@FETCH_STATUS = 0
BEGIN

SELECT  @xmlFileName = '\\SASSOAPPSRV\Grower XML Files\' + @FILENAME

INSERT INTO @XML_TABLE(xmlFileName, xml_data)
EXEC('
SELECT ''' + @xmlFileName + ''', xmlData 
FROM
(
    SELECT  * 
    FROM    OPENROWSET (BULK ''' + @xmlFileName + ''' , SINGLE_BLOB) AS XMLDATA
) AS FileImport (XMLDATA)
')
FETCH NEXT FROM XML_CURSOR 
INTO @FILENAME

END
CLOSE XML_CURSOR
DEALLOCATE XML_CURSOR

DECLARE @PARSED_XML TABLE(
S VARCHAR(200),
RUN_ID VARCHAR(20)
,SECTION_ID VARCHAR(50)
)



INSERT INTO @PARSED_XML
SELECT 
T.xmlFileName AS S,
    t.xml_data.value('(/Grower_Run/Run_ID)[1]', 'varchar(50)') AS 'RunID',
    Section.value('(Grower_Run_Section/SectionID)[1]', 'varchar(50)') as 'SectionID'
FROM
    @xml_table t
CROSS APPLY
    t.xml_data.nodes('/Grower_Run/Crucible/Section') AS Tmp(Section)

SELECT 
S
,RUN_ID
,SECTION_ID
 FROM @PARSED_XML
WHERE RUN_ID = 'SA1611030B'
 ORDER BY RUN_ID

And this is the result I'm getting.

\\SASSOAPPSRV\Grower XML Files\SA1611030B.xml   SA1611030B  SA1611030B1

This is the issue since I need to see data from both copies of the node in separate rows.


So I have many XML documents with the following structure

<Grower_Run xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Run_ID xsi:type="xsd:string">SA0111023B</Run_ID>
  <Crucible xsi:type="Crucible">
    <Section>
      <Grower_Run_Section>
        <SectionID xsi:type="xsd:string">SA0111023B1</SectionID>
      </Grower_Run_Section>
    </Section>
    <Section>
      <Grower_Run_Section>
        <SectionID xsi:type="xsd:string">SA0111023B9</SectionID>
      </Grower_Run_Section>
    </Section>
  </Crucible>
</Grower_Run>

That I've imported into an XML field of a temp table which I'm querying against with a query like this one

SELECT
   CAST(XML_DATA.query('data(/Grower_Run/Run_ID)') AS VARCHAR(20)) AS [RUN ID]
  ,CAST(XML_DATA.query('data(/Grower_Run/Crucible/Section/Grower_Run_Section/SectionID)') AS VARCHAR(50)) AS [SECTION ID]
FROM @XML_TABLE

So the problem is that my results from the multiple instances of the "SectionID" tag are showing up in the same row.

i.e.

   RunID    |   SectionID
--------------------------
SA0111023B  |    SA0111023B1  SA0111023B9

when it should be

RunID       |   SectionID
-----------------------------
SA0111023B  |    SA0111023B1  
SA0111023B  |    SA0111023B9

If I had [1] to then end of the xml query like this

CAST(XML_DATA.query('data(/Grower_Run/Crucible/Section/Grower_Run_Section/SectionID)')[1]

it will drop the second section ID all together which doesn't work cuz I need both.

Any help?

like image 987
Neberu Avatar asked Mar 09 '11 19:03

Neberu


1 Answers

If you want to select out multiple "rows" from a XML document, you need to use the .nodes() SQL Server XML function - something like this:

SELECT 
    @XMLTable.value('(/Grower_Run/Run_ID)[1]', 'varchar(50)') AS 'RunID',
    Section.value('(Grower_Run_Section/SectionID)[1]', 'varchar(50)') as 'SectionID'
FROM
    @XMLTable.nodes('/Grower_Run/Crucible/Section') AS Tmp(Section)

That XPath statement in the FROM clause basically defines a "pseudo-table" of XML elements - based on that XPath. So here you get a pseudo-table for each <Section> entry in your XML - from which you can then select individual elements using the .value() function.

If you want to select that from a table that contains a column of type XML, you might need to check into the CROSS APPLY command:

SELECT 
    t.SomeColumn,        
    t.XmlColumn.value('(/Grower_Run/Run_ID)[1]', 'varchar(50)') AS 'RunID',
    Section.value('(Grower_Run_Section/SectionID)[1]', 'varchar(50)') as 'SectionID'
FROM
    dbo.YourTable t
CROSS APPLY
    t.XmlColumn.nodes('/Grower_Run/Crucible/Section') AS Tmp(Section)
like image 61
marc_s Avatar answered Oct 11 '22 10:10

marc_s