https://msdn.microsoft.com/en-us/library/ms191491.aspx
方法一:Using SQL Server Management Studio
To detach a database
-
In SQL Server Management Studio Object Explorer, connect to the instance of the SQL Server Database Engine and then expand the instance.
-
Expand Databases, and select the name of the user database you want to detach.
-
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.
-
-
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
-
Connect to the Database Engine.
-
From the Standard bar, click New Query.
-
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';
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)
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.