CREATE PROCEDURE [dbo].[BO_AddTickerData]
    @TickerId INT, 
    @TickerTime datetime2, 
	@Interval NCHAR(1) = 'M', 
    @TickState INT = 0, 
    @Open DECIMAL(18, 10) = NULL, 
    @High DECIMAL(18, 10) = NULL, 
    @Close DECIMAL(18, 10) = NULL, 
    @Low DECIMAL(18, 10) = NULL, 
    @Volume DECIMAL(18, 10) = 0
AS
	DECLARE @TranStarted	BIT,
			@ERROR	SMALLINT,
			@RC	INT,
			@RowCount	INT,
			@ErrorMessage	VARCHAR(100)
	SET @TranStarted = 0 
	SET @RC	 = 0 

 BEGIN TRY      
	if exists (select 1 from TickerData where TickerId=@TickerId and TickerTime=@TickerTime and Interval=@Interval)
		update TickerData
			set TickState = @TickState, [Open] = @Open, High =    @High, [Close] =   @Close, Low=    @Low, Volume=     @Volume
			where TickerId=@TickerId and TickerTime=@TickerTime and Interval=@Interval
	else
		INSERT TickerData (   TickerId, TickerTime, 	Interval,     TickState,     [Open],     High,     [Close],     Low,     [Volume]) 
			VALUES(@TickerId, @TickerTime, 	@Interval,     @TickState,     @Open,     @High,     @Close,     @Low,     @Volume)
	if @@ROWCOUNT=0
	BEGIN 
		SET @RC =  	2	
		SET @ErrorMessage = 'Update problem'
		RAISERROR (@ErrorMessage,16,1);        
	END
	RETURN 0 
	  
  END TRY 
	 
  BEGIN CATCH  
	EXEC USP_LogError            
	EXEC USP_RethrowError                       
   
	IF @RC = 0
		SET @RC = 1   ---- Unexpected Error	   
	                     
	RETURN @RC                        
  END CATCH
GO
