﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[LTTestLinkedServer]    Script Date: 11/21/2024 11:29:05 AM ******/
SET
	ANSI_NULLS ON
GO
SET
	QUOTED_IDENTIFIER ON
GO
	-- =============================================
	-- Author:		<Author,,Name>
	-- Create date: <Create Date,,>
	-- Description:	<Description,,>
	-- =============================================
	CREATE PROCEDURE [dbo].[LTTestLinkedServer] AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	DECLARE @LotTravelerRevisionID INT DECLARE @LTWorkRequestID INT
SET
	@LTWorkRequestID = 8000184
SET
	@LotTravelerRevisionID = 103 DECLARE @ID INT DECLARE @BaseFlow VARCHAR(50) DECLARE @Location VARCHAR(50) DECLARE @OperSequence NCHAR(10) DECLARE @Operation VARCHAR(50) DECLARE @OperationDescription VARCHAR(100) DECLARE @ChangeInstructions VARCHAR(1000) DECLARE @UpdatedBy INT DECLARE @tempLTHoldStepID INT DECLARE @LotNumber VARCHAR(50) DECLARE @SPNCurrentSeqForTheLot NCHAR(10) BEGIN TRAN DECLARE CurHoldSteps CURSOR FOR
SELECT
	ID,
	BaseFlow,
	Location,
	OperSequence,
	Operation,
	OperationDescription,
	ChangeInstructions,
	UpdatedBy
FROM
	LTHoldStep
WHERE
	LTWorkRequestID = 8000184 OPEN CurHoldSteps;

FETCH NEXT
FROM
	CurHoldSteps INTO @ID,
	@BaseFlow,
	@Location,
	@OperSequence,
	@Operation,
	@OperationDescription,
	@ChangeInstructions,
	@UpdatedBy WHILE @ @FETCH_STATUS = 0 BEGIN -- Get the current location and operation for the respective lot
SET
	@LotNumber = (
		SELECT
			TOP 1 L.LotNumber
		FROM
			LTLotTravelerRevisionAttrib LTA
			INNER JOIN LTLotTravelerHoldSteps LH ON LTA.ID = LH.LotTravelerRevisionID
			INNER JOIN LTLot L ON LTA.LTLotID = L.ID
		WHERE
			LH.LotTravelerRevisionID = @LotTravelerRevisionID
	)
SET
	@SPNCurrentSeqForTheLot = (
		SELECT
			MAX(SEQ)
		FROM
			vFAB2SPN_WO_RECORD WO
			INNER JOIN vSPNPDB_FLOWLOCS F ON WO.WO_PROCESS = F.PROCESS
			AND WO.WO_LOCATION = F.LOC
			AND WO.WO_OPER_NO = F.OPER
		WHERE
			WO_LOT_NO = @LotNumber
			AND WO_LOCATION = @Location
			AND WO_PROCESS = @BaseFlow
	) IF (@OperSequence > @SPNCurrentSeqForTheLot)
	OR (@SPNCurrentSeqForTheLot IS NULL)
	OR (@OperSequence IS NULL) BEGIN PRINT 'Added ' + @Location PRINT 'Added ' + @OperSequence PRINT 'Added ' + @Operation
END FETCH NEXT
FROM
	CurHoldSteps INTO @ID,
	@BaseFlow,
	@Location,
	@OperSequence,
	@Operation,
	@OperationDescription,
	@ChangeInstructions,
	@UpdatedBy
END CLOSE CurHoldSteps;

DEALLOCATE CurHoldSteps;

COMMIT
END
GO