SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Drop All Users in the Database


Drop All Users in the Database

Author
Message
Mike Levan
Mike Levan
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3333 Visits: 1893
Is there a way to drop all users in a particular database instead of deleting each.
matt stockham
matt stockham
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2580 Visits: 3178
If you mean instead of deleting each individually, then you have 2 choices -
1) write a script
2) use the Object Explorer Details pane in SSMS to list the users, highlight those you want to delete, right click and select delete

It really depends on whether this is a one-off removal or for some repeated use.

I can expand on (1) if that is the direction you want to take.
Steve Jones
Steve Jones
SSC Guru
SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)

Group: Administrators
Points: 144508 Visits: 19424
I might suspect that the services are not started then. You want to use SQL tools, not the Windows ones to work with SQL Server.

Moving to SQL 2005 Admin

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Steve Jones
Steve Jones
SSC Guru
SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)SSC Guru (144K reputation)

Group: Administrators
Points: 144508 Visits: 19424
This sounds like something that would be fun, reduce work load, reduce phone calls. Wouldn't you want to enjoy this and delete them one by one BigGrinw00t

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Lowell
Lowell
SSC Guru
SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)

Group: General Forum Members
Points: 70516 Visits: 40924
i just slapped this together.
i'm using sp_revokeaccess instead of sp_dropuser, because the users might be orphaned and not tied to a login;
this yanks them out the way i expected:

ALTER PROCEDURE sp_DeleteUsers(@DeleteThem INT = 0)
AS
BEGIN
  DECLARE
@username VARCHAR(64)
  
DECLARE c1 CURSOR FOR
    SELECT
name  
    
FROM sysusers
    
WHERE name NOT IN('dbo','guest','INFORMATION_SCHEMA','sys','public')
      AND
LEFT(name,3) <> 'db_'
  
OPEN c1
  
FETCH next FROM c1 INTO @username
  
WHILE @@fetch_status <> -1
  
BEGIN
      PRINT
'Dropping ' + @username
      
IF @DeleteThem <> 0
        
EXEC dbo.sp_revokedbaccess @username
    
FETCH next FROM c1 INTO @username
  
END
CLOSE
c1
DEALLOCATE c1
END





Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Michael Valentine Jones
Michael Valentine Jones
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14241 Visits: 11848
Lowell (3/16/2009)
i just slapped this together.
i'm using sp_revokeaccess instead of sp_dropuser, because the users might be orphaned and not tied to a login;
this yanks them out the way i expected:

ALTER PROCEDURE sp_DeleteUsers(@DeleteThem INT = 0)
AS
BEGIN
  DECLARE
@username VARCHAR(64)
  
DECLARE c1 CURSOR FOR
    SELECT
name  
    
FROM sysusers
    
WHERE name NOT IN('dbo','guest','INFORMATION_SCHEMA','sys','public')
      AND
LEFT(name,3) <> 'db_'
  
OPEN c1
  
FETCH next FROM c1 INTO @username
  
WHILE @@fetch_status <> -1
  
BEGIN
      PRINT
'Dropping ' + @username
      
IF @DeleteThem <> 0
        
EXEC dbo.sp_revokedbaccess @username
    
FETCH next FROM c1 INTO @username
  
END
CLOSE
c1
DEALLOCATE c1
END





Here is another way to do that:

declare @sql nvarchar(max)
set @sql = ''

SELECT @sql = @sql+
'
print ''Dropping '+name+'''
execute master.dbo.sp_revokedbaccess '''+name+'''
'
FROM
dbo.sysusers
WHERE
name NOT IN('dbo','guest','INFORMATION_SCHEMA','sys','public')
AND LEFT(name,3) <> 'db_'
order by
name

execute ( @sql )



DJSH
DJSH
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 160
Hi,

Thank you for posting the script.

But in my case when i execute the above script, it generate Drop statments for all users in given database along with the database role.
i.e it creates drop statment for user defined db role, which won't allow me to drop.

Is there any way to exclude user defined db role from users in the script?

DJSH
Lowell
Lowell
SSC Guru
SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)SSC Guru (70K reputation)

Group: General Forum Members
Points: 70516 Visits: 40924
can't you just add to the WHERE statement to exclude items you don't want to include? like AND issqlrole = 0 or something?

Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Perry Whittle
Perry Whittle
SSC Guru
SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)SSC Guru (53K reputation)

Group: General Forum Members
Points: 53081 Visits: 17672
also you want to be using the sys.database_principals catalog view and not sysusers. This should be better


declare @sql nvarchar(max)
set @sql = ''

SELECT @sql = @sql+
'
print ''Dropping '+name+'''
execute master.dbo.sp_revokedbaccess '''+name+'''
'
FROM
sys.database_principals
WHERE
name NOT IN('dbo','guest','INFORMATION_SCHEMA','sys','public')
AND TYPE <> 'R'
order by
name

execute (@sql)



-----------------------------------------------------------------------------------------------------------

"Ya can't make an omelette without breaking just a few eggs" ;-)
Fozzie
Fozzie
Right there with Babe
Right there with Babe (715 reputation)Right there with Babe (715 reputation)Right there with Babe (715 reputation)Right there with Babe (715 reputation)Right there with Babe (715 reputation)Right there with Babe (715 reputation)Right there with Babe (715 reputation)Right there with Babe (715 reputation)

Group: General Forum Members
Points: 715 Visits: 1172
Or this - doesn't drop users who own a schema


declare @sql nvarchar(max)
set @sql = ''

SELECT @sql = @sql+
'
print ''Dropping '+name+'''
execute master.dbo.sp_revokedbaccess '''+name+'''
'
FROM
sys.database_principals
WHERE type <> 'R'
AND NOT EXISTS
(
SELECT 1
FROM sys.schemas s
WHERE s.principal_id = dp.principal_id
)
ORDER BY name

execute (@sql)



.. yes I know it was a couple of months back but I am using it today.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search