drop user in Multiple database in sql sever

hi
 is it a simple way to write a script dropping a user whom might in multiple database in sql server
thanks
tristonyipAsked:
Who is Participating?
 
sventhanCommented:
--drop user from all databases
EXEC sp_msforeachdb N'
      USE [?];

      DECLARE @user_name sysname;

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

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

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

http://social.msdn.microsoft.com/Forums/en/transactsql/thread/53503bef-0c12-47e9-be50-6526a1159e90
0
 
tristonyipAuthor Commented:
thanks, this is perfect
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.