查找数据库对象的关联对象列表

    在数据库维护时, 有时需查询数据库对象(如表,视图,存储过程等)的关联(即被参考)数据库对象, 可使用如下自定义存储过程查询.

if exists(select 1 from sys.objects where type='P' and nam='spFindObject')
   drop proc dbo.spFindObject
go

create procedure dbo.spFindObject
(@template varchar(200))
as
begin
 set nocount on

 declare @r table(ObjectID int,
                              ObjectName varchar(100),
                              ObjectType varchar(10),
                              CreateDate datetime,
                              LastExecute datetime,     -- 最后执行时间
                              ExecuteCount int,            -- 总执行次数
                              ElapsedTime bigint,        -- 总执行时间
                              Operation varchar(50)     -- 操作类型
                             )
 
 insert into @r(ObjectID,ObjectName,ObjectType,CreateDate)
 select distinct b.object_id 'ObjectID',
                        b.name 'ObjectName',
                        b.type 'ObjectType',
                        b.create_date 'CreateDate'
   from sys.sql_expression_dependencies a
   inner join sys.objects b on a.referencing_id=b.object_id
   where a.referenced_id=object_id(@template)
   and b.name<>@template

update a
  set a.Operation=case when e.is_selected=1 or e.is_select_all=1 then 'SELECT, ' else '' end
                              +case when e.is_updated=1 then 'UPDATE, ' else '' end
  from @r a
  inner join (select c.object_id,
                               is_selected=cast(max(cast(c.is_selected as tinyint)) as bit),
                               is_select_all=cast(max(cast(c.is_select_all as tinyint)) as bit),
                               is_updated=cast(max(cast(c.is_updated as tinyint)) as bit)
                    from sys.sql_dependencies c
                    inner join sys.objects d on c.referenced_major_id=d.object_id
                    where d.name=@template
                    group by c.object_id) e on a.ObjectID=e.object_id

 update a
   set a.LastExecute=b.last_execution_time,
         a.ExecuteCount=b.execution_count,
         a.ElapsedTime=b.total_elapsed_time
   from @r a
   inner join (select  database_id,
                                 object_id,
                                 last_execution_time=max(last_execution_time),
                                 execution_count=sum(execution_count),
                                 total_elapsed_time=sum(total_elapsed_time)
                      from sys.dm_exec_procedure_stats
                      where type='P'
                      and database_id=(select top 1 dbid from sys.sysprocesses where spid=@@spid)
                      group by database_id,object_id) b on a.ObjectID=b.object_id
   where a.ObjectType='P'

 select ObjectName,ObjectType,CreateDate,LastExecute,ExecuteCount,ElapsedTime,Operation
   from @r
   order by case ObjectType when 'V' then 0 
                                                when 'P' then 1 
                                                else 2 end,ExecuteCount desc
end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值