SQL Server always on switch host login and user cannot match solution

Source: Internet
Author: User

SQL Server always on switch host after login and user cannot match, looked for a long time no solution was found, and later asked Microsoft, said that because login in the login host SQL Server instance SID, because the server before the SQL The SID of the server instance is different, resulting in an unmatched match.

The solution is as follows: first create login and user in one host, run out the login script with SID with the following two SP, run the footsteps on the secondary server separately, generate login with SID, so all server sql The SID for login in the server instance is the same, switch auto-match OK again.

1. The SP calls the second SP and runs the login step with SID

Use [master]
GO
/****** object:storedprocedure [dbo].    [Sp_help_revlogin] Script date:8/4/2014 2:26:07 PM ******/
SET ANSI_NULLS on
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo]. [sp_help_revlogin] @login_name sysname = NULL as
DECLARE @name sysname
DECLARE @type varchar (1)
DECLARE @hasaccess int
DECLARE @denylogin int
DECLARE @is_disabled int
DECLARE @PWD_varbinary varbinary (256)
DECLARE @PWD_string varchar (514)
DECLARE @SID_varbinary varbinary (85)
DECLARE @SID_string varchar (514)
DECLARE @tmpstr varchar (1024)
DECLARE @is_policy_checked varchar (3)
DECLARE @is_expiration_checked varchar (3)

DECLARE @defaultdb sysname

IF (@login_name is NULL)
DECLARE login_curs CURSOR for

SELECT p.sid, P.name, P.type, p.is_disabled, P.default_database_name, l.hasaccess, L.denylogin from
Sys.server_principals p left JOIN sys.syslogins l
On (l.name = p.name) WHERE p.type in (' S ', ' G ', ' U ') and p.name <> ' sa '
ELSE
DECLARE login_curs CURSOR for


SELECT p.sid, P.name, P.type, p.is_disabled, P.default_database_name, l.hasaccess, L.denylogin from
Sys.server_principals p left JOIN sys.syslogins l
On (l.name = p.name) WHERE p.type in (' S ', ' G ', ' U ') and p.name = @login_name
OPEN login_curs

FETCH NEXT from login_curs to @SID_varbinary, @name, @type, @is_disabled, @defaultdb, @hasaccess, @denylogin
IF (@ @fetch_status =-1)
BEGIN
PRINT ' No login (s) found. '
CLOSE login_curs
DEALLOCATE login_curs
RETURN-1
END
SET @tmpstr = '/* sp_help_revlogin script '
PRINT @tmpstr
SET @tmpstr = ' * * Generated ' + CONVERT (varchar, GETDATE ()) + ' on ' + @ @SERVERNAME + ' * * '
PRINT @tmpstr
PRINT '
while (@ @fetch_status <>-1)
BEGIN
IF (@ @fetch_status <>-2)
BEGIN
PRINT '
SET @tmpstr = '--Login: ' + @name
PRINT @tmpstr
IF (@type in (' G ', ' U '))
BEGIN--NT authenticated Account/group

SET @tmpstr = ' CREATE LOGIN ' + QUOTENAME (@name) + ' from WINDOWS with default_database = [' + @defaultdb + '] '
END
ELSE BEGIN-SQL Server Authentication
--Obtain password and SID
SET @PWD_varbinary = CAST (LoginProperty (@name, ' PasswordHash ') as varbinary (256))
EXEC sp_hexadecimal @PWD_varbinary, @PWD_string out
EXEC sp_hexadecimal @SID_varbinary, @SID_string out

--Obtain Password policy state
SELECT @is_policy_checked = Case is_policy_checked If 1 Then ' OFF ' if 0 Then ' OFF ' ELSE NULL END from Sys.sql_logins WH ERE name = @name
SELECT @is_expiration_checked = Case is_expiration_checked If 1 then ' on ' while 0 Then ' OFF ' ELSE NULL END from sys.sql_l Ogins WHERE name = @name

SET @tmpstr = ' CREATE LOGIN ' + QUOTENAME (@name) + ' with PASSWORD = ' + @PWD_string + ' HASHED, SID = ' + ' + @SID_string + ', default_database = [' + @defaultdb + '] '

IF (@is_policy_checked is not NULL)
BEGIN
SET @tmpstr = @tmpstr + ', Check_policy = ' + @is_policy_checked
END
IF (@is_expiration_checked is not NULL)
BEGIN
SET @tmpstr = @tmpstr + ', check_expiration = ' + @is_expiration_checked
END
END
IF (@denylogin = 1)
BEGIN--Login is denied access
SET @tmpstr = @tmpstr + '; DENY CONNECT SQL to ' + QUOTENAME (@name)
END
ELSE IF (@hasaccess = 0)
BEGIN--Login exists but does not has access
SET @tmpstr = @tmpstr + '; REVOKE CONNECT SQL to ' + QUOTENAME (@name)
END
IF (@is_disabled = 1)
BEGIN--Login is disabled
SET @tmpstr = @tmpstr + '; ALTER LOGIN ' + QUOTENAME (@name) + ' DISABLE '
END
PRINT @tmpstr
END

FETCH NEXT from login_curs to @SID_varbinary, @name, @type, @is_disabled, @defaultdb, @hasaccess, @denylogin
END
CLOSE login_curs
DEALLOCATE login_curs
RETURN 0

2. Called SP

Use [master]
GO
/****** object:storedprocedure [dbo].    [Sp_hexadecimal] Script date:8/4/2014 2:26:50 PM ******/
SET ANSI_NULLS on
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo]. [Sp_hexadecimal]
@binvalue varbinary (256),
@hexvalue varchar (514) OUTPUT
As
DECLARE @charvalue varchar (514)
DECLARE @i int
DECLARE @length int
DECLARE @hexstring Char (16)
SELECT @charvalue = ' 0x '
SELECT @i = 1
SELECT @length = datalength (@binvalue)
SELECT @hexstring = ' 0123456789ABCDEF '
while (@i <= @length)
BEGIN
DECLARE @tempint int
DECLARE @firstint int
DECLARE @secondint int
SELECT @tempint = CONVERT (int, SUBSTRING (@binvalue, @i,1))
SELECT @firstint = Floor (@tempint/16)
SELECT @secondint = @tempint-(@firstint *16)
SELECT @charvalue = @charvalue +
SUBSTRING (@hexstring, @firstint +1, 1) +
SUBSTRING (@hexstring, @secondint +1, 1)
SELECT @i = @i + 1
END

SELECT @hexvalue = @charvalue

SQL Server always on switch host login and user cannot match solution

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.