Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 233039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:04:03+00:00 2026-05-11T20:04:03+00:00

When we copy a database down from production, we make a backup of the

  • 0

When we copy a database down from production, we make a backup of the database, zip it up and copy the backup down. Then we have to restore using the SQL Server GUI, which involves navigating through several menus and windows. As far as I know, you can not do this with SQL Server’s built in stored procedures because you may not know the logical filename of the database (which is required to restore). So doing this via query consists of the following:

RESTORE FILELISTONLY
FROM DISK = 'C:\backup_of_production_database.bak'
GO

The above provides the logical file names from the backup file, you then have to use these logical names in the next query…

RESTORE DATABASE NewDevelopmentDatabase
FROM DISK = 'C:\backup_of_production_database.bak'
WITH MOVE 'YourMDFLogicalName' TO 'C:\mssql\data\DataYourMDFFile.mdf',
MOVE 'YourLDFLogicalName' TO 'C:\mssql\data\DataYourLDFFile.mdf'

As you can see this seems inefficient because you must manually enter the logical file names into the next query.

You can find my solution to this problem as an answer below.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:04:03+00:00Added an answer on May 11, 2026 at 8:04 pm

    The solution:

    Using various resources I came up with the below stored procedure that lets you cut down this restore into one step. I hope it proves as useful to others as it has been to myself.

    ALTER PROCEDURE [dbo].[sp_makedev] 
        @backupfile sysname,
        @newdatabase sysname
    AS
    BEGIN
    
    DECLARE @fname VARCHAR(200) 
    DECLARE @dirfile VARCHAR(300) 
    DECLARE @LogicalName NVARCHAR(128) 
    DECLARE @PhysicalName NVARCHAR(260) 
    DECLARE @type CHAR(1) 
    DECLARE @sql NVARCHAR(1000) 
    DECLARE @mdfFilePath  varchar(1000)
    DECLARE @ldfFilePath varchar(1000)
    
    CREATE TABLE #dbfiles( 
     LogicalName NVARCHAR(128) 
    ,PhysicalName NVARCHAR(260) 
    ,Type CHAR(1) 
    ,FileGroupName NVARCHAR(128) 
    ,Size numeric(20,0) 
    ,MaxSize numeric(20,0) 
    ,FileId INT 
    ,CreateLSN numeric(25,0) 
    ,DropLSN numeric(25,0) 
    ,UniqueId uniqueidentifier 
    ,ReadOnlyLSN numeric(25,0) 
    ,ReadWriteLSN numeric(25,0) 
    ,BackupSizeInBytes INT 
    ,SourceBlockSize INT 
    ,FilegroupId INT 
    ,LogGroupGUID uniqueidentifier 
    ,DifferentialBaseLSN numeric(25) 
    ,DifferentialBaseGUID uniqueidentifier 
    ,IsReadOnly INT 
    ,IsPresent INT 
    )
    
    set @mdfFilePath = ''c:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\data''
    set @ldfFilePath = ''c:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\data''
    set @sql = ''RESTORE DATABASE '' + @newdatabase + '' FROM DISK = '''''' + @backupfile + '''''' WITH MOVE '' 
    
    DECLARE dbfiles CURSOR FOR 
    SELECT LogicalName, PhysicalName, [type] FROM #dbfiles 
    
    INSERT #dbfiles 
    EXEC(''RESTORE FILELISTONLY FROM DISK = '''''' + @backupfile + '''''''') 
    
    OPEN dbfiles 
    FETCH NEXT FROM dbfiles INTO @LogicalName, @PhysicalName, @type 
    WHILE @@FETCH_STATUS = 0 
    BEGIN 
    IF @type = ''D'' 
        SET @sql = @sql + '''''''' + @LogicalName + '''''' TO '''''' + @mdfFilePath + ''\'' + @newdatabase  + ''.mdf'''', MOVE '' 
    ELSE IF @type = ''L'' 
        SET @sql = @sql + '''''''' + @LogicalName + '''''' TO '''''' +  @ldfFilePath + ''\'' + @newdatabase  + ''.ldf'''''' 
    
    FETCH NEXT FROM dbfiles INTO @LogicalName, @PhysicalName, @type 
    END 
    
    CLOSE dbfiles
    DEALLOCATE dbfiles
    EXEC(@SQL)
    END
    

    I’m sure a few things about this query can be improved, however I already wasted enough time just trying to come to this solution. Regardless I’d love to hear some feedback. I hope that others find this useful!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to copy an entire database from a SQL Server 2005 on my
What is the SQL command to copy a table from one database to another
I have taken a copy of a database home with me so I can
When I want to make a copy of a database, I always create a
I am trying to make a copy of a database to a new database
I want to copy a live production database into my local development database. Is
I want to automatically commit the database when we copy files from one branch
Basically, I'm trying to selectively copy a table from one database to another. I
I would like to create a copy of a database with approximately 40 InnoDB
I'm trying to store a lightly filtered copy of a database for offline reference,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.