按名称删除存储过程组

时间:2010-03-15 09:13:42

标签: sql tsql stored-procedures

我有一组名为'somename_%'的存储过程。有没有办法用一个查询删除该SP,例如

  

DROP PROCEDURE的名字就像   'somename _%'

5 个答案:

答案 0 :(得分:12)

这适用于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)

答案 1 :(得分:2)

您可以通过查询数据字典来生成DDL。例如,在Oracle中:

SELECT 'DROP PROCEDURE "'||owner||'"."'||object_name||'";'
FROM all_procedures
WHERE procedure_name IS NULL
AND lower(object_name) LIKE 'somename_%';

答案 2 :(得分:0)

我总是倾向于做这些事情的方式只是使用我的批评从系统表中提取列表过程,然后创建命令列表 - 直接在sql例如SELECT 'DROP PROCEDURE ' + procName FROM system_procedures_table WHERE procName like...或在Excel中。

答案 3 :(得分:0)

在MS_Sql-Server中,您可以使用“FOR XML PATH('')”子句创建一个包含所有相关过程的语句(ab)...

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;

答案 4 :(得分:0)