User name problem of MSSQL database migration _mssql

Source: Internet
Author: User
Tags mssql
However, at this point user a exists in database A, and although new MSSQL can create user A, it cannot assign permissions to user A. Create a new User B bar, and the table and other information created by user A cannot be accessed again. Here's how to find the solution:
Run the following script on the source SQL Server. This script can create two stored procedures named Sp_hexadecimal and sp_help_revlogin in the master database. Proceed to step 2nd after the completion process is created.
Note: The following procedure depends on the SQL Server system table. The structure of these tables may change between different versions of SQL Server, and do not select directly from the system tables.
-----Begin Script, Create sp_help_revlogin procedure-----
Copy Code code as follows:

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 (256) OUTPUT
As
DECLARE @charvalue varchar (256)
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 @xstatus int
DECLARE @binpwd varbinary (256)
DECLARE @txtpwd sysname
DECLARE @tmpstr varchar (256)
DECLARE @SID_varbinary varbinary (85)
DECLARE @SID_string varchar (256)
IF (@login_name is NULL)
DECLARE login_curs CURSOR for
SELECT SID, Name, xstatus, password from master. sysxlogins
WHERE Srvid is NULL and name <> ' sa '
ELSE
DECLARE login_curs CURSOR for
SELECT SID, Name, xstatus, password from master. sysxlogins
WHERE Srvid is NULL and name = @login_name
OPEN login_curs
FETCH NEXT from login_curs into @SID_varbinary, @name, @xstatus, @binpwd
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 '
PRINT ' DECLARE @pwd sysname '
while (@ @fetch_status <>-1)
BEGIN
IF (@ @fetch_status <>-2)
BEGIN
PRINT '
SET @tmpstr = '--Login: ' + @name
PRINT @tmpstr
IF (@xstatus & 4) = 4
BEGIN--NT authenticated Account/group
IF (@xstatus & 1) = 1
BEGIN--NT login is denied access
SET @tmpstr = ' EXEC master. Sp_denylogin ' + @name + '
PRINT @tmpstr
End
ELSE BEGIN--NT login has access
SET @tmpstr = ' EXEC master. sp_grantlogin ' + @name + '
PRINT @tmpstr
End
End
ELSE BEGIN--SQL Server authentication
IF (@binpwd is not NULL)
BEGIN--non-null password
EXEC sp_hexadecimal @binpwd, @txtpwd out
IF (@xstatus & 2048) = 2048
Set @tmpstr = ' Set @pwd = CONVERT (varchar (256), ' + @txtpwd + ') '
ELSE
Set @tmpstr = ' Set @pwd = CONVERT (varbinary (256), ' + @txtpwd + ') '
PRINT @tmpstr
EXEC sp_hexadecimal @SID_varbinary, @SID_string out
SET @tmpstr = ' EXEC master. Sp_addlogin ' + @name
+ ', @pwd, @sid = ' + @SID_string + ', @encryptopt = '
End
ELSE BEGIN
--Null Password
EXEC sp_hexadecimal @SID_varbinary, @SID_string out
SET @tmpstr = ' EXEC master. Sp_addlogin ' + @name
+ ', NULL, @sid = ' + @SID_string + ', @encryptopt = '
End
IF (@xstatus & 2048) = 2048
--Login Upgraded from 6.5
SET @tmpstr = @tmpstr + "' Skip_encryption_old '"
ELSE
SET @tmpstr = @tmpstr + "' skip_encryption '"
PRINT @tmpstr
End
End
FETCH NEXT from login_curs into @SID_varbinary, @name, @xstatus, @binpwd
End
Close Login_curs
DEALLOCATE login_curs
return 0
Go

-----End Script-----
2. After you create the sp_help_revlogin stored procedure, run the Sp_help_revlogin procedure from the Query Analyzer on the source server. Sp_help_revlogin stored procedures can be used for both SQL Server 7.0 and SQL Server 2000. The output of the sp_help_revlogin stored procedure is a logon script that creates a login with the original SID and password. Save the output and paste it into the Query Analyzer on the destination SQL Server and run it. For example: EXEC master. Sp_help_revlogin
Copy Code code as follows:
Sp_defaultdb ' Cyiyun ', ' Db_wayup '

After the 1th step, when you run Sp_help_revlogin on the source server, the SQL that creates the user data is generated, for example:
Copy Code code as follows:

/* Sp_help_revlogin Script
* * Generated 2009 1:40pm on WORKGROU-B1XTVC * *
DECLARE @pwd sysname
--Login:hxtest
SET @pwd = CONVERT (varbinary (256), 0X0100CF4E7D342B359438E4BCCA72E6C83F44FCCF30C8016286DE2B359438E4BCCA72E6C83F44FCCF30C8016286DE)
EXEC Master.. Sp_addlogin ' 520web ', @pwd, @sid = 0X1738BB6AD0CD24498F67FB5589E8EDCB, @encryptopt = ' skip_encryption '
......

You can solve this problem by running the paragraph directly on the new server or by finding the appropriate username.

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.