Delete all data in SQL Server database

前端 未结 10 1063
粉色の甜心
粉色の甜心 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 06:07

    I'm aware this is late, but I agree with AlexKuznetsov's suggestion to script the database, rather than going through the hassle of purging the data from the tables. If the TRUNCATE solution will not work, and you happen to have a large amount of data, issuing (logged) DELETE statements might take a long time, and you'll be left with identifiers that have not been reseeded (i.e. an INSERT statement into a table with an IDENTITY column would get you an ID of 50000 instead of an ID of 1).

    To script a whole database, in SSMS, right-click the database, then select TASKS -> Generate scripts:

    enter image description here

    Click Next to skip the Wizard opening screen, and then select which objects you want to script:

    enter image description here

    In the Set scripting options screen, you can pick settings for the scripting, like whether to generate 1 script for all the objects, or separate scripts for the individual objects, and whether to save the file in Unicode or ANSI:

    enter image description here

    The wizard will show a summary, which you can use to verify everything is as desired, and close by clicking on 'Finish'.

提交回复
热议问题