Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

PerformanceMarker Stored Procedure - SQL Server

RSS
Modified on Fri, Dec 27, 2013, 12:29 PM by Administrator Categorized as SQL Server
if object_id(N'dbo.PerformanceMarker') is not null drop procedure dbo.PerformanceMarker
go
/*==============================================================================
                                                                                
    SAMPLE USAGE                                                                
        declare @prevTime datetime = getdate()                                  
        -- Do something here                                                    
        exec dbo.PerformanceMarker 'Doing something', @prevTime out             
        -- Do something else here                                               
        exec dbo.PerformanceMarker 'Doing something else', @prevTime out        
                                                                                
    SAMPLE OUTPUT                                                               
                                                                                
        (73 row(s) affected)                                                    
        Doing something took 0.023 second(s)                                    
                                                                                
        (177874 row(s) affected)                                                
        Doing something else took 2.210 second(s)                               
                                                                                
==============================================================================*/
create procedure dbo.PerformanceMarker (
     @stage     varchar(100)
    ,@prevTime  datetime OUT
    ) as

declare @currTime datetime = getdate()

declare @elap int = datediff(ms, @prevTime, @currTime)

declare @frac varchar(3) = convert(varchar(3), @elap % 1000)

set @frac = right('000' + @frac, 3)

print @stage + ' took ' + convert(varchar(20), @elap / 1000) + '.' + @frac + ' second(s)'

set @prevTime = @currTime

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.