Deleting all stored procedures, views or functions

From KnowWiki
Jump to: navigation, search

The following scripts cleans up an MS SQL database of stored procedures, views and/or functions. Sourced from a forum posting by psyco-simo.

Download

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
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox