There are cases where you might need to restore an entire SQL Server database (i.e. in the case of loss of data, etc.) on another SQL Server instance. A set of objects that are restored from the backup set and are included in the restored database are the database users. However, in such cases, the SQL Server logins are not restored, since they are server-level objects. This article explains how to create logins for orphaned database users in SQL Server.
In this case, you can use the following stored procedure for fixing such issues: sp_change_users_login
Syntax example for creating a login for an oprhaned database user in SQL Server
USE AdventureWorks GO EXEC sp_change_users_login 'Auto_Fix', 'UserName', NULL, 'Password'; GO
The above example will create a new SQL Server Login for the given user name and use as a password the given password string.
You can also assign existing logins to orphaned users, again by using the sp_change_users_login stored procedure.
Find and Handle Orphaned Database Users with DBA Security Advisor
DBA Security Advisor, is our SQL Server security tool, which can help you analyze your SQL Server Instances for security misconfigurations, and get useful recommendations.
The way it works, is that you select the security checks you want to perform, you then connect to one or more SQL Server instances, run the assessment, and you get a comprehensive report with recommendations.
One of the security checks, is checking for orphaned users.
Try DBA Security Advisor free for 14 days!
Strengthen your SQL Server Administration Skills – Enroll to our Online Course!
Check our online course on Udemy titled “Essential SQL Server Administration Tips”
(special limited-time discount included in link).Via the course, you will learn essential hands-on SQL Server Administration tips on SQL Server maintenance, security, performance, integration, error handling and more. Many live demonstrations and downloadable resources included!
Featured Online Courses:
- Boost SQL Server Database Performance with In-Memory OLTP
- Essential SQL Server Administration Tips
- SQL Server Fundamentals – SQL Database for Beginners
- Essential SQL Server Development Tips for SQL Developers
- Introduction to Computer Programming for Beginners
- .NET Programming for Beginners – Windows Forms with C#
- Introduction to SQL Server Machine Learning Services
- Introduction to Azure SQL Database for Beginners
- SQL Server 2019: What’s New – New and Enhanced Features
- Entity Framework: Getting Started – Complete Beginners Guide
- How to Import and Export Data in SQL Server Databases
- Learn How to Install and Start Using SQL Server in 30 Mins
- A Guide on How to Start and Monetize a Successful Blog
Read Also:
- What are Exactly Orphaned Users in SQL Server?
- How to Patch a SQL Server Failover Cluster
- How to Add a Database to a SQL Server Availability Group Using T-SQL
- Top 10 SQL Server DBA Daily Tasks List
- The “Public” Database Role in SQL Server
- … all our SQL Server Administration Articles
Subscribe to our newsletter and stay up to date!
Subscribe to our YouTube channel (SQLNetHubTV)!
Like our Facebook Page!
Check our SQL Server Administration articles.
Check out our latest software releases!
Check our eBooks!
Rate this article:
Reference: SQLNetHub.com (https://www.sqlnethub.com)
© SQLNetHub
Artemakis Artemiou is a seasoned Senior Database and AI/Automation Architect with over 20 years of expertise in the IT industry. As a Certified Database, Cloud, and AI professional, he has been recognized as a thought leader, earning the prestigious Microsoft Data Platform MVP title for nine consecutive years (2009-2018). Driven by a passion for simplifying complex topics, Artemakis shares his expertise through articles, online courses, and speaking engagements. He empowers professionals around the globe to excel in Databases, Cloud, AI, Automation, and Software Development. Committed to innovation and education, Artemakis strives to make technology accessible and impactful for everyone.