Migration data on SQL

从表里面导出数据XML:

-- export
declare @xml xml 
set @xml = (select * from ( select TableName = 'Schema', xmlData = ( select * from [Schema] where id = 337 for xml auto, root('Schemas') ) union select 'SchemaFile', ( select Id, SchemaId, Data = convert(varchar(max), Data), FileName, Fullpath, RelativePath from SchemaFile where SchemaId = 337 for xml auto, root('SchemaFiles') ) ) SchemaExport for xml auto, root('SchemaExports') ) select @xml

将导出XML数据插入到目的表中:

 1 -- now import
 2 
 3 declare @idoc int
 4 
 5       declare @tableName varchar(255)  
 6       declare @xmlData xml                
 7       
 8       declare @mappingExport table (TableName varchar(255), xmlData xml)
 9 
10       exec sp_xml_preparedocument @idoc output, @xml
11       insert into @mappingExport
12       select *
13       from openxml (@idoc, '/SchemaExports/SchemaExport')
14       with (TableName varchar(255),
15               xmlData nvarchar(max))
16       exec sp_xml_removedocument @idoc output 
17       
18       declare exportCursor cursor
19       for select TableName, xmlData 
20       from @mappingExport
21 
22       open exportCursor
23 
24       fetch from exportCursor into @tableName, @xmlData
25 
26       while @@fetch_status <> -1
27       begin
28             exec sp_xml_preparedocument @idoc output, @xmlData
29                   
30             if (@tableName = 'Schema')
31             begin
32                   print 'Copying Schema' 
33 
34                   select *
35                   from openxml (@idoc, '/Schemas/Schema')
36                   with (Id int,
37                         TenantId int,
38                         VersionNo int,
39                         ResponseSchemaId int,
40                         SchemaName nvarchar(200),
41                         SOAPActino varchar(200),
42                         LastUpdatedUTC datetime,
43                         IsCanonical bit,
44                         DocumentType nvarchar(512) )
45             end
46             else if (@tableName = 'SchemaFile')
47             begin
48                   print 'Copying SchemaFile'
49                   select *, datalength(data), convert(xml, data)
50                   from openxml (@idoc, '/SchemaFiles/SchemaFile')
51                   with (Id int,
52                         SchemaId int,
53                         Data varchar(max),
54                         FileName nvarchar(255),
55                         FullPath nvarchar(255),
56                         RelativePath nvarchar(255) )
57             end
58             exec sp_xml_removedocument @idoc 
59             fetch next from exportCursor into @tableName, @xmlData            
60       end   
61       
62 select datalength(data), convert(xml, data)
63 from schemafile
64 where schemaid = 337

 

转载于:https://www.cnblogs.com/Joyce-Wagner/p/4516644.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值