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

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

    Try this:

    declare @procName varchar(500)
    declare cur cursor 
    
    for SELECT 'DROP PROCEDURE [' + SCHEMA_NAME(p.schema_id) + '].[' + p.NAME + ']'
    FROM sys.procedures p 
    open cur
    fetch next from cur into @procName
    while @@fetch_status = 0
    begin
        exec( @procName )
        fetch next from cur into @procName
    end
    close cur
    deallocate cur
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-01-29 23:16

    ANSI compliant, without cursor

    DECLARE @SQL national character varying(MAX) 
    SET @SQL= ''
    
    SELECT @SQL= @SQL+ N'DROP PROCEDURE "' + REPLACE(SPECIFIC_SCHEMA, N'"', N'""') + N'"."' + REPLACE(SPECIFIC_NAME, N'"', N'""') + N'"; '
    FROM INFORMATION_SCHEMA.ROUTINES 
    
    WHERE (1=1) 
    AND ROUTINE_TYPE = 'PROCEDURE' 
    AND ROUTINE_NAME NOT IN 
    (
         'dt_adduserobject'
        ,'dt_droppropertiesbyid'
        ,'dt_dropuserobjectbyid'
        ,'dt_generateansiname'
        ,'dt_getobjwithprop'
        ,'dt_getobjwithprop_u'
        ,'dt_getpropertiesbyid'
        ,'dt_getpropertiesbyid_u'
        ,'dt_setpropertybyid'
        ,'dt_setpropertybyid_u'
        ,'dt_verstamp006'
        ,'dt_verstamp007'
    
        ,'sp_helpdiagrams'
        ,'sp_creatediagram'
        ,'sp_alterdiagram'
        ,'sp_renamediagram'
        ,'sp_dropdiagram'
    
        ,'sp_helpdiagramdefinition'
        ,'fn_diagramobjects'
        ,'sp_upgraddiagrams'
    ) 
    
    
    ORDER BY SPECIFIC_NAME 
    
    
    -- PRINT @SQL
    EXEC(@SQL) 
    

    Without cursor, non-ansi compliant:

    DECLARE @sql NVARCHAR(MAX) = N''
    , @lineFeed NVARCHAR(2) = CHAR(13) + CHAR(10) ;
    
    SELECT @sql = @sql + N'DROP PROCEDURE ' + QUOTENAME(SPECIFIC_SCHEMA) + N'.' + QUOTENAME(SPECIFIC_NAME) + N';' + @lineFeed
    FROM INFORMATION_SCHEMA.ROUTINES 
    WHERE ROUTINE_TYPE = 'PROCEDURE' 
    -- AND SPECIFIC_NAME LIKE 'sp[_]RPT[_]%'
    
    
    AND ROUTINE_NAME NOT IN 
    ( 
        SELECT name FROM sys.procedures WHERE is_ms_shipped <> 0 
    ) 
    
    
    ORDER BY SPECIFIC_NAME 
    
    
    -- PRINT @sql 
    EXECUTE(@sql)
    
    0 讨论(0)
  • 2021-01-29 23:17
    1. Click on Stored Procedures Tab
    2. Press f7 to Display All Stored Procedures
    3. Select All Procedure By Ctrl + A except System Table
    4. Press Delete button and Click OK.

    You can Delete Table as well as View in same manner.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-01-29 23:30

    I would prefer to do it this way:

    • first generate the list of stored procedures to drop by inspecting the system catalog view:

      SELECT 'DROP PROCEDURE [' + SCHEMA_NAME(p.schema_id) + '].[' + p.NAME + '];'
      FROM sys.procedures p 
      

      This generates a list of DROP PROCEDURE statements in your SSMS output window.

    • copy that list into a new query window, and possibly adapt it / change it and then execute it

    No messy and slow cursors, gives you the ability to check and double-check your list of procedure to be dropped before you actually drop it

    0 讨论(0)
提交回复
热议问题