I'm busy converting a website from xml file base to a SQL Server 2012 express database, now I've managed to get most of the tags from the xml to import into SQL Server without hassle
My issue now lies with a particular section of these xmls
This is the section I'm having issues with
<?xml version="1.0" encoding="utf-16"?>
<License xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Reference>00b2d4cf-699c-4427-bcf0-a0ca456654c2</Reference>
<MacAddresses>
<string>00:0D:87:FE:9A:21</string>
<string>00:20:ED:14:47:64</string>
<string>00:13:D3:11:7C:D2</string>
<string>00:13:D3:11:7C:D8</string>
<string>00:19:D1:04:12:98</string>
<string>00:19:66:C0:7F:AF</string>
</MacAddresses>
</License>
I have a reference for each license and the MAC Addresses that are assigned to that license.
Now i want it to import it into sql as follows:
Reference MACAddress
--------- ----------
00b2d4cf-699c-4427-bcf0-a0ca456654c2 00:0D:87:FE:9A:21
00b2d4cf-699c-4427-bcf0-a0ca456654c2 00:20:ED:14:47:64
00b2d4cf-699c-4427-bcf0-a0ca456654c2 00:13:D3:11:7C:D2
00b2d4cf-699c-4427-bcf0-a0ca456654c2 00:13:D3:11:7C:D8
00b2d4cf-699c-4427-bcf0-a0ca456654c2 00:19:D1:04:12:98
00b2d4cf-699c-4427-bcf0-a0ca456654c2 00:19:D1:04:12:98
00b2d4cf-699c-4427-bcf0-a0ca456654c2 00:19:66:C0:7F:AF
I would do it manually but I have 700+ license to import and it would take far too long to do it manually.
help would be appreciated
If you're using SQL Server 2005 or newer, you can use the native XQuery support to extract the data you want from that XML with this:
DECLARE @input XML = '<License xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Reference>00b2d4cf-699c-4427-bcf0-a0ca456654c2</Reference>
<MacAddresses>
<string>00:0D:87:FE:9A:21</string>
<string>00:20:ED:14:47:64</string>
<string>00:13:D3:11:7C:D2</string>
<string>00:13:D3:11:7C:D8</string>
<string>00:19:D1:04:12:98</string>
<string>00:19:66:C0:7F:AF</string>
</MacAddresses>
</License>'
SELECT
Reference = License.value('(Reference)[1]', 'varchar(50)'),
MacAddress = MacAddr.value('.', 'varchar(50)')
FROM
@input.nodes('License') AS XTbl1(License)
CROSS APPLY
License.nodes('MacAddresses/string') AS XTbl2(MacAddr)
Gives output of:
Reference MacAddress
00b2d4cf-699c-4427-bcf0-a0ca456654c2 00:0D:87:FE:9A:21
00b2d4cf-699c-4427-bcf0-a0ca456654c2 00:20:ED:14:47:64
00b2d4cf-699c-4427-bcf0-a0ca456654c2 00:13:D3:11:7C:D2
00b2d4cf-699c-4427-bcf0-a0ca456654c2 00:13:D3:11:7C:D8
00b2d4cf-699c-4427-bcf0-a0ca456654c2 00:19:D1:04:12:98
00b2d4cf-699c-4427-bcf0-a0ca456654c2 00:19:66:C0:7F:AF
And of course, you can also use an INSERT INTO dbo.YourTable(Reference, MacAddress) ....
before the SELECT
to automatically insert this data into the table
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.