Azure SQL 数据库最近获得了使用 BULK INSERT 或 OPENROWSET 从 Azure Blob 存储加载文件的能力。从这里开始。
使用您的示例代码和数据,您可以导入 xml,将其切碎,然后MERGE
将INSERT/UPDATE
其用于您的主表中。一个简单的演示:
DROP TABLE staging
DROP TABLE #tmp
DROP TABLE yourTable
CREATE TABLE staging ( rowId INT IDENTITY PRIMARY KEY, yourXML XML );
CREATE TABLE #tmp ( id INT PRIMARY KEY, name VARCHAR(30) NOT NULL, age INT NOT NULL );
SELECT * INTO yourTable FROM #tmp
INSERT INTO staging
SELECT *
FROM OPENROWSET(
BULK 'archive/temp.xml',
DATA_SOURCE = 'MyAzureBlobStorageAccount',
SINGLE_BLOB
) AS x
INSERT INTO #tmp ( id, name, age )
SELECT
x.c.value('(id/text())[1]', 'int' ) AS id,
x.c.value('(name/text())[1]', 'varchar(30)' ) AS name,
x.c.value('(age/text())[1]', 'int' ) AS age
FROM staging s
CROSS APPLY s.yourXML.nodes('Details/Person') AS x(c)
-- Merge
MERGE INTO dbo.yourTable t
USING
(
SELECT * FROM #tmp
) s ON t.id = s.id
-- Insert new records ( no match on primary key )
WHEN NOT MATCHED BY TARGET
THEN
INSERT ( id, name, age )
VALUES ( id, name, age )
WHEN MATCHED
AND t.name != s.name
OR t.age != s.age
-- Update existing ( no match on primary key )
THEN UPDATE
SET
t.name = s.name,
t.age = s.age;
SELECT *
FROM dbo.yourTable
您最好暂时忘记数据工厂。编写一个 SQL 脚本,成功完成上述设置并成功运行。当您完成该工作时,将其转换为存储过程。测试一下。当你完成这项工作后,你就可以开始考虑数据工厂了。您将需要一个输出数据集,但不需要输入数据集。在此处完成教程。