如何删除SQL Server数据库中的所有表?

我试图写一个脚本,将完全清空SQL Server数据库。 这是我迄今为止:

USE [dbname] GO EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all' EXEC sp_msforeachtable 'DELETE ?' 

当我在Management Studio中运行它时,我得到:

命令成功完成。

但是当我刷新表列表时,他们都还在那里。 我究竟做错了什么?

当有多个外键表时,它不适用于我。
我发现代码可以正常工作,并且可以执行所有的操作(删除数据库中的所有表):

 DECLARE @Sql NVARCHAR(500) DECLARE @Cursor CURSOR SET @Cursor = CURSOR FAST_FORWARD FOR SELECT DISTINCT sql = 'ALTER TABLE [' + tc2.TABLE_NAME + '] DROP [' + rc1.CONSTRAINT_NAME + ']' FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc1 LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc2 ON tc2.CONSTRAINT_NAME =rc1.CONSTRAINT_NAME OPEN @Cursor FETCH NEXT FROM @Cursor INTO @Sql WHILE (@@FETCH_STATUS = 0) BEGIN Exec sp_executesql @Sql FETCH NEXT FROM @Cursor INTO @Sql END CLOSE @Cursor DEALLOCATE @Cursor GO EXEC sp_MSforeachtable 'DROP TABLE ?' GO 

你可以在这里find这个post。 这是Groker的职位。

您也可以使用MSSMS工具(不使用sql)从数据库中删除所有表。 有时这种方式可以更舒适(特别是偶尔进行的时候)

我这样做一步一步如下:

  1. select数据库树(对象资源pipe理器)上的“表”
  2. 按F7打开对象资源pipe理器详细信息视图
  3. 在这个视图中select必须被删除的表(在这个例子中是所有的)
  4. 按Delete直到所有表都被删除(由于键约束/依赖关系,您重复的次数与错误数量相同)

delete用于删除表中的行。 你应该使用drop table来代替。

 EXEC sp_msforeachtable 'drop table [?]' 
 /* Drop all Primary Key constraints */ DECLARE @name VARCHAR(128) DECLARE @constraint VARCHAR(254) DECLARE @SQL VARCHAR(254) SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY TABLE_NAME) WHILE @name IS NOT NULL BEGIN SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME) WHILE @constraint is not null BEGIN SELECT @SQL = 'ALTER TABLE [dbo].[' + RTRIM(@name) +'] DROP CONSTRAINT [' + RTRIM(@constraint)+']' EXEC (@SQL) PRINT 'Dropped PK Constraint: ' + @constraint + ' on ' + @name SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' AND CONSTRAINT_NAME <> @constraint AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME) END SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY TABLE_NAME) END GO /* Drop all tables */ DECLARE @name VARCHAR(128) DECLARE @SQL VARCHAR(254) SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 ORDER BY [name]) WHILE @name IS NOT NULL BEGIN SELECT @SQL = 'DROP TABLE [dbo].[' + RTRIM(@name) +']' EXEC (@SQL) PRINT 'Dropped Table: ' + @name SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 AND [name] > @name ORDER BY [name]) END GO 

你几乎是正确的,而是使用:

 EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all' EXEC sp_msforeachtable 'DROP TABLE ?' 

但第二行你可能需要多执行一次,直到你停止出错:

 Could not drop object 'dbo.table' because it is referenced by a FOREIGN KEY constraint. 

信息:

 Command(s) completed successfully. 

意味着所有表都被成功删除。

简短而甜美:

 USE YOUR_DATABASE_NAME -- Disable all referential integrity constraints EXEC sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL' GO -- Drop all PKs and FKs declare @sql nvarchar(max) SELECT @sql = STUFF((SELECT '; ' + 'ALTER TABLE ' + Table_Name +' drop constraint ' + Constraint_Name from Information_Schema.CONSTRAINT_TABLE_USAGE ORDER BY Constraint_Name FOR XML PATH('')),1,1,'') EXECUTE (@sql) GO -- Drop all tables EXEC sp_MSforeachtable 'DROP TABLE ?' GO 

似乎命令应该没有方毯子

 EXEC sp_msforeachtable 'drop table ?' 

禁食的方式是:

  1. 新的数据库图表
  2. 添加所有表格
  3. Ctrl + Aselect全部
  4. 右键单击“从数据库中删除”
  5. 按Ctrl + S保存
  6. 请享用

接受的答案不支持Azure。 它使用未logging的存储过程“sp_MSforeachtable”。 如果您在运行时遇到“azure无法find存储过程sp_msforeachtable”错误,或者只是想避免依赖未logging的function(可以删除或在任何时候更改其function),请尝试执行以下操作。

此版本忽略entity framework迁移历史logging表“__MigrationHistory”和“database_firewall_rules”,这是Azure表,您将无权删除。

在Azure上轻松testing。 做检查,这对你的环境没有不良影响。

 DECLARE @sql NVARCHAR(2000) WHILE(EXISTS(SELECT 1 from INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_TYPE='FOREIGN KEY')) BEGIN SELECT TOP 1 @sql=('ALTER TABLE ' + TABLE_SCHEMA + '.[' + TABLE_NAME + '] DROP CONSTRAINT [' + CONSTRAINT_NAME + ']') FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_TYPE = 'FOREIGN KEY' EXEC(@sql) PRINT @sql END WHILE(EXISTS(SELECT * from INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME != '__MigrationHistory' AND TABLE_NAME != 'database_firewall_rules')) BEGIN SELECT TOP 1 @sql=('DROP TABLE ' + TABLE_SCHEMA + '.[' + TABLE_NAME + ']') FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME != '__MigrationHistory' AND TABLE_NAME != 'database_firewall_rules' EXEC(@sql) PRINT @sql END 

取自:

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/

如何删除整个数据库,然后再次创build它? 这对我有用。

 DROP DATABASE mydb; CREATE DATABASE mydb;