How to delete all stored procedures from MSSQL database using cursor

Here is a code for deleting all stored procedures in SQL Server database using cursor.

DECLARE @name varchar(500)
DECLARE @sql varchar(max)
SET @sql = ''

DECLARE cur CURSOR
      FOR SELECT [name] FROM sys.procedures
      OPEN cur

      FETCH NEXT FROM cur INTO @name
      WHILE @@fetch_status = 0
      BEGIN
			SET @sql = 'DROP PROC ' + @name
			PRINT @sql
			EXEC (@sql)			
            FETCH NEXT FROM cur INTO @name
      END
      CLOSE cur
      DEALLOCATE cur

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.