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

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You are using trig, essentially, to convert between different representations… May 12, 2026 at 12:36 am
  • Editorial Team
    Editorial Team added an answer JFTR: I had to use TextSelection.Insert(...), but to also get… May 12, 2026 at 12:36 am
  • Editorial Team
    Editorial Team added an answer You should write : if (self.a != 0) and (self.b… May 12, 2026 at 12:36 am

Related Questions

I have a desktop (winforms) application that uses a Firebird database as a data
I have two database servers running SQL Server 2005 Enterprise that I want to
I am having our company websites (x8 total 500,000per month) on a 1/3 share
In standard php or source code based projects we easily keep all of the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.