How to attach a database to a lost or damaged NDF File

Source: Internet
Author: User
In the forum, we can see that the NDF file is lost and there is no backup, so we cannot attach the database. I have also seen many answers on the Internet that the append fails without NDF. In fact, I have tested it myself, even if there is no NDF, it can be attached successfully. However, if there are conditions, the lost NDF file does not belong to Primaryfilegroup and SQLSer

In the forum, we can see that the NDF file is lost and there is no backup, so we cannot attach the database. I have also seen many answers on the Internet that the append fails without NDF. In fact, I have tested it myself, even if there is no NDF, it can be attached successfully. However, if there are conditions, the lost NDF file does not belong to the Primary file group and the SQL Ser

In the forum, we can see that the NDF file is lost and there is no backup, so we cannot attach the database. I have also seen many answers on the Internet that the append fails without NDF.

In fact, I have tested it myself, even if there is no NDF, it can be attached successfully. However, if possible, the lost NDF file does not belong to the Primary file group and the SQL Server is the Enterprise Edition.

The following is my test:

1. -- create a database

Create database [test1] CONTAINMENT = NONE ON PRIMARY

(NAME = N 'test1', FILENAME = n'c: \ data \ test1.mdf ', SIZE = 5120KB, MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ),

FILEGROUP [New]

(NAME = n' new', FILENAME = n' C: \ data \ new. ndf', SIZE = 5120KB, MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB)

LOG ON

(NAME = n'test1 _ log', FILENAME = n'c: \ data \ test1_log.ldf', SIZE = 1024KB, MAXSIZE = 2048 GB, FILEGROWTH = 10%)

GO

2. -- create two tables on the primary file group

Create tabletest (namevarchar (10) ON [PRIMARY]

Create tabletest2 (namevarchar (10) ON [PRIMARY]

-- Create a table on the NDF

Create tabletest1 (namevarchar (10) onnew

3. -- Insert 10 data records

Insert into testvalues ('kevin ')

Go 10

Insert into test1values ('kevin ')

Go 10

Insert into test2values ('kevin ')

Go 10

4. -- Detach Database

USE [master]

GO

EXEC master. dbo. sp_detach_db @ dbname = N 'test1'

GO

5. Copy the MDF, NDF, and LDF files to other folders.

6. Follow the steps in step 1 to create a database with the same name

-- Offline database

Use master

Go

Alter databasetest1setoffline

7. overwrite the MDF and LDF files backed up in step 1 to the existing database MDF and LDF.

8. Run the problematic NDF file Offline.

Alter databasetest1modifyFILE (name = new, offline)

9. Online

Alter databasetest1setonline

Now we can see that all three tables exist.

However, when accessing the test1 table, the following error occurs:

Msg 8653, Level 16, State 1, Line 1

The queryprocessor is unable to produce a plan for the table or view 'test1' because thetable resides in a filegroup which is not online.

Because the Filegroup of the table is offline, it cannot be accessed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.