drop user in Multiple database in sql sever

Posted on 2011-10-04
Last Modified: 2012-05-12
 is it a simple way to write a script dropping a user whom might in multiple database in sql server
Question by:tristonyip
    LVL 18

    Accepted Solution

    --drop user from all databases
    EXEC sp_msforeachdb N'
          USE [?];

          DECLARE @user_name sysname;

          SELECT @user_name =
          FROM sys.server_principals AS sp
          JOIN sys.database_principals AS dp ON
                dp.sid = sp.sid
          WHERE = ''SomeLogin'';

          IF @user_name IS NOT NULL
                SET @user_name = QUOTENAME(@user_name);
                RAISERROR(''Dropping server=%s, database=%s, user=%s'', 0, 1, @@SERVERNAME, ''?'', @user_name);
                EXEC(''DROP USER '' + @user_name);

    --drop login from server
    DROP LOGIN [SomeLogin];

    Author Closing Comment

    thanks, this is perfect

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
    Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
    Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
    Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now