从表里面导出数据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数据插入到目的表中:
-- now import declare @idoc int declare @tableName varchar(255)
declare @xmlData xml declare @mappingExport table (TableName varchar(255), xmlData xml) exec sp_xml_preparedocument @idoc output, @xml
insert into @mappingExport
select *
from openxml (@idoc, '/SchemaExports/SchemaExport')
with (TableName varchar(255),
xmlData nvarchar(max))
exec sp_xml_removedocument @idoc output declare exportCursor cursor
for select TableName, xmlData
from @mappingExport open exportCursor fetch from exportCursor into @tableName, @xmlData while @@fetch_status <> -1
begin
exec sp_xml_preparedocument @idoc output, @xmlData if (@tableName = 'Schema')
begin
print 'Copying Schema' select *
from openxml (@idoc, '/Schemas/Schema')
with (Id int,
TenantId int,
VersionNo int,
ResponseSchemaId int,
SchemaName nvarchar(200),
SOAPActino varchar(200),
LastUpdatedUTC datetime,
IsCanonical bit,
DocumentType nvarchar(512) )
end
else if (@tableName = 'SchemaFile')
begin
print 'Copying SchemaFile'
select *, datalength(data), convert(xml, data)
from openxml (@idoc, '/SchemaFiles/SchemaFile')
with (Id int,
SchemaId int,
Data varchar(max),
FileName nvarchar(255),
FullPath nvarchar(255),
RelativePath nvarchar(255) )
end
exec sp_xml_removedocument @idoc
fetch next from exportCursor into @tableName, @xmlData
end select datalength(data), convert(xml, data)
from schemafile
where schemaid = 337