Solved

Drop Login from all databases that has the login name

Posted on 2012-03-22
4
365 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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…
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…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

730 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