Showing posts with label SQL 2012. Show all posts
Showing posts with label SQL 2012. Show all posts
Friday, March 1, 2013
Generate scripts to create DB Users and their membership from existing DB
-- Create script to create the DB Users
SELECT 'CREATE USER [' + name + '] for login [' + name + ']' + char(13) + 'GO'
from sys.database_principals
where Type = 'S' AND default_schema_name='dbo'
-- Create script to create the Users' membership
SELECT 'EXECUTE sp_AddRoleMember ''' + roles.name + ''', ''' + users.name + '''' + char(13) + 'GO'
from sys.database_principals users
inner join sys.database_role_members link
on link.member_principal_id = users.principal_id
inner join sys.database_principals roles
on roles.principal_id = link.role_principal_id
WHERE users.Type = 'S' AND users.default_schema_name='dbo'
Generate scripts to create DB role and its securables from existing DB
declare @RoleName varchar(50) = 'RoleName'
-- Create script to create DB Role
select 'CREATE ROLE [' + @RoleName + ']' + char(13) + 'GO'
-- Create script to grant pemission of securable of the DB Role
select 'GRANT ' + prm.permission_name + ' ON [' + OBJECT_NAME(major_id) + '] TO [' + rol.name + ']' + char(13) COLLATE Latin1_General_CI_AS + 'GO'
from sys.database_permissions prm
join sys.database_principals rol on
prm.grantee_principal_id = rol.principal_id
where rol.name = @RoleName
-- Create script to create DB Role
select 'CREATE ROLE [' + @RoleName + ']' + char(13) + 'GO'
-- Create script to grant pemission of securable of the DB Role
select 'GRANT ' + prm.permission_name + ' ON [' + OBJECT_NAME(major_id) + '] TO [' + rol.name + ']' + char(13) COLLATE Latin1_General_CI_AS + 'GO'
from sys.database_permissions prm
join sys.database_principals rol on
prm.grantee_principal_id = rol.principal_id
where rol.name = @RoleName
Subscribe to:
Posts (Atom)