机器名更改后修正windows登录名

 

---首先以SA超级管理员身份登陆

 

USE [master]

GO

 

--更改servername

IF SERVERPROPERTY('ServerName')<>@@SERVERNAME

BEGIN

    DECLARE @srvname sysname

    SET @srvname=@@SERVERNAME

    EXEC sp_dropserver @server=@srvname

    SET @srvname=CAST(SERVERPROPERTY('ServerName') as sysname)

    EXEC sp_addserver @server = @srvname , @local = 'LOCAL'

END

GO

 

---修改windows登陆账号

DECLARE @NAME VARCHAR(100)

SELECT @NAME=NAME FROM SYSLOGINS WHERE name  LIKE '%/ADMINISTRATOR'

IF @@ROWCOUNT>0

EXEC('DROP LOGIN ['+@NAME+']')

 

DECLARE @SERVERNAME VARCHAR(100)

SELECT @SERVERNAME=@@SERVERNAME

 

EXEC('CREATE LOGIN ['+@SERVERNAME+'/Administrator] FROM WINDOWS WITH DEFAULT_DATABASE=[master]')

EXEC(' master..sp_addsrvrolemember @loginame = N'''+@SERVERNAME+'/Administrator'', @rolename = N''sysadmin''')

GO

 

 

DECLARE @NAME VARCHAR(100)

SELECT @NAME=NAME FROM SYSLOGINS WHERE name  LIKE '%/SQLAGENT'---启动账号

IF @@ROWCOUNT>0

EXEC('DROP LOGIN ['+@NAME+']')

 

DECLARE @SERVERNAME VARCHAR(100)

SELECT @SERVERNAME=@@SERVERNAME

 

EXEC('CREATE LOGIN ['+@SERVERNAME+'/sqlagent] FROM WINDOWS WITH DEFAULT_DATABASE=[master]')

EXEC(' master..sp_addsrvrolemember @loginame = N'''+@SERVERNAME+'/sqlagent'', @rolename = N''sysadmin''')

GO

 

 

你可能感兴趣的:(windows,login,database,server,go)