生成数据库角色权限
USE Public_Data GO /****** Object: StoredProcedure [dbo].[P_CopyUserPermission] Script Date: 01/19/2011 11:09:13 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[P_CopyUserPermission] (@UserName sysname, @ne
USE Public_Data
GO
/****** Object: StoredProcedure [dbo].[P_CopyUserPermission] Script Date: 01/19/2011 11:09:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create procedure [dbo].[P_CopyUserPermission]
(@UserName sysname,
@newusername sysname=null)
AS
set nocount on
BEGIN
if @newusername is null
set @newusername=@UserName
if (select object_id('tempdb..#tt')) is not null
drop table #tt
create table #tt
(owner sysname,
object sysname,
grantee sysname,
grantor sysname,
protecttype varchar(10),
actionname varchar(20),
columnname sysname
)
if (select object_id('tempdb..#t2')) is not null
drop table #t2
create table #t2
(sql varchar(max)
)
declare @db sysname
declare cu_ListUserPermission cursor for
select name from master..sysdatabases where name not like 'dbss%'
and status4260872
open cu_ListUserPermission
fetch next from cu_ListUserPermission into @db
while @@FETCH_STATUS=0
begin
begin try
insert #tt execute sp_helprotect @username = @UserName
insert #t2
select 'use '+@db
union all
select '
if not exists(select * from sysusers where name='''+@newusername+''')'
union all
select 'begin'
union all
select ' CREATE USER ['+@newusername+'] FOR LOGIN ['+@newusername+'] WITH DEFAULT_SCHEMA=[dbo]'
union all
select 'end '
union all
select
distinct rtrim(protecttype) + ' ' + actionname + '' +
case object when '.' then '' else ' on ' + '['+owner+'].['+object+']' +
case when columnname in('(All+New)','(All)','(New)','.') then '' else '('+columnname+')' end end
+' to ' + @newusername
from #tt
union all
SELECT 'EXEC sp_addrolemember ''' +Roles.Name+''','''+@newusername+''''
FROM sysusers Users, sysusers Roles, sysmembers Members
WHERE Roles.uid = Members.groupuid
AND Roles.issqlrole = 1
AND Users.uid = Members.memberuid
AND Users.name = @UserName
end try
begin catch
end catch
fetch next from cu_ListUserPermission into @db
end
close cu_ListUserPermission
deallocate cu_ListUserPermission
select * from #t2
END
上一篇: mysql 5.0存储过程学习
推荐阅读
-
采用easyui tree编写简单角色权限代码的方法
-
随机生成八位优惠码并保存至Mysql数据库
-
对python插入数据库和生成插入sql的示例讲解
-
Mybatis总结之如何自动生成数据库表结构
-
ThinkPHP的RBAC(基于角色权限控制)深入解析
-
Mysql数据库如何开启远程访问权限?
-
Python使用win32com模块实现数据库表结构自动生成word表格的方法
-
数据库写不进去IIS写入权限怎么设置
-
构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(24)-权限管理系统-将权限授权给角色
-
MSSQL2005在networkservice权限运行附加数据库报(Microsoft SQL Server,错误: 5120)