SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE procedure [dbo].[p_branches_add_default_department]
	@branch_id int = 0
as

	set nocount on;

    insert into act_branches_departments(	brdep_branch_id,	brdep_department_code,		brdep_l1,	brdep_l2)
	select									b.br_actor_id,		a.act_internal_reference,	'Main',		'Siège'
	from	act_branches b
			inner join act_actors a on a.act_actor_id = b.br_actor_id
	where	( @branch_id = 0 or b.br_actor_id = @branch_id)
			and not exists (	select	1
								from	act_branches_departments bd
								where	bd.brdep_branch_id = b.br_actor_id
							)
GO
