首页 / 数据库 / SQLServer / SQL 2005对xml文件与xml数据的操作
SQL 2005对xml文件与xml数据的操作2010-06-16 csdn博客 陈海雨由于数据库对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