Delete database all tables and all stored procedure SQL statements at once

Source: Internet
Author: User

Delete database all tables and all stored procedure SQL statements at once

Transferring database data today requires emptying the database's original tables and stored procedures.
To delete all tables:
If table failure is deleted because of a foreign KEY constraint, all constraints are removed first:

--/1th Step ********** Delete all table foreign KEY constraints *************************/DECLARE C1 Cursor for Select 'ALTER TABLE ['+ object_name (parent_obj) +'] Drop constraint ['+name+']; ' fromsysobjectswhereXtype ='F'Open c1declare @c1 varchar (8000) FETCH Next fromC1 into @c1 while(@ @fetch_status =0) begin EXEC (@c1) fetch next fromC1 into @c1endclose c1deallocate C1--/2nd Step ********** Delete all tables *************************/Use database declare @tname varchar (8000)Set@tname ="'Select@[email protected] + Name +','  fromsysobjectswhereXtype='U'Select@tname ='drop table'+ Left (@tname, Len (@tname)-1) EXEC (@tname)----empty stored procedure use [database name]declare @tname varchar (8000)Set@tname ="'Select@[email protected] + Name +','  fromsysobjectswhereXtype='P'Select@tname ='Drop PROCEDURE'+ Left (@tname, Len (@tname)-1) EXEC (@tname)

Transferred from: http://www.cnblogs.com/a-zx/articles/2405121.html

Delete database all tables and all stored procedure SQL statements at once

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.