----------------------------------------------------------------
--                                                              
--  File:   Install_Load_Timer_Proc.SQL                         
--          Microsoft TPC-E Benchmark Kit Ver. 1.14.0-1031      
--          Copyright Microsoft, 2008-2015                  
--                                                              
----------------------------------------------------------------
USE master
GO

if exists (select name from sysobjects where name = 'sp_tpce_timer' )
	drop procedure sp_tpce_timer
GO

CREATE PROC sp_tpce_timer       @step_name      varchar(100),
                                @flag           varchar(5)
						 
AS
DECLARE     @timestamp      char(30),
            @row_count      tinyint

BEGIN TRAN E

	SELECT	@timestamp = CONVERT(char(30), GETDATE(), 21)

	--- Now figure our if this is an end or start time
	IF (@flag = 'Begin')
		BEGIN
			--- determine if the row already exists from a previous execution
			SELECT	@row_count	= COUNT(*)
			FROM	tpce_build_timer
			WHERE	step_name = @step_name

			IF (@row_count = 0)
				--- the row does not exist
				INSERT INTO tpce_build_timer
							VALUES	(@step_name, @timestamp,0)
			ELSE
				--- the row exists so just update it with the new begin time
				UPDATE	tpce_build_timer
				SET		start_time	= @timestamp
				WHERE	step_name	= @step_name
		END
	ELSE
		BEGIN
			UPDATE	tpce_build_timer
			SET		end_time = @timestamp
			WHERE	step_name = @step_name
		END
	
COMMIT TRAN E
