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

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

    ANSI compliant, without cursor

    PRINT ('1.a. Delete stored procedures ' + CONVERT( VARCHAR(19), GETDATE(), 121));
    GO
    DECLARE @procedure NVARCHAR(max)
    DECLARE @n CHAR(1)
    SET @n = CHAR(10)
    SELECT @procedure = isnull( @procedure + @n, '' ) +
    'DROP PROCEDURE [' + schema_name(schema_id) + '].[' + name + ']'
    FROM sys.procedures
    
    EXEC sp_executesql @procedure
    PRINT ('1.b. Stored procedures deleted ' + CONVERT( VARCHAR(19), GETDATE(), 121));
    GO
    

提交回复
热议问题