删除特定SQL Server架构中的所有存储过程

时间:2012-02-08 10:21:22

标签: sql-server tsql stored-procedures

我有数百个由DataSync自动生成的程序。

我没有时间和感觉手动删除它们。

它们都以DataSync.

开头

有没有办法删除名称以DataSync.开头的所有存储过程?

6 个答案:

答案 0 :(得分:22)

使用information_schema.routines(在MSDB,Mysql等RDBMS中相当标准):

如果你的proc名称开始“DataSync”。然后他们可能在一个模式中,所以你可以找到它们:

select
    'DROP PROCEDURE [' + routine_schema + '].[' + routine_name + ']'
from 
    information_schema.routines where routine_schema = 'DataSync' and routine_type = 'PROCEDURE'

如果您的proc名称以“DataSync”开头,那么您可以通过以下方式找到它们:

select
    'DROP PROCEDURE [' + routine_schema + '].[' + routine_name + ']'
from 
    information_schema.routines where routine_name like 'DataSync%' and routine_type = 'PROCEDURE'

如果要执行所有这些drop语句,可以使用FOR XML PATH构建单个执行,如下所示:

declare @sql varchar(max)

set @sql = (
select
    'DROP PROCEDURE [' + routine_schema + '].[' + routine_name + '] ' 
from 
    information_schema.routines where routine_schema = 'DataSync' and routine_type = 'PROCEDURE'
FOR XML PATH ('')
)

exec (@sql)

答案 1 :(得分:5)

当您指定“SQL”时,假设您的意思是 SQL Server - 那么最简单的方法是:运行此查询:

SELECT 
    name, 
    DropCmd = 'DROP PROCEDURE DataSync.' + name 
FROM sys.procedures
WHERE 
     schema_id = SCHEMA_ID('DataSync')

和偶数“懒惰”版本将使用光标自动为您执行此操作:

DECLARE DropSpCursor CURSOR FAST_FORWARD FOR
    SELECT 
        name
    FROM sys.procedures
    WHERE schema_id = SCHEMA_ID('DataSync')

DECLARE @StoredProcName sysname
DECLARE @DropStatement NVARCHAR(1000)

OPEN DropSpCursor

FETCH NEXT FROM DropSpCursor INTO @StoredProcName, @SchemaName

WHILE (@@fetch_status <> -1)
BEGIN
    IF (@@fetch_status <> -2)
    BEGIN
        SET @DropStatement = N'DROP PROCEDURE DataSync.' + @StoredProcName
        EXEC(@DropStatement)
    END

    FETCH NEXT FROM DropSpCursor INTO @StoredProcName
END

CLOSE DropSpCursor 
DEALLOCATE DropSpCursor 

答案 2 :(得分:0)

DECLARE @name  AS VARCHAR(max)

DECLARE MyCursor CURSOR FAST_FORWARD READ_ONLY FOR
SELECT name FROM sys.objects WHERE type='P' AND schema_id=SCHEMA_ID('DataSync')

OPEN MyCursor

FETCH NEXT FROM MyCursor INTO @name

WHILE @@FETCH_STATUS = 0
BEGIN
    EXEC('DROP PROCEDURE DataSync.' + @name)
    FETCH NEXT FROM MyCursor INTO @name
END

CLOSE MyCursor
DEALLOCATE MyCursor   

编辑:更改了where子句,因为事实证明DataSync是模式名称。

答案 3 :(得分:0)

我一直在使用这个简短的脚本清除给定模式中的所有SP(使用SQL Server时)。它迭代直接sys.procedures。

DECLARE @schema VARCHAR(100)
SET @schema = 'DataSync'

DECLARE @CurrentStatement AS VARCHAR(MAX)
SET @CurrentStatement = (SELECT TOP(1)  'DROP PROCEDURE [' + @schema + '].[' + name + ']'
FROM sys.procedures 
WHERE schema_id = SCHEMA_ID(@schema))

WHILE @CurrentStatement IS NOT NULL
BEGIN
    EXEC (@CurrentStatement)    
    SET @CurrentStatement = (SELECT TOP(1)  'DROP PROCEDURE [' + @schema + '].[' + name + ']'
    FROM sys.procedures 
    WHERE schema_id = SCHEMA_ID(@schema))
END

前提与marc_s提供的答案非常相似;但是不使用游标进行迭代。虽然sys.procedures中的记录与我们的模式匹配,但我们需要将其删除。

答案 4 :(得分:0)

不需要XML或循环:

declare @sql varchar(max) = ''

select @sql += 'drop procedure [' + routine_schema + '].[' + routine_name + '];'
from information_schema.routines where routine_schema = 'DataSync' and routine_type = 'PROCEDURE'

exec(@sql)

答案 5 :(得分:0)

使用sql2012或更高版本尝试此操作

这将有助于按选定的架构删除所有对象 保留“ P”并仅删除存储过程的其余部分(o。输入IN('P')

DECLARE @MySchemaName VARCHAR(50)='dbo', @sql VARCHAR(MAX)='';
DECLARE @SchemaName VARCHAR(255), @ObjectName VARCHAR(255), @ObjectType VARCHAR(255), @ObjectDesc VARCHAR(255), @Category INT;

DECLARE cur CURSOR FOR
    SELECT  (s.name)SchemaName, (o.name)ObjectName, (o.type)ObjectType,(o.type_desc)ObjectDesc,(so.category)Category
    FROM    sys.objects o
    INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
    INNER JOIN sysobjects so ON so.name=o.name
    WHERE s.name = @MySchemaName
    AND so.category=0
    AND o.type IN ('P','PC','U','V','FN','IF','TF','FS','FT','PK','TT')

OPEN cur
FETCH NEXT FROM cur INTO @SchemaName,@ObjectName,@ObjectType,@ObjectDesc,@Category

SET @sql='';
WHILE @@FETCH_STATUS = 0 BEGIN    
    IF @ObjectType IN('FN', 'IF', 'TF', 'FS', 'FT') SET @sql=@sql+'Drop Function '+@MySchemaName+'.'+@ObjectName+CHAR(13)
    IF @ObjectType IN('V') SET @sql=@sql+'Drop View '+@MySchemaName+'.'+@ObjectName+CHAR(13)
    IF @ObjectType IN('P') SET @sql=@sql+'Drop Procedure '+@MySchemaName+'.'+@ObjectName+CHAR(13)
    IF @ObjectType IN('U') SET @sql=@sql+'Drop Table '+@MySchemaName+'.'+@ObjectName+CHAR(13)

    --PRINT @ObjectName + ' | ' + @ObjectType
    FETCH NEXT FROM cur INTO @SchemaName,@ObjectName,@ObjectType,@ObjectDesc,@Category
END
CLOSE cur;    
DEALLOCATE cur;
SET @sql=@sql+CASE WHEN LEN(@sql)>0 THEN 'Drop Schema '+@MySchemaName+CHAR(13) ELSE '' END
PRINT @sql
EXECUTE (@sql)