0% found this document useful (0 votes)
11 views

Orphaned Users_ Finding, Fixing and deleting.

The document discusses orphaned users in SQL Server, which are users without corresponding logins in the master database. It provides methods for finding, fixing, and deleting these orphaned users using SQL commands and stored procedures. The author emphasizes the importance of addressing orphaned users to maintain database security and integrity.

Uploaded by

mariagesept2024
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Orphaned Users_ Finding, Fixing and deleting.

The document discusses orphaned users in SQL Server, which are users without corresponding logins in the master database. It provides methods for finding, fixing, and deleting these orphaned users using SQL commands and stored procedures. The author emphasizes the importance of addressing orphaned users to maintain database security and integrity.

Uploaded by

mariagesept2024
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

20/06/2024 09:53 Orphaned Users: Finding, Fixing and deleting.

– DBA BLOGS

DBA BLOGS

NOVEMBER 14, 2019JUNE 9, 2021 BHUSHAN DHOMANE MS SQL

Orphaned Users: Finding, Fixing and deleting.

An orphaned user in SQL Server, is a user that exists in a database (Database-Security-Users) but for
Installer un ascenseur à domicile,
any reason, does not have a corresponding login in the instance’s security (master database).
combien
So as a standardça coute ? we have to fix the orphaned users by mapping it with login or removing
practice
Portail de Comparaison
orphaned user from any given database.

1. Finding Orphaned users

--List out Orphan USERS for a given database


