﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[fds__toggleRequestHidden]
	@Id bigint,
	@authuser varchar(25)
AS
BEGIN
	SET NOCOUNT ON;

	MERGE [dbo].[fds__custom_servicerequest] as TARGET
		USING (SELECT [id] FROM [mfr__servicerequests] as sr where [id] = @id) as SOURCE ([id]) ON TARGET.[EntityId] = SOURCE.[Id]
	WHEN MATCHED THEN
		UPDATE SET 
			[hidden] = CASE WHEN TARGET.[hidden] = 1 THEN 0 ELSE 1 END
	WHEN NOT MATCHED BY TARGET THEN
		INSERT
			([EntityId]
			 ,[hidden])
		VALUES
			(SOURCE.[Id]
			 ,1)
	OUTPUT 
		inserted.*;

END