
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[p_claim_actors_add]
	@claim_id int,
	@check_if_not_exists bit,
	@actors type_table_id2 READONLY
AS

	/*
	DGE
	ajouter les acteurs contenus dans la table @actors.


	si @check_if_not_exists != 0 - ajouter l'acteur uniquement s'il n'existe pas.
	si @check_if_not_exists = 0 - ajouter l'acteur. Si l'acteur existe déjà, l'insert va planter


	Si @actors.id2 != 0 => mettre en clact_type_code le code <TIER>

	*/

	declare @type_code ais_code_short

	select	@type_code = a.at_type_code
	from	act_types a
	where	a.at_tiers != 0



	insert into claim_actors(clact_claim_id, clact_actor_id, clact_type_code)
	select	@claim_id, 
			a.id1,
			case
			when a.id2 != 0 then @type_code
			else null
			end
	from	@actors a
	where	1 =		case
					when @check_if_not_exists != 0 and 
							not exists (	select	1 
											from	claim_actors ca 
											where	ca.clact_claim_id = @claim_id 
											and		ca.clact_actor_id = a.id1) then 1
					when @check_if_not_exists = 0 then 1
					else 0
					end

GO
