﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[MRBGet_SPN_HoldLots]    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].[MRBGet_SPN_HoldLots] -- Add the parameters for the stored procedure here
	@MRBNumber INT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET
	NOCOUNT ON;

IF NOT EXISTS (
	SELECT
		L.LotNumber,
		MD.MRBNumber
	FROM
		MRBDispositionByMRB MD
		INNER JOIN MRBLot L ON MD.DispositionType = L.DispoType
		INNER JOIN MRB M ON M.MRBNumber = MD.MRBNumber
	WHERE
		CloseToQDBOptionID = 1
		AND L.MRBNumber = @MRBNumber
		AND MD.MRBNumber = @MRBNumber
		AND L.LotNumber + CONVERT(VARCHAR(10), MD.MRBNumber) NOT IN (
			SELECT
				LotNumber + CONVERt(VARCHAR(10), MRBNumber)
			FROM
				MRB_SPN_HoldFlagLot
		)
) BEGIN RETURN;

END
INSERT INTO
	MRB_SPN_DataLog (Data)
SELECT
	DISTINCT L.LotNumber + '~' + STUFF(
		(
			SELECT
				'~' + CONVERT(VARCHAR(50), M.MRBNumber) AS [text()]
			FROM
				fnGetMRB_SPN_HoldLots(@MRBNumber) M
			WHERE
				M.LotNumber = L.LotNumber FOR XML PATH('')
		),
		1,
		1,
		''
	) AS Data
FROM
	fnGetMRB_SPN_HoldLots(@MRBNumber) L
SELECT
	DISTINCT L.LotNumber + '~' + STUFF(
		(
			SELECT
				'~' + CONVERT(VARCHAR(50), M.MRBNumber) AS [text()]
			FROM
				fnGetMRB_SPN_HoldLots(@MRBNumber) M
			WHERE
				M.LotNumber = L.LotNumber FOR XML PATH('')
		),
		1,
		1,
		''
	) AS Data
FROM
	fnGetMRB_SPN_HoldLots(@MRBNumber) L
END
GO