Parsing and importing XML into a table in SQL Server

I wrote a CLR assembly that exports table data to an XML file. Now I want to import this data into a temporary table on another instance. The structure of the XML file is as follows:

<row>
  <SystemInformationID>1</SystemInformationID>
  <Database_x0020_Version>10.00.80404.00</Database_x0020_Version>
  <VersionDate>2008-04-04T00:00:00</VersionDate>
  <ModifiedDate>2008-04-04T00:00:00</ModifiedDate>
</row>

I want the XML to be processed at the destination and imported into the temporary table. I also have a main table, so I can get the table structure from there. Is there any way? I am using OPENXML, but it seems to be working incorrectly. I can read the XML file into a table that will be stored in a column with an XML data type. My problem is analyzing the data in this column. This is a temporary attempt:

CREATE TABLE ##T (IntCol int, XmlCol xml)
GO

INSERT INTO ##T(XmlCol)
SELECT * FROM OPENROWSET(
   BULK 'c:\HISOutput.xml',
   SINGLE_CLOB) AS x
--works correctly up to this point

DECLARE @x xml
DECLARE @id int
SELECT @x=XmlCol FROM ##T

EXEC sp_xml_preparedocument @id OUTPUT, @x

SELECT    *
FROM       OPENXML (@id,'/row',2)
WITH 
dbo.awbuildversion

--I used dbo.awbuildversion table from AdventureWorks DB for testing
this doesn't show the first column no matter how I change the OPENXML instruction.

tx in advance

+3
source share
1 answer

, , OMG Ponies . temp table/table?

, OPENXML sp_xml_preparedocument SQL Server 2050 ( , CLR) - .

, , INTO #tempTable

DECLARE @foo xml

SET @foo = '<row>
  <SystemInformationID>1</SystemInformationID>
  <Database_x0020_Version>10.00.80404.00</Database_x0020_Version>
  <VersionDate>2008-04-04T00:00:00</VersionDate>
  <ModifiedDate>2008-04-04T00:00:00</ModifiedDate>
</row>'

SELECT
    bar.value('./SystemInformationID[1]','INT') AS 'SystemInformationID',
    bar.value('./Database_x0020_Version[1]','VARCHAR(14)') AS 'Database_x0020_Version',
    bar.value('./VersionDate[1]','DATETIME') AS 'VersionDate',
    bar.value('./ModifiedDate[1]','DATETIME') AS 'ModifiedDate'
INTO #tempTable    -- This?
FROM
    @foo.nodes('/row') AS foo(bar)     --use nodes not OPENXML
+3

Source: https://habr.com/ru/post/1724202/


All Articles