1

I have circumstances that a server can host many databases and a lot of them get offline and transfered elsewhere from time to time. This causes lots of server logins to be created and stay there after the transfer. I need a script to find all the server logins that are mapped to offline or non existing databases so I can clean them.

Thank you!

asked Jan 2, 2017 at 10:01

1 Answer 1

2

Here is a script to identify orphaned logins.

SELECT SL.name, SL.dbname 
, 'USE [master]; 
ALTER LOGIN [' + SL.name + '] WITH DEFAULT_DATABASE=[master];
CREATE USER [' + SL.name + '] FOR LOGIN [' + SL.name + '] 
WITH DEFAULT_SCHEMA = [dbo];' AS SQL_command 
FROM sys.[syslogins] SL
LEFT JOIN sys.[databases] SD
ON SL.[dbname] = SD.[name]
WHERE SD.name IS NULL
ORDER BY SL.[name], SL.[dbname];

The full article can be found on http://sqlmag.com/database-administration/identifying-orphaned-logins

answered Jan 2, 2017 at 15:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.