Attaching a database and migrating users

Source: Internet
Author: User

A website database is created by a user, such as myuser. Because the database needs to run on another server, I directly add the database to the database of the new server by appending the database. It also shows that all tables, stored procedures, and views belong to the user myuser, but the user is not visible in the security --> logon. Add the user again, and the system prompts that the user already exists, however, this user cannot be connected. After that, we searched the internet to solve this problem by migrating users.

1. Run the following script on the source SQL Server. This script creates two stored procedures in the master database: sp_hexadecimal and sp_help_revlogin. Proceed to Step 1 after the process is created.
----- Begin script, create sp_help_revlogin procedure -----

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
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
Select Sid, name, xstatus, password from Master .. sysxlogins
Where srvid is null and name <> 'sa'
Else
Declare login_curs cursor
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 'Clare @ 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 creating the sp_help_revlogin stored procedure, run the sp_help_revlogin process 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 logon with the original Sid and password. Save the output, paste it to the query analyzer on the target SQL Server, and run it. For example:
Exec master.. sp_help_revlogin

Reference: how to transfer logon and password between SQL Server instances

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.