Home > SQL Server > Xóa tất cả Procedures từ 1 database trong SQL Server

Xóa tất cả Procedures từ 1 database trong SQL Server

1. Xóa store procedures

Create Procedure dbo.DeleteAllProcedures
As
 declare @procName varchar(500)
 declare cur cursor
 for select [name] from sys.objects where type = 'p'
 open cur
 
 fetch next from cur into @procName
 while @@fetch_status = 0
 begin
 if @procName <> 'DeleteAllProcedures'
 exec('drop procedure ' + @procName)
 fetch next from cur into @procName
 end
 close cur
 deallocate cur
Go
 Grant Execute On dbo.DeleteAllProcedures To Public
Go

2. Xóa store procedures + views + functions

USE [db_name]
 
CREATE PROCEDURE dbo.__DeleteAllProcedures
As
declare @procName varchar(500)
 
-- Removes stored procedures
declare cur cursor
for select [name] from sys.objects where [type] = 'p'
open cur
fetch next from cur into @procName
while @@fetch_status = 0
begin
if @procName <> '__DeleteAllProcedures'
exec('drop procedure ' + @procName)
fetch next from cur into @procName
end
close cur
deallocate cur
 
-- Removes Views
declare cur cursor
for select [name] from sys.objects where [type] = 'v'
open cur
fetch next from cur into @procName
while @@fetch_status = 0
begin
exec('drop view ' + @procName)
fetch next from cur into @procName
end
close cur
deallocate cur
 
-- Removes Functions
declare cur cursor
for select [name] from sys.objects WHERE [type] = 'fn'
open cur
fetch next from cur into @procName
while @@fetch_status = 0
begin
exec('drop function ' + @procName)
fetch next from cur into @procName
end
close cur
deallocate cur
 
-- removes itselfs
DROP PROCEDURE __DeleteAllProcedures
 
Go
 
exec
__DeleteAllProcedures

 

Advertisements
Categories: SQL Server
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: