遇到旧网站数据迁移到新网站,往往旧网站的数据结构和新网站不一样,可以在旧网站的数据库中通过查询语句拼接的方式,生成导入数据的脚本
1.先查询旧网站中需要导入的数据
SELECT * FROM t_custom
2.在查询脚本上加工,编写拼接脚本
SELECT 'INSERT INTO Whir_Mem_Member (' + 'LoginName, LoginTimeNow, JZOrNot, RealName, Password, OldWebPassword, ' + 'Sex, IDNumber, MedicalCard, MedicalOtherCard, AccountState, Mobile, Email,' + 'UpdateDate, UpdateUser, CreateDate, CreateUser,' + 'SubjectID, TypeID, Sort, IsDel) ' +'VALUES (' + ''''+ custom_name +''',''2014-04-24 16:25'',1,'''+person_name+''','''','''+pwd+''',' + ''''+ (case sex when 1 then '男' else '女' end) +''','''+person_id+''','''+ isnull(cardId,'')+''','''+isnull(curenumber,'')+''','+convert(varchar,50,IsValid)+','''+isnull(Phone,'')+''','''+isnull(Mail,'')+''',' + '''2014-04-24 16:25'',''root'',''2014-04-24 16:25'',''root'',' + '0,1,'+convert(varchar,50,id)+',0' +');' AS sqlStr FROM t_custom
3.声明游标,输出导入的数据脚本
declare @sqlStr nvarchar(2048) declare sqlCur cursor --定义游标 FOR SELECT 'INSERT INTO NewWebSite_db.dbo.Whir_Mem_Member (' + 'LoginName, LoginTimeNow, JZOrNot, RealName, Password, OldWebPassword, ' + 'Sex, IDNumber, MedicalCard, MedicalOtherCard, AccountState, Mobile, Email,' + 'UpdateDate, UpdateUser, CreateDate, CreateUser,' + 'SubjectID, TypeID, Sort, IsDel) ' +'VALUES (' + ''''+ custom_name +''',''2014-04-24 16:25'',1,'''+person_name+''','''','''+pwd+''',' + ''''+ (case sex when 1 then '男' else '女' end) +''','''+person_id+''','''+ isnull(cardId,'')+''','''+isnull(curenumber,'')+''','+convert(varchar,50,IsValid)+','''+isnull(Phone,'')+''','''+isnull(Mail,'')+''',' + '''2014-04-24 16:25'',''root'',''2014-04-24 16:25'',''root'',' + '0,1,'+convert(varchar,50,id)+',0' +');' AS sqlStr FROM [t_custom] open sqlCur --开启游标 fetch next from sqlCur INTO @sqlStr print('--开始输出') while @@fetch_status=0 begin print(@sqlStr) --EXEC(@sqlStr) fetch next from sqlCur into @sqlStr END print('--输出结束') close sqlCur deallocate sqlCur
4.将输入的脚本在新数据库中执行即可(为防止意外事故发生,请事先备份好数据库)