日期:2014-05-18  浏览次数:20501 次

在SQL2000中把XML文件导出到表中
我有个XML文件
<?xml   version= "1.0 "   encoding= "UTF-8 "   ?>  
-   <v1:storage-down   xmlns:v1= "&quot;http://xml.e/doc/v1&quot; ">
    <LIB   Storage= "storage "   ConnectCode= "0002 "   CtrlDvsnCD= "00027495 "   ProcessFlag= "1 "   SequenceNo= "20070225000000000001 "   CompanyCode= "77946311 "   BranchCode= "0400 "   BranchName= " "   ShopCode= "0400 "   ShopGCode= "77802992 "   ShopName= "深圳总仓 "   ShopProvince= "广东省 "   ShopCity= "深圳 "   GoodsCode= "100386 "   GoodsName= "ES4823A/S   松下电动须刀 "   GoodsType= "ES4823A/S "   SupplyCode= "6022 "   GoodsGCode= "ES4823S405 "   ClassCode= "D03 "   ClassName= "其它白色小家电类 "   Qty= "40 "   StatQty= "41 "   UnitName= "台 "   StorageType= "N "   StatTime= "2007-02-24   00:00:00 "   CreatedTime= "2007-02-25   01:27:59 "   SendTime= "2007-02-25   01:27:59 "   TestID= "0 "   />  
    <LIB   Storage= "storage "   ConnectCode= "0002 "   CtrlDvsnCD= "00027495 "   ProcessFlag= "1 "   SequenceNo= "20070225000000000002 "   CompanyCode= "77946311 "   BranchCode= "0400 "   BranchName= " "   ShopCode= "0400 "   ShopGCode= "77802992 "   ShopName= "深圳总仓 "   ShopProvince= "广东省 "   ShopCity= "深圳 "   GoodsCode= "100387 "   GoodsName= "ES5821S松下电动须刀 "   GoodsType= "ES5821S "   SupplyCode= "6022 "   GoodsGCode= "ES5821S405 "   ClassCode= "D03 "   ClassName= "其它白色小家电类 "   Qty= "59 "   StatQty= "60 "   UnitName= "台 "   StorageType= "N "   StatTime= "2007-02-24   00:00:00 "   CreatedTime= "2007-02-25   01:27:59 "   SendTime= "2007-02-25   01:27:59 "   TestID= "0 "   />  
后面还有许多数据

问一下在SQL2000中能不能把XML文件的数据导入到一张表当中么?如果能的话,怎么导入呢?能不能给个例子呢。。谢谢大家了。。。

------解决方案--------------------
if exists(select * from master..sysobjects where id=object_id( 'xmltb '))
drop table xmltb
if exists(select * from master..sysobjects where id=object_id( 'xml_tmp '))
drop table xml_tmp
go
use master
go
create table master..xmltb(xmlstr varchar(8000))
-- 创建一个零时数据表
BULK INSERT master.dbo.[xmltb]
--把你XML创建到 c盘下 MYXML.XML 文件
FROM 'c:\myxml.xml '
WITH ( ROWTERMINATOR = '\n ' )
--或者 exec master..xp_cmdshell 'bcp master..xmltb in c:\myxml.xml -c '
exec ( 'alter table xmltb ADD I int identity(1,1) ')

go

declare @i int,@sql varchar(8000),@col varchar(8000),@value varchar(8000),@str varchar(8000)
select @i=0,@sql= ' ',@col= ' ',@value= ' ',@str= ' '

while @i!=-1
begin
select @i=@i+1,@col= ' ',@value= ' ',@str= ' ',@sql= ' '
select @sql=xmlstr from xmltb where i=@i
select @sql=stuff(@sql,1,2, ' ')
if (substring(ltrim(@sql),1,4)= ' <LIB ')
begin
while len(@sql)> 0
begin