删除数据库(避免正在使用发生的错误)

use master
declare @dbname sysname set @dbname='AAA' --这个是要删除的数据库库名
IF EXISTS (SELECT * FROM SYSDATABASES WHERE name=@dbname)
BEGIN
        declare @s nvarchar(1000) declare tb cursor local for
        select s='kill '+cast(spid as varchar)
        from master..sysprocesses
        where dbid=db_id(@dbname) open tb fetch next from tb into @s while @@fetch_status=0
        begin exec(@s) fetch next from tb into @s end close tb deallocate tb
        exec('drop database ['+@dbname+']')
END
GO

posted @ 2011-11-10 22:28  第7笔画  阅读(256)  评论(0编辑  收藏  举报