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 |
ravilobo
Master Smack Fu Yak Hacker
1184 Posts |
Posted - 2004-05-14 : 09:42:19
|
How to migrate the access rights with the db? Since logins are on the server level...------------------------I think, therefore I am |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2004-05-14 : 13:37:02
|
Migrate them to where? When you restore a database on another server, permissions come across.I've got a stored procedure that grants permissions. Search the forums for isp_Grant_Permissions. It grants EXEC on all stored procs. It wouldn't help migrate your permissions, but if all you want is EXEC on stored procs like you should have, then this would be handy.Tara |
 |
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-05-14 : 14:04:54
|
If you are moving a database to another server, you will need to recreate the logins. You can take a look at the scripts below and possibly modify them. I use them for server migrations to migrate logins. If you run this after you restore or attach the moved databases, it sets the logins for you and you're ready to go.SET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS ON GOALTER 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 = @charvalueGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOCREATE PROCEDURE sp_help_revlogin @login_name sysname = NULL AS--Declare needed variables.DECLARE @name sysname,@xstatus int,@binpwd varbinary (256),@txtpwd sysname,@tmpstr varchar (256),@SID_varbinary varbinary(85),@SID_string varchar(256),@dbname varchar(255)--Determine whether to process one login or all. Set up cursor accordingly.IF (@login_name IS NULL)BEGINDECLARE login_curs CURSOR FOR SELECT sxl.sid, sxl.name, sxl.xstatus, sxl.password,sd.name AS dbnameFROM master..sysxlogins sxlINNER JOIN master..sysdatabases sd ON sxl.dbid = sd.dbidWHERE sxl.srvid IS NULL AND sxl.name <> 'sa'ENDELSEBEGINDECLARE login_curs CURSOR FOR SELECT sxl.sid, sxl.name, sxl.xstatus, sxl.password,sd.name AS dbnameFROM master..sysxlogins sxlINNER JOIN master..sysdatabases sd ON sxl.dbid = sd.dbidWHERE sxl.srvid IS NULL AND sxl.name <> @login_nameENDOPEN login_curs FETCH NEXT FROM login_curs INTO @SID_varbinary, @name, @xstatus, @binpwd,@dbname--If no logins found, exit the procedure.IF (@@fetch_status = -1)BEGINPRINT 'No login(s) found.'CLOSE login_curs DEALLOCATE login_curs RETURN -1ENDSELECT @tmpstr = '/* sp_help_revlogin script ' PRINT @tmpstrSELECT @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 @tmpstr IF (@xstatus & 4) = 4BEGIN -- NT authenticated account/groupIF (@xstatus & 1) = 1BEGIN -- NT login is denied accessSET @tmpstr = 'EXEC master..sp_denylogin ''' + @name + ''''PRINT @tmpstr ENDELSEBEGIN -- NT login has accessSET @tmpstr = 'EXEC master..sp_grantlogin ''' + @name + ''''PRINT @tmpstr ENDENDELSEBEGIN -- SQL Server authenticationIF (@binpwd IS NOT NULL)BEGIN -- Non-null passwordEXEC sp_hexadecimal @binpwd, @txtpwd OUTIF (@xstatus & 2048) = 2048BEGINSET @tmpstr = 'SET @pwd = CONVERT (varchar(256), ' + @txtpwd + ')'END ELSEBEGINSET @tmpstr = 'SET @pwd = CONVERT (varbinary(256), ' + @txtpwd + ')'END PRINT @tmpstrEXEC sp_hexadecimal @SID_varbinary,@SID_string OUTSET @tmpstr = 'EXEC master..sp_addlogin ''' + @name + ''', @pwd, @sid = ' + @SID_string + ', @encryptopt = 'ENDELSE BEGIN BEGIN-- Null passwordEXEC sp_hexadecimal @SID_varbinary,@SID_string OUTSET @tmpstr = 'EXEC master..sp_addlogin ''' + @name + ''', NULL, @sid = ' + @SID_string + ', @encryptopt = 'ENDENDIF (@xstatus & 2048) = 2048BEGIN-- login upgraded from 6.5SET @tmpstr = @tmpstr + '''skip_encryption_old''' PRINT @tmpstr ENDELSE BEGINSET @tmpstr = @tmpstr + '''skip_encryption'''PRINT @tmpstr ENDEND--Add the default database.SET @tmpstr = 'EXEC master..sp_defaultdb ''' + @name + ''',''' + @dbname + ''''PRINT @tmpstrENDFETCH NEXT FROM login_curs INTO @SID_varbinary, @name, @xstatus, @binpwd,@dbnameENDCLOSE login_curs DEALLOCATE login_curs RETURN 0GOMeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
ravilobo
Master Smack Fu Yak Hacker
1184 Posts |
Posted - 2004-05-17 : 03:53:19
|
Will the above script asign db userids to proper login? I heard that there is a mismatch of SID when migrating logins accross servers and pwd is assigned blank? What are orphan users?------------------------I think, therefore I am |
 |
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2004-05-17 : 08:52:13
|
Yes. This one was rewrote to assign the proper SID. Just make sure you run it AFTER the databases are restored.MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
Gradwohk
Starting Member
6 Posts |
Posted - 2004-08-17 : 08:21:51
|
marvellous!!!Many thanks!Karl |
 |
|
|
|
|
|
|