﻿USE [FabApprovalSystem]
GO
	/****** Object:  StoredProcedure [dbo].[LTGetHoldSteps]    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].[LTGetHoldSteps] @WorkRequestID INT AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET
	NOCOUNT ON;

-- Insert statements for procedure here
SELECT
	DISTINCT LH.*,
	FL.FileNames,
	WR.Revision AS Revision,
	WR.SWRNumber
FROM
	LTHoldStep LH
	INNER JOIN LTWorkRequest WR ON LH.LTWorkRequestID = WR.ID
	LEFT JOIN LTWorkRequestAttachment A ON LH.ID = A.HoldStepID
	LEFT JOIN (
		SELECT
			DISTINCT LH.ID,
			STUFF (
				(
					SELECT
						DISTINCT ',' + CAST(A1.FileName AS varchar(512)) + '~' + CAST(A1.FileGuid AS varchar(512))
					FROM
						LTWorkRequestAttachment A1
					WHERE
						A1.HoldStepID = LH.ID FOR XML PATH('')
				),
				1,
				1,
				''
			) AS FileNames
		FROM
			LTHoldStep LH
	) AS FL ON LH.ID = FL.ID
WHERE
	LH.LTWorkRequestID = @WorkRequestID
ORDER BY
	Location,
	OperSequence
END
GO