How to transfer logins and passwords between instances of SQL Server

Source: Internet
Author: User

Use master
GO
IF object_id (' sp_hexadecimal ') is not NULL
DROP PROCEDURE Sp_hexadecimal
GO
CREATE PROCEDURE 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
GO
 
if object_id  (' sp_help_revlogin ')  is not null
  drop procedure sp_help_revlogin
GO
CREATE  procedure 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  
declare  @PWD_string   varchar  (514)
declare  @SID_varbinary   varbinary 
declare  @SID_string  varchar  (514)
declare  @tmpstr    varchar  (1024x768)
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
GO

EXEC sp_help_revlogin


Http://www.qqworker.com/post/31.html

How to transfer logins and passwords between instances of SQL Server

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.