sql,scope_identity,procedure,tran,substring,cast,convert,charindex,插入角色的同时插入角色拥有的权限,权限使用权限列表表示,列表用逗号分隔权限的id,更新角色信息,同时更新权限信息
2008-07-01 12:50
585 查看
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
@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
相关文章推荐
- 使用逗号分隔含有ID的输入参数(sql 查询)
- SQL查询ID以逗号分隔,id对应的详细信息
- oracle 查看当前用户拥有的角色权限信息的命令
- ado.net 向sql中插入新数据的同时获取自增重的id值
- 批量(导入导出)迁移AD中的用户信息和密码到新环境中,同时保持用户在MOSS中的权限【addusers,ldifde,copypwd,UserInfo,tp_SystemID】
- SQL插入数据同时返回当前自增长ID
- Authid Current_User--存储过程中也能使用role角色权限
- SQL SERVER 插入数据获取自增ID,SCOPE_IDENTITY、IDENT_CURRENT 和 @@IDENTITY的比较
- MySQL使用GROUP_CONCAT()函数按ID分组将多条记录的某个字段以逗号或者指定分隔号隔开成一条记录
- sql中charindex和cast结合使用
- SQL中取当前记录的ID----->SCOPE_IDENTITY()
- 使用SQL如何把用逗号等字符隔开的字符串转换成列表
- 自己制作一个链表用来存储列表信息,并查找当前id信息,找上一条信息,下一条信息(信息浏览的时候方便使用)
- sqlserver中利用Tran_sql把逗号分隔的字符串拆成临时表
- [翻译]Scott Mitchell 的ASP.NET 2.0数据教程之四十九:使用SqlDataSource插入、更新以及删除数据
- [SQL] cast 与 convert 都在什么情况下使用
- horizon实现多个角色同时拥有一个菜单权限
- 请问一下,我要向一个表插入数据,根据ID,重复的更新,没有的插入,不使用COUNT后判断,能做到吗?
- 用户admin所属角色组的id是固定的,用一条sql列出admin所有的角色信息
- SQL: 返回刚插入记录的ID 及 output 更新记录时得到更新记录的ID值 .