How to drop all stored procedures at once in SQL Server database?

后端 未结 14 2052
囚心锁ツ
囚心锁ツ 2021-01-29 22:53

Currently we use separate a drop statements for each stored procedure in the script file:

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N\'[         


        
14条回答
  •  花落未央
    2021-01-29 23:16

    create below stored procedure in your db(from which db u want to delete sp's)

    then right click on that procedure - click on Execute Stored Procedure..

    then click ok.

    create Procedure [dbo].[DeleteAllProcedures]
    As 
    declare @schemaName varchar(500)    
    declare @procName varchar(500)
    declare cur cursor
    for select s.Name, p.Name from sys.procedures p
    INNER JOIN sys.schemas s ON p.schema_id = s.schema_id
    WHERE p.type = 'P' and is_ms_shipped = 0 and p.name not like 'sp[_]%diagram%'
    ORDER BY s.Name, p.Name
    open cur
    
    fetch next from cur into @schemaName,@procName
    while @@fetch_status = 0
    begin
    if @procName <> 'DeleteAllProcedures'
    exec('drop procedure ' + @schemaName + '.' + @procName)
    fetch next from cur into @schemaName,@procName
    end
    close cur
    deallocate cur
    

提交回复
热议问题