Wednesday 29 October 2014

Recent execution of Procedures in SQL Server

         Recently I was working on one functionality. Interesting thing was I did not know it fully.
I need to complete it as early as .I can say it was enhancement .When term come as enhancement then as SQL developer we know it involves  lots of R&D if application is complex.It was same scenario which I caught.I  didn't  know anything but need to complete as early as possible.
      So I thought its time to do smart work not hard work!!!
 I knew one thing everything in that functionality was handling through procedures.So I thought If I will get to know flow of execution of procedures I mean order of calling of procedures .If I will get that I can easily capture list of procedures and following flow I can get sequence of procedures .
    I tried and it worked for me.I got list of  procedure and modified code as per  requirement .Unit tested and done all regression it passed all cases.So sometime smartwork also work.
I will demonstrate as  follow
e.g
create procedure X
as select 1
Go
create procedure Y
as select 1
GO
exec x
GO
exec y


  I have created two procedure named X and Y .I have executed this procedures .
I have used following query to get result


SELECT    db_name(DMV.database_id), 
  OBJECT_NAME(object_id, database_id) AS proc_name, 
  DMV.last_execution_time
  FROM sys.dm_exec_procedure_stats AS DMV
  where db_name(DMV.database_id) is not null
   order by   DMV.last_execution_time desc 





First Database In Sql Server