Migrate Logins From a Copy of the Master Database

  • Hello Everyone

    I am working on a Login migration issue. I have the Master database from the original database, that I have attached as a User Database, call it OriginalMaster. I also renamed the mdf and ldf files, before attaching, just in case. I want to select the logins from the OriginalMaster, and using sp_help_revlogin script all the Logins out. I know there were many logins in that database. But what I am seeing, are all the Logins from the Current Master database. Which are only 7 Logins.

    How can I select the logins, or script out the Logins from the OriginalMaster database?

    Thank You in advance for all your time, assistance and patience.

    Andrew SQLDBA

  • Very odd situation, but this script will create login scripts from existing logins in ACTUAL master. The only question is can you access the tables the script hits from a restored copy of master. Please let us know if you can. Note that this script will keep the SID, allowing you to make logins that can be "reassociated" with database users on other machines that have user databases restored to them using sp_change_users_login.

    SELECT

    'create login [' + p.name + '] ' +

    case when p.type in('U','G') then 'from windows ' else '' end +

    'with ' +

    case when p.type = 'S' then 'password = ' + master.sys.fn_varbintohexstr(l.password_hash) + ' hashed, ' +

    'sid = ' + master.sys.fn_varbintohexstr(l.sid) +

    ', check_expiration = ' + case when l.is_expiration_checked > 0 then 'ON, ' else 'OFF, ' end +

    'check_policy = ' + case when l.is_policy_checked > 0 then 'ON, ' else 'OFF, ' end +

    case when l.credential_id > 0 then 'credential = ' + c.name + ', ' else '' end

    else '' end +

    'default_database = ' + p.default_database_name +

    case when len(p.default_language_name) > 0 then ', default_language = ' + p.default_language_name else '' end

    FROM sys.server_principals p

    LEFT JOIN sys.sql_logins l ON p.principal_id = l.principal_id

    LEFT JOIN sys.credentials c ON l.credential_id = c.credential_id

    WHERE p.type in('S','U','G')

    AND p.name <> 'sa'

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

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

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