SQL 文件目录查找失败,出现操作系统错误 3(系统找不到指定的路径。)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36602930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:33:09  来源:igfitidea点击:

Directory lookup for the file failed with the operating system error 3 (The system cannot find the path specified.)

sqlsql-serverdatabasemigrationosql

提问by lukegf

I am trying to re-create a database (MyDB) from one SQL server (Source) to another one (Target). Sourceis located on my local machine and is SQL Server 2014. Targetis located on a remote machine and it's SQL Server 2012. Here are the steps I've taken:

我正在尝试从一个 SQL 服务器(Source)到另一个(Target)重新创建一个数据库(MyDB )。位于我的本地机器上,是 SQL Server 2014。目标位于远程机器上,它是 SQL Server 2012。以下是我采取的步骤:

  1. On my local machine I go to SQL Server Management studio, I right click on MyDB and go to Tasks--> Generate Scripts.
    1. There I select "Script entire database and all database objects".
    2. I click Next and on the next page, under Advanced, I select "Schema and data".
    3. That generates a SQL file (scripts.sql) that contains the definition for MyDB.
    4. Then I use the following osql command to re-create the database on Target:
  1. 在我的本地机器上,我转到 SQL Server Management Studio,右键单击 MyDB 并转到任务--> 生成脚本。
    1. 在那里我选择“编写整个数据库和所有数据库对象的脚本”。
    2. 我单击“下一步”,然后在下一页的“高级”下选择“架构和数据”。
    3. 这会生成一个包含 MyDB 定义的 SQL 文件 (scripts.sql)。
    4. 然后我使用以下 osql 命令在Target上重新创建数据库:

osql -S target -d master -E -i scripts.sql -o output.log

osql -S target -d master -E -i scripts.sql -o output.log

  1. After execution is finished I get this error in the log file "output.log":
  1. 执行完成后,我在日志文件“output.log”中收到此错误:

1> 2> 1> 2> 3> 4> 5> 6> 7> 8> Msg 5133, Level 16, State 1, Server Target, Line 2 Directory lookup for the file "C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\MyDB.mdf" failed with the operating system error 3(The system cannot find the path specified.). Msg 1802, Level 16, State 1, Server Target, Line 2 CREATE DATABASE failed. Some file names listed could not be created. Check related errors. 1> 2> Msg 5011, Level 14, State 5, Server Target, Line 1 User does not have permission to alter database 'MyDB', the database does not exist, or the database is not in a state that allows access checks. Msg 5069, Level 16, State 1, Server Target, Line 1 ALTER DATABASE statement failed.

1> 2> 1> 2> 3> 4> 5> 6> 7> 8> Msg 5133, Level 16, State 1, Server Target, Line 2 目录查找文件“C:\Program Files\Microsoft SQL Server\ MSSQL12.MSSQLSERVER\MSSQL\DATA\MyDB.mdf”失败,出现操作系统错误 3(系统找不到指定的路径。)。消息 1802,级别 16,状态 1,服务器目标,第 2 行创建数据库失败。无法创建列出的某些文件名。检查相关错误。1> 2> 消息 5011,级别 14,状态 5,服务器目标,第 1 行用户无权更改数据库“MyDB”,该数据库不存在,或该数据库未处于允许访问检查的状态。消息 5069,级别 16,状态 1,服务器目标,第 1 行 ALTER DATABASE 语句失败。

Here are the first few lines of "scripts.sql":

这是“scripts.sql”的前几行:

USE [master]
GO
/****** Object:  Database [MyDB]    Script Date: 4/12/2016 4:30:20 PM ******/
CREATE DATABASE [MyDB]
 CONTAINMENT = NONE
 ON  PRIMARY 
( NAME = N'MyDB', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\MyDB.mdf' , SIZE = 513024KB , MAXSIZE = UNLIMITED, FILEGROWTH = 262144KB )
 LOG ON 
( NAME = N'MyDB_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\DATA\MyDB_log.ldf' , SIZE = 1317504KB , MAXSIZE = 2048GB , FILEGROWTH = 131072KB )
GO
ALTER DATABASE [MyDB] SET COMPATIBILITY_LEVEL = 100
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [MyDB].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [MyDB] SET ANSI_NULL_DEFAULT ON

I do have the file MyDB.mdf at the location it's complaining about on Source, but not on Target. There is no directory "MSSQL12.MSSQLSERVER" on Target. How can I fix this?

我在Source上抱怨的位置确实有文件 MyDB.mdf ,但在Target上没有。Target上没有目录“MSSQL12.MSSQLSERVER” 。我怎样才能解决这个问题?

回答by lukegf

For those interested in the solution to this, the problem was that there was no "MSSQL12.MSSQLSERVER" directory on Targetbecause it's on a different version of SQL Server, namely 2012. What I had to do was create the directory manually and it started working after that.

对于那些对此解决方案感兴趣的人,问题在于Target上没有“MSSQL12.MSSQLSERVER”目录,因为它位于不同版本的 SQL Server 上,即 2012。我必须手动创建目录并启动在那之后工作。