https://msdn.microsoft.com/en-us/library/ms191491.aspx

方法一:Using SQL Server Management Studio

To detach a database

  1. In SQL Server Management Studio Object Explorer, connect to the instance of the SQL Server Database Engine and then expand the instance.

  2. Expand Databases, and select the name of the user database you want to detach.

  3. Right-click the database name, point to Tasks, and then click Detach. The Detach Database dialog box appears.

    Databases to detach

    Lists the databases to detach.

    Database Name

    Displays the name of the database to be detached.

    Drop Connections

    Disconnect connections to the specified database.

    Note

    You cannot detach a database with active connections.

    Update Statistics

    By default, the detach operation retains any out-of-date optimization statistics when detaching the database; to update the existing optimization statistics, click this check box.

    Keep Full-Text Catalogs

    By default, the detach operation keeps any full-text catalogs that are associated with the database. To remove them, clear the Keep Full-Text Catalogs check box. This option appears only when you are upgrading a database from SQL Server 2005.

    Status

    Displays one of the following states: Ready or Not ready.

    Message

    The Message column may display information about the database, as follows:

    • When a database is involved with replication, the Status is Not ready and the Message column displays Database replicated.

    • When a database has one or more active connections, the Status is Not ready and the Message column displays<number_of_active_connections> Active connection(s) — for example: 1 Active connection(s). Before you can detach the database, you need to disconnect any active connections by selecting Drop Connections.

    To obtain more information about a message, click the hyperlinked text to open Activity Monitor.

  4. When you are ready to detach the database, click OK.

Note

The newly detached database will remain visible in the Databases node of Object Explorer until the view is refreshed. You can refresh the view at any time: Click in the Object Explorer pane, and from the menu bar select View and then Refresh.

方法二:Using Transact-SQL

 

To detach a database

  1. Connect to the Database Engine.

  2. From the Standard bar, click New Query.

  3. Copy and paste the following example into the query window and click Execute. This example detaches the AdventureWorks2012 database with skipchecks set to true.

 
 
EXEC sp_detach_db 'AdventureWorks2012', 'true';

https://docs.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-detach-db-transact-sql

Detaches a database that is currently not in use from a server instance and, optionally, runs UPDATE STATISTICS on all tables before detaching.

EXEC sys.sp_detach_db @dbname = NULL ,             -- sysname
@skipchecks = N'' , -- nvarchar(10)
@keepfulltextindexfile = N'' -- nvarchar(10)

[ @skipchecks = ] 'skipchecks'
Specifies whether to skip or run UPDATE STATISTIC. skipchecks is a nvarchar(10) value, with a default value of NULL. To skip UPDATE STATISTICS, specify true. To explicitly run UPDATE STATISTICS, specify false.

By default, UPDATE STATISTICS is performed to update information about
the data in the tables and indexes. Performing UPDATE STATISTICS is
useful for databases that are to be moved to read-only media.

[ @keepfulltextindexfile= ] 'KeepFulltextIndexFile'
Specifies that the full-text index file associated with the database
that is being detached will not be dropped during the database detach
operation. KeepFulltextIndexFile is a nvarchar(10) value with a default of true. If KeepFulltextIndexFile is false,
all the full-text index files associated with the database and the
metadata of the full-text index are dropped, unless the database is
read-only. If NULL or true, full-text related metadata are kept.

The@keepfulltextindexfile parameter will be removed in a future version of SQL Server. Do not use this parameter in new development work, and modify applications that currently use this parameter as soon as possible.

EXEC sys.sp_detach_db @dbname = N'TW_LS_RPS' , -- sysname
@skipchecks = N'true'; -- nvarchar(10)

https://docs.microsoft.com/en-us/sql/t-sql/statements/update-statistics-transact-sql

Updates query optimization statistics on a table or indexed view. By default, the query optimizer already updates statistics as necessary to improve the query plan; in some cases you can improve query performance by using UPDATE STATISTICS or the stored procedure sp_updatestats to update statistics more frequently than the default updates.

Updating statistics ensures that queries compile with up-to-date statistics. However, updating statistics causes queries to recompile. We recommend not updating statistics too frequently because there is a performance tradeoff between improving query plans and the time it takes to recompile queries. The specific tradeoffs depend on your application. UPDATE STATISTICS can use tempdb to sort the sample of rows for building statistics.

Cannot drop database because it is currently in use

before dropping a database, you drop the connection to that database first.

I have found a solution at http://www.kodyaz.com/articles/kill-all-processes-of-a-database.aspx

DECLARE @DatabaseName nvarchar(50)
SET @DatabaseName = N'YOUR_DABASE_NAME' DECLARE @SQL varchar(max) SELECT @SQL = COALESCE(@SQL,'') + 'Kill ' + Convert(varchar, SPId) + ';'
FROM MASTER..SysProcesses
WHERE DBId = DB_ID(@DatabaseName) AND SPId <> @@SPId --SELECT @SQL
EXEC(@SQL)

https://docs.microsoft.com/en-us/sql/relational-databases/system-compatibility-views/sys-sysprocesses-transact-sql

Contains information about processes that are running on an instance of SQL Server. These processes can be client processes or system processes. To access sysprocesses, you must be in the master database context, or you must use the master.dbo.sysprocesses three-part name.

查找进程
SELECT *
FROM master..sysprocesses;

https://docs.microsoft.com/en-us/sql/t-sql/functions/db-id-transact-sql

Returns the database identification (ID) number.

