Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

execute sp_droprolemember foreach db

Posted on 2014-12-14
2
Medium Priority
?
242 Views
Last Modified: 2014-12-30
Hello,

I search a script for execute sp_droprolemember on all databases.

Thanks
0
Comment
Question by:bibi92
2 Comments
 
LVL 25

Accepted Solution

by:
chaau earned 2000 total points
ID: 40499627
According to MSDN:
This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature. Use ALTER ROLE instead.
So, I recommend you use ALTER ROLE. To execute the script for each database use the following cursor:
DECLARE @DB_Name varchar(100)
DECLARE @Command nvarchar(200)

DECLARE database_cursor CURSOR FOR
SELECT name
FROM MASTER.sys.sysdatabases

OPEN database_cursor

FETCH NEXT FROM database_cursor INTO @DB_Name

WHILE @@FETCH_STATUS = 0
BEGIN
     SELECT @Command = 'use '+ @DB_Name +' ALTER ROLE Sales DROP MEMBER Barry'
     EXEC sp_executesql @Command

     FETCH NEXT FROM database_cursor INTO @DB_Name
END

CLOSE database_cursor
DEALLOCATE database_cursor 

Open in new window

0
 

Author Closing Comment

by:bibi92
ID: 40523343
Thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question