--用SQL多條可以將多條數據組成一棵XML樹L一次插入 --將XML樹作為varchar參數傳入用 --insert xx select xxx from openxml() 的語法插入數據 -----------------------------------導入,導出xml--------------------------
--1導入實例 --單個表 create table Xmltable(Name nvarchar(20),Nowtime nvarchar(20)) declare @s as nvarchar(2000); set @s = N'' <Xmltables> <Xmltable Name="1" Nowtime="1900-1-1">0</Xmltable> <Xmltable Name="2" Nowtime="1900-1-1">0</Xmltable> <Xmltable Name="3" Nowtime="1900-1-1">0</Xmltable> <Xmltable Name="4" Nowtime="1900-1-1">0</Xmltable> <Xmltable Name="5" Nowtime="1900-1-1">0</Xmltable> </Xmltables>''; declare @idHandle as int ; EXEC sp_xml_preparedocument @idHandle OUTPUT, @s insert into Xmltable(Name,Nowtime) select * from openxml(@idHandle,N''/Xmltables/Xmltable'') with dbo.xmltable EXEC sp_xml_removedocument @idHandle select * from Xmltable -----------------------讀入第二個表數據-------------------- create table Xmlta(Name nvarchar(20),Nowtime nvarchar(20)) declare @s as nvarchar(4000); set @s =N'' <Xmltables> <Xmltb Name="6" Nowtime="1900-2-1">0</Xmltable> <Xmlta Name="11" Nowtime="1900-2-1">0</Xmlta> </Xmltables> ''; declare @idHandle as int ; EXEC sp_xml_preparedocument @idHandle OUTPUT, @s insert into Xmlta(Name,Nowtime) select * from openxml(@idHandle,N''/Xmltables/Xmlta'') with dbo.xmlta EXEC sp_xml_removedocument @idHandle select * from Xmlta drop table Xmlta -----------------------同時讀入多表數據---------------- create table Xmlta(Name nvarchar(20),Nowtime datetime) create table Xmltb(Name nvarchar(20),Nowtime datetime) declare @s as nvarchar(4000); set @s =N'' <Xmltables> <Xmlta Name="1" Nowtime="1900-2-1">0</Xmlta> <Xmltb Name="2" Nowtime="1900-2-1">0</Xmltb> </Xmltables> ''; --<Xmlta ></Xmlta> 則插入的數據為null declare @idHandle as int ; EXEC sp_xml_preparedocument @idHandle OUTPUT, @s --表a insert into Xmlta(Name,Nowtime) select * from openxml(@idHandle,N''/Xmltables/Xmlta'') with dbo.Xmlta --表b insert into Xmltb(Name,Nowtime) select * from openxml(@idHandle,N''/Xmltables/Xmltb'') with dbo.Xmltb EXEC sp_xml_removedocument @idHandle select * from Xmlta select * from Xmltb drop table Xmlta,Xmltb --生成xml文件單表 DECLARE @xVar XML SET @xVar = (SELECT * FROM Xmltable FOR XML AUTO,TYPE) select @xVar