{"id":213,"date":"2012-02-10T17:08:56","date_gmt":"2012-02-10T23:08:56","guid":{"rendered":"https:\/\/jackdonnell.com\/?p=213"},"modified":"2015-12-27T22:34:00","modified_gmt":"2015-12-28T04:34:00","slug":"scripting-user-database-rights","status":"publish","type":"post","link":"https:\/\/jackdonnell.com\/?p=213","title":{"rendered":"Scripting User Database Rights"},"content":{"rendered":"<p>All DBA&#8217;s will be tasked at time to refresh a copy of production to a development, reporting or promote as adatbase to a UAT or QA environment. Many times it is important to retain the existing users rights on the target instance.<br \/>\nThe script uses the sys.database_role_members, sys.database_principals, sys.database_permissions, sys.sysobjects and sys.sysusers tables.<\/p>\n<p>Some things to note:<\/p>\n<ul>\n<li>Script does not remove or reduce rights if the user already exists.<\/li>\n<li>Create user statements that test if users already exits.<\/li>\n<li>Add users to existing database roles.<\/li>\n<li>Scripts object level grants.<\/li>\n<\/ul>\n<p><!--more--><br \/>\nUse the following to script out the existing target database users prior to the restore.<\/p>\n<p><code><br \/>\nSET NOCOUNT ON<br \/>\nGO<br \/>\n-----To find out user access to dB--------<br \/>\n\/*<br \/>\nJCD 11-08-2011<br \/>\nScript rights in a database before overlay.<br \/>\n+ Set Results to Text<br \/>\n+ Does not create logins or add to Server level roles<br \/>\n*\/<\/code><\/p>\n<p>&#8212; CREATE USERS<br \/>\nPRINT &#8216;USE &#8216;+ DB_NAME() +&#8217;<br \/>\nGO&#8217;<\/p>\n<p>PRINT &#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-&#8216;<br \/>\nPRINT &#8216;&#8211; Create users if they do not exists &#8212; &#8216;<br \/>\nPRINT &#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-&#8216;<br \/>\nGO<\/p>\n<p>SELECT &#8216;IF NOT EXISTS(Select name from sys.database_principals WHERE name = &#8221;&#8217;+p.name+&#8221;&#8217;)<br \/>\nBEGIN<br \/>\nCREATE USER &#8216; + QUOTENAME(p.name) + &#8216; FOR LOGIN &#8216; + QUOTENAME(p.name) + &#8216;;<br \/>\nEND<br \/>\nGO &#8216;<br \/>\nFROM sys.database_principals p<br \/>\nJOIN sys.sysusers as u<br \/>\non p.name=u.name<br \/>\nWHERE p.[TYPE] &lt;&gt; &#8216;R&#8217;<br \/>\nand p.principal_id&gt;=5<br \/>\nORDER BY p.NAME ASC<br \/>\nGO<\/p>\n<p>&#8212; ADD ROLE MEMBERS<br \/>\nPRINT &#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-&#8216;<br \/>\nPRINT &#8216;&#8211; Add Users to DB Roles &#8212; &#8216;<br \/>\nPRINT &#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-&#8216;<br \/>\nGO<\/p>\n<p>SELECT &#8216;EXEC master..sp_addrolemember &#8221;&#8217;+pp.name+&#8221;&#8217;, &#8221;&#8217;+p.name+&#8221;&#8217;;<br \/>\nGO&#8217;<\/p>\n<p>FROM sys.database_role_members roles<br \/>\nJOIN sys.database_principals p<br \/>\nON roles.member_principal_id = p.principal_id<br \/>\nJOIN sys.database_principals pp<br \/>\nON roles.role_principal_id = pp.principal_id<br \/>\nWHERE pp.name != &#8216;db_executor&#8217;<br \/>\n&#8211;and p.type!=&#8217;s&#8217;<br \/>\nORDER BY pp.NAME ASC<br \/>\nGO<\/p>\n<p>&#8212; OBJECT LEVEL GRANTS<br \/>\nPRINT &#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-&#8216;<br \/>\nPRINT &#8216;&#8211; Grant Object Level Rights &#8212; &#8216;<br \/>\nPRINT &#8216;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-&#8216;<br \/>\nGO<\/p>\n<p>SELECT &#8216;GRANT &#8216; +( perm.permission_name COLLATE SQL_Latin1_General_Pref_CP1_CI_AS)+&#8217; ON &#8216;+ QUOTENAME(OBJECT_SCHEMA_NAME (perm.major_id))+&#8217;.&#8217;+QUOTENAME(OBJECT_NAME (perm.major_id))+&#8217; to &#8216; + p.name+&#8217;;<br \/>\nGO&#8217;<br \/>\nfrom sys.database_permissions perm<br \/>\njoin sys.database_principals p<br \/>\non p.principal_id = perm.grantee_principal_id<br \/>\njoin sys.sysobjects as o<br \/>\non o.id=perm.major_id ;<br \/>\nGO<\/p>\n","protected":false},"excerpt":{"rendered":"<p>All DBA&#8217;s will be tasked at time to refresh a copy of production to a development, reporting or promote as adatbase to a UAT or QA environment. Many times it is important to retain the existing users rights on the &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/jackdonnell.com\/?p=213\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[113,22,5],"tags":[303,315,249,35,36,295,52,331,330,332,329,291],"class_list":["post-213","post","type-post","status-publish","format-standard","hentry","category-reportingbi","category-sql-server","category-t-sql","tag-dba","tag-query","tag-script","tag-select","tag-sql","tag-sql-server","tag-ssms","tag-sys-database_permissions","tag-sys-database_principals","tag-sys-sysobjects","tag-sys-sysusers","tag-t-sql"],"_links":{"self":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts\/213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=213"}],"version-history":[{"count":22,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts\/213\/revisions"}],"predecessor-version":[{"id":506,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=\/wp\/v2\/posts\/213\/revisions\/506"}],"wp:attachment":[{"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jackdonnell.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}