• 基础平台数据导入(游标的使用)



    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
    --用户
    insert into [SysUser]
    (UserGuid,UserCode,UserID,AreaCode,UserName,UserSex,UserIsUse,UserPass)
    select Replace(newid(),'-',''),StaffNo,StaffNo,'001',StaffName,case sex when '0' then '' else '' end ,'0','123'
     
    from basStaff where userID not like '%admin%'

    --机构
    insert into tbl_Dept
    (DeptGuid,DeptCode,DeptName,IsUse,lasttime)
     
    select Replace(newid(),'-',''),GroupNo,GroupName,1,getdate() from basGroups

    --机构2位改成3位
    --
    ==========================================================
    --
    开始事务
    BEGIN TRAN

    --不显示计数信息
    SET NOCOUNT ON

    DECLARE @tempDeptCode varchar(512)

    --声明游标
    DECLARE CRMPSContact_cursor CURSOR FOR 
    select [DeptCode] from dbo.tbl_Dept

    --打开游标
    OPEN CRMPSContact_cursor

    --取第一行的值给变量
    FETCH NEXT FROM CRMPSContact_cursor 
    INTO @tempDeptCode

    --执行语句
    declare @current int
    declare @length int
    declare @source varchar(50)
    declare @temp varchar(50)
    declare @dest varchar(100)

    set @source = @tempDeptCode
    set @length = len(@source)
    set @dest = ''
    set @current = 1
    print @source
    while @current < @length
    begin
        
    set @temp = substring(@source@current2)
        
    set @dest = @dest +'0' + @temp
        
    set @current = @current + 2
    end
    print @dest
    update tbl_Dept Set DeptCode=@dest where DeptCode=@tempDeptCode

    --执行错误回滚
    if @@error!=0
    begin
    rollback tran
    return
    end

    --移动游标,其它所有行更新操作(当到结尾时退出)
    WHILE @@FETCH_STATUS = 0
    BEGIN 
    --游标移到下一行
    FETCH NEXT FROM 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@current2)
        
    set @dest = @dest +'0' + @temp
        
    set @current = @current + 2
    end
    print @dest
    update tbl_Dept Set DeptCode=@dest where DeptCode=@tempDeptCode



    --执行错误回滚
    if @@error!=0
    begin
    rollback tran
    return
    end

    END

    --关闭游标
    CLOSE CRMPSContact_cursor

    --释放游标
    DEALLOCATE CRMPSContact_cursor

    --提交所有变更
    COMMIT TRAN

    --恢复设置
    SET NOCOUNT OFF
    GO 
    --==========================================================


    --
    insert into 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
    if exists (select id from dbo.sysobjects where id = object_id(N'[dbo].[ConvertCode]'and xtype in (N'FN', N'IF', N'TF'))
    drop function [dbo].[ConvertCode]
    GO

    CREATE FUNCTION [dbo].[ConvertCode] (@Source VARCHAR (50))
      
    RETURNS VARCHAR (100AS
    BEGIN
        
    declare @current int,
            
    @length int,
            
    @temp varchar(50),
            
    @dest varchar(100)

        
    set @length = len(@Source)
        
    set @dest = ''
        
    set @current = 1
        
    while @current < @length
        
    begin
            
    set @temp = substring(@Source@current2)
            
    set @dest = @dest +'0' + @temp
            
    set @current = @current + 2
        
    end
        
    return @dest
    END
    GO

    declare @source varchar(50)
    set @source = '12345678'
    select dbo.ConvertCode(@source)
    GO

    --要有好的心情(270148119) 16:03:22
    --
    函数体可以根据你的具体要求在修改

  • 相关阅读:
    【leetcode】71. Simplify Path
    【leetcode】891. Sum of Subsequence Widths
    【leetcode】68. Text Justification
    【leetcode】84. Largest Rectangle in Histogram
    【leetcode】726. Number of Atoms
    【leetcode】429. N-ary Tree Level Order Traversal
    【leetcode】436. Find Right Interval
    【leetcode】778. Swim in Rising Water
    BEC listen and translation exercise 9
    BEC listen and translation exercise 8
  • 原文地址:https://www.cnblogs.com/gfwei/p/1060105.html
Copyright © 2020-2023  润新知