Script to un-orphan users

  • In configuring our logshipping I'm trying to setup a scripted method for un-orphaning the users. This script seems to work, but I'd like to know if it is a correct way of doing things :

    DECLARE @myvar sysname

    DECLARE testcursor CURSOR FOR

    select rtrim(name) from sysusers where issqlrole = 0

    open testcursor

    FETCH NEXT FROM testcursor INTO @myvar

    While (@@fetch_status = 0)

    begin

    exec sp_change_users_login 'update_one', @myvar

    FETCH NEXT FROM testcursor INTO @myvar

    end

    close testcursor

    deallocate testcursor

  • Just try this also

    SELECT

    'EXEC sp_change_users_login ''Update_One'', ''' + su.name + ''', ''' + su.name + ''''

    FROM

    sysusers su

    LEFT JOIN master.dbo.syslogins sl ON su.sid = sl.sid

    WHERE

    uid > 3-- exclude public, dbo, guest, INFORMATION_SCHEMA

    AND uid <> gid-- exclude groups

    AND uid < 16384-- exclude database roles

    AND sl.sid IS NULL-- user not linked to a login

    ORDER BY

    su.name

Viewing 2 posts - 1 through 1 (of 1 total)

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