{"id":116,"date":"2009-04-30T16:34:25","date_gmt":"2009-04-30T20:34:25","guid":{"rendered":"http:\/\/www.dev-notes.com\/blog\/2009\/04\/30\/fixing-orphaned-users-after-attaching-or-restoring-a-sql-server-database\/"},"modified":"2009-04-30T16:34:25","modified_gmt":"2009-04-30T20:34:25","slug":"fixing-orphaned-users-after-attaching-or-restoring-a-sql-server-database","status":"publish","type":"post","link":"https:\/\/www.dev-notes.com\/blog\/2009\/04\/30\/fixing-orphaned-users-after-attaching-or-restoring-a-sql-server-database\/","title":{"rendered":"Fixing orphaned users after attaching or restoring a SQL Server database"},"content":{"rendered":"<p>I ran into this issue when I took a backup set from a production database and restored it into a machine in my development environment.  I noticed that the recently restored database has an user named &#8220;bob&#8221;, but when looking at bob&#8217;s entry in the newly restored database, the Login Name field is blank.  To fix this, I made use of SQL Server&#8217;s built-in stored procedure <b>sp_change_users_login<\/b>.  The syntax is below:<\/p>\n<pre class=\"code\"><b>Syntax:<\/b>\n\nsp_change_users_login 'action'\n\t[, 'user']\n\t[, 'login']\n\t[, 'password']\n\n<b>Valid values for 'action':<\/b>\n\nReport\nUpdate_One\nAuto_Fix<\/pre>\n<p>The &#8220;Report&#8221; action displays a report of all possible orphans in the current database and requires no further parameters.  Sample usage:<\/p>\n<pre class=\"code\">exec sp_change_users_login 'Report'<\/pre>\n<p>The &#8220;Update_One&#8221; action links a specified orphaned user in the current database to an existing SQL Server login.  With this action, the &#8220;user&#8221; and &#8220;login&#8221; parameters must be provided, and the &#8220;password&#8221; parameter must be NULL or not provided.  Sample usage:<\/p>\n<pre class=\"code\">exec sp_change_users_login 'Update_One', 'bob', 'bob'<\/pre>\n<p>Finally, the method I chose to perform to fix my issue was the &#8220;Auto-Fix&#8221; action, which saves me the step of having to manually create the &#8220;bob&#8221; SQL Server login first because it will automatically create the login if it does not already exist.  The &#8220;Auto_Fix&#8221; action requires the &#8220;user&#8221; and &#8220;password&#8221; parameters, and the &#8220;login&#8221; parameter must be NULL.  Sample usage:<\/p>\n<pre class=\"code\">exec sp_change_users_login 'Auto_Fix', 'bob', NULL, 'password123'<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>SQL Server makes moving (attaching) or restoring a database a very simple process.  However, when the process is complete, you may have users in the database that are orphaned; or in other words, they do not have the appropriate users created in the SQL Server&#8217;s master database.  This note provides a very simple way to repair this problem using the stored procedure <i>sp_change_users_login<\/i>.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39],"tags":[],"class_list":["post-116","post","type-post","status-publish","format-standard","hentry","category-sql-server"],"_links":{"self":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/comments?post=116"}],"version-history":[{"count":0,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/116\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/media?parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/categories?post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/tags?post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}