SQL Server deletes all tables (table structure and data)

Source: Internet
Author: User

To delete a database, or to delete all tables in the database (delete table structure and data), you need to delete the foreign key constraint between the tables before you can delete the table. such as deleting all tables in the database Db_wy:

--/1th Step ********** Delete all table foreign KEY constraints *************************/declare C1 cursor forselect ' ALTER TABLE [' + object_name (parent_ obj) + '] drop constraint [' +name+ ']; ' From sysobjectswhere xtype = ' F ' open c1declare @c1 varchar (8000) fetch next from C1 into @c1while (@ @fetch_status =0) Beginex EC (@c1) fetch next from C1 to @c1endclose c1deallocate c1--/2nd step ********** Delete all tables *************************/use db_ Wygodeclare @sql varchar (8000) while (select COUNT (*) from sysobjects where type= ' U ') >0beginselect @sql = ' drop table ' + Namefrom sysobjectswhere (type = ' U ') ORDER by ' drop table ' + nameexec (@sql) End

  

SQL Server deletes all tables (table structure and data)

Related Article

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.