sql,scope_identity,procedure,tran,substring,cast,convert,charindex,插入角色的同时插入角色拥有的权限,权限使用权限列表表示,列表用逗号分隔权限的id,更新角色信息,同时更新权限信息
2008-07-01 12:50 Virus-BeautyCode 阅读(579) 评论(0) 编辑 收藏 举报/*
插入角色的同时插入角色拥有的权限,权限使用权限列表表示,列表用逗号分隔权限的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
/*插入角色的同时插入角色拥有的权限,权限使用权限列表表示,列表用逗号分隔权限的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
@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