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'
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment