Howto: Batch delete stored procedures and tables

Source: Internet
Author: User
1. delete a stored procedure Code
Begin   Transaction

Declare Drop_p_cursor Cursor   For
Select   ' Drop procedure '   +   [ Name ]   From Sysobjects Where Xtype =   ' P '   And Category =   0

OpenDrop_p_cursor

Declare   @ SQL Sysname
Fetch   Next   From Drop_p_cursor Into   @ SQL
While ( @ Fetch_status   =   0 )
Begin
Exec ( @ SQL )
Fetch   Next   From Drop_p_cursor Into   @ SQL

2. delete a table
Note: When deleting a table, delete the constraint first. Nocheck does not work in this example. Drop is required.

Code
Begin   Transaction
Declare   @ SQL Sysname

Declare Drop_u_cursor Cursor   For
Select   ' Drop table '   +   [ Name ]   From Sysobjects Where Xtype =   ' U '   And Category =   0
Declare Forbiden_f_cursor Cursor   For
Select   ' ALTER TABLE '   + TBL. Name +   ' Drop Constraint '   + CST. Name
From Sysobjects TBL, sysobjects CST Where CST. xtype =   ' F '   And CST. parent_obj = TBL. ID

Open Forbiden_f_cursor
Fetch   Next   From Forbiden_f_cursor Into   @ SQL
While ( @ Fetch_status   =   0 )
Begin
Print   @ SQL
Exec ( @ SQL )
Fetch   Next   From Forbiden_f_cursor Into   @ SQL
End
Close Forbiden_f_cursor
Deallocate Forbiden_f_cursor

Open Drop_u_cursor
Fetch   Next   From Drop_u_cursor Into   @ SQL
While ( @ Fetch_status   =   0 )
Begin
Print   @ SQL
Exec ( @ SQL )
Fetch   Next   From Drop_u_cursor Into   @ SQL
End

CloseDrop_u_cursor
DeallocateDrop_u_cursor

Go
If   @ Error   <>   0
Begin
Rollback   Transaction
Return
End
Else
  Commit   Transaction  

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.