{"id":652,"date":"2014-06-26T01:43:23","date_gmt":"2014-06-26T01:43:23","guid":{"rendered":"http:\/\/www.theSQLReport.com\/?p=652"},"modified":"2014-11-19T11:56:50","modified_gmt":"2014-11-19T11:56:50","slug":"script-to-change-the-servername-in-sql-server","status":"publish","type":"post","link":"https:\/\/www.theSQLReport.com\/?p=652","title":{"rendered":"Script to change the SERVERNAME property in SQL Server"},"content":{"rendered":"<p>When the Master Database is restored or moved to another server with with a different hostname, the master database is unable to detect that it is on a new server.\u00a0 The SERVERNAME property will need to be updated with the new server&#8217;s hostname.<\/p>\n<p>This SERVERNAME property is written to five tables in the MSDB database.\u00a0 Probably the most recognizable table is: <em>msdb.dbo.backupset<\/em>.\u00a0 This may cause problem if you are using a 3rd party monitoring or backup software.<\/p>\n<p>To check that the SQL Server&#8217;s SERVERNAME matches the operating system&#8217;s HOSTNAME.\u00a0 The queries below (nice for cut &amp; paste) will reveal if there is a difference.\u00a0 Like in the picture below,\u00a0 SQL Server has the hostname (or SERVERNAME) as &#8220;SQL2014&#8221;, and the actual operating system hostname is &#8220;PRODUCTION&#8221;.<\/p>\n<p><em><strong>Please Note: \u00a0the\u00a0web browser may change some of the characters below, \u00a0please check to match the script to the picture below<\/strong><\/em><\/p>\n<pre style=\"padding-left: 60px;\"> --\r\n --\u00a0 The Hostname SQL Server recorded\r\n --\r\n select @@SERVERNAME as \"SQL SERVER HOSTNAME\"\r\n go\r\n --\r\n --\u00a0 The Hostname pulled from the operating system\r\n --\r\n select SERVERPROPERTY('MachineName') as HOSTNAME\r\n go\r\n --<\/pre>\n<p><a href=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/ServerName_Problem.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-656 size-full\" src=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/ServerName_Problem.jpg\" alt=\"ServerName_Problem\" width=\"1110\" height=\"483\" srcset=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/ServerName_Problem.jpg 1110w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/ServerName_Problem-300x130.jpg 300w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/ServerName_Problem-1024x445.jpg 1024w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/ServerName_Problem-210x91.jpg 210w\" sizes=\"(max-width: 1110px) 100vw, 1110px\" \/><\/a><\/p>\n<p>The scripts provided grab the hostname from the operating system, and loads it into a variable.\u00a0 Then it drop the old SERVICENAME, and then it adds back the SERVERNAME as the operating system&#8217;s hostname.\u00a0 Nice cut &amp; paste, because you do not have input anything.<\/p>\n<p><strong>Script shown below can be used to change the SERVERNAME for a default instance:<\/strong><\/p>\n<pre style=\"padding-left: 60px;\"> --\r\n -- Declare Variable\r\n --\r\n DECLARE @new_name nvarchar(50)\r\n --\r\n -- Push OS Hostame into the variable\r\n --\r\n set @new_name = convert(nvarchar(50), SERVERPROPERTY('MachineName'))\r\n --\r\n -- Drop the hostname in SQL Server\r\n --\r\n exec sp_dropserver @@servername\r\n --\r\n -- Add the new hostname in SQL Server from variable\r\n --\r\n exec sp_addserver @new_name,'local';\r\n go\r\n --<\/pre>\n<h5><a href=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/ServerName_Change.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-654 size-full\" src=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/ServerName_Change.jpg\" alt=\"ServerName_Change\" width=\"1314\" height=\"452\" srcset=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/ServerName_Change.jpg 1314w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/ServerName_Change-300x103.jpg 300w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/ServerName_Change-1024x352.jpg 1024w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/ServerName_Change-210x72.jpg 210w\" sizes=\"(max-width: 1314px) 100vw, 1314px\" \/><\/a><\/h5>\n<p><strong>Script shown below can be used to change the SERVERNAME for a\u00a0named instance:<\/strong><\/p>\n<pre style=\"padding-left: 60px;\"> --\r\n -- Declare Variable\r\n --\r\n DECLARE @new_name nvarchar(50)\r\n --\r\n -- Push OS Hostame into the variable\r\n --\r\n set @new_name = convert(nvarchar(50), SERVERPROPERTY('MachineName'))\r\n --\r\n -- Add Named Instance to the string\r\n --\r\n set @new_name = @new_name + '\\' + convert(nvarchar(50), SERVERPROPERTY('InstanceName'))\r\n --\r\n --\r\n -- Drop the hostname in SQL Server\r\n --\r\n exec sp_dropserver @@servername\r\n --\r\n -- Add the new hostname in SQL Server from variable\r\n --\r\n exec sp_addserver @new_name,'local';\r\n go\r\n --<\/pre>\n<p><a href=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/ServerName_Change_With_Named_Instance.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-655 size-full\" src=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/ServerName_Change_With_Named_Instance.jpg\" alt=\"ServerName_Change_With_Named_Instance\" width=\"1236\" height=\"554\" srcset=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/ServerName_Change_With_Named_Instance.jpg 1236w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/ServerName_Change_With_Named_Instance-300x134.jpg 300w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/ServerName_Change_With_Named_Instance-1024x458.jpg 1024w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/ServerName_Change_With_Named_Instance-210x94.jpg 210w\" sizes=\"(max-width: 1236px) 100vw, 1236px\" \/><\/a><\/p>\n<p>After the SERVERNAME property has been updated, the SQL Server Instance will need to be restarted for the SERVERNAME property to take effect.\u00a0 The configuration manager is one of many way to restart the\u00a0the SQL Server Instance.<\/p>\n<p>Highlight the Instance in the SQL Server Configuration Manager, then click on the restart button, like pictured below:<\/p>\n<p><a href=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/RestartSQLServer.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-653 size-full\" src=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/RestartSQLServer.jpg\" alt=\"RestartSQLServer\" width=\"725\" height=\"190\" srcset=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/RestartSQLServer.jpg 725w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/RestartSQLServer-300x78.jpg 300w, http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/RestartSQLServer-210x55.jpg 210w\" sizes=\"(max-width: 725px) 100vw, 725px\" \/><\/a><\/p>\n<p>After the SQL Server Instance has been restarted, verify using the same SQL queries listed above.<\/p>\n<p><a href=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/ServerName_Resolved.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-657 size-full\" src=\"http:\/\/www.theSQLReport.com\/wp-content\/uploads\/2014\/06\/ServerName_Resolved.jpg\" alt=\"ServerName_Resolved\" width=\"1067\" height=\"497\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When the Master Database is restored or moved to another server with with a different hostname, the master database is unable to detect that it is on a new server.\u00a0 &hellip; <a class=\"readmore\" href=\"https:\/\/www.theSQLReport.com\/?p=652\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":657,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[35,34,64],"class_list":["post-652","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sql-server","tag-hostname","tag-servername","tag-sql-server"],"_links":{"self":[{"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts\/652"}],"collection":[{"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=652"}],"version-history":[{"count":17,"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts\/652\/revisions"}],"predecessor-version":[{"id":661,"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts\/652\/revisions\/661"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/media\/657"}],"wp:attachment":[{"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=652"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}