{"id":1277,"date":"2015-09-18T11:57:19","date_gmt":"2015-09-18T01:57:19","guid":{"rendered":"http:\/\/ntsblog.homedev.com.au\/?p=1277"},"modified":"2024-06-08T20:27:56","modified_gmt":"2024-06-08T10:27:56","slug":"sql-server-script-user-sid","status":"publish","type":"post","link":"https:\/\/ntsblog.homedev.com.au\/index.php\/2015\/09\/18\/sql-server-script-user-sid\/","title":{"rendered":"SQL Server Script user with SID"},"content":{"rendered":"<div id=\"ntsbl-2727132281\" class=\"ntsbl-before-content ntsbl-entity-placement\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6288941070289539\" crossorigin=\"anonymous\"><\/script><ins class=\"adsbygoogle\" style=\"display:inline-block;width:728px;height:90px;\" \ndata-ad-client=\"ca-pub-6288941070289539\" \ndata-ad-slot=\"9356781486\"><\/ins> \n<script> \n(adsbygoogle = window.adsbygoogle || []).push({}); \n<\/script>\n<\/div>\n<h2 class=\"wp-block-heading\">The Problem<\/h2>\n\n\n\n<p>When you move sql logins around servers the logins are created with an SID that uniquely identifies the login.<\/p>\n\n\n\n<p>Assume you setup two logins manually, one on your production server and one on your dev server.<\/p>\n\n\n\n<p>When you restore your prod database to development your Login will be &#8220;disconnected&#8221; from the restored database and the SID of the &#8220;User&#8221; in the restored production database does not match the SID of the Login on your dev box.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Solution<\/h2>\n\n\n\n<p>Create your login specifying the SID.<br>The easiest way is to let SQL set and SID and then read it off the DB and use it in your new login creation script<br>This script should do it<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql line-numbers\">Use master\nGO\n\n-- Step 1 - Create Login\nIF EXISTS (SELECT * FROM sys.server_principals WHERE name = N'usr_website')\nDROP LOGIN [usr_website]\nGO\n\nCREATE LOGIN [usr_website] WITH PASSWORD = 'password:-)'\nGO\n\n-- Step 2 - Get SID\nDeclare @name nvarchar(254)\n\nset @name = 'usr_website'\n\nSELECT\n[sid]\n,[status]\n,[name]\n,[dbname]\n,[language]\n,[denylogin]\n,[hasaccess]\n,[isntname]\n,[isntgroup]\n,[isntuser]\n,[sysadmin]\n,[securityadmin]\n,[serveradmin]\n,[setupadmin]\n,[processadmin]\n,[diskadmin]\n,[dbcreator]\n,[bulkadmin]\n,[loginname]\nFROM [msdb].[sys].[syslogins]\nwhere name = @name or @name is null\n\n-- Step 3 - Create Login with SID\nIF EXISTS (SELECT * FROM sys.server_principals WHERE name = N'usr_website')\nDROP LOGIN [usr_website]\nGO\n\nCREATE LOGIN [usr_website] WITH PASSWORD = 'password:-)', SID = 0x346837D1EC9C484C91A3B46D7EC7EABF\nGO<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The Problem When you move sql logins around servers the logins are created with an SID that uniquely identifies the login. Assume you setup two [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13,28],"tags":[],"class_list":["post-1277","post","type-post","status-publish","format-standard","hentry","category-sql-server","category-t-sql"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/1277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/comments?post=1277"}],"version-history":[{"count":0,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/1277\/revisions"}],"wp:attachment":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/media?parent=1277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/categories?post=1277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/tags?post=1277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}