Solved

Drop Login from all databases that has the login name

Posted on 2012-03-22
4
356 Views
Last Modified: 2012-03-24
I need a generic script that drops a particular user from all the databases(which consists the user). I tried drop login username, but it does not delete from databases, it just deletes the login entry from the MAIN SECURITY folder of the SQL Instance.

If I have a login names 'LOGINABC', which has access to 15 out of 15databases in a instance.

When I execute the generic script, it should drop the login entry from main SECURITY>LOGINS under the SQL Instance and also from the 15 Databases (which will be under the Database>Security>Users).
0
Comment
Question by:w3brang3r
  • 2
4 Comments
 
LVL 16

Expert Comment

by:DcpKing
ID: 37755704
Try wrapping this line in a stored procedure:
EXECUTE sp_msforeachdb 'USE ? DROP LOGIN login_name'
You might try wrapping the code within the ticks in a try/catch block, and so print a message that it succeeded or failed in dropping each login (use the question mark instead of a variable holding the name of the database).
0
 

Author Comment

by:w3brang3r
ID: 37756582
Need a ready-made solution please.

Note: while manual operation, the main login is dropped using DROP LOGIN LOGINABC, but coming to each an every DB, it is DROP USER LOGINABC.

Hope you got the difference....
0
 
LVL 16

Expert Comment

by:DcpKing
ID: 37757140
I didn't think that this was that difficult to write!

create procedure dbo.dropusers
( @strusername varchar(32) )
as
Begin
      EXECUTE sp_msforeachdb 'USE ? DROP USER login_name'
End

BTW, handle this with care: I haven't tested it (I can't, really, in work!!!)
0
 
LVL 6

Accepted Solution

by:
LCSandman8301 earned 300 total points
ID: 37758507
create procedure dbo.dropusers
( @strusername varchar(32) )
as
Begin
      declare @cmd nvarchar(4000)
      set @cmd = 'USE ? IF EXISTS (select 1 from sysusers where name = '''+@strusername+''') BEGIN DROP USER '+@STRUSERNAME+' END'
      EXECUTE sp_msforeachdb @cmd
      set @cmd = 'DROP LOGIN' + @strusername
      exec sp_executesql @cmd
End
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

760 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

24 Experts available now in Live!

Get 1:1 Help Now