Batch Delete tables and stored procedures using SQL.

Source: Internet
Author: User

Recently, I used Godaddy space. Due to the large number of tables in the system, it was very troublesome to delete them one by one. I collected some solutions online.

To share with you:

 

1. Batch delete stored procedure declare @ procname varchar (500)

Declare cur cursor

For select [name] From SYS. objects where type = 'P'

Open cur

Fetch next from cur into @ procname

While @ fetch_status = 0

Begin

If @ procname <> 'deleteallprocedures'

Exec ('drop procedure '+ @ procname)

Fetch next from cur into @ procname

End

Close cur

Deallocate cur
2. Batch Delete Foreign keys

Declare C1 cursor
Select 'alter table ['+ object_name (parent_obj) +'] Drop constraint ['+ name +'];'
From sysobjects
Where xtype = 'F'
Open C1
Declare @ C1 varchar (8000)
Fetch next from C1 into @ C1
While (@ fetch_status = 0)
Begin
Exec (@ C1)
Fetch next from C1 into @ C1
End
Close C1
Deallocate C1
3. Batch Delete tables

Declare C2 cursor
Select 'drop table ['+ name +'];'
From sysobjects
Where xtype = 'U'
Open C2
Declare @ C2 varchar (8000)
Fetch next from C2 into @ C2
While (@ fetch_status = 0)
Begin
Exec (@ C2)
Fetch next from C2 into @ C2
End
Close C2
Deallocate C2
-- Batch clear table content:

-- 1. Disable foreign key constraints
Declare C1 cursor
Select 'alter table ['+ object_name (parent_obj) +'] nocheck constraint ['+ name +'];'
From sysobjects
Where xtype = 'F'
Open C1
Declare @ C1 varchar (8000)
Fetch next from C1 into @ C1
While (@ fetch_status = 0)
Begin
Exec (@ C1)
Fetch next from C1 into @ C1
End
Close C1
Deallocate C1
-- 2. Clear table content
Declare C2 cursor
Select 'truncate table ['+ name +'];'
From sysobjects
Where xtype = 'U'
Open C2
Declare @ C2 varchar (8000)
Fetch next from C2 into @ C2
While (@ fetch_status = 0)
Begin
Exec (@ C2)
Fetch next from C2 into @ C2
End
Close C2
Deallocate C2
-- 3. enable foreign key constraints
Declare C1 cursor
Select 'alter table ['+ object_name (parent_obj) +'] Check constraint ['+ name +'];'
From sysobjects
Where xtype = 'F'
Open C1
Declare @ C1 varchar (8000)
Fetch next from C1 into @ C1
While (@ fetch_status = 0)
Begin
Exec (@ C1)
Fetch next from C1 into @ C1
End
Close C1
Deallocate C1

 

 

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.