【问题标题】:SQL Server Alert on Missing Stored Procedure缺少存储过程的 SQL Server 警报
【发布时间】:2021-04-17 19:34:04
【问题描述】:

我正在清理一个大型数据库,并将删除许多存储过程。我可以非常确定但不是 100% 没有使用 SP。如何提醒我某些东西正在调用丢失的存储过程并引发错误:

Could not find stored procedure ‘X’

【问题讨论】:

  • 您可以检查查询缓存并验证它们没有被使用。您还可以搜索 GIT 或您正在使用的任何源代码控制软件来查找 proc 调用的存在。 '

标签: sql sql-server sql-server-2008


【解决方案1】:

您可以为每个已删除的过程创建同义词并替换不同的过程(使用匹配的参数)。在下面的示例中,该过程不带任何参数。

代码:

  1. 创建名为“dbo.test_replacement_proc”的“替换”过程
  2. exec 'dbo.test_replacement_proc'
  3. 创建名为“dbo.test_proc”的“测试”过程
  4. 执行“dbo.test_proc”
  5. 删除“dbo.test_proc”
  6. 为“dbo.test_replacement_proc”创建同义词“dbo.test_proc”
  7. 执行“dbo.test_proc”

当#4 执行时,它是原始过程。当#7 执行时,它是替换过程。

drop proc if exists dbo.test_replacement_proc;
go
create proc dbo.test_replacement_proc
as
set nocount on;
select 'Could not find stored procedure' replacement_message
go

-- run the proc
exec dbo.test_replacement_proc;

-- Create proc
drop synonym if exists dbo.test_proc;
drop proc if exists dbo.test_proc;
go
create proc dbo.test_proc
as
set nocount on;
select 'This is the text' procedure_message
go

-- run the proc
exec dbo.test_proc;

-- drop procedure
drop proc if exists dbo.test_proc;

-- Create a synonym for the deleted procedure
create synonym dbo.test_proc
for dbo.test_replacement_proc;  
go  

-- run the proc (which is now a synonym for the replacement proc)
exec dbo.test_proc;

输出#2

replacement_message
Could not find stored procedure

输出#4

procedure_message
This is the text

输出#7

replacement_message
Could not find stored procedure

【讨论】:

  • 或者您可以删除并使用相同的名称重新创建
【解决方案2】:

使用 SQL Server 代理来create an alert 处理错误 #2812。

您还需要创建一个运算符,然后您可以设置警报通知。

请参阅MSSQLTips,了解如何操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多