生成数据库角色权限
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
推荐阅读
-
powerdesigner15创建数据库生成脚本
-
sqlserver数据库主键的生成方式小结(sqlserver,mysql)
-
PowerDesigner 建立与SQLSERVER 2005数据库的连接以便生成数据库和从数据库生成到PD中
-
PowerDesigner 建立与数据库的连接以便生成数据库和从数据库生成到PD中(Oracle 10G版)
-
C# T4 模板 数据库实体类生成模板(带注释,娱乐用)
-
Orcale权限、角色查看创建方法
-
如何使用Visual Studio 2010在数据库中生成随机测试数据
-
Mysql数据库如何开启远程访问权限?
-
PowerDesigner生成数据库时的列中文注释乱码问题的设置方法
-
PowerDesigner 建立与SQLSERVER 2005数据库的连接以便生成数据库和从数据库生成到PD中