MSSQL deletes all tables in the database

Source: Internet
Author: User
Tags mssql

<pre name= "code" class= "HTML" > for this operation we can go first to understand the system table sysobjects (there is written on the blog), and drop, truncate,delete the difference.
------------Delete the foreign key constraint for all tables (drop) when deleting the data (Truncate;delete)-------DECLARE C1 cursor forselect ' ALTER TABLE [' + object_name (Parent_obj) + '] drop constraint [' +name+ ']; ' From sysobjectswhere xtype = ' F ' Open c1--create cursor declare @c1 varchar (8000) fetch next from C1 into @c1while (@ @fetch_status =0) b Eginexec (@c1) fetch next from C1 to @c1endclose c1deallocate C1--------------------Delete all tables-------------------------- Use database name Godeclare @sql varchar (8000) while (select COUNT (*) from sysobjects where type= ' U ') >0beginselect @sql = ' drop tab Le ' + namefrom sysobjectswhere (type = ' U ') ORDER by ' drop table ' + nameexec (@sql) End

<pre name= "code" class= "HTML" >    1. Truncate and delete only deletes data without deleting the structure of the table (definition) The    drop statement will delete the structure of the table that is dependent on the constraint (constrain), A trigger (trigger), index, or stored procedure/function that relies on the table will remain, but becomes invalid (invalid).       2. Truncate delete all records, reset the table (most obviously the automatic ID zero), delete the data will not be backed up; Delete delete record, automatic ID continue delect Delete will be a DDL language, like all other DDL languages, he will be implicitly committed, You cannot use the rollback command with truncate.     3. Be careful with drop and truncate, with delete and the Where condition, of course, customers that most of them will have backup three actually no priority, appropriate time with the appropriate method   # to delete some data rows with delete, note the WHERE clause .  # Want to delete the table, of course, with the drop      # want to keep the table and all the data deleted, if not related to the transaction, with truncate. If it is related to a transaction, or if you want to trigger trigger, use Delete.      # If you are defragmenting the inside of the table, you can use truncate to keep up with reuse stroage and re-import/insert the data.


MSSQL deletes all tables in the database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.