{"id":197,"date":"2011-05-17T12:51:53","date_gmt":"2011-05-17T12:51:53","guid":{"rendered":"http:\/\/eldar.pro\/?p=197"},"modified":"2019-07-18T22:15:34","modified_gmt":"2019-07-18T22:15:34","slug":"123","status":"publish","type":"post","link":"https:\/\/eldar.pro\/?p=197","title":{"rendered":"How to change the &#8220;model&#8221; database owner"},"content":{"rendered":"<p>In one server SQL Server 2005 when I tried to open the properties of the database using the GUI I saw:<br \/>\n<a href=\"\/\/eldar.pro\/wp-content\/uploads\/2012\/09\/model_owner.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-185 aligncenter\" title=\"model_owner\" src=\"\/\/eldar.pro\/wp-content\/uploads\/2012\/09\/model_owner.jpg\" alt=\"\" width=\"611\" height=\"183\" \/><\/a><br \/>\nIndeed, we cannot determine the database owner:<\/p>\n<pre data-enlighter-language=\"sql\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\">select owner_sid, suser_sname(owner_sid) from sys.databases where name='model'<\/code><\/pre>\n<table border=\"1\">\n<tbody>\n<tr>\n<td>owner_sid<\/td>\n<td>owner name<\/td>\n<\/tr>\n<tr>\n<td>0x010500000000000515000000B92ACD62A473F62D825A8A49A3780000<\/td>\n<td>NULL<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>It was probably the domain user that no longer exists in AD.<br \/>\nChange the owner with procedure <a href=\"\/\/msdn.microsoft.com\/en-us\/library\/ms178630(v=sql.90).aspx\">sp_changedbowner<\/a> in the database model is prohibited::<\/p>\n<pre data-enlighter-language=\"sql\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\">use model; EXEC sp_changedbowner 'sa'<\/code><\/pre>\n<p><em>Msg 15109, Level 16, State 1, Line 1<br \/>\nCannot change the owner of the master, model, tempdb or distribution database.<\/em><\/p>\n<p>We can change the sid directly in the system table, as described <a href=\"\/\/social.msdn.microsoft.com\/Forums\/en-US\/sqlsecurity\/thread\/2efd25a0-53b3-4334-9178-2d74b8dab14f\">here<\/a>, but it is, really, is not recommended by MS way.<\/p>\n<p>So we use the fact that owner of any attached database become the current user, and running SQL Server with the key \/T3608, execute under the user&#8217;s session sa:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\">EXEC master.dbo.sp_detach_db @dbname = N'model'\nGO\nCREATE DATABASE [model] ON\n( FILENAME = N'E:\\DATA\\model.mdf' ),\n( FILENAME = N'E:\\DATA\\modellog.ldf' )\nFOR ATTACH\nGO<\/pre>\n<p>Restart the SQL Server, we see:<\/p>\n<pre data-enlighter-language=\"sql\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\">select owner_sid, suser_sname(owner_sid) 'owner name' from sys.databases where name='model'<\/code><\/pre>\n<table border=\"1\">\n<tbody>\n<tr>\n<td>owner_sid<\/td>\n<td>owner name<\/td>\n<\/tr>\n<tr>\n<td>0x01<\/td>\n<td>sa<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>In one server SQL Server 2005 when I tried to open the properties of the database using the GUI I saw: Indeed, we cannot determine the database owner: select owner_sid, suser_sname(owner_sid) from sys.databases where name=&#8217;model&#8217; owner_sid owner name 0x010500000000000515000000B92ACD62A473F62D825A8A49A3780000 NULL It was probably the domain user that no longer exists in AD. Change the owner [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"class_list":["post-197","post","type-post","status-publish","format-standard","hentry","category-s-sql-server-en"],"_links":{"self":[{"href":"https:\/\/eldar.pro\/index.php?rest_route=\/wp\/v2\/posts\/197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eldar.pro\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eldar.pro\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eldar.pro\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eldar.pro\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=197"}],"version-history":[{"count":8,"href":"https:\/\/eldar.pro\/index.php?rest_route=\/wp\/v2\/posts\/197\/revisions"}],"predecessor-version":[{"id":484,"href":"https:\/\/eldar.pro\/index.php?rest_route=\/wp\/v2\/posts\/197\/revisions\/484"}],"wp:attachment":[{"href":"https:\/\/eldar.pro\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eldar.pro\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eldar.pro\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}