如何在SQL Server数据库中一次性删除所有存储过程?
目前我们在脚本文件中为每个存储过程使用单独的drop语句:
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MySP]') AND type in (N'P', N'PC')) DROP PROCEDURE [dbo].[MySP]
有没有一种方法可以将它们一次全部放下,也可以循环放置?
类似于( 在SQL Server中使用存储过程从数据库中删除所有过程时发现的)。
顺便说一下,这似乎是一个非常危险的事情,只是一个想法…
declare @procName varchar(500) declare cur cursor for select [name] from sys.objects where type = 'p' open cur fetch next from cur into @procName while @@fetch_status = 0 begin exec('drop procedure [' + @procName + ']') fetch next from cur into @procName end close cur deallocate cur
我宁愿这样做:
-
首先通过检查系统目录视图来生成要删除的存储过程列表:
SELECT 'DROP PROCEDURE [' + SCHEMA_NAME(p.schema_id) + '].[' + p.NAME + ']' FROM sys.procedures p
这将在您的SSMS输出窗口中生成一个
DROP PROCEDURE
语句列表。 -
将该列表复制到一个新的查询窗口,并可能调整它/更改它,然后执行它
没有杂乱和缓慢的游标,使您能够检查并仔细检查您的程序清单,然后再放下它
- 点击Stored Procedures选项卡
- 按f7显示所有存储过程
- 按Ctrl + Aselect除系统表以外的所有过程
- 按删除button,然后单击确定。
您可以以相同的方式删除表格以及查看。
在你的db中创build下面的存储过程(从哪个数据库你想删除sp的)
然后右键单击该过程 – 单击执行存储过程。
然后点击确定。
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
关于Rizwana
希望对你有帮助
获取数据库中所有存储过程的drop语句SELECT'DROP PROCEDURE'+''+ F.NAME +';' FROM SYS.objects AS F where type ='P'
我认为这是最简单的方法:
DECLARE @sql VARCHAR(MAX)=''; SELECT @sql=@sql+'drop procedure ['+name +'];' FROM sys.objects WHERE type = 'p' AND is_ms_shipped = 0 exec(@sql);
DECLARE @sql VARCHAR(MAX) SET @sql='' SELECT @sql=@sql+'drop procedure ['+name +'];' FROM sys.objects WHERE type = 'p' AND is_ms_shipped = 0 exec(@sql);
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
尝试这个:
DECLARE @sql NVARCHAR(MAX) = N''; SELECT @sql += N'DROP PROCEDURE dbo.' + QUOTENAME(name) + '; ' FROM sys.procedures WHERE name LIKE N'spname%' AND SCHEMA_NAME(schema_id) = N'dbo'; EXEC sp_executesql @sql;
ANSI兼容,无需光标
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)
无游标,不符合ANSI标准:
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)
通过混合光标和系统过程,我们将有一个优化的解决scheme,如下所示:
DECLARE DelAllProcedures CURSOR FOR SELECT name AS procedure_name FROM sys.procedures; OPEN DelAllProcedures DECLARE @ProcName VARCHAR(100) FETCH NEXT FROM DelAllProcedures INTO @ProcName WHILE @@FETCH_STATUS!=-1 BEGIN DECLARE @command VARCHAR(100) SET @command='' SET @command=@command+'DROP PROCEDURE '+@ProcName --DROP PROCEDURE @ProcName EXECUTE (@command) FETCH NEXT FROM DelAllProcedures INTO @ProcName END CLOSE DelAllProcedures DEALLOCATE DelAllProcedures
ANSI兼容,无需光标
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
尝试这个:
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