CREATE PROCEDURE [dbo].[sp_TestHistoryDelete]
	@TestHistoryId			BIGINT = NULL,
	@TestSetupId            INT = NULL
AS
BEGIN
	IF @TestHistoryId IS NOT NULL
	BEGIN
		DELETE A FROM [dbo].[SensorTestHistory] AS A INNER JOIN [dbo].[TestHistory] AS B ON A.TestHistoryId=B.TestHistoryId WHERE A.TestHistoryId=@TestHistoryId
		DELETE FROM [dbo].[TestHistory] WHERE [TestHistoryId]=@TestHistoryId
	END
	ELSE IF @TestSetupId IS NOT NULL
	BEGIN
	   DELETE A FROM [dbo].[SensorTestHistory] AS A INNER JOIN [dbo].[TestHistory] AS B ON A.TestHistoryId=B.TestHistoryId WHERE B.TestSetupId=@TestSetupId
	   DELETE FROM [dbo].[TestHistory] WHERE [TestSetupId]=@TestSetupId
	END
	ELSE
	BEGIN
	   DELETE FROM [dbo].[SensorTestHistory]
	   DELETE FROM [dbo].[TestHistory]
	END
END