我正在尝试编写一个脚本,该脚本将完全清空SQL Server数据库。这是我到目前为止的内容:
USE [dbname]
GO
EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all'
EXEC sp_msforeachtable 'DELETE ?'
在Management Studio中运行它时,我得到:
命令已成功完成。
但是当我刷新表格列表时,它们仍然存在。我究竟做错了什么?
我正在尝试编写一个脚本,该脚本将完全清空SQL Server数据库。这是我到目前为止的内容:
USE [dbname]
GO
EXEC sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT all'
EXEC sp_msforeachtable 'DELETE ?'
在Management Studio中运行它时,我得到:
命令已成功完成。
但是当我刷新表格列表时,它们仍然存在。我究竟做错了什么?
Answers:
当有多个外键表时,它对我也不起作用。
我发现该代码可以正常工作,并且可以完成您尝试的所有操作(从数据库中删除所有表):
DECLARE @Sql NVARCHAR(500) DECLARE @Cursor CURSOR
SET @Cursor = CURSOR FAST_FORWARD FOR
SELECT DISTINCT sql = 'ALTER TABLE [' + tc2.TABLE_SCHEMA + '].[' + 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
你可以在这里找到帖子。这是Groker的帖子。
Could not find stored procedure 'sp_MSForEachTable'.
dbo
。如果有自定义模式,您需要更改sql
到:SELECT DISTINCT sql = 'ALTER TABLE [' + tc2.CONSTRAINT_SCHEMA + '].[' + tc2.TABLE_NAME + '] DROP [' + rc1.CONSTRAINT_NAME + ']'
sp_MSforeachtable
在Azure中不可用。请使用@CountZero的答案。
您也可以仅使用MSSMS UI工具(不使用SQL脚本)从数据库中删除所有表。有时,这种方式可能会更舒适(尤其是偶尔执行)
我按如下步骤进行操作:
在SSMS中:
现在,这将删除所有内容,包括数据库。确保删除不想删除的项目的代码。或者,在“选择对象”部分中,而不是选择对整个数据库编写脚本,只需选择要删除的项目即可。
delete
用于从表中删除行。您应该drop table
改用。
EXEC sp_msforeachtable 'drop table [?]'
NOCHECK CONSTRAINT
行的语法错误吗?
接受的答案不支持Azure。它使用未记录的存储过程“ sp_MSforeachtable”。如果在运行时遇到“无法找到存储过程'sp_msforeachtable”的错误,或者只是想避免依赖未记录的功能(可以随时删除或更改其功能),请尝试以下操作。
此版本忽略实体框架迁移历史记录表“ __MigrationHistory”和“ database_firewall_rules”(这是您无权删除的Azure表)。
在Azure上进行了轻松的测试。请进行检查,以确保这不会对您的环境造成不良影响。
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/
/* 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
sp_msforeachtable
为sp_MSforeachtable
,强烈建议添加use yourdatabase
第一行。像魅力一样工作。
对我来说,最简单的方法是:
--First delete all constraints
DECLARE @sql NVARCHAR(MAX);
SET @sql = N'';
SELECT @sql = @sql + N'
ALTER TABLE ' + QUOTENAME(s.name) + N'.'
+ QUOTENAME(t.name) + N' DROP CONSTRAINT '
+ QUOTENAME(c.name) + ';'
FROM sys.objects AS c
INNER JOIN sys.tables AS t
ON c.parent_object_id = t.[object_id]
INNER JOIN sys.schemas AS s
ON t.[schema_id] = s.[schema_id]
WHERE c.[type] IN ('D','C','F','PK','UQ')
ORDER BY c.[type];
EXEC sys.sp_executesql @sql;
-- Then drop all tables
exec sp_MSforeachtable 'DROP TABLE ?'
发现!!
您可以使用下面的查询从数据库中删除所有表
EXEC sp_MSforeachtable @ command1 =“ DROP TABLE吗?”
编码愉快!
如何删除整个数据库然后再次创建它?这对我有用。
DROP DATABASE mydb;
CREATE DATABASE mydb;
我知道这是一篇老文章,但是我已经在许多数据库上尝试过这里所有的答案,但我发现它们有时都可以工作,但对于SQL Server的各种(我只能假设)怪癖来说,并非总是如此。
最终我想到了这个。我已经在所有地方(通常来说)进行了测试,并且可以正常工作(没有任何隐藏的存储过程)。
需要注意的主要是有关SQL Server 2014的信息。(但是我尝试过的大多数其他版本似乎也能正常工作)。
我尝试了while循环和null等,游标和各种其他形式,但是它们似乎总是在某些数据库上失败,而在其他数据库上却没有明显的原因而失败。
在我测试过的所有内容上,获取计数并使用它进行迭代似乎总是可行的。
USE [****YOUR_DATABASE****]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Drop all referential integrity constraints --
-- Drop all Primary Key constraints. --
DECLARE @sql NVARCHAR(296)
DECLARE @table_name VARCHAR(128)
DECLARE @constraint_name VARCHAR(128)
SET @constraint_name = ''
DECLARE @row_number INT
SELECT @row_number = Count(*) FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc1
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc2 ON tc2.CONSTRAINT_NAME = rc1.CONSTRAINT_NAME
WHILE @row_number > 0
BEGIN
BEGIN
SELECT TOP 1 @table_name = tc2.TABLE_NAME, @constraint_name = rc1.CONSTRAINT_NAME FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc1
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc2 ON tc2.CONSTRAINT_NAME = rc1.CONSTRAINT_NAME
AND rc1.CONSTRAINT_NAME > @constraint_name
ORDER BY rc1.CONSTRAINT_NAME
SELECT @sql = 'ALTER TABLE [dbo].[' + RTRIM(@table_name) +'] DROP CONSTRAINT [' + RTRIM(@constraint_name)+']'
EXEC (@sql)
PRINT 'Dropped Constraint: ' + @constraint_name + ' on ' + @table_name
SET @row_number = @row_number - 1
END
END
GO
-- Drop all tables --
DECLARE @sql NVARCHAR(156)
DECLARE @name VARCHAR(128)
SET @name = ''
DECLARE @row_number INT
SELECT @row_number = Count(*) FROM sysobjects WHERE [type] = 'U' AND category = 0
WHILE @row_number > 0
BEGIN
SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 AND [name] > @name ORDER BY [name])
SELECT @sql = 'DROP TABLE [dbo].[' + RTRIM(@name) +']'
EXEC (@sql)
PRINT 'Dropped Table: ' + @name
SET @row_number = @row_number - 1
END
GO
对于Temporal Tables,由于可能存在一些外键和例外,所以它稍微复杂一些:
Drop table operation failed on table XXX because it is not a supported operation on system-versioned temporal tables
您可以使用的是:
-- Disable constraints (foreign keys)
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
GO
-- Disable system versioning (temporial tables)
EXEC sp_MSForEachTable '
IF OBJECTPROPERTY(object_id(''?''), ''TableTemporalType'') = 2
ALTER TABLE ? SET (SYSTEM_VERSIONING = OFF)
'
GO
-- Removing tables
EXEC sp_MSForEachTable 'DROP TABLE ?'
GO