Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Loop through every SQL Server database on server, create user if they don't exist, and set that user's permissions
exec master.dbo.sp_foreachdb
'USE ?
IF NOT EXISTS (SELECT 1 FROM sys.database_principals WHERE name = ''Domain\UserOrGroup'')
BEGIN
CREATE USER [Domain\UserOrGroup] FROM LOGIN [Domain\UserOrGroup]
END
exec sp_addrolemember ''db_backupoperator'', ''IQMETRIXHO\Development'';
'
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment