{"id":82025,"date":"2007-12-05T00:06:46","date_gmt":"2007-12-05T00:06:46","guid":{"rendered":"https:\/\/www.webstaging.red-gate.com\/simple-talk\/?p=73175"},"modified":"2018-12-12T13:38:18","modified_gmt":"2018-12-12T13:38:18","slug":"changing-the-owner-of-a-database","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/changing-the-owner-of-a-database\/","title":{"rendered":"Changing the owner of a database"},"content":{"rendered":"<p>Tonight, as I was creating my sample database for my chapter on implementing the database, I learned something new, that existed in 2005.&nbsp; I had always used sp_changedbowner to change the owner of a database, but I was reading in another section about ALTER AUTHORIZATION earlier to change the owner of an object in a schema, so I said to my self &#8220;Louis&#8221; (I call myself that) &#8220;Louis, wonder if ALTER AUTHORIZATION works on other stuff?&#8221;<\/p>\n<p>Bizarre and surreal conversations with myself aside, the answer was Yes, you sure could.&nbsp; The syntax is:<\/p>\n<p><strong>ALTER AUTHORIZATION ON Database::&lt;databaseName&gt; TO &lt;serverPrincipal&gt;<\/strong><\/p>\n<p>To demonstrate, first, I will create a login:<\/p>\n<p>create login test with password = &#8216;like, password, dude&#8217;<br \/>go<\/p>\n<p>Then a database just taking all of the defaults.&nbsp; Setting an owner is not a part of the CREATE DATABASE syntax anyhow:<\/p>\n<p>create database showChangeOwner<br \/>go<\/p>\n<p>Then, check the owner:<\/p>\n<p>select SUSER_SNAME(owner_sid)<br \/>from&nbsp;&nbsp; sys.databases<br \/>where&nbsp; name = &#8216;showChangeOwner&#8217; <\/p>\n<p>This will return the login that you resolve to when you created the database.&nbsp; <\/p>\n<p>MYDOMAIN\\LBDAVI  <\/p>\n<p>Next, run the following statement to change the owner: <\/p>\n<p><strong>ALTER AUTHORIZATION ON Database::showChangeOwner TO test<\/strong>  <\/p>\n<p>Then run the previous statement to see that the database is now owned by login: Test . <\/p>\n<p>As an aside, if you use EXECUTE AS to change context to a different login, any database you create will be created as the user you are EXECUTING AS, so this could technically be the way to set the owner at creation time.&nbsp; I will give the test login rights to create a database then execute as it. (My new favorite permission is &#8220;create any database&#8221;.&nbsp; I looked, but despite the connotation, there is not a &#8220;create almost any database&#8221;, &#8220;create just this one database&#8221;, or even &#8220;create database named fred&#8221; rights.  <\/p>\n<p>grant create any database to test<br \/>execute as login = &#8216;test&#8217;<br \/>create database showChangeOwner2  <\/p>\n<p>Checking the owner again:  <\/p>\n<p>select SUSER_SNAME(owner_sid)<br \/>from&nbsp;&nbsp; sys.databases<br \/>where&nbsp; name = &#8216;showChangeOwner2&#8217;  <\/p>\n<p>Will show that this is owned by user &#8220;test&#8221;. ALTER AUTHORIZATION will actually let you change the owner of quite a few different things.&nbsp; For more information, please visit your public library&#8230;wait, no this is 2007, not 1970&#8230;&nbsp; Check <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/ms187359.aspx\" target=\"_blank\">here<\/a> in books online.<\/p>\n<p>Don&#8217;t forget to clean up!<\/p>\n<p>drop database showChangeOwner, showChangeOwner2<\/p>\n<p><em>(did you know you could do that?&nbsp; Saved me having to type drop database two times.&nbsp; Wow, the time I saved by not typing drop database.&nbsp; Wait, I typed drop database even more times after that.&nbsp; And yes, this obvious lack of material is why I haven&#8217;t been blogging of late&#8230;)<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tonight, as I was creating my sample database for my chapter on implementing the database, I learned something new, that existed in 2005.&nbsp; I had always used sp_changedbowner to change the owner of a database, but I was reading in another section about ALTER AUTHORIZATION earlier to change the owner of an object in a&#8230;&hellip;<\/p>\n","protected":false},"author":56085,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[19684],"class_list":["post-82025","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82025","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/56085"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=82025"}],"version-history":[{"count":1,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82025\/revisions"}],"predecessor-version":[{"id":82375,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82025\/revisions\/82375"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=82025"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=82025"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=82025"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=82025"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}