How to change the data file type of a database

Avyayah 1,291 Reputation points
2021-04-05T22:33:57.837+00:00

During the process of rename the datafile file one of the datafile file type is 'File' instead of 'SQL Server Database Primary Data File'. Is there anyway we can convert the 'data file type to SQL Server Database Primary Data File?
84587-datfilerename.jpg

SQL Server Other
0 comments No comments
{count} votes

Accepted answer
  1. CathyJi-MSFT 22,396 Reputation points Microsoft External Staff
    2021-04-06T02:23:03.783+00:00

    Hi @Avyayah ,

    Did you change the File Extension for a Data File? We recommends you use MDF (Primary Data File), NDF (Secondary Data File - this is optional) for SQL server data files.
    84658-screenshot-2021-04-06-103513.jpg

    Below is a test in my environment, we need to change the File Extension to MDF for the SQL Data File. Then the data file type is changed to 'SQL Server Database Primary Data File'.

    1. Set database offline, then change the File Extension for database data file. ALTER DATABASE [test2] SET OFFLINE
      GO
      ALTER DATABASE [test2]   
      MODIFY FILE (name = 'test2', filename = 'C:\Program Files\Microsoft SQL Server\MSSQL15.SQL2019\MSSQL\DATA\test2.data')  
      GO  
      
    2. Change the File Extension for database data file by renaming data file

    84793-screenshot-2021-04-06-101452.jpg

    3 Set database online.

    ALTER DATABASE [test2] SET ONLINE  
    GO  
    

    When you want to change the data file type to 'SQL Server Database Primary Data File'. Please follow below steps.

    1. Set database offline, then change the File Extension for database data file. ALTER DATABASE [test2] SET OFFLINE
      GO
      ALTER DATABASE [test2]   
      MODIFY FILE (name = 'test2', filename = 'C:\Program Files\Microsoft SQL Server\MSSQL15.SQL2019\MSSQL\DATA\test2.mdf')  
      GO  
      
    2. Change the File Extension for database data file by renaming data file 84675-screenshot-2021-04-06-101913.jpg

    3 Set database online.

    ALTER DATABASE [test2] SET ONLINE  
    GO  
    

    If the response is helpful, please click "Accept Answer" and upvote it, thank you. 2: /api/attachments/84628-screenshot-2021-04-06-101452.jpg?platform=QnA

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.