删除名称以特定string开头的所有表
我想要一个脚本删除所有名称以给定string开头的表格。 我相信这可以用一些dynamicsql和INFORMATION_SCHEMA
表来完成。
如果有人有脚本,或者可以快速敲一下,请张贴。
如果没有人在我自己找出答案之前发布答案,我会发布我的解决scheme。
如果数据库中有多个,则可能需要修改查询以包含所有者。
DECLARE @cmd varchar(4000) DECLARE cmds CURSOR FOR SELECT 'drop table [' + Table_Name + ']' FROM INFORMATION_SCHEMA.TABLES WHERE Table_Name LIKE 'prefix%' OPEN cmds WHILE 1 = 1 BEGIN FETCH cmds INTO @cmd IF @@fetch_status != 0 BREAK EXEC(@cmd) END CLOSE cmds; DEALLOCATE cmds
这比使用生成脚本和运行的两步法更清洁。 但脚本生成的一个优点是,它使您有机会在实际运行之前查看将要运行的所有内容。
我知道,如果我要对生产数据库做这件事,我会尽可能的小心。
编辑代码示例已修复。
SELECT 'DROP TABLE "' + TABLE_NAME + '"' FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE '[prefix]%'
这将生成一个脚本。
删除前添加子句来检查表的存在:
SELECT 'IF OBJECT_ID(''' +TABLE_NAME + ''') IS NOT NULL BEGIN DROP TABLE [' + TABLE_NAME + '] END;' FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE '[prefix]%'
这将以外键顺序为您提供表格,并避免删除由SQL Server创build的一些表格。 t.Ordinal
值将把表分成依赖层。
WITH TablesCTE(SchemaName, TableName, TableID, Ordinal) AS ( SELECT OBJECT_SCHEMA_NAME(so.object_id) AS SchemaName, OBJECT_NAME(so.object_id) AS TableName, so.object_id AS TableID, 0 AS Ordinal FROM sys.objects AS so WHERE so.type = 'U' AND so.is_ms_Shipped = 0 AND OBJECT_NAME(so.object_id) LIKE 'MyPrefix%' UNION ALL SELECT OBJECT_SCHEMA_NAME(so.object_id) AS SchemaName, OBJECT_NAME(so.object_id) AS TableName, so.object_id AS TableID, tt.Ordinal + 1 AS Ordinal FROM sys.objects AS so INNER JOIN sys.foreign_keys AS f ON f.parent_object_id = so.object_id AND f.parent_object_id != f.referenced_object_id INNER JOIN TablesCTE AS tt ON f.referenced_object_id = tt.TableID WHERE so.type = 'U' AND so.is_ms_Shipped = 0 AND OBJECT_NAME(so.object_id) LIKE 'MyPrefix%' ) SELECT DISTINCT t.Ordinal, t.SchemaName, t.TableName, t.TableID FROM TablesCTE AS t INNER JOIN ( SELECT itt.SchemaName AS SchemaName, itt.TableName AS TableName, itt.TableID AS TableID, Max(itt.Ordinal) AS Ordinal FROM TablesCTE AS itt GROUP BY itt.SchemaName, itt.TableName, itt.TableID ) AS tt ON t.TableID = tt.TableID AND t.Ordinal = tt.Ordinal ORDER BY t.Ordinal DESC, t.TableName ASC
CREATE PROCEDURE usp_GenerateDROP @Pattern AS varchar(255) ,@PrintQuery AS bit ,@ExecQuery AS bit AS BEGIN DECLARE @sql AS varchar(max) SELECT @sql = COALESCE(@sql, '') + 'DROP TABLE [' + TABLE_NAME + ']' + CHAR(13) + CHAR(10) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE @Pattern IF @PrintQuery = 1 PRINT @sql IF @ExecQuery = 1 EXEC (@sql) END
我看到这个职位,当我正在寻找mysql语句来删除所有的基于@Xenph的WordPress表格颜这里是我最终做的:
SELECT CONCAT( 'DROP TABLE `', TABLE_NAME, '`;' ) AS query FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'wp_%'
这会给你所有表的丢弃查询的集合开始于wp_
在Oracle XE上这个工作:
SELECT 'DROP TABLE "' || TABLE_NAME || '";' FROM USER_TABLES WHERE TABLE_NAME LIKE 'YOURTABLEPREFIX%'
或者如果你想删除约束和释放空间 ,使用这个:
SELECT 'DROP TABLE "' || TABLE_NAME || '" cascade constraints PURGE;' FROM USER_TABLES WHERE TABLE_NAME LIKE 'YOURTABLEPREFIX%'
这将产生一堆DROP TABLE cascade constraints PURGE
语句…
对于VIEWS
使用这个:
SELECT 'DROP VIEW "' || VIEW_NAME || '";' FROM USER_VIEWS WHERE VIEW_NAME LIKE 'YOURVIEWPREFIX%'
阎焱的答案比我的要干净得多,但这里是我的。
DECLARE @startStr AS Varchar (20) SET @startStr = 'tableName' DECLARE @startStrLen AS int SELECT @startStrLen = LEN(@startStr) SELECT 'DROP TABLE ' + name FROM sysobjects WHERE type = 'U' AND LEFT(name, @startStrLen) = @startStr
只要将tableName
更改为您想要search的字符。
这是我的解决scheme:
SELECT CONCAT('DROP TABLE `', TABLE_NAME,'`;') FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'TABLE_PREFIX_GOES_HERE%';
当然,你需要用你的前缀来replaceTABLE_PREFIX_GOES_HERE
。
EXEC sp_MSforeachtable 'if PARSENAME("?",1) like ''%CertainString%'' DROP TABLE ?'
谢谢柯特,那就是我自己中途遇到的那种解决方法。
你的比我的更好 – 它适合于简单的修改。 我join了一个工会,消除了一些意见;)
declare @cmd varchar(4000) declare cmds cursor for Select 'drop table [' + Table_Name + ']' From INFORMATION_SCHEMA.TABLES Where Table_Name like 'prefix%' union Select 'drop view [' + Table_Name + ']' From INFORMATION_SCHEMA.VIEWS Where Table_Name like 'prefix%' open cmds while 1=1 begin fetch cmds into @cmd if @@fetch_status != 0 break exec(@cmd) end close local deallocate local
不要担心,这不是一个生产数据库 – 这只是为了方便清理我的开发数据库,而我尝试的东西了。
select 'DROP TABLE ' + name from sysobjects where type = 'U' and sysobjects.name like '%test%'
– testing是表名
SELECT 'if object_id(''' + TABLE_NAME + ''') is not null begin drop table "' + TABLE_NAME + '" end;' FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE '[prefix]%'
我不得不对Xenph Yan的答案做一个小小的推导,因为我的表格不在默认模式中。
SELECT 'DROP TABLE Databasename.schema.' + TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'strmatch%'
在临时表的情况下,你可能想尝试
SELECT 'DROP TABLE "' + t.name + '"' FROM tempdb.sys.tables t WHERE t.name LIKE '[prefix]%'