views:

39

answers:

4

I have group of stored procedures with names like 'somename_%'. Are there any way to delete that SP with one query, forexample

DROP PROCEDURE where name like 'somename_%'

.

+1  A: 

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)
Martin Smith
It works great!
Kate
Good stuff. Maybe it would be a bit neater to use the QUOTENAME function rather than concatenating but glad that it works!
Martin Smith
@Kate: If this works for you then you should **edit your question** to add the `sql-server` tag, and **accept the answer** (http://meta.stackoverflow.com/questions/5234/accepting-answers-what-is-it-all-about)
Peter Lang
A: 

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.

Joel Goodwin
A: 

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_%';
Jeffrey Kemp
Thank you, but I use MS SQL
Kate
Sorry, I should have known.
Jeffrey Kemp
A: 

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;
ebis