Skip to content

benjf.com

  • Home
  • About
  • ✞ Faith
  • Opinion
    • Politics
    • Health
    • Movies
    • Music
    • ☺ For Fun
    • Random Awesomeness
  • Technology
    • Desktop
    • Android
    • ✉ Email
    • Privacy
    • Programming
      • HTML
      • CSS
      • Javascript
        • jQuery
      • PHP
      • SQL
        • Current Page Parent SQL Server
      • Powershell
      • MS Access
      • WordPress
  • Productivity
SQL Server

Fixing Orphaned Users On A Large Scale

A script that generates a script to fix all orphaned users at once.

Fixing one orphaned user is simple.  Fixing tens or even hundreds would be laborious.  Here is a quick script that will generate a script that will fix them all in one fell swoop.  Notice that this script does not make the changes.  You must run the output generated by this script.

Be sure you’re connected to the database with the orphaned users. I learned the hard way that this will run without error on Master but it will fix nothing unless the orphaned user is in Master.

USE [DATABASE_NAME]
GO

DECLARE @t TABLE( username varchar(100), UserSID varchar(100), dbname varchar(100) null);

INSERT INTO @t (username, UserSID)
exec sp_change_users_login 'report'

UPDATE @t SET dbname = '?';

SELECT 'EXEC sp_change_users_login ''auto_fix'', ''' + username + '''
GO ' FROM @t;

 

  • 2016-03-16

Tags: orphaned userssp_change_users_login

You may also like...

  • Licorice for Adrenals

    Licorice for Adrenals

  • Tracking risk

    Tracking risk

  • Freaky Japanese Milk Ad

    Freaky Japanese Milk Ad

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Next PowerShell – Replace All Instances Of Something In All Files In Directory
  • Previous Recovering a database in RESTORING state

Archives

Recent Posts

  • 1Password App Integration With Browser Extension on Kubuntu (or Debian Linux)
  • Tracking risk
  • Fixing Sound Problems With Behringer UMC204HD On Linux
  • Faith, Biblically Speaking
  • Major Federal Budget Cuts – do the math

Tags

2020 election 2024election aliens android autohotkey bible biden christian universalism corona virus covid-19 database database files droid email failover clustering firefox gmail google government health hell humor Iphone jesus joe biden memes nextcloud online privacy php politics powershell president trump privacy search security sql server sql services ssms troubleshooting trump TSQL universal salvation us constitution vaccines verizon wireless

benjf.com © 2025. All Rights Reserved.

Powered by WordPress. Theme by Alx.