select*from[ESFM_PlatForm2].[dbo].[SysUser]-- where UserName not like '%管理%' select*from dbo.basStaff select*from dbo.tbl_Dept select*from dbo.basGroups select*from dbo.tbl_Group select*from dbo.tbl_Group_FromXH --用户 insertinto[SysUser] (UserGuid,UserCode,UserID,AreaCode,UserName,UserSex,UserIsUse,UserPass) selectReplace(newid(),'-',''),StaffNo,StaffNo,'001',StaffName,case sex when'0'then'女'else'男'end ,'0','123' from basStaff where userID notlike'%admin%' --机构 insertinto tbl_Dept (DeptGuid,DeptCode,DeptName,IsUse,lasttime) selectReplace(newid(),'-',''),GroupNo,GroupName,1,getdate() from basGroups --机构2位改成3位 --========================================================== --开始事务 BEGINTRAN --不显示计数信息 SET NOCOUNT ON DECLARE@tempDeptCodevarchar(512) --声明游标 DECLARE CRMPSContact_cursor CURSORFOR select[DeptCode]from dbo.tbl_Dept --打开游标 OPEN CRMPSContact_cursor --取第一行的值给变量 FETCHNEXTFROM CRMPSContact_cursor INTO@tempDeptCode --执行语句 declare@currentint declare@lengthint declare@sourcevarchar(50) declare@tempvarchar(50) declare@destvarchar(100) set@source=@tempDeptCode set@length=len(@source) set@dest='' set@current=1 print@source while@current<@length begin set@temp=substring(@source, @current, 2) set@dest=@dest+'0'+@temp set@current=@current+2 end print@dest update tbl_Dept Set DeptCode=@destwhere DeptCode=@tempDeptCode --执行错误回滚 if@@error!=0 begin rollbacktran return end --移动游标,其它所有行更新操作(当到结尾时退出) WHILE@@FETCH_STATUS=0 BEGIN --游标移到下一行 FETCHNEXTFROM CRMPSContact_cursor INTO@tempDeptCode --执行语句,从第二行开始 set@source=@tempDeptCode set@length=len(@source) set@dest='' set@current=1 print@source while@current<@length begin set@temp=substring(@source, @current, 2) set@dest=@dest+'0'+@temp set@current=@current+2 end print@dest update tbl_Dept Set DeptCode=@destwhere DeptCode=@tempDeptCode --执行错误回滚 if@@error!=0 begin rollbacktran return end END --关闭游标 CLOSE CRMPSContact_cursor --释放游标 DEALLOCATE CRMPSContact_cursor --提交所有变更 COMMITTRAN --恢复设置 SET NOCOUNT OFF GO --========================================================== --组 insertinto tbl_Group (GroupNo,GroupName,Memo,CreatorID,LastTime,IsPublic,Guid) select GroupNo,GroupName,Memo,CreatorID,Getdate(),1,Replace(newid(),'-','') from tbl_Group_FromXH
用函数也应该可以实现二位分级到三位分级的转换
下面是网上位朋友的代码,有空再调试一下
--要有好的心情(270148119) 16:03:02 ifexists (select id from dbo.sysobjects where id =object_id(N'[dbo].[ConvertCode]') and xtype in (N'FN', N'IF', N'TF')) dropfunction[dbo].[ConvertCode] GO CREATEFUNCTION[dbo].[ConvertCode] (@SourceVARCHAR (50)) RETURNSVARCHAR (100) AS BEGIN declare@currentint, @lengthint, @tempvarchar(50), @destvarchar(100) set@length=len(@Source) set@dest='' set@current=1 while@current<@length begin set@temp=substring(@Source, @current, 2) set@dest=@dest+'0'+@temp set@current=@current+2 end return@dest END GO declare@sourcevarchar(50) set@source='12345678' select dbo.ConvertCode(@source) GO --要有好的心情(270148119) 16:03:22 --函数体可以根据你的具体要求在修改