查找数据库对应的数据库编号

SELECT DB_ID(N'd_lisa_CMS_dev01_v6000')

SELECT *
FROM master..sysprocesses
WHERE dbid = 142;

https://docs.microsoft.com/en-us/sql/t-sql/language-elements/kill-transact-sql

Terminates a user process that is based on the session ID or unit of work (UOW). If the specified session ID or UOW has much work to undo, the KILL statement may take some time to complete, particularly when it involves rolling back a long transaction.

KILL can be used to terminate a normal connection, which internally terminates the transactions that are associated with the specified session ID. The statement can also be used to terminate orphaned and in-doubt distributed transactions when Microsoft Distributed Transaction Coordinator (MS DTC) is in use.

Detach a Database的更多相关文章

  1. 如何rename sqlserver database

    Problem Sometimes there is a need to change the name of your database whether this is because the or ...

  2. How to Kill All Processes That Have Open Connection in a SQL Server Database[关闭数据库链接 最佳方法] -摘自网络

    SQL Server database administrators may frequently need in especially development and test environmen ...

  3. SQL Server has encountered 1 occurrence(s) of cachestore flush for the 'Object Plans' cachestore (part of plan cache) due to some database maintenance or reconfigure operations.

    2017-11-01 09:49:44.35 spid166 SQL Server has encountered 1 occurrence(s) of cachestore flush for th ...

  4. 【iOS】FMDB/SQLCipher数据库加解密,迁移

    2016-04-19更新:本文代码可能有些问题,请移步 http://zhengbomo.github.io/2016-04-18/sqlcipher-start/ 查看 sqlite应用几乎在所有的 ...

  5. iOS 使用FMDB SQLCipher给数据库加密

    关于SQLite,SQLCipher和FMDB SQLite是一个轻量的.跨平台的.开源的数据库引擎,它的在读写效率.消耗总量.延迟时间和整体简单性上具有的优越性,使其成为移动平台数据库的最佳解决方案 ...

  6. SQLite Helper (C#) zt

    http://www.codeproject.com/Articles/746191/SQLite-Helper-Csharp This small class (SQLiteHelper.cs) i ...

  7. iOS Sqlite加密(FMDB/SQLCipher)

    /** * 对数据库加密 * * @param path path description * * @return return value description */ + (BOOL)encryp ...

  8. sqlite内存数据库和文件数据库的同步[转]

    由于sqlite对多进程操作支持效果不太理想,在项目中,为了避免频繁读写 文件数据库带来的性能损耗,我们可以采用操作sqlite内存数据库,并将内存数据库定时同步到文件数据库中的方法. 实现思路如下: ...

  9. SQL Server遇到的错误和有用的tools

    1.The target principal name is incorrect.  Cannot generate SSPI context. 检查IIS的profile,可能是密码错误 2.The ...

随机推荐

  1. vim之补全2(完全个人定制版)

    关于补全的方面要说的的确很多, 这里选择分为两个章叙述. 如果你想学vim, 你需要有很强的耐心, 如果你想锻炼这种耐心, 你可以试着先看完我之前的文章. 好了, 下面继续我们的vim补全吧. vim ...

  2. Qt 窗体间传值(代码备份)

    刚开始看的时候看的云里雾里的,现在稍微明白一点了.现在假设有一个form,一个MainWindow,如图所示: 实现点击PushButton,将文本框中的内容传输到MainWindow中,显示为Lab ...

  3. OpenGL第23-26小结

    到后面代码相对而言比较复杂了,因为没有系统的看红宝书(就跟字典一样,兴趣缺缺),很多操作的步骤比较迷糊. 23讲讲解了如何将环境纹理贴在球体.圆柱体等非矩形物体表面,从而达到一个反射周围景色的效果(恩 ...

  4. UICollectionView框架总结

    一.UIcollectionView介绍 1.1.简介 首先看苹果官方文档 UICollectionView Class Reference 的介绍: The UICollectionView cla ...

  5. C# 获得固定年月日

    /// <summary> /// 获得固定年月日,时和分不固定 : 2019-01-01 00:00:00 /// </summary> /// <returns> ...

  6. ThinkPHP框架表单验证AJAX

    验证有两种方式:静态验证与动态验证. 一.静态验证 在模型类里面预先定义好该模型的自动验证规则,我们称为静态定义. 验证时要在test表的Model里面加验证条件:新建testModel.class. ...

  7. 6 个 Linux 运维典型问题,大牛的分析解决思路在这里

    作为一名合格的 Linux 运维工程师,一定要有一套清晰.明确的解决故障思路,当问题出现时,才能迅速定位.解决问题,这里给出一个处理问题的一般思路: 重视报错提示信息:每个错误的出现,都是给出错误提示 ...

  8. Win32中 DLL、Lib 库的创建机器使用

    Windows 下 的静态库和动态库 一.静态函数库(Lib) 1. 静态函数库的制作(C/C++) —— 打开新建项目,然后选中Win32项目,接着在创建项目中选择 Lib,再接着将函数.实现功能的 ...

  9. 15.most_fields策略进行cross-fields search

    主要知识点: cross-fields 的使用场景 cross-fields 使用方法 cross-fields 的缺点     一.cross-fields 的使用场景     cross-fiel ...

  10. 【转载】Apache shutdown unexpectedly启动错误解决方法

    http://blog.csdn.net/dong123dddd/article/details/21372179 xampp启动时显示的错误为: 9:52:41  [Apache] Attempti ...