SQL wholesale table deletion and stored procedure Deletion

Source: Internet
Author: User

The main idea is as follows:

First, find the name of the table to be processed or the name of the stored procedure in the system table, and process it with a cursor.

Note that the values of sysobjects. xtype are different deletion commands. For example, when you delete a stored procedure, you can use drop procedure procedurename to delete a table. The value of drop table tablename sysobjects. xtype indicates the following table:

C: Check constraints.

D: Default Constraint

F: foreign key constraint

L: Log

P: Stored Procedure

PK: primary key constraint

RF: copying and filtering stored procedures

S: system table

TR: trigger

U: used for table cells.

Uq: unique constraints.

Batch ProcessingCodeAs follows:

Declare cursorname cursor for select 'drop procedure '+ name from sysobjects where name like 'xx %' and xtype = 'P' -- delete the corresponding Stored Procedure

Declare cursorname cursor for select 'drop table' + name from sysobjects where name like 'xx % 'and xtype = 'U' -- delete the corresponding table

Open cursorname

Declare @ curname sysname

Fetch next from cursorname into @ curname

While (@ fetch_status = 0)

Begin

Exec (@ curname)

Fetch next from cursorname into @ curname

End

Close cursorname

Deallocate cursorname

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.