简体   繁体   中英

Importing XML data to SQL 2019 only returns NULL or empty values

I have the following data in an XML file:

<?xml version="1.0" encoding="utf-16"?>
<documents>
  <document ArchivedFileName="20254834\21507.tif" DocumentId="21507" FileSize="525208" OriginalFileName="20254834_439303_20090613_105156_564.tif" DocumentTypeName="5" FileNumber="20254834" RecordingNumber="&lt;NULL&gt;" Description="" CreationDate="8/24/2006 12:00:00 AM" ModificationDate="8/24/2006 12:00:00 AM" VersionId="1" />
  <document ArchivedFileName="20288517\69932.tif" DocumentId="69932" FileSize="35342" OriginalFileName="20288517_126_20090613_121001_591.tif" DocumentTypeName="49" FileNumber="20288517" RecordingNumber="&lt;NULL&gt;" Description="#1" CreationDate="8/30/2006 12:00:00 AM" ModificationDate="8/30/2006 12:00:00 AM" VersionId="1" />
  <document ArchivedFileName="20257573\21600.tif" DocumentId="21600" FileSize="33519" OriginalFileName="20257573_408080_20090613_105204_562.tif" DocumentTypeName="25" FileNumber="20257573" RecordingNumber="&lt;NULL&gt;" Description="GI RUN" CreationDate="9/12/2006 12:00:00 AM" ModificationDate="10/30/2020 2:43:55 PM" VersionId="1" />
</documents>

I'm trying to import this xml file, but no matter what I try I end up with NULL or empty values. Here are a few SQL scripts I have tried:

--///////////////////////////  empty NO RESULTS
SELECT
   MY_XML.document.query('ArchivedFileName').value('.', 'VARCHAR(100)')
  FROM (SELECT CAST(MY_XML AS xml)
      FROM OPENROWSET(BULK 'C:\temp\Index3_sample.xml', SINGLE_BLOB) AS T(MY_XML)) AS T(MY_XML)
      CROSS APPLY MY_XML.nodes('documents/document') AS MY_XML (document);
--///////////////////  NULL VALUES
declare @X xml;
select @X = T.MY_XML
from openrowset(bulk 'C:\temp\Index3_sample.xml', single_blob) as T(MY_XML)
select 
  MY_XML.document.value('(ArchivedFileName/text())[1]', 'VARCHAR(100)'),
  MY_XML.document.value('(DocumentID/text())[1]', 'VARCHAR(100)'),--,
  MY_XML.document.value('(Filesize/text())[1]', 'VARCHAR(100)'),
  MY_XML.document.value('(OriginalFileName/text())[1]', 'VARCHAR(100)'),
  MY_XML.document.value('(DocumentTypeName/text())[1]', 'VARCHAR(100)'),
  MY_XML.document.value('(FileNumber/text())[1]', 'VARCHAR(100)'),
  MY_XML.document.value('(RecordingNumber/text())[1]', 'VARCHAR(100)'),
  MY_XML.document.value('(Description/text())[1]', 'VARCHAR(100)'),
  MY_XML.document.value('(Creationdate/text())[1]', 'VARCHAR(100)'),
  MY_XML.document.value('(Modificationdate/text())[1]', 'VARCHAR(100)'),
  MY_XML.document.value('(VersionID/text())[1]', 'VARCHAR(100)')
from @X.nodes('documents/document') AS MY_XML (document);
--/////////////////////////////  NULL VALUES
SELECT
MY_XML.document.value('(ArchivedFileName/text())[1]', 'VARCHAR(100)')--,
FROM (SELECT CAST(MY_XML AS xml)
FROM OPENROWSET(BULK 'C:\temp\Index3_sample.xml', SINGLE_BLOB) AS T(MY_XML)) AS T(MY_XML)
CROSS APPLY MY_XML.nodes('documents/document') AS MY_XML (document);
--/////////////////////  EMPTY VALUES
select 
my_xml.Document.query('ArchivedFileName').value('.', 'Varchar(100)')
from (select cast(my_xml as xml)
from openrowset (bulk 'C:\temp\Index3_sample.xml', single_blob)
as t(my_xml)) as T(my_xml)
cross apply my_xml.nodes('documents/document')
as my_xml (document);

Can you help me out and tell me what I'm doing wrong? I'm not super familiar with XML and honestly have copy/pasted some of these scripts.

Thanks!

XQuery is case sensitive and attributes are accessed with @ .

So something like:

declare @X xml = '
<documents>
  <document ArchivedFileName="20254834\21507.tif" DocumentId="21507" FileSize="525208" OriginalFileName="20254834_439303_20090613_105156_564.tif" DocumentTypeName="5" FileNumber="20254834" RecordingNumber="&lt;NULL&gt;" Description="" CreationDate="8/24/2006 12:00:00 AM" ModificationDate="8/24/2006 12:00:00 AM" VersionId="1" />
  <document ArchivedFileName="20288517\69932.tif" DocumentId="69932" FileSize="35342" OriginalFileName="20288517_126_20090613_121001_591.tif" DocumentTypeName="49" FileNumber="20288517" RecordingNumber="&lt;NULL&gt;" Description="#1" CreationDate="8/30/2006 12:00:00 AM" ModificationDate="8/30/2006 12:00:00 AM" VersionId="1" />
  <document ArchivedFileName="20257573\21600.tif" DocumentId="21600" FileSize="33519" OriginalFileName="20257573_408080_20090613_105204_562.tif" DocumentTypeName="25" FileNumber="20257573" RecordingNumber="&lt;NULL&gt;" Description="GI RUN" CreationDate="9/12/2006 12:00:00 AM" ModificationDate="10/30/2020 2:43:55 PM" VersionId="1" />
