﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[_8DUpdateD5D6CorrectiveAction]    Script Date: 11/21/2024 11:29:04 AM ******/
SET
	ANSI_NULLS ON
GO
SET
	QUOTED_IDENTIFIER ON
GO
	-- =============================================
	-- Author:		<Author,,Name>
	-- Create date: <Create Date,,>
	-- Description:	<Description,,>
	-- =============================================
	CREATE PROCEDURE [dbo].[_8DUpdateD5D6CorrectiveAction] -- Add the parameters for the stored procedure here
	@D5D6CAID INT,
	@CorrectiveAction VARCHAR(1000),
	@Result VARCHAR(1000),
	@ECNLinks VARCHAR(1000),
	@ResponsibilityOwnerID INT,
	@ImprovementIDs VARCHAR(200),
	@ECD DATE,
	@ImplementedDate DATE,
	@CARequired BIT,
	@ActionType VARCHAR(200) AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET
	NOCOUNT ON;

-- Insert statements for procedure here
UPDATE
	_8D_D5D6CorrectiveAction
SET
	CorrectiveAction = @CorrectiveAction,
	CARequired = @CARequired,
	Result = @Result,
	ECNLinks = @ECNLinks,
	ResponsibilityOwnerID = @ResponsibilityOwnerID,
	ECD = @ECD,
	ImplementedDate = @ImplementedDate,
	Type = CASE
		WHEN @ACTIONTYPE = 'Corrective' then 1
		WHEN @ActionType = 'Preventitive' then 2
	end
WHERE
	ID = @D5D6CAID DECLARE @SEPERATOR as VARCHAR(1) DECLARE @SP INT DECLARE @VALUE VARCHAR(1000)
SET
	@SEPERATOR = ',' CREATE TABLE #tempTab (ImprovementID int not null)
	WHILE PATINDEX('%' + @SEPERATOR + '%', @ImprovementIDs) <> 0 BEGIN
SELECT
	@SP = PATINDEX('%' + @SEPERATOR + '%', @ImprovementIDs)
SELECT
	@VALUE = LEFT(@ImprovementIDs, @SP - 1)
SELECT
	@ImprovementIDs = STUFF(@ImprovementIDs, 1, @SP, '')
INSERT INTO
	#tempTab (ImprovementID) VALUES (@VALUE)
END
DELETE FROM
	_8D_D5D6ImprovemntByCA
WHERE
	D5D6CAID = @D5D6CAID
INSERT INTO
	_8D_D5D6ImprovemntByCA
SELECT
	@D5D6CAID,
	ImprovementID
FROM
	#tempTab
	DROP TABLE #tempTab
END
GO