Delete a database for all trigger and SP in SQL

Source: Internet
Author: User

SQLDelete all trigger and SP from a database

Author : CC Abba

2014-6-14

in Daily SQL database operation, how to quickly delete all Trigger and SP ?

There are three ways to handle this quickly.

-- First Kind

-- How transactions are handled

Begin Transaction

Begin try

Declare @SQL varchar(max)

Set @SQL="

select Span style= "Font-size:10.0pt;font-family:"courier new";" > @SQL = @SQL Span style= "Color:gray" >+ name + from sysobjects where xtype = ' TR ' and name <> ' dropdatabase '

If ISNULL(@SQL,')! ='

Begin

Set @SQL=' Drop Trigger '+left (@SQL,len( @SQL )-1)

Select @SQL as aa

--exec (@SQL)

End

Commit Transaction

End Try

Begin Catch

rollback Tran

End Catch

-- The second method of

-- the way the cursor is used

--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 '% ' and type = ' TR ' --delete the corresponding trigger

Open cursorname

Declare @curname sysname

Fetch next from cursorname to @curname

while (@ @fetch_status=0)

begin

--exec (@curname)

Select @curname as AA

Fetch next from cursorname to @curname

End

Close cursorname

deallocate cursorname

-- The third method of

-- Simple method, after querying, then execute

in the database

select Span style= "Font-size:10.0pt;font-family:"courier new";" > ' drop Trigger ' + name from sys objects where name like and type = ' TR '

select Span style= "Font-size:10.0pt;font-family:"courier new";" > ' drop PROCEDURE ' + name from sys objects where name like and type = ' P '

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.