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

Source: Internet
Author: User

Find the table name or the name of the stored procedure in the system table before processing it with a cursor

Ps:sqlserver 2000 is using the system table is sysobjects, the Type field is: xtype; The system table for SQL Server 2005 or later is sys.objects, and the Type field is type

In this article, for example, Sql2005, Sql2000 version, please follow the above instructions to replace


Note the value of type in sys.objects is different from the delete command

Delete a trigger with DROP table tablename Drop Trigger triggername If you delete a stored procedure by dropping PROCEDURE procedurename

The value of the sys.objects.type is expressed in the following table:
C: Check the constraint.
D: Default constraint
F: FOREIGN KEY constraint
L: Log
P: Stored Procedure
PK: PRIMARY KEY constraint
RF: Replication Filtering stored Procedures
S: System table
TR: Trigger
U: For tables.
UQ: a unique constraint.

The code for batch processing is as follows:
DECLARE cursorname cursor FOR select ' Drop PROCEDURE ' +name from sys.objects where name like ' xx% ' and xtype = ' P '--delete corresponding The stored procedures
DECLARE cursorname cursor FOR select ' Drop Trigger ' +name from sys.objects where name like ' xx% ' and xtype = ' TR '--delete corresponding trigger Manager

Open Cursorname
declare @curname sysname
FETCH NEXT from Cursorname to @curname
while (@ @fetch_status =0)
Begin
EXEC (@curname)
FETCH NEXT from Cursorname to @curname
End
Close Cursorname
Deallocate Cursorname

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

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.