Reseting Logins after detach and attach

  • After sp_detachdb and sp_attachdb what is the procedure names I need to run to re-set my logins?

  • sp_change_users_login 'Auto_fix',your user name, NULL

  • use userdb -- User-db

    go

    print 'print @@servername + '' / '' + db_name()'

    print 'go'

    go

    declare @username varchar(128)

    declare @Musername varchar(128)

    declare @IsNtName bit

    declare @sql_stmt varchar(500)

    --cursor returns with names of each username to be tied to its respective

    DECLARE user_cursor CURSOR FOR

    SELECT su.name as Name, msu.name as MasterName , su.isntname

    FROM sysusers su

    left join master.dbo.sysxlogins msu

    on upper(su.name) = upper(msu.name)

    WHERE su.sid > 0x00

    ORDER BY Name

    --for each user:

    OPEN user_cursor

    FETCH NEXT FROM user_cursor INTO @username, @Musername, @IsNtName

    WHILE @@FETCH_STATUS = 0

    BEGIN

    IF @username NOT IN ('dbo', 'list of names you want to avoid')

    BEGIN

    if @Musername is null

    begin

    if @IsNtName = 1

    begin

    print 'if not exists (select * from master.dbo.syslogins where loginname = N''NtDomain**\' + @username + ''')'

    print ' begin '

    print 'exec sp_grantlogin N''NtDomain**\' + @username + ''''

    print 'exec sp_defaultdb N''NtDomein**\' + + @username + ''', N'''+ db_name() + ''''

    print ' end'

    set @sql_stmt = '--Windows account '

    end

    else

    begin

    SELECT @sql_stmt = 'sp_change_users_login @Action = ''Auto_Fix'',@UserNamePattern = ''' + @username + ''''

    end

    end

    else

    begin

    SELECT @sql_stmt = 'sp_change_users_login @Action = ''Update_One'',@UserNamePattern = ''' + @username + ''', @LoginName = ''' + @username + ''''

    end

    PRINT @sql_stmt

    print 'go'

    print '--*** opgelet : exec commented !!! ***'

    --EXECUTE (@sql_stmt)

    END

    FETCH NEXT FROM user_cursor INTO @username, @Musername, @IsNtName

    END --of table-cursor loop

    --clean up

    CLOSE user_cursor

    DEALLOCATE user_cursor

    Print '** endUser-sync **'

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • Only needed if you're attaching to a different server and only if you have SQL logins (unless its a different domain). If you're going to do this a lot it's easier to change the SID in sysxlogins in master to match on both servers.

    Andy

    http://www.sqlservercentral.com/columnists/awarren/

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply