I found few database users (SQL users) with no login in the database. These are orphaned users
The following query is run in the database context
-- SQL to run to identify users without login :
SELECT CASE WHEN DATALENGTH(sid) = 28
AND type = 'S' -- only want SQL users
AND principal_id > 4 -- ignore built in users
THEN 1 ELSE 0 END AS is_user_without_login,*
FROM sys.database_principals
--I found the user where is_user_without_login = 1
--dropped the user
--and created the user linking the user to the login
use db_01
GO
drop user dbuser1
create user dbuser1 for login svrlogin1
This blog is created to add all the information and experiences that I have learnt while working on MS sql server as a DBA. The blog is also updated frequently with the real world problem that I encounter hands on at work and the resolutions to resolve them. I hope this blog will be of some use to you and you will revisit. Thank you for stopping by and you are welcome to leave comments.
Showing posts with label orphan users. Show all posts
Showing posts with label orphan users. Show all posts
Thursday, February 16, 2017
Thursday, October 22, 2015
Work Adventure: Fix Orphaned user
When I restored a database to a new server, the users were imported but the login were not. The database users were imported as the orphan users in SQL 2012
Here is how I found the orphaned users in a database
EXEC sp_change_users_login @Action='Report'
--OR we can run the following in the current database context
select * from sysusers where issqluser = 1 and (sid <> 0x0
and sid is not null)
and (LEN(sid) <= 16 ) AND suser_sname(sid) is null
-----------------------------------------
The in order to link the orphan user to the existing login I did the following
sp_change_users_login 'Update_One', 'OrphanUser, 'Logintolinkto'
Here is how I found the orphaned users in a database
EXEC sp_change_users_login @Action='Report'
--OR we can run the following in the current database context
select * from sysusers where issqluser = 1 and (sid <> 0x0
and sid is not null)
and (LEN(sid) <= 16 ) AND suser_sname(sid) is null
-----------------------------------------
The in order to link the orphan user to the existing login I did the following
sp_change_users_login 'Update_One', 'OrphanUser, 'Logintolinkto'
Subscribe to:
Posts (Atom)