批量刪除資料庫的SQL語句

2021-07-01 23:32:49 字數 3036 閱讀 1667

由於專案需要,每次執行case的時候都需要建立資料庫。雖然每次執行結束都會刪除,但是不保證每次都能刪除成功(這裡有許多原因,我就不列舉了)。所以我寫了個指令碼去批量刪除資料庫。首先為確保我們的資料庫是有用的,不被錯刪除的。所以就必須先備份在刪除。

備份資料庫:

declare @name varchar(50) -- database name  

declare @path varchar(256) -- path for backup files

declare @filename varchar(256) -- filename for backup

declare @filedate varchar(20) -- used for file name

set @path = 'd:\abc\backup\'

select @filedate = convert(varchar(20),getdate(),112)

declare db_cursor cursor for

select name

from master.dbo.sysdatabases

where name in ('e1db_abc','scheduler')

open db_cursor

fetch next from db_cursor into @name

while @@fetch_status = 0

begin

set @filename = @path + @name + '_' + @filedate + '.bak'

backup database @name to disk = @filename

fetch next from db_cursor into @name

end

close db_cursor

deallocate db_cursor

刪除資料庫:

方法1:

declare @path varchar(256) -- path for backup files  

declare @filename varchar(256) -- filename for backup

declare @filedate varchar(20) -- used for file name

set @path = 'd:\backup\'

select @filedate = convert(varchar(20),getdate(),112)

declare db_cursor cursor for

select name

from master.dbo.sysdatabases

where name not in ('master','model','msdb','tempdb','reportserver'

open db_cursor

declare @databasename varchar(50) -- database name

declare @databasecount int =0

fetch next from db_cursor into @databasename

while @@fetch_status = 0

begin

declare @statement nvarchar(50)

print 'dropping databse: '+@databasename

exec('

use master

alter database '+@databasename+' set single_user with rollback immediate

drop database '+@databasename)

set @databasecount=@databasecount+1

fetch next from db_cursor into @databasename

end

print 'dropped databse count: '+convert(varchar(12),@databasecount )

close db_cursor

deallocate db_cursor

方法2:

declare @dbtodelete table(name varchar(50))

declare @databasename varchar(100)

insert into @dbtodelete values('abc')

insert into @dbtodelete values('def')

insert into @dbtodelete values('gh')

insert into @dbtodelete select name from master..sysdatabases where name like 'abc%'

while exists(select name from @dbtodelete)

begin

select top 1 @databasename=name from @dbtodelete

if exists(select * from sys.databases where name = @databasename)

begin

print 'dropping databse: '+@databasename

exec('

use master

alter database '+@databasename+' set single_user with rollback immediate

drop database '+@databasename)

enddelete from @dbtodelete where name = @databasename

end

sql語句 刪除資料庫所有表裡的資料

declare name nvarchar 255 sql nvarchar 4000 declare task cursor cursor local for 游標定義 select name from sysobjects where type u 獲取當前資料庫中的所有表的表名 open ta...

執行SQL刪除資料庫

use master exec msdb.dbo.sp delete database backuphistory database name n 資料庫名稱 alter database 資料庫名稱 set single user with rollback immediate drop data...

如何批量刪除資料庫表?

可以通過執行如下的儲存過程來刪除資料庫表,以下儲存過程刪除的是所有表名以abc開頭的表 declare table nvarchar 30 declare tmpcur cursor for select name from sys.objects where type u and name lik...