如果文件的格式是这样的,如何从XML文件插入SQL Server?
我想要列Description
,Alias
和Name
。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ldmlBCP47 SYSTEM "../../common/dtd/ldmlBCP47.dtd">
<ldmlBCP47>
<version number="$Revision: 6625 $"/>
<generation date="$Date: 2012-03-01 15:01:04 -0800 (Thu, 01 Mar 2012) $"/>
<keyword>
<key description="Time zone key" alias="timezone" name="tz">
<type description="Andorra" alias="Europe/Andorra" name="adalv"/>
<type description="Dubai, United Arab Emirates" alias="Asia/Dubai" name="aedxb"/>
<type description="Kabul, Afghanistan" alias="Asia/Kabul" name="afkbl"/>
<type description="Antigua" alias="America/Antigua" name="aganu"/>
<type description="Anguilla" alias="America/Anguilla" name="aiaxa"/>
<type description="Tirane, Albania" alias="Europe/Tirane" name="altia"/>
答案 0 :(得分:2)
假设您的意思是从<type>
XML元素中获取三个属性,那么您可以尝试这样做:
DECLARE @input XML = '<ldmlBCP47>
<version number="$Revision: 6625 $"/>
<generation date="$Date: 2012-03-01 15:01:04 -0800 (Thu, 01 Mar 2012) $"/>
<keyword>
<key description="Time zone key" alias="timezone" name="tz">
<type description="Andorra" alias="Europe/Andorra" name="adalv"/>
<type description="Dubai, United Arab Emirates" alias="Asia/Dubai" name="aedxb"/>
<type description="Kabul, Afghanistan" alias="Asia/Kabul" name="afkbl"/>
<type description="Antigua" alias="America/Antigua" name="aganu"/>
<type description="Anguilla" alias="America/Anguilla" name="aiaxa"/>
<type description="Tirane, Albania" alias="Europe/Tirane" name="altia"/>
</key>
</keyword>
</ldmlBCP47>'
SELECT
[Description] = XTbl.TypeNode.value('@description', 'varchar(50)'),
[Alias] = XTbl.TypeNode.value('@alias', 'varchar(50)'),
[Name] = XTbl.TypeNode.value('@name', 'varchar(50)')
FROM
@input.nodes('/ldmlBCP47/keyword/key/type') AS XTbl(TypeNode)
输出结果如下: