How to drop a SQL Server database?
2012-04-24 18:55
477 查看
今天做数据库实验遇到一个问题—建立的数据库删除不了!问题错误提示如下:
Cannot drop database " DatabaseName
" because it is currently in use.寻思了好久,加上在网上搜索资料才解决了。现将与dropa SQL Server database相关的知识记录如下:
The easy answer is to issue the following command in Query Analyzer:
However, you will sometimes receive the following error:
Hopefully you aren't in the habit of trying to drop production databases; so,more often than not, this error occurs because your current Query Analyzerwindow is actually set to the context of 数据库名. So, I usually recommend gettingrid of extraneous Query Analyzer
windows, and using the following commandinstead:
You might still get the error, if other users are connected without yourknowledge. One way to get rid of them immediately:
This will drop kick any connections and roll back their transactions, at whichpoint you should be able to drop the database successfully.
However, this leaves out SQL Server 7.0 users, who don't have access to such acommand. Here is a stored procedure I created for 7.0:
Sample usage:
You might have to call the procedure multiple times before it completes theprocess of wiping out existing users.
And because it happens so often, I'm going to suggest again: please make surethat your current Query Analyzer window isn't the elusive process that won't goaway! Make sure you are using the Master database when trying to drop userdatabases!
Cannot drop database " DatabaseName
" because it is currently in use.寻思了好久,加上在网上搜索资料才解决了。现将与dropa SQL Server database相关的知识记录如下:
The easy answer is to issue the following command in Query Analyzer:
DROP DATABASE 数据库名 |
Server: Msg 3702, Level 16, State 3, Line 1 Cannot drop the database '数据库名' because it is currently in use. |
windows, and using the following commandinstead:
USE MASTER GO DROP DATABASE 数据库名 |
ALTER DATABASE 数据库名 SET SINGLE_USER WITH ROLLBACK IMMEDIATE |
However, this leaves out SQL Server 7.0 users, who don't have access to such acommand. Here is a stored procedure I created for 7.0:
CREATE PROCEDURE dbo.clearDBUsers @dbName SYSNAME AS BEGIN SET NOCOUNT ON DECLARE @spid INT, @cnt INT, @sql VARCHAR(255) SELECT @spid = MIN(spid), @cnt = COUNT(*) FROM master..sysprocesses WHERE dbid = DB_ID(@dbname) AND spid != @@SPID PRINT 'Starting to KILL '+RTRIM(@cnt)+' processes.' WHILE @spid IS NOT NULL BEGIN PRINT 'About to KILL '+RTRIM(@spid) SET @sql = 'KILL '+RTRIM(@spid) EXEC(@sql) SELECT @spid = MIN(spid), @cnt = COUNT(*) FROM master..sysprocesses WHERE dbid = DB_ID(@dbname) AND spid != @@SPID PRINT RTRIM(@cnt)+' processes remain.' END END GO |
EXEC dbo.clearDBUsers '数据库名' |
And because it happens so often, I'm going to suggest again: please make surethat your current Query Analyzer window isn't the elusive process that won't goaway! Make sure you are using the Master database when trying to drop userdatabases!
相关文章推荐
- How to refresh database parallelly on sql server
- How do I drop a SQL Server database?
- [SQL Server][FILESTREAM] -- How to Detach and Attach a SQL Server FILESTREAM Enabled Database
- How to find user who ran DROP or DELETE statements on your SQL Server Objects
- How to backup and restore database in SQL Server
- How to read and save Images in a Sql Server Database using ADO.NET and C#
- How to determine SQL Server database transaction log usage
- Database Engine Tuning Adviser: How to tune your new SQL Server 2005
- How to use a SQL Server database with your PHP web site
- How to recover a SQL Server Database
- How to select the data type in SQLserver database such as varchar, nvarchar
- How to Downgrade a Database from SQL Server 2005 to SQL Server 2000
- How to move databases between computers that are running SQL Server
- How to recreate the msdb database in SQL Server 2005
- How to get the SQL Server name (Database server)?
- How to move the databases that are used by SharePoint Portal Server 2003 to a computer that is running SQL Server
- How to move databases between computers that are running SQL Server
- How to Kill All Processes That Have Open Connection in a SQL Server Database[关闭数据库链接 最佳方法] -摘自网络
- How to shrink the tempdb database in SQL Server
- SQL Server How to Perform Database Mirroring