Web design and hosting, database, cloud and social media solutions that deliver business results
  • Business Solutions
    • Robotic Process Automation
    • Software
    • Database Consultancy Services
      • Data Integration
      • Datawarehouse Services
      • Power BI
      • Server Upgrade and DBA Services
    • Web Site Design Services
      • Logo Design
      • Payment Gateways
      • Web Localisation and Translation
      • Web Site Optimisation
      • Web Site Security
      • Technical Tools
    • Cloud Services
      • Amazon Web Services
      • Google Cloud Services
      • Microsoft Azure
    • Microsoft Office
    • Social Media Management and Advice Services
  • Academy
    • Our Test Environment
    • Learning Databases
      • The Basics
      • Get Open Query
      • SQL Server Data
      • SQL Server Maintenance
      • Using SQL Server Dates
      • Using SQL Server Functions
      • Using SQL Server Pivot-Unpivot
      • Technical Tools
    • Learning Web Design
      • Building Ousia Content Management System
      • Using ASP-NET
      • Using CSS
      • Using JavaScript
    • Learning Cloud and IT Services
      • Task Scheduler Error 2147943645
      • Requesting SSL and Generation of PFX file in OpenSSL Simple Steps
    • Using Social Media
      • Asking for a Google Review
      • Changing a Facebook account from personal to business
      • Choosing where to focus Social Media effort
      • Social Media Image Sizes
      • Using Meta Data to set Social Media Images
  • About Us
    • Blog
      • Building an entry level gaming machine
      • Google Core Update Jan 2020
      • Hot Chilli Internet Closure
      • How To Choose Content For Your Website Adverts Leaflets
      • Preventing Online Scam
      • Skimmers of the gig economy
      • The most annoying things about websites on the Internet
      • Top 5 websites for free Vector Graphics
    • Careers
      • Translator English-Portuguese
      • Translator English-Spanish
    • Portfolio
    • Team
      • Adrian Anandan
      • Ali Al Amine
      • Ayse Hur
      • Chester Copperpot
      • Deepika Bandaru
      • Gavin Clayton
      • Sai Gangu
      • Suneel Kumar
      • Surya Mukkamala
عربى (AR)čeština (CS)Deutsch (DE)English (EN-GB)English (EN-US)Español (ES)فارسی (FA)Français (FR)हिंदी (HI)italiano (IT)日本語 (JA)polski (PL)Português (PT)русский (RU)Türk (TR)中国的 (ZH)

Remove a user without leaving orphan data in SQL Server

Completely remove users from SQL Server without leaving orphan data

Context

Dropping a user from the logins section can create orphaned users on the databases, so it is advised for you to remove database access before then deleting the user.

This can be quite time consuming, so armed with the various information I found we came up with the code below.

It has saved me time when removing users, so hopefully it may help you.

SQL

DECLARE @UserName SysName='dfgsfdg'--Add Domain if Windows UserSET NOCOUNT ONCREATE TABLE #UserTable(UserName sysname,GroupName sysname,LoginName sysname NULL,DefDBName sysname NULL,DefSchemaName sysname NULL,UserID smallint,SID smallint)CREATE TABLE #Databases(DATABASE_NAME sysname,DATABASE_SIZE INT, REMARKS varchar(254))INSERT INTO #Databases EXEC sp_databasesDECLARE @DBName sysnameDECLARE c1 CURSOR FOR (SELECT DATABASE_NAME FROM #Databases)open c1fetch next from c1 into @DBNameWHILE @@FETCH_STATUS= 0BEGINPRINT @DBNameEXEC ('USE '+ @DBName +' INSERT INTO #UserTable EXEC sp_helpuser')  IF (SELECT COUNT(*)FROM #UserTable WHERE UserName=@UserName)>0  BEGIN  PRINT'Removing '''+@UserName +''' FROM '+@DBName  EXEC ('USE '+ @DBName +' EXEC sp_dropuser '''+@UserName +'''')  ENDDELETE FROM #UserTable--ClearTablefetch next from c1 into @DBNameENDCLOSE C1deallocate c1PRINT 'Revoking Login'IF CHARINDEX(@UserName,'\')>1 BEGINEXEC ('EXEC sp_revokelogin '''+ @UserName +'''')--Check string for domain info, assume windows if '/' foundENDIF CHARINDEX(@UserName,'\')=0 AND EXISTS(SELECT * FROM sys.server_principals WHERE name =@UserName) BEGIN--Check if SQL LoginEXEC ('DROP LOGIN ['+ @UserName +']')ENDDROP TABLE #UserTableDROP TABLE #Databases

Author

Helpful?

Please note, this commenting system is still in final testing.
Copyright Claytabase Ltd 2020

Registered in England and Wales 08985867

Site Links

RSSLoginLink Cookie PolicySitemap

Social Media

facebook.com/Claytabaseinstagram.com/claytabase/twitter.com/Claytabaselinkedin.com/company/claytabase-ltd

Get in Touch

+15125961417info@claytabase.comClaytabase USA, 501 Congress Avenue, Suite 150, Austin, Texas, 78701, United States

Partnered With

The settings on this site are set to allow all cookies. These can be changed on our Cookie Policy & Settings page.
By continuing to use this site you agree to the use of cookies.
Ousia Logo
Logout
Ousia CMS Loader