{"id":200,"date":"2013-04-14T23:31:59","date_gmt":"2013-04-14T23:31:59","guid":{"rendered":"http:\/\/www.theSQLReport.com\/?p=200"},"modified":"2014-02-13T02:34:29","modified_gmt":"2014-02-13T02:34:29","slug":"using-a-batch-file-to-change-the-hostname-in-stand-alone-instance-of-sql-server","status":"publish","type":"post","link":"http:\/\/www.theSQLReport.com\/?p=200","title":{"rendered":"Using a batch file to change the hostname in a stand-alone instance of SQL Server"},"content":{"rendered":"<p>Recently I was asked for a DOS batch script, that a tech person could run, to give a stand-alone instance of SQL Server the new hostname of its server.\u00a0 This batch file would be run once the server name was changed.<\/p>\n<p>This batch file removed the old hostname from SQL Server, and pulled the new computer name from the operating system\u2019s environmental variables.\u00a0 Then it stops &amp; restarts SQL Server service.\u00a0 The net start &amp; net stop should reflect the current name of the SQL Server service, and may need to also restart the SQL Agent service.<\/p>\n<p>Note: Normally I am asked to do this in PowerShell, but these are older servers that do not have PoweShell installed.\u00a0 Therefore the script had to be done as a DOS batch script.\u00a0 The script assumes local administrator privileges &amp; that sqlcmd is in the executable search path.<\/p>\n<p>DOS Batch Script:<\/p>\n<pre>:what SQL thinks the old server hostname is\r\nsqlcmd -E -Q \"select @@servername as 'Old Hostname';\"\r\n\r\n:SQL drops the old server name\r\nsqlcmd -E -Q \"exec sp_dropserver @@servername\"\r\n\r\n:Get the New Computer Name\r\nSET newcomputername = %COMPUTERNAME%\r\n\r\nECHO **** The New Computer Name is : %newcomputername %\r\n\r\n:SQL adds the new servername\r\nsqlcmd -E -v new=%newcomputername %\u00a0 -Q \"exec sp_addserver\u00a0 '$(new)','local'\"\r\n\r\n:Stop &amp; Restart SQL Server\r\nnet stop mssqlserver\r\n\r\nnet start mssqlserver\r\n\r\n:SQL Server displays new servername\r\nsqlcmd -E -Q \"select @@servername;\"\r\n\r\n:pause to check for errors before exiting\r\npause<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Recently I was asked for a DOS batch script, that a tech person could run, to give a stand-alone instance of SQL Server the new hostname of its server.\u00a0 This &hellip; <a class=\"readmore\" href=\"http:\/\/www.theSQLReport.com\/?p=200\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":222,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-200","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sql-server"],"_links":{"self":[{"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts\/200"}],"collection":[{"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=200"}],"version-history":[{"count":34,"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts\/200\/revisions"}],"predecessor-version":[{"id":234,"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/posts\/200\/revisions\/234"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=\/wp\/v2\/media\/222"}],"wp:attachment":[{"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=200"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.theSQLReport.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}