﻿CREATE PROCEDURE [dbo].[sp_DiagnosticRunsUpdateInsert]
	@Id					BIGINT NULL,
	@DataPROUser		NVARCHAR(50),
	@TestId				INT NULL,
	@TestName			NVARCHAR(50),
	@PreTest			BIT,
	@errorNumber		INT OUTPUT,
	@errorMessage		NVARCHAR (255) OUTPUT,
	@new_id				BIGINT OUTPUT
AS
BEGIN

	set @errorNumber = 0; 	set @errorMessage = space(0);
	

	if(exists(select Id from [dbo].[DiagnosticRuns] where Id = @Id))
	begin
		set @new_id = @Id;
		UPDATE [dbo].[DiagnosticRuns] SET [DataPROUser]=@DataPROUser, [TestId]=@TestId, [TestName]=@TestName WHERE [Id]=@Id;
	end
	else
	begin
		INSERT INTO [dbo].[DiagnosticRuns] ([DataPROUser], [PreTest], [TestId], [TestName]) VALUES (@DataPROUser, @PreTest, @TestId, @TestName);
		SET @new_id = SCOPE_IDENTITY();
	end
	IF(@@error != 0)
		BEGIN
			SET @errorNumber = error_number()
			SET @errorMessage = error_message()
		END
END