SQL Server 使用OpenXML 导入xml格式的数据

declare @xmlText xml
declare @club varchar(50)
DECLARE @idoc int
  
set @xmlText = (select doc from openrowset(bulk 'D:\RF_Integration\Code\Import\RF.Integration.Import.BusinessLogic\bin\Debug\ModifiedPackageFiles\golf_b6dd63fb30f84a9b941a9cdcbbacb30f_201209041607[007-004].xml',single_blob) as t(doc))  

EXEC sp_xml_preparedocument @idoc OUTPUT,@xmlText
    
set @club='golf'   

insert into [dbo].Address_Temp([AddressType] ,[Country],[PostalCity] ,[PostalNumber],[Street],[CO_Address],[PersonID],[PersonNum],[PersonOptType]) 
            select [AddressType] ,[Country],[PostalCity] ,[PostalNumber],[Street],[CO_Address],[PersonID],[PersonalNumber] as [PersonNum],'Insert' as [PersonOptType]
            from openxml(@Idoc,'/persons/InsertRecord/Person/Addresses/Address',2)
            With(
                [AddressType] varchar(50),
                [Country] varchar(50),
                [PostalCity] varchar(50),
                [PostalNumber] varchar(50),
                [Street]  varchar(50),
                [CO_Address]  varchar(50),
                [PersonId] varchar(50) ..//..//PersonId',--(此处应该为单斜杠,应为blog编辑器写单斜杠有问题,所以将其写为双斜杠)
                [PersonalNumber] varchar(50) '..//..//@PersonalNumber'
                )

 

    上边的代码中, 数据库直接读取了xml文件,然后解析 xml 数据,将其保存到Address_Temp 表中,蓝色文字部门,'..//..//PersonId' 表示单签节点 上两级节点 字段  PersonId 的值,如果带上'@' ,表示属性值。
    具体的openxml 的用法,可以参看一下链接:http://www.cnblogs.com/wuming/archive/2010/01/18/1650755.html

http://technet.microsoft.com/zh-cn/ms175160.aspx

 

posted @ 2012-09-04 19:04  鸿鹄_A  阅读(3707)  评论(0编辑  收藏