27,582
社区成员
发帖
与我相关
我的任务
分享
--哦,最后不要忘了
EXEC sp_xml_removedocument @idoc
DECLARE @idoc int;
DECLARE @doc varchar(4000);
SET @doc ='<?xml version="1.0" encoding="GB2312" ?> <FLIGHT>
<DATETIME> 2007-11-22 09:27:04 </DATETIME>
<METHOD> MOD </METHOD>
<TYPE> A </TYPE>
<FLIGHTINFOR>
<FLIGHTATTR> 21 </FLIGHTATTR>
<FLIGHTTYPE> 0 </FLIGHTTYPE>
<AIRCRAFTTYPE> E145 </AIRCRAFTTYPE>
<REGISTRATION> 3062 </REGISTRATION>
<FLIGHTNUMBER> CZ3321 </FLIGHTNUMBER>
<PRIMARYFLT />
<RELATEDFLT />
<FLIGHTSTATUS> COM </FLIGHTSTATUS>
<PORTNO />
<FLIGHTDATETIME>
<PLANDATE> 2007-11-22 </PLANDATE>
<ADT />
<EDT> 2007-11-22 10:33:00 </EDT>
<SDT> 2007-11-22 09:30:00 </SDT>
</FLIGHTDATETIME>
<STATION>
<ORIGIN> CAN </ORIGIN>
<DESTINATION> YIH </DESTINATION>
<VIA />
</STATION>
</FLIGHTINFOR>
<DEP>
<CHECKIN>
<COUNTEROPENDT />
<COUNTER />
</CHECKIN>
<DEPTERMINAL />
<GATE />
</DEP>
<ARR>
<BELT> 1 </BELT>
</ARR>
</FLIGHT>
'
create table temp (datetime datetime,method varchar(20),type char(1))
EXEC sp_xml_preparedocument @Idoc OUTPUT, @doc
insert temp
SELECT * FROM OPENXML (@Idoc, '/FLIGHT',2)
WITH (DATETIME DATETIME ,METHOD varchar(20),TYPE char(1))
select * from temp
/*
datetime method type
------------------------------------------------------ -------------------- ----
2007-11-22 09:27:04.000 MOD A
*/
可以将多条数据组成一棵XML树
将XML树作为varchar参数传入
用insert xx select xxx from openxml() 的语法插入数据
一个小例子:*/
create table xmltable(name nvarchar(20),Intro nvarchar(20))
declare @ssss as nvarchar(2000) ;
set @ssss = N'<root>
<dbo.xmltable name="XML中的" Intro="XML中的"></dbo.xmltable>
<dbo.xmltable name="XML中的" Intro="XML中的"></dbo.xmltable>
<dbo.xmltable name="XML中的" Intro="XML中的"></dbo.xmltable>
</root>' ;
declare @idHandle as int ;
EXEC sp_xml_preparedocument @idHandle OUTPUT, @ssss
insert into xmltable(Name,Intro)
select * from openxml(@idHandle,N'/root/dbo.xmltable')
with dbo.xmltable
EXEC sp_xml_removedocument @idHandle