Create new DB with existing filepath

Maria Benich 0 Reputation points
2023-12-26T08:32:07.4533333+00:00

I want to create new DB that use the filepath the same as used for another DB, is it possible?

I get the error:

Unable to open the physical file "E:\Test\SQL\Data\TestDB.mdf". Operating system error 32: "32(The process cannot access the file because it is being used by another process.)".

Msg 1802, Level 16, State 7, Line 14

CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

This is the code:

CREATE DATABASE [TestDBNew]
 ON  PRIMARY 
( NAME = N'TestDB', FILENAME = N'E:\Test\SQL\Data\TestDB.mdf' , SIZE = 7680000KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB ), 
 FILEGROUP [TestDBFilestream] CONTAINS FILESTREAM  DEFAULT
( NAME = N'TestDBFS_01', FILENAME = N'E:\Test\SQL\Data\TestDBFS_01' , MAXSIZE = UNLIMITED)
 LOG ON 
( NAME = N'TestDB_log', FILENAME = N'E:\Test\SQL\Logs\TestDB_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )

  FOR ATTACH
GO

SQL Server | Other
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Olaf Helper 47,611 Reputation points
    2023-12-27T07:09:11.2833333+00:00

    SQL Server hold exclusive locks on all (database) file, you can not share file for other databases.

    0 comments No comments

  2. Anonymous
    2023-12-27T02:30:26.45+00:00

    Hi @Maria Benich

    The error message is already obvious. You cannot create a new DB with the same file path as an existing database.

    Best regards,

    Percy Tang

    0 comments No comments

  3. Erland Sommarskog 132.4K Reputation points MVP Volunteer Moderator
    2023-12-26T09:41:04.5666667+00:00

    If there already is a file E:\Test\SQL\Data\TestDB.mdf, you cannot create a new database using that file, even less so if the existing file is attached to another SQL Server instance, which seems to be the case here.

    Is there any particular reason you want to create the database with this very path that is already in use?

    0 comments No comments

Your answer

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