“Renaming” SQL Server created from a template VM

Home / “Renaming” SQL Server created from a template VM

When a VM is created from a template machine that has SQL Server installed, that SQL instance must be told that the machine name has changed.

If a “foo” machine is created from a template named “template”, the T-SQL statement run against the default SQL instance select @@SERVERNAME returns “template” instead of the desired “foo”.

The solution is to execute the following T-SQL and then restart the SQL Server process.

Here’s a C# version of the rename and the SQL Server process restart using PowerShell and the SqlDataClient.