如何从SQL Server数据库的所有表中删除所有行?
如何从SQL Server数据库的所有表中删除所有行?
请注意,如果您有任何参照完整性设置,TRUNCATE将不起作用。
在这种情况下,这将工作:
EXEC sp_MSForEachTable 'DISABLE TRIGGER ALL ON ?' GO EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL' GO EXEC sp_MSForEachTable 'DELETE FROM ?' GO EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL' GO EXEC sp_MSForEachTable 'ENABLE TRIGGER ALL ON ?' GO
我不得不删除所有的行,并使用下一个脚本:
DECLARE @Nombre NVARCHAR(MAX); DECLARE curso CURSOR FAST_FORWARD FOR Select Object_name(object_id) AS Nombre from sys.objects where type = 'U' OPEN curso FETCH NEXT FROM curso INTO @Nombre WHILE (@@FETCH_STATUS <> -1) BEGIN IF (@@FETCH_STATUS <> -2) BEGIN DECLARE @statement NVARCHAR(200); SET @statement = 'DELETE FROM ' + @Nombre; print @statement execute sp_executesql @statement; END FETCH NEXT FROM curso INTO @Nombre END CLOSE curso DEALLOCATE curso
希望这可以帮助!
在我最近的项目中,我的任务是使用sql语句清理整个数据库,每个表都有许多约束,如主键和外键。 数据库中有超过1000个表,所以不可能在每个表上写一个删除查询。
通过使用一个名为sp_MSForEachTable的存储过程,该存储过程允许我们轻松处理针对单个数据库中的每个表的一些代码。 这意味着它被用来针对数据库中的每个表处理单个T-SQL命令或不同的T-SQL命令。
所以请按照以下步骤截断SQL Server数据库中的所有表:
步骤1-使用下面的sql查询禁用数据库的所有约束:
EXEC sys.sp_msforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
步骤2-使用以下sql命令对数据库的每个表执行删除或截断操作:
EXEC sys.sp_msforeachtable 'DELETE FROM ?'
步骤3-通过使用下面的sql语句启用数据库的所有约束:
EXEC sys.sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
Set nocount on Exec sp_MSForEachTable 'Alter Table ? NoCheck Constraint All' Exec sp_MSForEachTable ' If ObjectProperty(Object_ID(''?''), ''TableHasForeignRef'')=1 Begin -- Just to know what all table used delete syntax. Print ''Delete from '' + ''?'' Delete From ? End Else Begin -- Just to know what all table used Truncate syntax. Print ''Truncate Table '' + ''?'' Truncate Table ? End ' Exec sp_MSForEachTable 'Alter Table ? Check Constraint All'
这是一个解决scheme:
- 通过
INFORMATION_SCHEMA.TABLES
迭代特定的数据库 - 根据一些search条件
SELECTS
表格 - 从这些表中删除所有数据
最初我在做EXECUTE sp_MSforeachtable 'TRUNCATE TABLE ?'
,但删除了我的图表:(。它也应该与限制感谢这篇文章。
USE <DB Name>; GO -- Disable all constraints in the database EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all" DECLARE @tbl NVARCHAR(250); DECLARE i CURSOR LOCAL FAST_FORWARD FOR SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME != 'sysdiagrams' OPEN i; FETCH NEXT FROM i INTO @tbl; WHILE @@FETCH_STATUS = 0 BEGIN DECLARE @sql NVARCHAR(MAX) = N'DELETE FROM [' + @tbl + '];' PRINT @sql -- Make sure these are the commands you want to execute, and then uncomment the 'EXECUTE' statement -- EXECUTE sys.sp_executesql @sql FETCH NEXT FROM i INTO @tbl; END CLOSE i; DEALLOCATE i; -- Re-enable all constraints again EXEC sp_msforeachtable "ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all"
您可以使用Rubensbuild议的方法从所有表中删除所有行,也可以删除并重新创build所有表。 总是有一个好主意,无论如何拥有完整的数据库创build脚本,以便可能是最简单/最快的方法。
如果要删除整个表,则必须遵循下一条SQL指令
Delete FROM TABLE Where PRIMARY_KEY_ is Not NULL;
对于一些要求,我们可能不得不跳过某些表格。 我写了下面的脚本来添加一些额外的条件来过滤表的列表。 下面的脚本还会显示预删除计数和删除后计数。
IF OBJECT_ID('TEMPDB..#TEMPRECORDCOUNT') IS NOT NULL DROP TABLE #TEMPRECORDCOUNT CREATE TABLE #TEMPRECORDCOUNT ( TABLENAME NVARCHAR(128) ,PREDELETECOUNT BIGINT ,POSTDELETECOUNT BIGINT ) INSERT INTO #TEMPRECORDCOUNT (TABLENAME, PREDELETECOUNT, POSTDELETECOUNT) SELECT O.name TableName ,DDPS.ROW_COUNT PREDELETECOUNT ,NULL FROM sys.objects O INNER JOIN ( SELECT OBJECT_ID, SUM(row_count) ROW_COUNT FROM SYS.DM_DB_PARTITION_STATS GROUP BY OBJECT_ID ) DDPS ON DDPS.OBJECT_ID = O.OBJECT_ID WHERE O.type = 'U' AND O.name NOT LIKE 'OC%' AND O.schema_id = 1 DECLARE @TableName NVARCHAR(MAX); DECLARE TableDeleteCursor CURSOR FAST_FORWARD FOR SELECT TableName from #TEMPRECORDCOUNT OPEN TableDeleteCursor FETCH NEXT FROM TableDeleteCursor INTO @TableName WHILE (@@FETCH_STATUS <> -1) BEGIN IF (@@FETCH_STATUS <> -2) BEGIN DECLARE @STATEMENT NVARCHAR(MAX); SET @STATEMENT = ' DISABLE TRIGGER ALL ON ' + @TableName + '; ALTER TABLE ' + @TableName + ' NOCHECK CONSTRAINT ALL' + '; DELETE FROM ' + @TableName + '; ALTER TABLE ' + @TableName + ' CHECK CONSTRAINT ALL' + '; ENABLE TRIGGER ALL ON ' + @TableName; PRINT @STATEMENT EXECUTE SP_EXECUTESQL @STATEMENT; END FETCH NEXT FROM TableDeleteCursor INTO @TableName END CLOSE TableDeleteCursor DEALLOCATE TableDeleteCursor UPDATE T SET T.POSTDELETECOUNT = I.ROW_COUNT FROM #TEMPRECORDCOUNT T INNER JOIN ( SELECT O.name TableName, DDPS.ROW_COUNT ROW_COUNT FROM sys.objects O INNER JOIN ( SELECT OBJECT_ID, SUM(row_count) ROW_COUNT FROM SYS.DM_DB_PARTITION_STATS GROUP BY OBJECT_ID ) DDPS ON DDPS.OBJECT_ID = O.OBJECT_ID WHERE O.type = 'U' AND O.name NOT LIKE 'OC%' AND O.schema_id = 1 ) I ON I.TableName COLLATE DATABASE_DEFAULT = T.TABLENAME SELECT * FROM #TEMPRECORDCOUNT ORDER BY TABLENAME ASC