Find broken objects in SQL Server

前端 未结 11 525
孤独总比滥情好
孤独总比滥情好 2020-12-02 17:26

Is there a tool that will find all objects in SQL Server (functions, procs, views) that cannot possibly work because they refer to objects that don\'t exist?

11条回答
  •  时光取名叫无心
    2020-12-02 18:02

     create table #BrokenObjects (Name nvarchar(500), Error nvarchar(max))
     select * into #objects from(
     select name from sys.views
     union select name from sys.procedures
     union select name from sys.tables
     )x
     declare @name nvarchar(500),@err nvarchar(max)
     while exists(select top 1 * from #objects)
     begin
     select top 1 @name = name from #objects
     begin try
     EXEC sys.sp_refreshsqlmodule @name
     end try
     begin catch
     select @err = ERROR_MESSAGE()
     insert into #BrokenObjects (name,error) values (@name,@err)
     end catch
     delete from #objects
     where name = @name
     end
     drop table #objects
     select * from #BrokenObjects
     where Error not like 'Could not find object % or you do not have permission.'
    
     drop table #BrokenObjects
    

提交回复
热议问题