获取XML以馈入SQL Server表

时间:2019-08-29 20:17:31

标签: sql-server xml ssms

我正在尝试获取一个(现在)简单的XML来馈入SQL Server表。

XML是:

<?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>

并保存到C:\ Users \ cj \ Documents \ EodBalance.xml

我已经建立了SQL Server表[dbo].[EndofDay],该表具有每个列的确切位置:

SQL Layout

这是我正在尝试的查询:

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);

当我运行它时,我得到:

    (0 rows affected)        

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

显然应该在其中填充两行,但表中没有任何内容。

2 个答案:

答案 0 :(得分:3)

此处调整为工作SQL。准备好后,只需取消注释INSERT行即可。

  

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);

答案 1 :(得分:0)

**编辑**如下面的评论所指出的,此答案使用了遗留函数和SP,因此除非您在2005之前的SQL版本上运行


这是一种稍微不同的方法,使用变量存储OPENROWSET中的XML和存储过程sp_xml_preparedocument来将其转换为XML文档。

一旦使用XML文档格式,就可以使用OPENXML()进行查询。这样做的好处是,如果您有一个大型或复杂的XML结构,希望从中提取多个数据,则可以重复使用XML文档,而不必重新加载原始XML文件。

在完成处理后,请确保使用sp_xml_removedocument删除XML文档以释放服务器缓存。

-- 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;