sql-server-SQLCMD Restore在Management Studio中有效,但在DOS提示符下不起作用

您知道为什么我的Restore命令在Management Studio 2008中运行时能正常运行,而在dos提示符下运行时却不能正常运行的想法吗?

下面显示的是从dos提示符下运行时的错误.

C:\>SQLCMD -s local\SQL2008 -d master -Q "RESTORE DATABASE [Sample.Db] FROM  DISK = N'C:\Sample.Db.bak' WITH  FILE = 1,  MOVE N'Sample.Db' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\Sample.Db.mdf',  MOVE N'Sample.Db_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\Sample.Db_log.ldf',  NOUNLOAD,  REPLACE,  STATS = 10" 
Msg 3634, Level 16, State 1, Server GAUTAM, Line 1
The operating system returned the error '32(The process cannot access the file because it is being used by another process.)' while attempting 'RestoreContainer::ValidateTargetForCreation' on 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\Sample.Db.mdf'.
Msg 3156, Level 16, State 8, Server GAUTAM, Line 1
File 'Sample.Db' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\Sample.Db.mdf'. Use WITH MOVE to identify a valid location for the file.
Msg 3634, Level 16, State 1, Server GAUTAM, Line 1
The operating system returned the error '32(The process cannot access the file because it is being used by another process.)' while attempting 'RestoreContainer::ValidateTargetForCreation' on 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\Sample.Db_log.ldf'.
Msg 3156, Level 16, State 8, Server GAUTAM, Line 1
File 'Sample.Db_log' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\Sample.Db_log.ldf'. Use WITH MOVE to identify a valid location for the file.
Msg 3119, Level 16, State 1, Server GAUTAM, Line 1
Problems were identified while planning for the RESTORE statement. Previous messages provide details.
Msg 3013, Level 16, State 1, Server GAUTAM, Line 1
RESTORE DATABASE is terminating abnormally.

但是,如果我直接在Management Studio 2008中执行此操作,则效果很好:

RESTORE DATABASE [Sample.Db] FROM  DISK = N'C:\Sample.Db.bak' WITH  FILE = 1,  MOVE N'Sample.Db' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\Sample.Db.mdf',  MOVE N'Sample.Db_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\Sample.Db_log.ldf',  NOUNLOAD,  REPLACE,  STATS = 10

没有锁定或安全问题,服务器上不存在数据库.

我不知道.

有任何想法吗?

最佳答案
我发现了问题.我使用的命令行错误:

 SQLCMD -s local\SQL2008 ...

是-s.这不是服务器名称的开关,而是colseparator的开关

正确的方法是(注意大写字母S和本地主机):

 SQLCMD -S localhost\SQL2008 ...

问题解决了的家伙,感谢您抽出宝贵的时间来看看. 🙂

转载注明原文:sql-server-SQLCMD Restore在Management Studio中有效,但在DOS提示符下不起作用 - 代码日志