--花了點(diǎn)時(shí)間搜羅了網(wǎng)上的相關(guān)文章,然后作了簡單匯總,以方便查閱
--1、在主庫上建立用戶并查詢出SID,用于輔庫上創(chuàng)建同步用戶
use tsdb
GO
select [sid] from sys.syslogins where name='user'
GO
use master
select [sid] from sys.syslogins where name='user'
或
SELECT * FROM [dbName]..sysusers
--2、在輔庫上創(chuàng)建同步用戶,使用主庫用戶的SID,以繼承訪問許可
CREATE LOGIN [user] WITH PASSWORD = 'password',
SID = 0x00000000000000000000000000000000, --從上面的查詢從獲取
DEFAULT_DATABASE = [tsdb], --默認(rèn)數(shù)據(jù)庫
CHECK_EXPIRATION = OFF, --強(qiáng)制密碼過期 關(guān)閉
CHECK_POLICY = OFF --強(qiáng)制密碼策略 關(guān)閉
--網(wǎng)上傳的兩位大神的腳本和導(dǎo)出語句,原本不改羅列如下,
--方法一
--先在主庫創(chuàng)建好用戶,并用以下腳本創(chuàng)建2個(gè)存儲過程(sp_help_revlogin和sp_hexadecimal)
--然后執(zhí)行sp_help_revlogin存儲過程,在結(jié)果里找到所需創(chuàng)建用戶的語句并復(fù)制該語句到副庫執(zhí)行以創(chuàng)建同步用戶
1.在主服務(wù)器添加自定義登陸用戶,比如TestUser
2.在主服務(wù)器執(zhí)行如下SQL,在master數(shù)據(jù)庫創(chuàng)建存儲過程sp_hexadecimal,sp_help_revlogin
創(chuàng)建sp_help_revlogin存儲過程
3.執(zhí)行sp_help_revlogin存儲過程,在得到的結(jié)果文本里,找到TestUser相關(guān)的那條創(chuàng)建用戶的腳本。
4.復(fù)制該腳本至其他副本數(shù)據(jù)庫執(zhí)行
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 (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