由於數據庫對xml數據直接處理有很多優勢,05也對這方面加強了功能。
但這方面資料少,所以自己做了一些總結,希望會給大家帶來幫助
--charry0110(曉風殘月)
--用SQL多條可以將多條數據組成一棵XML樹L一次插入
--將XML樹作為varchar參數傳入用
--insert xx select xxx from openxml() 的語法插入數據
-----------------------------------導入,導出xml--------------------------
--1導入實例
--單個表
--charry0110(曉風殘月)
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