我有一个XML格式的数据库,包含结构和数据,但我没有数据库可以从XML导入数据。
有没有办法将XML文件导入SQL Server,以便SQL Server读取XML文件,然后创建表结构并将数据插入到这些表中?或者有没有使用SSIS服务将XML文件转换为数据库表的方法?
示例代码:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="dbo.AWB_Location_detail" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="dbo.AWB_Location_detail">
<xs:complexType>
<xs:sequence>
<xs:element name="Stock_Id" type="xs:int" />
<xs:element name="LocationId" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<dbo.AWB_Location_detail>
<Stock_Id>1</Stock_Id>
<LocationId>2</LocationId>
</NewDataSet>
答案 0 :(得分:0)
以下是非常hacky,并且 - 肯定 - 不适合你所有的桌子......
我们需要临时表
CREATE TABLE #tblXMLTables(TableName NVARCHAR(150),Content XML);
使用此功能,您可以将其中一个文件插入
DECLARE @oneTableXML XML=
(
SELECT CAST(BulkColumn AS XML) FROM (SELECT BulkColumn FROM OPENROWSET(BULK 'F:\test.xml',SINGLE_BLOB) AS x) AS y
);
WITH XMLNAMESPACES('http://www.w3.org/2001/XMLSchema' AS xs
,'urn:schemas-microsoft-com:xml-msdata' AS msdata)
INSERT INTO #tblXMLTables(TableName,Content)
SELECT REPLACE(@oneTableXML.value('(/NewDataSet/xs:schema/xs:element/@msdata:MainDataTable)[1]','nvarchar(max)'),'dbo.','')
,@oneTableXML;
将所有XML填入temp-Table后 您可以使用以下代码:
注意:您必须手动处理NULL / NOT NULL,约束,外键,索引等...这将适用于&#34;简单&#34;仅限列。如果您的表格涉及更复杂的结构,那么您必须相应地更改代码......
DECLARE @tn NVARCHAR(250);
DECLARE @columns NVARCHAR(MAX);
DECLARE @cmd NVARCHAR(MAX);
DECLARE tableName CURSOR FOR SELECT TableName FROM #tblXMLTables;
OPEN tableName;
FETCH NEXT FROM tableName INTO @tn;
WHILE @@FETCH_STATUS=0
BEGIN
SET @columns=
STUFF(
(
SELECT ',A.B.value(''' + A.B.value('@name','nvarchar(max)') + '[1]'',''' + REPLACE(A.B.value('@type','nvarchar(max)'),'xs:','') + ''') AS ' + A.B.value('@name','nvarchar(max)')
FROM #tblXMLTables
CROSS APPLY Content.nodes('//*:sequence/*:element') AS A(B)
WHERE TableName=@tn
FOR XML PATH('')
),1,1,'');
SET @cmd=
'SELECT ' + @columns +
' INTO [' + @tn + ']
FROM #tblXMLTables
CROSS APPLY Content.nodes(''//dbo.' + @tn + ''') AS A(B)
WHERE TableName=''' + @tn + ''';'
;
PRINT @cmd;
EXEC(@cmd)
SET @cmd='SELECT * FROM ' + @tn;
EXEC(@cmd);
FETCH NEXT FROM tableName INTO @tn;
END
CLOSE tableName
DEALLOCATE tableName;
GO
--Clean Up
--DROP TABLE #tblXMLTables;
答案 1 :(得分:0)
看起来你要导入的xml只是一个保存为xml的.net DataSet对象。几年前我写了一个快速的脏.Net库,它将创建相应的sql表并通过BulkCopy方法将数据导入SQL Server。
虽然这不是纯粹的SQL解决方案,但它实现起来相对容易。以下代码示例演示如何使用库。
SqlConnectionStringBuilder connectionBuilder = new SqlConnectionStringBuilder();
connectionBuilder.ApplicationName = "Import";
connectionBuilder.ConnectTimeout = 30;
connectionBuilder.DataSource = "(local)";
connectionBuilder.InitialCatalog = "CodeExamples";
connectionBuilder.IntegratedSecurity = true;
string connectionString = connectionBuilder.ConnectionString;
DataSet dataSet = new DataSet();
dataSet.ReadXml(@"C:\temp\xmlData.xml");
foreach (DataTable table in dataSet.Tables)
{
SqlTableManager tableManager = new SqlTableManager(table, connectionString);
tableManager.SyncTableSchemas();
tableManager.UploadTableToSql();
}
我已将代码上传到File Dropper,请参阅以下链接:
http://www.filedropper.com/sqltablemanagement
以下是演示应用程序和代码的链接。
http://www.filedropper.com/sqltablemanagementdemo
以下是演示用于多个文件的链接: