2015-08-19 48 views
0

我在SQL Server中有一個數據庫,其中有很多類似的表格,如dbo.Dos_150602_xyz。我試圖通過鍵入以下內容刪除其中只有1506的表格:刪除/刪除SQL Server中的類似表格

drop table dbo.Dos_1506*; 

但這並不奏效。我還能如何執行此操作?

謝謝。

+1

此[鏈接](http://stackoverflow.com/questions/4393/drop-all-tables-whose-names-begin-with-a-certain -string)可能會有所幫助。 – learningNew

+0

@learningNew分享了一個有用的鏈接,這將生成一個腳本,這也將幫助你交叉檢查你的願望表來刪除它 – wiretext

+0

腳本將完成所有工作,只需修改/添加條件 - '其中Table_Name類似'%1506 %'和TABLE_TYPE ='BASE TABLE'' – Abhishek

回答

1

只是爲了讓OP更容易一些。

示例表創建腳本: create table table_pattern_name_1 (s1 varchar(20), n1 int); create table table_pattern_name_2 (s1 varchar(20), n1 int); create table table_pattern_name_3 (s1 varchar(20), n1 int); create table table_pattern_name_4 (s1 varchar(20), n1 int);

表刪除腳本: declare @cmd varchar(4000) declare cmds cursor for select 'drop table [' + Table_Name + ']' from INFORMATION_SCHEMA.TABLES where Table_Name like 'table_pattern_name_%' open cmds while 1=1 begin fetch cmds into @cmd if @@fetch_status != 0 break print @cmd exec(@cmd) end close cmds; deallocate cmds

SSMS輸出: drop table [table_pattern_name_1] drop table [table_pattern_name_2] drop table [table_pattern_name_3] drop table [table_pattern_name_4]

讓我知道這是否爲你的榜樣。

謝謝, 馬特·瓊斯 的Microsoft SQL Server