0

如果文件格式是这样的,如何从 XML 文件插入 SQL Server?

我想要列DescriptionAliasName

<?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"/> 
4

1 回答 1

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)

结果输出如下:

在此处输入图像描述

于 2012-12-20T06:13:20.383 回答