简体   繁体   中英

Parse XML and output in stored procedure in SQL Server

Here is the XML schema:

<details schemaVersion="1">
  <dataWarning>
    <Locations>
      <Failed_to_Create LocationID="1234">Location<Location_Description>TEST</Location_Description><FacilityID>TEST</FacilityID><Active>Y</Active></Failed_to_Create>
    </Locations>
  </dataWarning>
</details>

How do I parse the XML to be able to surface it in a stored procedure?

Thanks!

enter code here

Using Xquery nodes() method. It allows to access any element or attribute.

SQL

DECLARE @xml XML = N'<Failed_to_Create LocationID="">Location
    <Location_Description>TEST</Location_Description>
    <FacilityID>TEST</FacilityID>
    <Active>Y</Active>
</Failed_to_Create>';

SELECT c.value('(./text())[1]', 'VARCHAR(10)') AS [location]
    , c.value('(Location_Description/text())[1]', 'VARCHAR(100)') AS [Location_Description]
    , c.value('(FacilityID/text())[1]', 'VARCHAR(100)') AS [FacilityID]
    , c.value('(Active/text())[1]', 'VARCHAR(100)') AS [Active]
FROM @xml.nodes('/Failed_to_Create') AS t(c);

Output

+------------+----------------------+------------+--------+
|  location  | Location_Description | FacilityID | Active |
+------------+----------------------+------------+--------+
| Location   | TEST                 | TEST       | Y      |
+------------+----------------------+------------+--------+

Andrea, you need to be more disciplined. It is your responsibility to provide a DDL and sample data population.

SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE  (ID INT IDENTITY(1,1) PRIMARY KEY, Details XML);
INSERT INTO @tbl (Details)
VALUES
(N'<details schemaVersion="1">
  <dataWarning>
    <Locations>
      <Failed_to_Create LocationID="1234">Location<Location_Description>TEST</Location_Description><FacilityID>TEST</FacilityID><Active>Y</Active></Failed_to_Create>
    </Locations>
  </dataWarning>
</details>')
, (N'<details schemaVersion="1">
  <dataWarning>
    <Locations>
      <Failed_to_Create LocationID="5678">Location<Location_Description>Real Description</Location_Description><FacilityID>770</FacilityID><Active>N</Active></Failed_to_Create>
    </Locations>
  </dataWarning>
</details>');
-- DDL and sample data population, end

SELECT c.value('@LocationID', 'VARCHAR(10)') AS [locationID]
    , c.value('(Location_Description/text())[1]', 'VARCHAR(100)') AS [Location_Description]
    , c.value('(FacilityID/text())[1]', 'VARCHAR(100)') AS [FacilityID]
    , c.value('(Active/text())[1]', 'VARCHAR(100)') AS [Active]
FROM @tbl AS tbl
      CROSS APPLY tbl.Details.nodes('/details/dataWarning/Locations/Failed_to_Create') AS t(c);

Output

+------------+----------------------+------------+--------+
| locationID | Location_Description | FacilityID | Active |
+------------+----------------------+------------+--------+
|       1234 | TEST                 | TEST       | Y      |
|       5678 | Real Description     | 770        | N      |
+------------+----------------------+------------+--------+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM