Home Forums SQL Server 2012 SQL Server 2012 - T-SQL Restore DB from PROD to Lower Environments - Create Identical Symmetric Keys on Two Servers RE: Restore DB from PROD to Lower Environments - Create Identical Symmetric Keys on Two Servers

  • UPDATE ON THIS RESTORE ISSUE --- HOWEVER, I think by getting the SQL SERVER Service Key and Master Key correct the restore worked correctly as well. I will need to test this theory and will return with results. Bust as gurus know, there is work to be done.

    I had an old DB GURU friend provide a script and instead of all this code all I had to run to restore the DB was as follows, but I still think I had to have the KEYS CORRECT. ALL IS WORKING ----

    -- SET Staging to Single User Mode to be able to RESTORE DB---

    -- STEP 1 (Works No Problems Here)

    USE master;

    GO

    ALTER DATABASE Staging

    SET SINGLE_USER

    WITH ROLLBACK IMMEDIATE;

    GO

    RESTORE DATABASE Staging

    FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\MyDBRestore.bak' ;

    GO

    DB GURU CODE THAT REALIGNS IDs

    DECLARE

    @username varchar(25)

    DECLARE fixusers CURSOR FAST_FORWARD FOR

    SELECT UserName = [name] FROM sysusers

    WHERE issqluser = 1 AND (sid IS NOT NULL AND sid <> 0x0)

    and suser_sname(sid) IS NULL

    ORDER BY [name]

    OPEN fixusers

    FETCH FROM fixusers INTO @username

    WHILE @@FETCH_STATUS = 0

    BEGIN

    BEGIN TRY

    EXEC sp_change_users_login 'update_one', @username, @username

    END TRY

    BEGIN CATCH

    PRINT 'User ' + @username + ' is a schema, windows login, or a SQL login that does not exist at the instance level. Moving on to next user.'

    END CATCH

    FETCH NEXT FROM fixusers INTO @username

    END

    CLOSE fixusers

    DEALLOCATE fixusers

    PRINT 'Remapping complete.'