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

  • Home
  • SEARCH
  • 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 6175777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:58:59+00:00 2026-05-23T23:58:59+00:00

I create database "Test" in folder "d:\test". Database files are "d:\test\Test.mdf" and "d:\Test\Test_log.ldf". I

  • 0

I create database "Test" in folder "d:\test". Database files are "d:\test\Test.mdf" and "d:\Test\Test_log.ldf". I detach database from MS SQL Server 2008 R2, copy all files to new folder ("d:\test_new"), delete log file ("d:\test_new\Test_log.ldf"), and try to attach database again from new location. When I use SQL Server Management Studio, and choose "d:\test_new\Test.mdf" file, it determines that log file is located in "d:\test\Test_log.ldf" (old location). How can I attach this database with rebuilding log in new location? Just imagine, that I cannot copy ldf file again to new location, and that it is still available there, so SQL Server see it anyway. I want to say to SQL Server – "please, forget that log file, and create new log file here". It’s be better if you help me with T-SQL script, but if it will be steps in Management studio – I will convert it to script myself.

What I had tried already:

1.

CREATE DATABASE [test] 
ON ( FILENAME = N'D:\test_new\test.mdf' ) 
FOR ATTACH_REBUILD_LOG

attaches log file from old location (FOR ATTACH – the same)

2.

CREATE DATABASE [test] 
ON ( FILENAME = N'D:\test_new\test.mdf' ) 
LOG ON ( FILENAME = N'D:\test_new\test_log.ldf' )
FOR ATTACH_REBUILD_LOG

returns an error: Unable to open the physical file "D:\test_new\test_log.ldf". Operating system error 2: "2(File not found.)".

3.

 sp_attach_db and sp_attach_single_file_db

was tried too. And I even had checked their source codes – they just create dynamic SQL and call CREATE DATABASE … FOR ATTACH statement.

The question is slowly changed to: "Is it possible?"

UPDATE

Well, it looks like it’s not possible with current versions of SQL Server. If anybody knows a way to do it – please, I will be very pleased to know it too!

  • 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-23T23:58:59+00:00Added an answer on May 23, 2026 at 11:58 pm

    Edit2: To my knowledge, it is not possible for SQL Server to recreate a log file. It can shrink the ldf, but not create it when only the mdf exists.


    When you copy your files from d:\test\ to d:\test_new\, do not delete the d:\test_new\Test_log.ldf.

    Leave the log file there, because you cannot reattach the new DB without that log file. Afterwards, you can shrink that log to a minimum size.

    So, to synthesize:

    1. Copy your files from d:\test\ to d:\test_new\ and leave the log
      file there.
    2. Run your create database script that you posted in your question (point 2).
    3. Run the following script to shrink the log to a minimum size

    .

    USE test
    GO
    DBCC SHRINKFILE(logicalFileName, 1)
    GO
    

    To find out what logicalFileName is, run sp_helpfile, that will give you the logical file name for your log file:

    USE test
    GO
    EXEC sp_helpfile
    GO
    

    more info here

    Edit:
    I think you need first to detach the test database from the old location:

    (You might create a script that does it all, from the following commands)

    C:\> osql -E 
    1> sp_detach_db 'test'
    2> go 
    3> quit 
    C:\> 
    

    Then copy the files to the new location.

    C:\> copy d:\test\* d:\test_new\*
    

    Next, attach the test DB to the new path location:

    C:\> osql -E 
    1> sp_attach_db @dbname = N'test', @filename1 = N'd:\test_new\Test.mdf', @filename2 = N'd:\test_new\Test_log.ldf' 
    2> go 
    3> quit 
    C:\> 
    

    to test if the new database was successfully attached:

    C:\> osql -E 
    1> use test
    2> go 
    3> quit 
    C:\> 
    

    If there are no errors after the go command, then all is ok

    Hope this helps

    Microsoft article on how to move files

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

Sidebar

Related Questions

Is there an automatic way in SQL Server 2005 to create a database from
How do you create a database from an Entity Data Model. So I created
How to create a database using T SQL script on a specified location? Let's
I'm working in SQL Server 2008, and I'm trying to select into a temp
How would you create a database in Microsoft Access that is searchable only by
I need to create a database table to store different changelog/auditing (when something was
I would like to create a database backed interactive AJAX webapp which has a
I am using NHibernate's SchemaExport to create my database schema. I have 1 legacy
When I am testing my DAL I need to create some database entities before
So I have the following user defined type in my oracle database: CREATE OR

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.