使用SUBST虚拟D盘创建SQL数据库遇错误3(找不到指定文件)求助
Let's break down why this is happening and how to fix it—this is a common gotcha with SUBST and Windows services like SQL Server.
The Root Cause
When you use SUBST to create a virtual drive, that mapping is only visible in your current user session. But SQL Server runs in Session 0, a separate non-interactive session used by Windows services. That means the SQL Server process can't see your SUBST-created D:\ at all, hence the "system cannot find the file specified" error—even if you set permissions correctly on the underlying folder.
Solutions to Try
1. Use a System-Wide Mount (Replace SUBST with mountvol)
Instead of SUBST, use the mountvol command to create a persistent, system-level mount point. This will be visible to all sessions, including Session 0 where SQL Server runs.
Run Command Prompt as Administrator and execute:
mountvol D: C:\Your\Actual\Physical\Directory\Path
Replace C:\Your\Actual\Physical\Directory\Path with the real folder you're mapping to. To remove the mount later, use mountvol D: /D.
2. Test with the Physical Path First
Rule out any permission issues by creating the database directly using the physical folder path instead of the virtual drive. Run this SQL query:
CREATE DATABASE YourDBName ON PRIMARY (NAME = YourDB_Data, FILENAME = 'C:\Your\Actual\Physical\Directory\Path\YourDB.mdf') LOG ON (NAME = YourDB_Log, FILENAME = 'C:\Your\Actual\Physical\Directory\Path\YourDB.ldf');
If this works, you can confirm the problem is definitely the SUBST drive's session visibility, not permissions.
3. Double-Check Permissions on the Physical Folder
Even though you granted full access, make sure you applied the permissions correctly:
- Navigate to the actual physical folder (not the virtual D:), right-click > Properties > Security tab.
- Add the SQL Server service account (usually
NT SERVICE\MSSQLSERVERif it's the default instance) and grant it Full Control over the folder and all subobjects. - Click "Advanced" to verify the permission is inherited by child items and applied correctly.
4. Avoid SUBST for SQL Server Paths
SUBST is great for user-level shortcuts, but it's not reliable for services that run in separate sessions. Stick to physical paths or system-level mounts with mountvol when working with SQL Server data files.
内容的提问来源于stack exchange,提问作者Amarnath

