----把Person的数据迁移到Emploee
----在test1运行此脚本
insert into [test2].[dbo].Emploee
([ID],[EmploeeName],[Sex],[Department])
select ID,Name,Sex,null
from Person
----把Person的数据迁移到Emploee
----在test1运行此脚本
insert into [test2].[dbo].Emploee
([ID],[EmploeeName],[Sex],[Department])
select newid(),Name,Sex,null
from Person
create function getSex(@type nvarchar(20))
returns varchar(1)
begin
declare @returntype varchar(1)
if(@type='男')
set
if(@type='女')
set @returntype='0'
return @returntype
end
----把Person的数据迁移到Emploee
----在test1运行此脚本
insert into [test2].[dbo].Emploee
([ID],[EmploeeName],[Sex],[Department])
select newid(),Name,dbo.getSex(Sex),null
from Person
4.如果导入多个表存在主外键情况,先到主表,后导字表,如果数据迁移时主键可以直接迁移到新表则比较好办,如果不能,则要建立个临时字段记录原主键,以便和子表关联。
----建立临时列OldID保存ID
alter table [test2].[dbo].Emploee add OldID varchar(36) null;
----把Person的数据迁移到Emploee
----在test1运行此脚本
insert into [test2].[dbo].Emploee
([ID],[EmploeeName],[Sex],[Department],[OldID])
select newid(),Name,dbo.getSex(Sex),null,ID
from Person
----把PersonSchool的数据迁移到EmploeeSchool
----在test1运行此脚本
insert into [test2].[dbo].EmploeeSchool
(ID,EmploeeID,SchoolName,SchoolAddress)
select newid(),Emploee.ID,SchoolName,SchoolAddress
from PersonSchool ,[test2].[dbo].Emploee
where PersonSchool.PersonID=[test2].[dbo].Emploee.OldID
-----删除临时列OldID
alter table [test2].[dbo].Emploee drop column OldID;