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

后端 未结 14 2058
囚心锁ツ
囚心锁ツ 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:22

    DECLARE @DeleteProcCommand NVARCHAR(500)
    
    DECLARE Syntax_Cursor CURSOR
    FOR
    SELECT 'DROP PROCEDURE ' + p.NAME
    FROM sys.procedures p
    
    OPEN Syntax_Cursor
    
    FETCH NEXT FROM Syntax_Cursor
    
    INTO @DeleteProcCommand
    
    WHILE (@@FETCH_STATUS = 0)
    BEGIN
    
    EXEC (@DeleteProcCommand)
    
    FETCH NEXT FROM Syntax_Cursor
    INTO @DeleteProcCommand
    
    END
    
    CLOSE Syntax_Cursor
    
    DEALLOCATE Syntax_Cursor
    

提交回复
热议问题