如何删除SQL Server数据库中的所有表?
我正在尝试编写一个脚本来完全清空 SQL Server 数据库。这是我到目前为止所拥有的:
USE [dbname]
GO
EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all'
EXEC sp_msforeachtable 'DELETE ?'
当我在 Management Studio 中运行它时,我得到:
命令成功完成。
但当我刷新表格列表时,它们仍然在那里。我做错了什么?
I'm trying to write a script that will completely empty a SQL Server database. This is what I have so far:
USE [dbname]
GO
EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all'
EXEC sp_msforeachtable 'DELETE ?'
When I run it in the Management Studio, I get:
Command(s) completed successfully.
but when I refresh the table list, they are all still there. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
您还可以仅使用 SSMS UI 工具(不使用 SQL 脚本)从数据库中删除所有表。有时,此方法可能更方便,特别是偶尔执行时。
我按如下步骤执行此操作:
案例,全部)。
由于密钥错误,需要重复多次
约束或依赖)。
You can also delete all tables from a database using only the SSMS UI tools (without using a SQL script). Sometimes, this method can be more convenient, especially if it is performed occasionally.
I do this step-by-step as follows:
case, all of them).
need to repeat this as many times as there are errors due to key
constraints or dependencies).
当有多个外键表时,它对我也不起作用。
我发现该代码可以工作并且可以完成您尝试的所有操作(从数据库中删除所有表):
您可以找到帖子此处。这是 Groker 的帖子。
It doesn't work for me either when there are multiple foreign key tables.
I found that code that works and does everything you try (delete all tables from your database):
You can find the post here. It is the post by Groker.
在 SSMS 中:
现在,这将删除所有内容,包括数据库。请务必删除您不想丢弃的物品的代码。或者,在“选择对象”部分中,不必选择对整个数据库编写脚本,只需选择要删除的项目。
In SSMS:
Now, this will drop everything, including the database. Make sure to remove the code for the items you don't want dropped. Alternatively, in the "Choose Objects" section, instead of selecting to script entire database just select the items you want to remove.
接受的答案不支持 Azure。它使用未记录的存储过程“sp_MSforeachtable”。如果您在运行时收到“azure 找不到存储过程 'sp_msforeachtable”错误,或者只是想避免依赖未记录的功能(可以随时删除或更改其功能),请尝试以下操作。
此版本忽略实体框架迁移历史记录表“__MigrationHistory”和“database_firewall_rules”,这是一个您无权删除的 Azure 表。
在 Azure 上进行了轻微测试。请务必检查以确保不会对您的环境产生不良影响。
摘自:
https://edspencer.me.uk/2013/02/25/drop-all-tables-in-a-sql-server-database-azure-friend/
http://www.sqlservercentral.com/blogs/ sqlservertips/2011/10/11/remove-all-foreign-keys/
The accepted answer doesn't support Azure. It uses an undocumented stored procedure "sp_MSforeachtable". If you get an "azure could not find stored procedure 'sp_msforeachtable" error when running or simply want to avoid relying on undocumented features (which can be removed or have their functionality changed at any point) then try the below.
This version ignores the entity framework migration history table "__MigrationHistory" and the "database_firewall_rules" which is an Azure table you will not have permission to delete.
Lightly tested on Azure. Do check to make this this has no undesired effects on your environment.
Taken from:
https://edspencer.me.uk/2013/02/25/drop-all-tables-in-a-sql-server-database-azure-friendly/
http://www.sqlservercentral.com/blogs/sqlservertips/2011/10/11/remove-all-foreign-keys/
delete
用于从表中删除行。您应该使用drop table
来代替。delete
is used for deleting rows from a table. You should usedrop table
instead.您几乎是对的,请改为使用:
但第二行您可能需要执行多次,直到不再出现错误:
消息:
意味着所有表已成功删除。
You are almost right, use instead:
but second line you might need to execute more then once until you stop getting error:
Message:
means that all table were successfully deleted.
简短而甜蜜:
Short and sweet:
最快的方法是:
The fasted way is:
现货!
您可以使用下面的查询从数据库中删除所有表
EXEC sp_MSforeachtable @command1 = "DROP TABLE ?"
编码愉快!
Spot on!!
You can use below query to remove all the tables from database
EXEC sp_MSforeachtable @command1 = "DROP TABLE ?"
Happy coding !
似乎命令应该没有方毯
Seems the command should be without the square blanket
对我来说,最简单的方法:
For me, the easiest way:
删除整个数据库然后重新创建怎么样?这对我有用。
How about dropping the entire database and then creating it again? This works for me.
Azure SQL + 表(带有约束)采用与 dbo + ipv6_database_firewall_rules 条件不同的架构。
这是 https://stackoverflow.com/a/43128914/4510954 答案的一个小扩展。
Azure SQL + tables (with constraints) in a different schema than dbo + ipv6_database_firewall_rules condition.
This is a little extension for https://stackoverflow.com/a/43128914/4510954 answer.
sp_msforeachtable
在 Azure SQL 中不可用对于 Azure SQL:
此查询将删除外键约束
这将从数据库中删除所有表
sp_msforeachtable
is not available in Azure SQLFor Azure SQL:
This query will drop Foreign Key constraints
This will drop all the tables from the database
对于时态表来说,由于可能存在一些外键,并且还有异常,情况会稍微复杂一些:
表XXX上的删除表操作失败,因为它不是系统支持的操作-版本化时态表
您可以使用的是:
For Temporal Tables it is a bit more complicated due to the fact there may be some foreign keys and also exception:
Drop table operation failed on table XXX because it is not a supported operation on system-versioned temporal tables
What you can use is:
我知道这是一篇旧文章,但我已经在大量数据库上尝试了这里的所有答案,我发现它们有时都有效,但并非总是适用于 SQL Server 的各种(我只能假设)怪癖。
最终我想出了这个。我已经在任何地方(一般来说)我可以测试它并且它有效(没有任何隐藏的存储过程)。
主要针对 SQL Server 2014 进行说明。(但我尝试过的大多数其他版本似乎也运行良好)。
我尝试过 while 循环和空值等、游标和各种其他形式,但它们似乎总是在某些数据库上失败,但在其他数据库上却失败,没有明显的原因。
获取计数并使用它进行迭代似乎总是对我测试过的所有内容都有效。
I know this is an old post now but I have tried all the answers on here on a multitude of databases and I have found they all work sometimes but not all of the time for various (I can only assume) quirks of SQL Server.
Eventually I came up with this. I have tested this everywhere (generally speaking) I can and it works (without any hidden store procedures).
For note mostly on SQL Server 2014. (but most of the other versions I tried it also seems to worked fine).
I have tried while loops and nulls etc etc, cursors and various other forms but they always seem to fail on some databases but not others for no obvious reason.
Getting a count and using that to iterate always seems to work on everything Ive tested.
这个效果很好:
This one works perfectly :