Getting XML to feed into SQL Server table

安稳与你 提交于 2021-02-11 12:32:16

问题


I am trying to get a (for right now) simple XML to feed into a SQL Server table.

The XML is:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfSafeEODBalance xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <SafeEODBalance>
        <Lane>1</Lane>
        <PouchId>06292019053041001</PouchId>
        <BusinessDay>6/29/2019</BusinessDay>
        <BusinessStartingTime>6/29/2019 5:36:58 AM</BusinessStartingTime>
        <BusinessEndingTime>6/30/2019 12:15:55 AM</BusinessEndingTime>
        <StartingBalance>0.0000</StartingBalance>
        <EndingBalance>8</EndingBalance>
    </SafeEODBalance>
    <SafeEODBalance>
        <Lane>2</Lane>
        <PouchId>06292019053042002</PouchId>
        <BusinessDay>6/29/2019</BusinessDay>
        <BusinessStartingTime>6/29/2019 5:36:58 AM</BusinessStartingTime>
        <BusinessEndingTime>6/30/2019 12:15:55 AM</BusinessEndingTime>
        <StartingBalance>100.0000</StartingBalance>
        <EndingBalance>2</EndingBalance>
    </SafeEODBalance>
</ArrayOfSafeEODBalance>

And saved to C:\Users\cj\Documents\EodBalance.xml

I have set up the SQL Server table [dbo].[EndofDay] which has the columns of each of these exactly:

SQL Layout

Here is the query I am trying:

INSERT INTO [dbo].[EndofDay] ([PouchID], [Lane], [BusinessDay], BusinessStartingTime, BusinessEndingTime, [StartingBalance], [EndingBalance])
    SELECT
        MY_XML.SafeEODBalance.query('PouchId').value('.', 'VARCHAR(25)'),
        MY_XML.SafeEODBalance.query('Lane').value('.', 'NCHAR(2)'),
        MY_XML.SafeEODBalance.query('BusinessDay').value('.', 'DATE'),
        MY_XML.SafeEODBalance.query('BusinessStartingTime').value('.', 'DATETIME'),
        MY_XML.SafeEODBalance.query('BusinessEndingTime').value('.', 'DATETIME'),
        MY_XML.SafeEODBalance.query('StartingBalance').value('.', 'NCHAR(10)'),
        MY_XML.SafeEODBalance.query('EndingBalance').value('.', 'NCHAR(10)')
    FROM 
        (SELECT CAST(MY_XML AS XML)
         FROM OPENROWSET(BULK 'C:\Users\cj\Documents\EodBalance.xml',SINGLE_BLOB) AS T(MY_XML)) AS T(MY_XML)
         CROSS APPLY MY_XML.nodes('SafeEODBalance/SafeEODBalances') AS MY_XML (SafeEODBalance);

When I run this I get:

    (0 rows affected)        

    Completion time: 2019-08-29T16:07:12.3361442-04:00

Which obviously should feed two lines into this, but it is giving nothing in the table.


回答1:


Here is adjusted working SQL. Just uncomment the INSERT lines when you are ready.

SQL

WITH XmlFile (xmlData) AS
(
   SELECT CAST(BulkColumn AS XML) 
   FROM OPENROWSET(BULK 'C:\Users\cj\Documents\EodBalance.xml', SINGLE_BLOB) AS x
)
--INSERT INTO [dbo].[EndofDay] 
--([PouchID], [Lane], [BusinessDay], BusinessStartingTime, BusinessEndingTime, [StartingBalance], [EndingBalance])
SELECT c.value('(PouchId/text())[1]', 'VARCHAR(25)') AS [PouchId]
   , c.value('(Lane/text())[1]', 'NCHAR(2)') AS [Lane]
   , c.value('(BusinessDay/text())[1]', 'DATE') AS [BusinessDay]
   , c.value('(BusinessStartingTime)[1]', 'datetime') AS [BusinessStartingTime]
   , c.value('(BusinessEndingTime/text())[1]', 'datetime') AS [BusinessEndingTime]
   , c.value('(StartingBalance/text())[1]', 'MONEY') AS [StartingBalance]
   , c.value('(EndingBalance/text())[1]', 'MONEY') AS [EndingBalance]
FROM XmlFile CROSS APPLY xmlData.nodes('/ArrayOfSafeEODBalance/SafeEODBalance') AS t(c);



回答2:


** EDIT ** As pointed out in the comments below, this answer uses legacy functions and SPs so should not be used unless you are running on a pre-2005 version of SQL


Here is a slightly different approach, using a variable to store the XML from OPENROWSET and the stored procedure sp_xml_preparedocument to convert it into an XML document.

Once in XML document form it can be queried using OPENXML(). This has the possible advantage that if you have a large or complex XML structure from which you wish to make several extracts, you can re-use the XML document repeatedly without having to reload the original XML file.

Be sure to remove the XML document using sp_xml_removedocument when you have finished with it to free up the server cache.

-- Load the XML file and convert it to an XML document
DECLARE @XML AS XML, @hXML AS INT;
SELECT @XML = CONVERT(XML, x.BulkColumn) 
    FROM OPENROWSET(BULK 'C:\Users\cj\Documents\EodBalance.xml\EodBalance.xml', SINGLE_BLOB) AS x;
EXEC sp_xml_preparedocument @hXML OUTPUT, @XML


-- Select data from the XML document
SELECT Lane, PouchID, BusinessDay, BusinessStartingTime, BusinessEndingTime,  StartingBalance, EndingBalance

    FROM OPENXML(@hXML, 'ArrayOfSafeEODBalance/SafeEODBalance') WITH
    (
        Lane                    [varchar](2)    'Lane',
        PouchId                 [varchar](50)   'PouchId',
        BusinessDay             [date]          'BusinessDay',
        BusinessStartingTime    [datetime]      'BusinessStartingTime',
        BusinessEndingTime      [datetime]      'BusinessEndingTime',
        StartingBalance         [varchar](50)   'StartingBalance',
        EndingBalance           [varchar](50)   'EndingBalance'
    );

-- Remove the XML document from the cache
EXEC sp_xml_removedocument @hXML;


来源:https://stackoverflow.com/questions/57717239/getting-xml-to-feed-into-sql-server-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!