Batch Delete SQL Server objects (tables, stored procedures, triggers)

Source: Internet
Author: User

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.

PS: sqlserver 2000 uses the system table sysobjects and the type field is xtype. The system table of SQL Server 2005 or later is sys. Objects and the type field is type.

This article uses sql2005 as an example. Please replace SQL2000 according to the above instructions.

Note that different delete commands have different types in SYS. Objects.

For example, if you delete the stored procedure, use drop procedure procedurename to delete the table, and use drop table tablename to delete the trigger, use drop trigger triggername.

The meanings of SYS. Objects. type are as follows:
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 tables.
Uq: unique constraints.

Batch ProcessingCodeAs follows:
Declare cursorname cursor for select 'drop procedure '+ name from SYS. objects where name like 'xx %' and xtype = 'P' -- delete the corresponding Stored Procedure
Declare cursorname cursor for select 'drop trigger' + name from SYS. objects where name like 'xx % 'and xtype = 'tr' -- delete the corresponding trigger

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

Related Article

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.