Cấp bậc tác giả:

DATABASE

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

Được viết bởi webmaster ngày 04/08/2013 lúc 06:38 PM
Hướng dẫn viết thủ tục xóa hết Procedures có trong Database
  • 0
  • 9381

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

Thay vì phải xóa từng thủ tục 1, thì đây là cách hay để giải quyết khó khăn mà chúng ta thường mắc phải trong việc sử dụng Database

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

Nguồn bài viết: DOTNET.VN

BÌNH LUẬN BÀI VIẾT

Bài viết mới nhất

LIKE BOX

Bài viết được xem nhiều nhất

HỌC HTML