Sql 数据导出到XMl 与 从XML导入到数据库

1、导出到XMl select * from Brand  for xml auto ,root('Brands')

<Brands>
  <Brand BrandID="E584596D-4D66-4F2F-B6F7-71C3BEB4CA21" Name="inganico" />
  <Brand BrandID="19B04451-DDC4-4CDF-BE30-CB4E703B27DA" Name="安付达" />
  <Brand BrandID="3C6C8E12-7C4A-4F19-B491-4C0A64A48303" Name="安智" />
  <Brand BrandID="BF6C361A-8993-4660-A89D-EB32CCC9CE49" Name="百富" />
  <Brand BrandID="8E7FE420-3AE3-4017-80AB-B53CA29C80CA" Name="海博通" />
  <Brand BrandID="505C5565-08C5-4EF5-9316-55CA76C1E9F3" Name="惠尔丰" />
  <Brand BrandID="E5BA2A72-B1D1-457A-9AFD-A9D9B336E7C0" Name="利普门" />
  <Brand BrandID="1982A195-5263-45CC-B872-96F3C145FCCD" Name="联迪" />
  <Brand BrandID="E460CDA6-4A83-4C62-B049-3B980516AD79" Name="瑞柏" />
  <Brand BrandID="06BACF99-BB7E-447C-B021-CD8C3FFAE85A" Name="萨基姆" />
  <Brand BrandID="165510D9-342D-4402-882D-0A00DBFDAAE3" Name="新大陆" />
  <Brand BrandID="2FA88654-2311-4C51-85B1-A39077C34C6D" Name="新国都" />
</Brands>

2、将上边XML内容导回 数据库

CreatePROCEDURE [dbo].[P_BrandSynchronization]
@xmlDocument NVARCHAR (MAX)
AS
BEGIN
 
 SET NOCOUNT ON;
 DECLARE @docHandle int
 set @xmlDocument='<Brands>'+@xmlDocument+'</Brands>'
 EXEC sp_xml_preparedocument @docHandle OUTPUT, @xmlDocument
 declare   @BrandID   uniqueidentifier
 declare   @Name   varchar(16)  ,  @message varchar(500)
 
 
 declare mycursor cursor for (SELECT   *  FROM   OPENXML   (@docHandle,'/Brands/Brand',1)  
 WITH  (
    BrandID   uniqueidentifier,
    Name   varchar(16)
    ))
 open mycursor
 FETCH NEXT FROM mycursor INTO @BrandID,@Name
 while(@@FETCH_STATUS = 0) 
  BEGIN
 
  if not exists(select  1 from  brand where brandId =@BrandID)  
   begin  
     insert   into   Brand(BrandID,Name)   values(@BrandID,@Name)  
   end 
  else
   update Brand set BrandID=@BrandID,Name=@Name where BrandID=@BrandID
  FETCH NEXT FROM mycursor INTO @BrandID,@Name
  END 
  --select @message      
 close mycursor
 DEALLOCATE mycursor
 end

阅读更多
个人分类: DataBase
想对作者说点什么? 我来说一句

XML文件数据导入到数据库

2013年03月21日 75KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