
-- =============================================
-- Author:		ALacan
-- Create date: 08/06/2021
-- ---------------------------------------------
-- Description:	Mise a jour Incident Qualite/Type
--				@istypeaction = IU 	: Insertion ou mise à jour
--								D 	: Supression
-- ---------------------------------------------
-- 08/06/2021	ALacan	Creation
-- =============================================
ALTER PROCEDURE cq_ps_majIncidentQualiteType(
	 @typeactionsql varchar(2)					= 'IU'
	,@idincidentqualitetype 	SMALLINT	 	= NULL
	,@libincidentqualitetype	VARCHAR(250) 	= NULL
	,@username					VARCHAR(50)		= NULL
	,@isactif 					BIT 			= NULL
) 
AS
BEGIN
	------------------------------------------
	-- Constantes
	------------------------------------------
	
	------------------------------------------
	-- Variables
	------------------------------------------
	BEGIN TRY
		IF (@typeactionsql = 'D')
		BEGIN -- 'D'
			------------------------------------------
			-- Suppression
			------------------------------------------
			IF (@idincidentqualitetype IS NOT NULL)
			BEGIN
				DELETE FROM cq_incidentqualitetype
				WHERE (idincidentqualitetype = @idincidentqualitetype)
			END
		END
		ELSE
		BEGIN -- 'IU'
			IF (@idincidentqualitetype IS NOT NULL)
			BEGIN
				------------------------------------------
				-- Mise a jour si existe
				------------------------------------------
				UPDATE dest
				SET  dest.libincidentqualitetype	= isnull(@libincidentqualitetype,dest.libincidentqualitetype)
					,dest.isactif					= isnull(@isactif,dest.isactif)
					,dest.username					= @username
					,dest.dtupdate					= getdate()
				FROM	cq_incidentqualitetype dest
				WHERE	(dest.idincidentqualitetype = @idincidentqualitetype);
			END
			ELSE
			BEGIN
				------------------------------------------
				-- Nouveaux si n'existe pas
				------------------------------------------
				INSERT INTO cq_incidentqualitetype
				(
					[libincidentqualitetype]
				   ,[isactif]
				   ,[dtupdate]
				   ,[username]
				)
				SELECT DISTINCT
					 @libincidentqualitetype
					,@isactif
					,getdate()
					,@username
			END
		END
	END TRY
	BEGIN CATCH
		print 'cq_incidentqualitetype : erreur FOREIGN KEY'
	END CATCH
END
