Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
PatDeV
Posting Yak Master
197 Posts |
Posted - 2006-09-12 : 10:55:37
|
how can i script all the user and their permision from database to replicated database!! I don;t want to restore master or msdb for that!!Thanks |
|
Luis Martin
Yak Posting Veteran
54 Posts |
Posted - 2006-09-12 : 11:02:18
|
Try this one:CREATE PROCEDURE sp_help_revlogin @login_name sysname = NULL ASDECLARE @name sysnameDECLARE @xstatus intDECLARE @binpwd varbinary (256)DECLARE @txtpwd sysnameDECLARE @tmpstr varchar (256)DECLARE @SID_varbinary varbinary(85)DECLARE @SID_string varchar(256)IF (@login_name IS NULL)DECLARE login_curs CURSOR FORSELECT sid, name, xstatus, password FROM master..sysxloginsWHERE srvid IS NULL AND name <> 'sa'ELSEDECLARE login_curs CURSOR FORSELECT sid, name, xstatus, password FROM master..sysxloginsWHERE srvid IS NULL AND name = @login_nameOPEN login_cursFETCH NEXT FROM login_curs INTO @SID_varbinary, @name, @xstatus, @binpwdIF (@@fetch_status = -1)BEGINPRINT 'No login(s) found.'CLOSE login_cursDEALLOCATE login_cursRETURN -1ENDSET @tmpstr = '/* sp_help_revlogin script 'PRINT @tmpstrSET @tmpstr = '** Generated '+ CONVERT (varchar, GETDATE()) + ' on ' + @@SERVERNAME + ' */'PRINT @tmpstrPRINT ''PRINT 'DECLARE @pwd sysname'WHILE (@@fetch_status <> -1)BEGINIF (@@fetch_status <> -2)BEGINPRINT ''SET @tmpstr = '-- Login: ' + @namePRINT @tmpstrIF (@xstatus & 4) = 4BEGIN -- NT authenticated account/groupIF (@xstatus & 1) = 1BEGIN -- NT login is denied accessSET @tmpstr = 'EXEC master..sp_denylogin ''' + @name + ''''PRINT @tmpstrENDELSE BEGIN -- NT login has accessSET @tmpstr = 'EXEC master..sp_grantlogin ''' + @name + ''''PRINT @tmpstrENDENDELSE BEGIN -- SQL Server authenticationIF (@binpwd IS NOT NULL)BEGIN -- Non-null passwordEXEC sp_hexadecimal @binpwd, @txtpwd OUTIF (@xstatus & 2048) = 2048SET @tmpstr = 'SET @pwd = CONVERT (varchar(256), ' + @txtpwd + ')'ELSESET @tmpstr = 'SET @pwd = CONVERT (varbinary(256), ' + @txtpwd + ')'PRINT @tmpstrEXEC sp_hexadecimal @SID_varbinary,@SID_string OUTSET @tmpstr = 'EXEC master..sp_addlogin ''' + @name+ ''', @pwd, @sid = ' + @SID_string + ', @encryptopt = 'ENDELSE BEGIN-- Null passwordEXEC sp_hexadecimal @SID_varbinary,@SID_string OUTSET @tmpstr = 'EXEC master..sp_addlogin ''' + @name+ ''', NULL, @sid = ' + @SID_string + ', @encryptopt = 'ENDIF (@xstatus & 2048) = 2048-- login upgraded from 6.5SET @tmpstr = @tmpstr + '''skip_encryption_old'''ELSESET @tmpstr = @tmpstr + '''skip_encryption'''PRINT @tmpstrENDENDFETCH NEXT FROM login_curs INTO @SID_varbinary, @name, @xstatus, @binpwdENDCLOSE login_cursDEALLOCATE login_cursRETURN 0GO------------------------------------------------------------------------It also needs sp_hexadecimal:CREATE PROCEDURE sp_hexadecimal@binvalue varbinary(256),@hexvalue varchar(256) OUTPUTASDECLARE @charvalue varchar(256)DECLARE @i intDECLARE @length intDECLARE @hexstring char(16)SELECT @charvalue = '0x'SELECT @i = 1SELECT @length = DATALENGTH (@binvalue)SELECT @hexstring = '0123456789ABCDEF'WHILE (@i <= @length)BEGINDECLARE @tempint intDECLARE @firstint intDECLARE @secondint intSELECT @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 + 1ENDSELECT @hexvalue = @charvalueGOAlso transfer passwords from one server to other. |
 |
|
|
|
|
|
|