删除名称以特定字符串开头的所有表
如何删除名称以给定字符串开头的所有表?
我认为这可以通过一些动态 SQL 和 INFORMATION_SCHEMA 表来完成。
How can I drop all tables whose names begin with a given string?
I think this can be done with some dynamic SQL and the INFORMATION_SCHEMA
tables.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
如果数据库中有多个所有者,您可能需要修改查询以包含所有者。
这比使用生成脚本加运行的两步方法更干净。 但脚本生成的优点之一是,它使您有机会在实际运行之前检查将要运行的全部内容。
我知道如果我要对生产数据库执行此操作,我会尽可能小心。
编辑代码示例已修复。
You may need to modify the query to include the owner if there's more than one in the database.
This is cleaner than using a two-step approach of generate script plus run. But one advantage of the script generation is that it gives you the chance to review the entirety of what's going to be run before it's actually run.
I know that if I were going to do this against a production database, I'd be as careful as possible.
Edit Code sample fixed.
这将生成一个脚本。
添加子句以在删除之前检查表是否存在:
This will generate a script.
Adding clause to check existence of table before deleting:
这将使您按外键顺序获取表,并避免删除 SQL Server 创建的某些表。
t.Ordinal
值会将表分割为依赖层。This will get you the tables in foreign key order and avoid dropping some of the tables created by SQL Server. The
t.Ordinal
value will slice the tables into dependency layers.在 Oracle XE 上,这有效:
或者,如果您还想删除约束并释放空间,请使用:
这将生成一堆
DROP TABLE 级联约束 PURGE
语句...对于
VIEWS
使用:On Oracle XE this works:
Or if you want to remove the constraints and free up space as well, use this:
Which will generate a bunch of
DROP TABLE cascade constraints PURGE
statements...For
VIEWS
use this:这是我的解决方案:
当然,您需要将
TABLE_PREFIX_GOES_HERE
替换为您的前缀。Here is my solution:
And of course you need to replace
TABLE_PREFIX_GOES_HERE
with your prefix.当我正在寻找 mysql 语句来删除基于 @Xenph Yan 的所有 WordPress 表时,我看到了这篇文章,这就是我最终所做的:
这将为您提供以 wp_ 开头的所有表的删除查询集
I saw this post when I was looking for mysql statement to drop all WordPress tables based on @Xenph Yan here is what I did eventually:
this will give you the set of drop queries for all tables begins with wp_
Xenph Yan 的答案比我的干净得多,但这里仍然是我的。
只需将
tableName
更改为您要搜索的字符即可。Xenph Yan's answer was far cleaner than mine but here is mine all the same.
Just change
tableName
to the characters that you want to search with.编辑:
sp_MSforeachtable 未记录,因此不适合生产,因为它的行为可能因 MS_SQL 版本而异。
Edit:
sp_MSforeachtable is undocumented hence not suitable for production because it's behavior may vary depending on MS_SQL version.
如果您的查询返回多行,您可以收集结果并将它们合并到查询中。
If your query returns more than one line, you can collect the results and merge them into a query.
尝试以下代码:
此 SQL 脚本在不使用光标的情况下执行。
Try following code:
This SQL script is executed without using a cursor.
这对我有用。
This worked for me.
-- test 是表名
-- Test is the table name
我怀疑我必须对 Xenph Yan 的答案进行一些推导,因为我的表不在默认模式中。
I had to do a slight derivation on Xenph Yan's answer I suspect because I had tables not in the default schema.
如果是临时表,您可能想尝试
In case of temporary tables, you might want to try
我想发布我的解决方案提案,该解决方案将基于通配符(例如“table_20210114”)删除(不仅仅是生成和选择删除命令)所有早于特定天数的表。
I would like to post my proposal of the solution which DROP (not just generate and select a drop commands) all tables based on the wildcard (e.g. "table_20210114") older than particular amount of days.
如果突然需要删除外键链接的表。
If you suddenly need to delete tables linked by foreign keys.