Delete all data in SQL Server database

前端 未结 10 1048
粉色の甜心
粉色の甜心 2020-12-04 05:11

How I can delete all records from all tables of my database? Can I do it with one SQL command or I need for one SQL command per one table?

相关标签:
10条回答
  • 2020-12-04 05:48

    Below a script that I used to remove all data from an SQL Server database

    ------------------------------------------------------------
    /* Use database */ 
    -------------------------------------------------------------
    
    use somedatabase;
    
    GO
    
    ------------------------------------------------------------------
    /* Script to delete an repopulate the base [init database] */
    ------------------------------------------------------------------
    
    -------------------------------------------------------------
    /* Procedure delete all constraints */ 
    -------------------------------------------------------------
    
    IF EXISTS (SELECT name  
               FROM  sysobjects 
               WHERE name = 'sp_DeleteAllConstraints' AND type = 'P')
        DROP PROCEDURE dbo.sp_DeleteAllConstraints
    GO
    
    CREATE PROCEDURE sp_DeleteAllConstraints
    AS
        EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
        EXEC sp_MSForEachTable 'ALTER TABLE ? DISABLE TRIGGER ALL'
    GO
    
    -----------------------------------------------------
    /* Procedure delete all data from the database */ 
    -----------------------------------------------------
    
    IF EXISTS (SELECT name  
               FROM  sysobjects 
               WHERE name = 'sp_DeleteAllData' AND type = 'P')
        DROP PROCEDURE dbo.sp_DeleteAllData
    GO
    
    CREATE PROCEDURE sp_DeleteAllData
    AS
        EXEC sp_MSForEachTable 'DELETE FROM ?'
    GO
    
    -----------------------------------------------
    /* Procedure enable all constraints */ 
    -----------------------------------------------
    
    IF EXISTS (SELECT name  
               FROM  sysobjects 
               WHERE name = 'sp_EnableAllConstraints' AND type = 'P')
        DROP PROCEDURE dbo.sp_EnableAllConstraints
    GO
    -- ....
    -- ....
    -- ....
    
    0 讨论(0)
  • 2020-12-04 05:53

    As an alternative answer, if you Visual Studio SSDT or possibly Red Gate Sql Compare, you could simply run a schema comparison, script it out, drop the old database (possibly make a backup first in case there would be a reason that you will need that data), and then create a new database with the script created by the comparison tool. While on a very small database this may be more work, on a very large database it will be much quicker to simply drop the database then to deal with the different triggers and constraints that may be on the database.

    0 讨论(0)
  • 2020-12-04 05:53

    Yes, it is possible to delete with a single line of code

    SELECT 'TRUNCATE TABLE ' + d.NAME + ';' 
    FROM   sys.tables d 
    WHERE  type = 'U' 
    
    0 讨论(0)
  • 2020-12-04 05:56

    Usually I will just use the undocumented proc sp_MSForEachTable

    -- disable referential integrity
    EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL' 
    GO 
    
    EXEC sp_MSForEachTable 'TRUNCATE TABLE ?' 
    GO 
    
    -- enable referential integrity again 
    EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL' 
    GO
    

    See also: Delete all data in database (when you have FKs)

    0 讨论(0)
  • 2020-12-04 05:56

    It is usually much faster to script out all the objects in the database, and create an empty one, that to delete from or truncate tables.

    0 讨论(0)
  • 2020-12-04 06:03
    1. First you'll have to disable all the triggers :

      sp_msforeachtable 'ALTER TABLE ? DISABLE TRIGGER all';
      
    2. Run this script : (Taken from this post Thank you @SQLMenace)

      SET NOCOUNT ON
      GO
      
      SELECT 'USE [' + db_name() +']';
      ;WITH a AS 
      (
           SELECT 0 AS lvl, 
                  t.object_id AS tblID 
           FROM sys.TABLES t
           WHERE t.is_ms_shipped = 0
             AND t.object_id NOT IN (SELECT f.referenced_object_id 
                                     FROM sys.foreign_keys f)
      
           UNION ALL
      
           SELECT a.lvl + 1 AS lvl, 
                  f.referenced_object_id AS tblId
           FROM a
           INNER JOIN sys.foreign_keys f ON a.tblId = f.parent_object_id 
                                         AND a.tblID <> f.referenced_object_id
      )
      SELECT 
          'Delete from ['+ object_schema_name(tblID) + '].[' + object_name(tblId) + ']' 
      FROM a
      GROUP BY tblId 
      ORDER BY MAX(lvl),1
      

    This script will produce DELETE statements in proper order. starting from referenced tables then referencing ones

    1. Copy the DELETE FROM statements and run them once

    2. enable triggers

      sp_msforeachtable 'ALTER TABLE ? ENABLE TRIGGER all'
      
    3. Commit the changes :

      begin transaction
      commit;
      
    0 讨论(0)
提交回复
热议问题