EXEC sp_change_users_login 'Report';
OR
select db_name() as DBName,* from sys.database_principals where sid not in (sel

--List out Orphan USERS from all the databases


exec sp_msforeachdb ' use [?] EXEC sp_change_users_login ''Report''';
OR
select db_name() as DBName,* from sys.database_principals where sid not in (sel

Note: sp_change_users_login will not show orphan users for windows\AD accounts.

2. Fix Orphaned users

--If you already have a Login_Name and Password for User_Name, fix it by doing
EXEC sp_change_users_login 'Auto_Fix', 'User_Name';

--If you want to create a new Login_Name with password (Ex. P@ssword123) for Us
EXEC sp_change_users_login 'Auto_Fix', 'User_Name', 'Login_Name', 'P@ssword123

https://bhushandhomanedbadmin.wordpress.com/2019/11/14/orphaned-users-finding-fixing-and-deleting/ 1/5
20/06/2024 09:53 Orphaned Users: Finding, Fixing and deleting. – DBA BLOGS

--Fix all Orphaned users at once


USE DB_Name -- change db name for which you want to fix orphan users issue
GO

declare @name varchar(150)


DECLARE cur CURSOR FOR
select name from master..syslogins

Open cur
FETCH NEXT FROM cur into @name

WHILE @@FETCH_STATUS = 0
BEGIN
EXEC sp_change_users_login 'AUTO_FIX', @name
FETCH NEXT FROM cur into @name
END

CLOSE cur
DEALLOCATE cur

3. Delete Orphaned users

https://bhushandhomanedbadmin.wordpress.com/2019/11/14/orphaned-users-finding-fixing-and-deleting/ 2/5
20/06/2024 09:53 Orphaned Users: Finding, Fixing and deleting. – DBA BLOGS

--Drop orphaned user from Database.


use DB_Name
go
DROP user User_Name
go

--Drop all orphaned users from custom SP


use [master]
go

create proc dbo.sp_Drop_OrphanedUsers


as
begin
set nocount on
-- get orphaned users
declare @user varchar(max)
declare c_orphaned_user cursor for
select name
from sys.database_principals
where type in ('G','S','U')
and authentication_type<>2 -- Use this filter only if you are running on SQL Se
and [sid] not in ( select [sid] from sys.server_principals where type in ('G',
and name not in ('dbo','guest','INFORMATION_SCHEMA','sys','MS_DataCollectorInte
fetch next from c_orphaned_user into @user
while(@@FETCH_STATUS=0)
begin
-- alter schemas for user
declare @schema_name varchar(max)
declare c_schema cursor for
select name from sys.schemas where USER_NAME(principal_id)=@user
open c_schema
fetch next from c_schema into @schema_name
while (@@FETCH_STATUS=0)
begin
declare @sql_schema varchar(max)
select @sql_schema='ALTER AUTHORIZATION ON SCHEMA::['+@schema_name+ '] TO [dbo]
print @sql_schema
exec(@sql_schema)
fetch next from c_schema into @schema_name
end
close c_schema
deallocate c_schema

-- alter roles for user


declare @dp_name varchar(max)
declare c_database_principal cursor for
select name from sys.database_principals
where type='R' and user_name(owning_principal_id)=@user
open c_database_principal
fetch next from c_database_principal into @dp_name

https://bhushandhomanedbadmin.wordpress.com/2019/11/14/orphaned-users-finding-fixing-and-deleting/ 3/5
20/06/2024 09:53 Orphaned Users: Finding, Fixing and deleting. – DBA BLOGS

while (@@FETCH_STATUS=0)
begin
declare @sql_database_principal varchar(max)
select @sql_database_principal ='ALTER AUTHORIZATION ON ROLE::['+@dp_name+ '] T
print @sql_database_principal
exec(@sql_database_principal )
fetch next from c_database_principal into @dp_name
end
close c_database_principal
deallocate c_database_principal

-- drop roles for user


declare @role_name varchar(max)
declare c_role cursor for
select dp.name--,USER_NAME(member_principal_id)
from sys.database_role_members drm
inner join sys.database_principals dp
on dp.principal_id= drm.role_principal_id
where USER_NAME(member_principal_id)=@user
open c_role
fetch next from c_role into @role_name
while (@@FETCH_STATUS=0)
begin
declare @sql_role varchar(max)
select @sql_role='EXEC sp_droprolemember N'''+@role_name+''', N'''+@user+''''
print @sql_role
exec (@sql_role)
fetch next from c_role into @role_name
end
close c_role
deallocate c_role

-- drop user
declare @sql_user varchar(max)
set @sql_user='DROP USER ['+@user +']'
print @sql_user
exec (@sql_user)
fetch next from c_orphaned_user into @user
end
close c_orphaned_user
deallocate c_orphaned_user
set nocount off
end
go
-- Mark stored procedure as a system stored procedure
exec sys.sp_MS_marksystemobject sp_Drop_OrphanedUsers
go
-------------------------------------------------------------------------------
--To delete orphaned users from given DB (DB_Name)
USE [DB_Name]
GO
EXEC sp_Drop_OrphanedUsers;
https://bhushandhomanedbadmin.wordpress.com/2019/11/14/orphaned-users-finding-fixing-and-deleting/ 4/5
20/06/2024 09:53 Orphaned Users: Finding, Fixing and deleting. – DBA BLOGS

go
-------------------------------------------------------------------------------
--To delete orphaned users from all databases
USE [master]
GO
EXEC sp_msforeachdb 'USE [?]; EXEC sp_Drop_OrphanedUsers'
go

Advertisements
REPORT THIS ADPRIVACY

Published by Bhushan Dhomane

I am Database Administrator and started working on MS SQL Server, Oracle ,SAP Sybase and
MySQL Databases in 2011. I have worked on SQL Server versions 2000 to 2022, Oracle 8i to 19C.
Sybase 12 to 15 and MySQL 5.1 to 8.0. I have worked on SQL DB Clustering, Log Shipping,
Replication, Mirroring and Always On. Oracle Standalone and RAC databases, Golden Gate, Data
Guard and Sybase ASE, Backup Server and Replication Server. I am passionate about learning new
database things and implement it to get better knowledge and experience. View all posts by Bhushan
Dhomane

CREATE A FREE WEBSITE OR BLOG AT WORDPRESS.COM.

https://bhushandhomanedbadmin.wordpress.com/2019/11/14/orphaned-users-finding-fixing-and-deleting/ 5/5

You might also like