Site Search:
Sign in | Join | Help
4Penny.net

SQL Server (T-SQL)

Comments and notes on SQL Server 2000, 2005, and T-SQL

Renaming a SQL Server

Following is a T-SQL script you can use to rename your SQL Server after you have changed the computer name. This script will work if you are connected to the default instance or a named instance. After running the script, you must restart SQL Server for the action to complete.

DECLARE @machinename sysname, 
 @servername sysname, 
 @instancename sysname 


SELECT 
 @instancename = 
  CASE WHEN charindex('\', @@servername) = 0 THEN '' 
  ELSE SUBSTRING(@@servername, CHARINDEX('\', @@servername), (len(@@servername)+ 1) - CHARINDEX('\', @@servername)) 
  END 
SET @machinename = convert(nvarchar(100), serverproperty('machinename')) + @instancename; 


EXEC sp_dropserver @@servername; 
EXEC sp_addserver @machinename, 'local' 
print 'renamed to ' + @machinename

IMPORTANT:

Stop and start the SQL Server to complete the process

Comments

 

mpolino said:

Steve,

I love SQL scripts so much you got your own blog mention.

http://msdynamicsgp.blogspot.com/2007/04/killer-sqlgp-resource.html

Keep up the great work.

Mark

April 10, 2007 11:56 AM [Delete]
 

Joe M said:

Nice, thanks!

April 14, 2007 12:14 PM [Delete]

Leave a Comment

(required)  
(optional)
(required)  
Add

About Steve Gray

Steve is a seasoned (translate: old) developer in VB and ASP.NET. He spends most of his time in Dynamics GP, writing custom mods for consulting firms. Crystal reports, eConnect, VS Tools for Dynamics... anything that comes along.