我想将XML文件中的数据迁移到MS SQL Server 2016
My Table fileds如下所示:
[propertyid], [type], [object], [keyname], [keyvalue]
Data Inside表将从XML映射,如下所示:
1, community, Private Community, name, Private Community
2, community, Private Community, communityname, Private Community
3, community, Private Community, server, chip.xyz.com:423132
4, community, Private Community, windowtitle, Private Community
5, community, Private Community, websiteurl, http://something
XML文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<doc>
<object type="community">
<name>
<![CDATA[Private Community]]>
</name>
<communityname>
<![CDATA[Private Community]]>
</communityname>
<server>
<![CDATA[chip.xyz.com:423132]]>
</server>
<windowtitle>
<![CDATA[Private Community]]>
</windowtitle>
<websiteurl>
<![CDATA[http://something]]>
</websiteurl>
</object>
</doc>
我有什么方法可以通过脚本或任何方式来实现它,因为上面提到的XML只是我正在使用的大XML的一部分并且有很多记录
答案 0 :(得分:0)
试试这样:
DECLARE @xml XML=
'<?xml version="1.0" encoding="UTF-8"?>
<doc>
<object type="community">
<name>
Private Community
</name>
<communityname>
Private Community
</communityname>
<server>
chip.xyz.com:423132
</server>
<windowtitle>
Private Community
</windowtitle>
<websiteurl>
http://something
</websiteurl>
</object>
</doc>';
我更换换行符并修剪琴弦,希望这符合您的需求
我不知道的是:如果有更多的数据,结构如何。我的查询以doc
为根,以doc/name
为&#34;输入&#34;并且所有节点名称为&#34; keyname&#34;。
使用&#34; INTO MyNewlyCreatedTable&#34;在运行中创建具有所需结构的表。如果您已经有了自己的牌桌,那么您可以选择此线并在INSERT INTO MyTable(colName1,colName2,...)
后面加SELECT ...
SELECT ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) AS propertyid
,LTRIM(RTRIM(REPLACE(REPLACE(Doc.value('(object/@type)[1]','varchar(max)'),CHAR(13),''),CHAR(10),''))) AS [type]
,LTRIM(RTRIM(REPLACE(REPLACE(Doc.value('(object/name)[1]','varchar(max)'),CHAR(13),''),CHAR(10),''))) AS [object]
,LTRIM(RTRIM(REPLACE(REPLACE(Nod.value('local-name(.)','varchar(max)'),CHAR(13),''),CHAR(10),''))) AS keyname
,LTRIM(RTRIM(REPLACE(REPLACE(Nod.value('.','varchar(max)'),CHAR(13),''),CHAR(10),''))) AS keyvalue
INTO MyNewlyCreatedTable
FROM @xml.nodes('/doc') AS A(Doc)
CROSS APPLY Doc.nodes('object/*') AS B(Nod)
这是新创建的表的内容
SELECT * FROM MyNewlyCreatedTable;
+------------+-----------+-------------------+---------------+---------------------+
| propertyid | type | object | keyname | keyvalue |
+------------+-----------+-------------------+---------------+---------------------+
| 1 | community | Private Community | name | Private Community |
+------------+-----------+-------------------+---------------+---------------------+
| 2 | community | Private Community | communityname | Private Community |
+------------+-----------+-------------------+---------------+---------------------+
| 3 | community | Private Community | server | chip.xyz.com:423132 |
+------------+-----------+-------------------+---------------+---------------------+
| 4 | community | Private Community | windowtitle | Private Community |
+------------+-----------+-------------------+---------------+---------------------+
| 5 | community | Private Community | websiteurl | http://something |
+------------+-----------+-------------------+---------------+---------------------+
仅供测试
--CleanUp
DROP TABLE MyNewlyCreatedTable;