Delete all tables below a database

Source: Internet
Author: User

Use test
--Delete all (constraint F)
DECLARE @countF INT
SET @countF = (SELECT COUNT (*) from test.sys.objects WHERE type = ' F ')
PRINT ' A total of ' + CAST (@countF as VARCHAR (5)) + ' constraint. ‘
While @countF >0
BEGIN
DECLARE @F_Name VARCHAR (50)
DECLARE @tbName VARCHAR (50)
DECLARE @Cmd VARCHAR (100)
DECLARE @parent_Object_ID INT
SET @F_Name = (SELECT TOP (1) Name from test.sys.objects
WHERE name in (SELECT t.name from test.sys.objects as [t]) and type= ' F '
)
SET @parent_Object_ID = (SELECT parent_object_id from test.sys.objects WHERE [email protected]_name)
SET @tbName = (SELECT name from Test.sys.tables WHERE [email protected]_object_id)
SELECT @Cmd = ' ALTER TABLE ' + @tbName + ' DROP constraint ' + @F_Name
PRINT @Cmd
EXEC (@Cmd)

SET @[email protected]
END

--Delete all the tables
DECLARE @temp INT
SET @temp = (SELECT COUNT (*) from Sys.tables)
PRINT ' A total of ' + CAST (@temp as VARCHAR (20)) + ' table. ‘
While @temp >0
BEGIN
DECLARE @tbName VARCHAR (50)
SET @tbName = (select TOP (1) name from Sys.tables WHERE name in (select T.name from sys.tables as [t]))
EXEC (' DROP TABLE ' + @tbName)
PRINT ' table: ' + @tbName + ' Delete succeeded! ‘
SET @[email protected]
END

Delete all tables below a 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.