首页 > 代码库 > 修windows主机名以后修复sqlserver实例名一致性

修windows主机名以后修复sqlserver实例名一致性


1、验证一致性

select @@SERVERNAME
SELECT SERVERPROPERTY(‘MachineName‘)

修改windows主机名以后,使用上面命令查看时显示不一致,需要修复。

2、使用如下脚本执行并修复
DECLARE @server_name varchar(50);
set @server_name = (select @@SERVERNAME);
DECLARE @SERVER_PROPERTY varchar(50);
set @SERVER_PROPERTY = CONVERT(varchar(50),(SELECT SERVERPROPERTY(‘MachineName‘)));
exec sp_dropserver @server = @server_name,@droplogins = null ;
exec sp_addserver @server=@SERVER_PROPERTY, @local = ‘LOCAL‘, @duplicate_ok = null ;
DECLARE @login_server_name_cmd varchar(100);
DECLARE @LOGIN_SERVER_PROPERTY_CMD varchar(200);
DECLARE @LOGIN_SERVER_PROPERTY_RCMD varchar(200);
set @login_server_name_cmd=‘DROP LOGIN [‘+@server_name+‘\Administrator]‘
set @LOGIN_SERVER_PROPERTY_CMD=‘CREATE LOGIN [‘+@SERVER_PROPERTY+‘\Administrator] FROM WINDOWS WITH DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[简体中文]‘;
set @LOGIN_SERVER_PROPERTY_RCMD=‘master..sp_addsrvrolemember @loginame=[‘+@SERVER_PROPERTY+‘\Administrator], @rolename = [sysadmin]‘
IF  EXISTS (SELECT * FROM sys.server_principals WHERE name =@server_name+‘\Administrator‘)
exec (@login_server_name_cmd);
exec (@LOGIN_SERVER_PROPERTY_MD);
exec (@LOGIN_SERVER_PROPERTY_RCMD);

注意:对于sqlserver2008,请使用sa或其他具有sysadmin权限用户登陆执行。


3、重启sqlserver数据库生效


本文出自 “composer” 博客,请务必保留此出处http://zuoqujia.blog.51cto.com/9151800/1437704