﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[_8DInsertD5D6CorrectiveAction]    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].[_8DInsertD5D6CorrectiveAction] -- Add the parameters for the stored procedure here
	@CANo INT,
	@CorrectiveAction VARCHAR(1000),
	@Result VARCHAR(1000),
	@ECNLinks VARCHAR(1000),
	@ResponsibilityOwnerID INT,
	@ImprovementIDs VARCHAR(200),
	@ActionType VARCHAR(200),
	@ECD DATE,
	@ImplementedDate DATE,
	@CARequired BIT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET
	NOCOUNT ON;

-- Insert statements for procedure here
INSERT
	_8D_D5D6CorrectiveAction (
		CANo,
		CorrectiveAction,
		CARequired,
		Result,
		ECNLinks,
		ResponsibilityOwnerID,
		ECD,
		ImplementedDate,
		Type
	)
VALUES
	(
		@CANo,
		@CorrectiveAction,
		@CARequired,
		@Result,
		@ECNLinks,
		@ResponsibilityOwnerID,
		@ECD,
		@ImplementedDate,
		CASE
			WHEN @ACTIONTYPE = 'Corrective' then 1
			WHEN @ActionType = 'Preventitive' then 2
		end
	) DECLARE @D5D6CorrectiveActionID INT
SET
	@D5D6CorrectiveActionID = SCOPE_IDENTITY() 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
INSERT INTO
	_8D_D5D6ImprovemntByCA
SELECT
	@D5D6CorrectiveActionID,
	ImprovementID
FROM
	#tempTab
	DROP TABLE #tempTab
END
GO