How to load an XML file into a database using an SSIS package?(如何使用 SSIS 包将 XML 文件加载到数据库中?)
问题描述
我在 Visual Studio 2008 中使用 SSIS.我有很多 XML 文件需要处理并放入现有的数据库结构 (SQL Server 2005) 中.这是我第一次尝试使用 SSIS 并且有点卡住了.我找到了 XML 数据流任务,为它分配了一个测试 xml 文件及其关联的 XSD,并将一个节点映射到数据库表.我的问题是,如何将许多 xsd 节点与许多表相关联?当然我不必为每个表设置一个 XML 源?
I am using SSIS in Visual Studio 2008. I have many XML files that I need to process and place into an existing DB structure (SQL Server 2005). This is my first attempt at using SSIS and am a little stuck. I have found the XML Data Flow task, assigned it a test xml file and it's associated XSD, and mapped one node to a Database Table. My question is, how do I associate many xsd nodes with many tables? Surely I don't have to set up an XML source for each table?
推荐答案
这是一个可能的选项,它演示了如何将具有相同定义的多个 XML 文件加载到 SQL Server 表中.该示例使用 SQL Server 2008 R2 和 SSIS 2008 R2.此处显示的示例在 XML Source 组件的帮助下使用 SSIS Data Flow Task 将三个 XML 文件加载到 SQL 表中.
Here is a possible option which demonstrates how to load multiple XML files having same definition into an SQL Server table. The example uses SQL Server 2008 R2 and SSIS 2008 R2. The example shown here loads three XML files into an SQL table using SSIS Data Flow Task with the help of XML Source component.
分步过程:
- 使用 SQL 脚本 部分下给出的脚本创建一个名为
dbo.Items的表. - 使用 XSD 文件 部分下提供的内容在文件夹路径 C: empxsd 中创建名为
Items.xsd的 XSD 文件. - 在文件夹路径 C: 中创建三个 XML 文件,分别为
Items_1.xml、Items_2.xml和Items_3.xmltempxml 使用 XML 文件 部分下提供的内容. - 在包上,创建 3 个变量,即
FileExtension、FilePath和FolderPath,如截图 #1. - 在包的连接管理器上,创建一个名为
SQLServer的 OLE DB 连接以连接到 SQL Server 实例,如屏幕截图 #2 所示. - 在
Control Flow选项卡上,在 Foreach 循环容器中放置一个Foreach 循环容器和一个Data Flow Task,如屏幕截图所示#3. - 配置
Foreach Loop 容器,如屏幕截图 #4 和 #5 所示. - 双击
Data Flow Task以导航到Data Flow选项卡.放置一个XML Source组件和一个OLE DB Destination,如屏幕截图 #6 所示. - 配置
XML 源,如屏幕截图 #7 和 #8 所示.XML 文件路径将从变量 FilePath 中检索.此变量将由Foreach 循环容器填充. - 配置
OLE DB 目标,如屏幕截图 #9 和 #10 所示. - 屏幕截图 #11 和 #12 显示了包的执行情况.
- 屏幕截图 #13 显示了包执行之前的表数据.屏幕截图 #14 显示了包执行后 的表数据.表
dbo.Items中的数据现在包含存在于三个 XML 文件中的数据.
- Create a table named
dbo.Itemsusing the script given under SQL Scripts section. - Create an XSD file named
Items.xsdin the folder path C: empxsd using the content provided under XSD File section. - Create three XML files namely
Items_1.xml,Items_2.xmlandItems_3.xmlin the folder path C: empxml using the content provided under XML Files section. - On the package, create 3 variables namely
FileExtension,FilePathandFolderPathas shown in screenshot #1. - On the package's Connection Managers, create an OLE DB Connection named
SQLServerto connect to the SQL Server Instance as shown in screenshot #2. - On the
Control Flowtab, place aForeach loop containerand aData Flow Taskwithin the Foreach loop container as shown in screenshot #3. - Configure the
Foreach Loop containeras shown in screenshots #4 and #5. - Double-click on the
Data Flow Taskto navigate to theData Flowtab. Place anXML Sourcecomponent and anOLE DB Destinationas shown in screenshot #6. - Configure the
XML Sourceas shown in screenshot #7 and #8. The XML file path will be retrieved from the variable FilePath. This variable will be populated by theForeach Loop container. - Configure the
OLE DB Destinationas shown in screenshots #9 and #10. - Screenshots #11 and #12 show the package execution.
- Screenshot #13 shows the table data before the package execution. Screenshot #14 shows the table data after the package execution. The data in the table
dbo.Itemsnow contains the data present in three XML files.
希望有所帮助.
SQL 脚本:
CREATE TABLE [dbo].[Items](
[Id] [int] IDENTITY(1,1) NOT NULL,
[ItemNumber] [nvarchar](50) NOT NULL,
[ItemName] [nvarchar](60) NOT NULL,
[Price] [numeric](18, 2) NOT NULL,
CONSTRAINT [PK_Items] PRIMARY KEY CLUSTERED ([Id] ASC)) ON [PRIMARY]
GO
XSD 文件
<xsd:schema xmlns:schema="ItemsXSDSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes" targetNamespace="ItemsXSDSchema" elementFormDefault="qualified">
<xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/sqltypes" schemaLocation="http://schemas.microsoft.com/sqlserver/2004/sqltypes/sqltypes.xsd" />
<xsd:element name="Items">
<xsd:complexType>
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="unbounded" name="Item">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Id" type="sqltypes:int" />
<xsd:element name="ItemNumber">
<xsd:simpleType>
<xsd:restriction base="sqltypes:nvarchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
<xsd:maxLength value="20" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="ItemName">
<xsd:simpleType>
<xsd:restriction base="sqltypes:nvarchar" sqltypes:localeId="1033" sqltypes:sqlCompareOptions="IgnoreCase IgnoreKanaType IgnoreWidth" sqltypes:sqlSortId="52">
<xsd:maxLength value="60" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
<xsd:element name="Price">
<xsd:simpleType>
<xsd:restriction base="sqltypes:numeric">
<xsd:totalDigits value="18" />
<xsd:fractionDigits value="2" />
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
XML 文件
Items_1.xml
<?xml version="1.0"?>
<Items xmlns="ItemsXSDSchema">
<Item>
<Id>1</Id>
<ItemNumber>I2345343</ItemNumber>
<ItemName>Monitor</ItemName>
<Price>299.99</Price>
</Item>
</Items>
Items_2.xml
<?xml version="1.0"?>
<Items xmlns="ItemsXSDSchema">
<Item>
<Id>1</Id>
<ItemNumber>J1231231</ItemNumber>
<ItemName>Mouse</ItemName>
<Price>29.99</Price>
</Item>
</Items>
Items_3.xml
<?xml version="1.0"?>
<Items xmlns="ItemsXSDSchema">
<Item>
<Id>1</Id>
<ItemNumber>K0456212</ItemNumber>
<ItemName>Keyboard</ItemName>
<Price>49.99</Price>
</Item>
</Items>
屏幕截图 #1:
屏幕截图 #2:
屏幕截图 #3:
屏幕截图 #4:
屏幕截图 #5:
截图 #6:
屏幕截图 #7:
截图 #8:
屏幕截图 #9:
屏幕截图 #10:
屏幕截图 #11:
屏幕截图 #12:
屏幕截图 #13:
屏幕截图 #14:
这篇关于如何使用 SSIS 包将 XML 文件加载到数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 SSIS 包将 XML 文件加载到数据库中?
- SQL 临时表问题 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
