CREATE PROCEDURE [dbo].[sp_TestHistoryUpdateInsert]
		   @TestHistoryId           bigint=NULL,
		   @TestSetupId             int=NULL,
		   @TestSetupName           nvarchar(50),
		   @TestSetupDescription    nvarchar(50)=NULL,
		   @TestId                  nvarchar(MAX)=NULL,
		   @Destructive             bit,
		   @ArmTime					datetime,
		   @TestSetup				varbinary(MAX)=NULL,
		   @new_id					int output,
		   @errorNumber			    int output,
		   @errorMessage			nvarchar(250) output
AS
BEGIN
	set @errorNumber = 0; 	
	set @errorMessage = space(0);
	begin try
	  IF @TestHistoryId is NOT NULL
	  BEGIN
		UPDATE [dbo].[TestHistory] SET [TestSetupId]=@TestSetupId, [TestSetupName]=@TestSetupName, [TestSetupDescription]=@TestSetupDescription, [TestId]=@TestId, [Destructive]=@Destructive, [ArmTime]=@ArmTime, [TestSetup]=@TestSetup
		SET @new_id=@TestHistoryId
	  END
	  ELSE
	  BEGIN
	  	INSERT INTO [dbo].[TestHistory] ([TestSetupId],[TestSetupName],[TestSetupDescription],[TestId],[Destructive],[ArmTime], [TestSetup])
		VALUES
		(@TestSetupId, @TestSetupName, @TestSetupDescription, @TestId, @Destructive, @ArmTime, @TestSetup)
		set @new_id = scope_identity();
	  END
	end try
	begin catch
		set @errorMessage	= error_message()
		set @errorNumber	= error_number()
	end catch;
END