将XML导入SQL Server的过程可以分为以下几个步骤:
以下是一个示例,演示如何将XML文件导入到SQL Server中:
<?xml version="1.0" encoding="UTF-8"?><Customers>
<Customer>
<CustomerID>1</CustomerID>
<FirstName>John</FirstName>
<LastName>Doe</LastName>
<Email>john.doe@example.com</Email>
</Customer>
<Customer>
<CustomerID>2</CustomerID>
<FirstName>Jane</FirstName>
<LastName>Doe</LastName>
<Email>jane.doe@example.com</Email>
</Customer>
</Customers>
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
FirstName NVARCHAR(50),
LastName NVARCHAR(50),
Email NVARCHAR(100)
);
INSERT INTO Customers (CustomerID, FirstName, LastName, Email)
SELECT
CustomerID = x.value('CustomerID[1]', 'INT'),
FirstName = x.value('FirstName[1]', 'NVARCHAR(50)'),
LastName = x.value('LastName[1]', 'NVARCHAR(50)'),
Email = x.value('Email[1]', 'NVARCHAR(100)')
FROM OPENROWSET(
BULK 'C:\path\to\customers.xml',
SINGLE_BLOB
) AS x(xmlData);
INSERT INTO Customers (CustomerID, FirstName, LastName, Email)
SELECT
CustomerID = x.value('CustomerID[1]', 'INT'),
FirstName = x.value('FirstName[1]', 'NVARCHAR(50)'),
LastName = x.value('LastName[1]', 'NVARCHAR(50)'),
Email = x.value('Email[1]', 'NVARCHAR(100)')
FROM (
SELECT CAST(xmlData AS XML)
FROM OPENROWSET(
BULK 'C:\path\to\customers.xml',
SINGLE_BLOB
) AS x(xmlData)
) AS T(x);
SELECT * FROM Customers;
这将显示从XML文件导入到SQL Server表中的数据。
云+社区沙龙online [国产数据库]
中国数据库前世今生
云+社区沙龙online [新技术实践]
云+社区沙龙online [国产数据库]
云+社区技术沙龙[第19期]
云+社区技术沙龙[第17期]
云+社区沙龙online [国产数据库]
Elastic 实战工作坊
Elastic 实战工作坊
DB-TALK 技术分享会
领取专属 10元无门槛券
手把手带您无忧上云