删除数据库但不删除 *.mdf/*.ldf [英] Drop DB but don't delete *.mdf / *.ldf

查看:59
本文介绍了删除数据库但不删除 *.mdf/*.ldf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自动执行分离和删除数据库的过程(通过 VBS objshell.run)如果我手动使用 SSMS 进行分离和删除,然后我可以将数据库文件复制到另一个位置...但是如果我使用:

I am trying to automate a process of detaching and dropping a database (via a VBS objshell.run) If I manually use SSMS to detach and drop I can then copy to database files to another location... however if I use:

sqlcmd -U sa -P MyPassword -S (local) -Q "ALTER DATABASE MyDB set single_user With rollback IMMEDIATE"

然后

sqlcmd -U sa -P MyPassword -S (local) -Q "DROP DATABASE MyDB"

它分离/删除然后删除文件.如何在不删除的情况下进行分离和删除?

It detaches/drops and then deletes the files. How do I get the detach and drop without the delete?

推荐答案

DROP DATABASE 是关于在不删除文件的情况下删除数据库(在一般备注下):

The MSDN Documentation on DROP DATABASE has this to say about dropping the database without deleting the files (under General Remarks):

删除数据库会从 SQL Server 实例中删除数据库并删除数据库使用的物理磁盘文件.如果数据库或其任何文件在删除时处于脱机状态,则不会删除磁盘文件.可以使用 Windows 资源管理器手动删除这些文件.要从当前服务器中删除数据库而不从文件系统中删除文件,请使用 sp_detach_db.

Dropping a database deletes the database from an instance of SQL Server and deletes the physical disk files used by the database. If the database or any one of its files is offline when it is dropped, the disk files are not deleted. These files can be deleted manually by using Windows Explorer. To remove a database from the current server without deleting the files from the file system, use sp_detach_db.

因此,为了让您删除数据库而不使用 sqlcmd 删除文件,您可以将其更改为:

So in order for you to drop the database without having the files get deleted with sqlcmd you can change it to do this:

sqlcmd -U sa -P MyPassword -S (local) -Q "EXEC sp_detach_db 'MyDB', 'true'"

免责声明:老实说,我以前从未使用过 sqlcmd,但从它的使用语法假设我相信这应该可以帮助您解决问题.

DISCLAIMER: I have honestly never used sqlcmd before but assuming from the syntax of how it's used I believe this should help you with your problem.

这篇关于删除数据库但不删除 *.mdf/*.ldf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