zoukankan      html  css  js  c++  java
  • sql,scope_identity,procedure,tran,substring,cast,convert,charindex,插入角色的同时插入角色拥有的权限,权限使用权限列表表示,列表用逗号分隔权限的id,更新角色信息,同时更新权限信息 Virus


    /*
    插入角色的同时插入角色拥有的权限,权限使用权限列表表示,列表用逗号分隔权限的id
    */

    create procedure AddRole
    @RoleName varchar(50),
    @Remark  varchar(200),
    @AuthorizeList varchar(8000),
    @lastID int output
    as
    begin tran
      
    insert into dbo.RoleInfo(RoleName,Remark) values(@RoleName,@Remark)
      
    select @lastID=scope_identity()
      
    declare @i int
      
    set   @AuthorizeList=@AuthorizeList+','  
      
    set   @i=charindex(',',@AuthorizeList)  
      
    while   @i>0  
      
    begin 
        
    insert into RoleAuthorizeInfo values(cast(@lastID as int),convert(int,left(@AuthorizeList,@i-1)))
        
    set   @AuthorizeList=substring(@AuthorizeList,@i+1,8000)  
        
    set   @i=charindex(',',@AuthorizeList)       
      
    end
      
    if @@error<>0
        
    begin
          
    rollback tran
        
    end
      
    else
        
    begin
          
    commit tran
        
    end
    go


    declare @id int
    exec AddRole 'sdfsdf','sfsdfsf','111,222',@id output
    select @id
    go
    /*
    更新角色信息,同时更新权限信息
    */
    create procedure UpdateRoleAuth
    @RoleID int,
    @RoleName varchar(50),
    @Remark varchar(200),
    @AuthList varchar(8000)
    as
    begin tran
      
    update RoleInfo set RoleName=@RoleID,Remark=@Remark where RoleInfoAutoID=@RoleID
    delete from RoleAuthorizeInfo where RoleInfoAutoID=@RoleID
       
    declare @i int
      
    set   @AuthList=@AuthList+','  
      
    set   @i=charindex(',',@AuthList)  
      
    while   @i>0  
      
    begin
        
        
    insert into RoleAuthorizeInfo values(@RoleID,convert(int,left(@AuthList,@i-1)))
        
    set   @AuthList=substring(@AuthList,@i+1,8000)  
        
    set   @i=charindex(',',@AuthList)       
      
    end
      
    if @@error<>0
        
    begin
          
    rollback tran
        
    end
      
    else
        
    begin
          
    commit tran
        
    end
    go

    【Blog】http://virusswb.cnblogs.com/

    【MSN】jorden008@hotmail.com

    【说明】转载请标明出处,谢谢

    反馈文章质量,你可以通过快速通道评论:

  • 相关阅读:
    使用 ES2015 编写 Gulp 构建
    ES6 Promise 接口
    Git 文件比较
    JavaScript 属性描述符
    Vim 插件之 NERDTree
    Raspberry Pi 3 Model B 安装 OSMC
    How ADB works
    [Linux] zip 与 unzip 命令
    在 Ubuntu 配置 PPTP Server
    [Linux] 查看系统启动时间
  • 原文地址:https://www.cnblogs.com/virusswb/p/1233098.html
Copyright © 2011-2022 走看看