Working to learn how to import XML to SQL Server and used an example from here which works.
Using the tutorial from the link, it works fine. Data from that sample XML loads data to columns in a table as expected.
When I tried, using a different XML and updated query, SQL Server says
(0 rows affected)
I created a table:
CREATE TABLE [FDAGUDID](
[publicDeviceRecordKey] [varchar](100) NOT NULL,
[publicVersionStatus] [varchar](100) NOT NULL,
[deviceRecordStatus] [varchar](100) NOT NULL,
[publicVersionNumber] [varchar](100) NOT NULL,
[publicVersionDate] [varchar](100) NOT NULL,
[devicePublishDate] [varchar](100) NOT NULL,
[deviceCommDistributionEndDate] [varchar](100) NOT NULL,
[brandName] [varchar](100) NOT NULL,
[versionModelNumber] [varchar](100) NOT NULL
)
Here is the XML I tried - just a sample of the full XML:
<?xml version="1.0" encoding="UTF-8"?>
<gudid xmlns="http://www.fda.gov/cdrh/gudid" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0" xsi:schemaLocation="http://www.fda.gov/cdrh/gudid gudid.xsd">
<header>
<database frequency="daily" id="5625" type="DELTA_INCREMENTAL">
<downloadFile part="1" totalParts="1"/>
<numberRecordXML>11094</numberRecordXML>
<numberRecordsDatabase>11094</numberRecordsDatabase>
</database>
<creationDate>2024-07-25T00:58:05</creationDate>
<period end="2024-07-25T00:58:05" start="2024-07-24T00:52:59"/>
</header>
<device>
<publicDeviceRecordKey>6ddcef64-e8e6-49ed-bb9c-fa19855f1329</publicDeviceRecordKey>
<publicVersionStatus>Update</publicVersionStatus>
<deviceRecordStatus>Published</deviceRecordStatus>
<!--Note: publicVersionNumber is system generated and assigned to all device records to track public release updates to a device record-->
<publicVersionNumber>2</publicVersionNumber>
<publicVersionDate>2024-07-25</publicVersionDate>
<devicePublishDate>2021-06-01</devicePublishDate>
<deviceCommDistributionEndDate xsi:nil="true"></deviceCommDistributionEndDate>
<deviceCommDistributionStatus>In Commercial Distribution</deviceCommDistributionStatus>
<brandName>Top Quality Manufacturing Barrier Film</brandName>
<versionModelNumber>BF21BLU</versionModelNumber>
</device>
</gudid>
Here is the SQL I tried yet I am now sure what I am missing.
INSERT INTO [dbo].[FDAGUDID]
([publicDeviceRecordKey],[publicVersionStatus]
,[deviceRecordStatus],[publicVersionNumber]
,[publicVersionDate],[devicePublishDate]
,[deviceCommDistributionEndDate],[brandName]
,[versionModelNumber])
SELECT
MY_XML.device.query('publicDeviceRecordKey').value('.', 'VARCHAR(100)'),
MY_XML.device.query('publicVersionStatus').value('.', 'VARCHAR(100)'),
MY_XML.device.query('deviceRecordStatus').value('.', 'VARCHAR(100)'),
MY_XML.device.query('publicVersionNumber').value('.', 'VARCHAR(100)'),
MY_XML.device.query('publicVersionDate').value('.', 'VARCHAR(100)'),
MY_XML.device.query('devicePublishDate').value('.', 'VARCHAR(100)'),
MY_XML.device.query('deviceCommDistributionEndDate').value('.', 'VARCHAR(100)'),
MY_XML.device.query('brandName').value('.', 'VARCHAR(100)'),
MY_XML.device.query('versionModelNumber').value('.', 'VARCHAR(100)')
FROM (SELECT CAST(MY_XML AS xml)
FROM OPENROWSET(BULK 'C:\temp\test.xml', SINGLE_BLOB) AS T(MY_XML)) AS T(MY_XML)
CROSS APPLY MY_XML.nodes('gudid/device') AS MY_XML (device)
This returned no error codes and just said:
(0 rows affected)
Appreciate any help and guidance on how to load this data to a table and load the data to columns.
/guidin the anonymous namespace is not the same element as/guidin thehttp://www.fda.gov/cdrh/gudidnamespace. Compare and contrast the results... db<>fiddle>..query('publicDeviceRecordKey').value('.', 'VARCHAR(100)'),you can do.value('(publicDeviceRecordKey/text())[1]', 'VARCHAR(100)'),which is much more efficient.