</documents>';
select 
  MY_XML.document.value('@ArchivedFileName', 'VARCHAR(100)'),
  MY_XML.document.value('@DocumentId', 'VARCHAR(100)'),--,
  MY_XML.document.value('@FileSize', 'VARCHAR(100)'),
  MY_XML.document.value('@OriginalFileName', 'VARCHAR(100)'),
  MY_XML.document.value('@DocumentTypeName', 'VARCHAR(100)'),
  MY_XML.document.value('@FileNumber', 'VARCHAR(100)'),
  MY_XML.document.value('@RecordingNumber', 'VARCHAR(100)'),
  MY_XML.document.value('@Description', 'VARCHAR(100)'),
  MY_XML.document.value('@CreationDate', 'VARCHAR(100)'),
  MY_XML.document.value('@ModificationDate', 'VARCHAR(100)'),
  MY_XML.document.value('@VersionId', 'VARCHAR(100)')
from @X.nodes('/documents/document') AS MY_XML (document);

Here is a full solution for the task.

Your XML is attributes based. That's why your attempts treating XML as elements didn't work. <documents> and <document are XML elements. Everything else is attributes.

While shredding XML into a rectangular/relational format, it is better to use real data types: INT , DATETIME , VARCHAR(...) , etc.

I saved your XML as e:\Temp\Index3_sample.xml file.

<?xml version="1.0" encoding="utf-16"?>
<documents>
    <document ArchivedFileName="20254834\21507.tif" DocumentId="21507"
              FileSize="525208"
              OriginalFileName="20254834_439303_20090613_105156_564.tif"
              DocumentTypeName="5" FileNumber="20254834"
              RecordingNumber="&lt;NULL&gt;" Description=""
              CreationDate="8/24/2006 12:00:00 AM"
              ModificationDate="8/24/2006 12:00:00 AM" VersionId="1"/>
    <document ArchivedFileName="20288517\69932.tif" DocumentId="69932"
              FileSize="35342"
              OriginalFileName="20288517_126_20090613_121001_591.tif"
              DocumentTypeName="49" FileNumber="20288517"
              RecordingNumber="&lt;NULL&gt;" Description="#1"
              CreationDate="8/30/2006 12:00:00 AM"
              ModificationDate="8/30/2006 12:00:00 AM" VersionId="1"/>
    <document ArchivedFileName="20257573\21600.tif" DocumentId="21600"
              FileSize="33519"
              OriginalFileName="20257573_408080_20090613_105204_562.tif"
              DocumentTypeName="25" FileNumber="20257573"
              RecordingNumber="&lt;NULL&gt;" Description="GI RUN"
              CreationDate="9/12/2006 12:00:00 AM"
              ModificationDate="10/30/2020 2:43:55 PM" VersionId="1"/>
</documents>

SQL

WITH rs (xmlData) AS
(
   SELECT TRY_CAST(BulkColumn AS XML) 
   FROM OPENROWSET(BULK N'e:\Temp\Index3_sample.xml', SINGLE_BLOB) AS x
)
--INSERT INTO targetTable (...)
SELECT c.value('@ArchivedFileName', 'VARCHAR(100)') AS ArchivedFileName
    , c.value('@DocumentId', 'INT') AS DocumentId
    , c.value('@FileSize', 'INT') AS FileSize
    , c.value('@OriginalFileName', 'VARCHAR(100)') AS OriginalFileName
    , c.value('@DocumentTypeName', 'INT') AS DocumentTypeName
    , c.value('@FileNumber', 'INT') AS FileNumber
    , c.value('@RecordingNumber', 'VARCHAR(100)') AS RecordingNumber
    , c.value('@Description', 'VARCHAR(100)') AS [Description]
    , c.value('@CreationDate', 'DATETIME') AS CreationDate
    , c.value('@ModificationDate', 'DATETIME') AS ModificationDate
    , c.value('@VersionId', 'INT')AS VersionId
FROM rs 
   CROSS APPLY xmlData.nodes('/documents/document') AS t(c);

Output

+--------------------+------------+----------+-----------------------------------------+------------------+------------+-----------------+-------------+-------------------------+-------------------------+-----------+
|  ArchivedFileName  | DocumentId | FileSize |            OriginalFileName             | DocumentTypeName | FileNumber | RecordingNumber | Description |      CreationDate       |    ModificationDate     | VersionId |
+--------------------+------------+----------+-----------------------------------------+------------------+------------+-----------------+-------------+-------------------------+-------------------------+-----------+
| 20254834\21507.tif |      21507 |   525208 | 20254834_439303_20090613_105156_564.tif |                5 |   20254834 | <NULL>          |             | 2006-08-24 00:00:00.000 | 2006-08-24 00:00:00.000 |         1 |
| 20288517\69932.tif |      69932 |    35342 | 20288517_126_20090613_121001_591.tif    |               49 |   20288517 | <NULL>          | #1          | 2006-08-30 00:00:00.000 | 2006-08-30 00:00:00.000 |         1 |
| 20257573\21600.tif |      21600 |    33519 | 20257573_408080_20090613_105204_562.tif |               25 |   20257573 | <NULL>          | GI RUN      | 2006-09-12 00:00:00.000 | 2020-10-30 14:43:55.000 |         1 |
+--------------------+------------+----------+-----------------------------------------+------------------+------------+-----------------+-------------+-------------------------+-------------------------+-----------+

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