Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop all logins where loginname like

I'm looking to bulk remove users from a database with a script for every login with a name that matches a certain pattern. What is the best way to do this with TSQL?

DELETE syslogins WHERE.... does not work

Trying to delete straight from syslogins will return "Ad hoc updates to system catalogs are not allowed"

like image 232
Russell Steen Avatar asked Jun 06 '11 15:06

Russell Steen


People also ask

How do I disconnect all users from a database?

When you right click on a database and click Tasks and then click Detach Database , it brings up a dialog with the active connections. By clicking on the hyperlink under "Messages" you can kill the active connections. You can then kill those connections without detaching the database.

How do I drop an orphaned user in SQL Server?

Once you have identified orphan users it is extremely simple to remove them. You remove them by using the sp_revokeuser SP. Here is an example that removes the database users 'USERX', from the current database in use.

How do I delete users in SSMS?

If you are using SQL Server Management Studio you can browse to the user and right-click selecting delete.


1 Answers

How about this query to create your TSQL statements? Then copy this SQL into a new query and exec.

select 'drop login [' + name + '];'
from sys.server_principals 
WHERE name like 'foo%'

This is basically the script that'll be created and run when you delete/drop a login from SSMS.

like image 149
p.campbell Avatar answered Oct 10 '22 19:10

p.campbell