Drop group of stored procedures by name
I have group of stored procedures with names like 'somename_%'. Are there any way to delete that SP with one query, forexample
开发者_Go百科DROP PROCEDURE where name like 'somename_%'
.
This works for MSSQL 2005 +
DECLARE @DropScript varchar(max)
set @DropScript = ''
SELECT @DropScript = @DropScript + 'DROP PROCEDURE [' + schema_name(schema_id)+ '].' + '[' + name + ']
' FROM sys.procedures
where name like 'somename_%'
exec (@DropScript)
You can generate the DDL by querying the data dictionary. For example, in Oracle:
SELECT 'DROP PROCEDURE "'||owner||'"."'||object_name||'";'
FROM all_procedures
WHERE procedure_name IS NULL
AND lower(object_name) LIKE 'somename_%';
The way I always tend to do these kind of things is just extract the list procedures from the system tables using my critierion and then create the command list - either direct in sql e.g. SELECT 'DROP PROCEDURE ' + procName FROM system_procedures_table WHERE procName like...
or in Excel.
In MS_Sql-Server you cn create a Statement with all the relevant Procedures to drop through (ab)using the "FOR XML PATH ('')" clause...
BEGIN TRANSACTION;
GO
CREATE PROC Test_1 AS
BEGIN;
PRINT '1'
END;
GO
CREATE PROC Test_2 AS
BEGIN;
PRINT '2'
END;
GO
SELECT * FROM sys.objects WHERE name LIKE 'Test%' AND TYPE = 'P';
DECLARE @Stmt NVARCHAR(MAX);
SET @Stmt = ( SELECT 'DROP PROC ' + STUFF (x.Stmt, 1, 2, SPACE(0))
FROM (SELECT ', ' + SCHEMA_NAME(Obj.Schema_ID) + CHAR(46) + Obj.Name
FROM sys.objects AS Obj
WHERE Obj.name LIKE 'Test%'
AND obj.TYPE = 'P'
FOR XML PATH ('')
) AS X (Stmt)
);
SELECT @Stmt;
EXEC sp_ExecuteSQL @Stmt;
SELECT * FROM sys.objects WHERE name LIKE 'Test%' AND TYPE = 'P';
ROLLBACK;
Check this TSQL script that automatically drops a list of stored procedures.
精彩评论