
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[p_acc_trn_bordereaux_delete_by_table_id]
	@table_id type_table_id READONLY
AS

	set nocount on;

	/*
	DGE 05/06/2015
	Suppression d'un (ou de plusieurs) bordereau(x) de transactions.
	*/

	-- interdire la suppression des bordereaux qui ne sont pas temporaires
	if exists (	select	1 
				from	acc_trn_bordereaux b
						inner join @table_id t on t.id = b.trnb_bordereau_id
				where	b.trnb_tmp = 0 )
	BEGIN
		raiserror('Cannot delete a final bordereau', 16,1)
		return
	END
	
	delete	a
	from	acc_trn_bordereaux_accounts a
			inner join @table_id t on t.id = a.trnbacc_bordereau_id

	delete	a
	from	acc_trn_bordereaux_companies a
			inner join @table_id t on t.id = a.trnbcie_bordereau_id	

	delete	a
	from	acc_trn_bordereaux_departments a
			inner join @table_id t on t.id = a.trnbdep_bordereau_id

	delete	a
	from	acc_trn_bordereaux_details a
			inner join @table_id t on t.id = a.trnbd_bordereau_id
			
	delete	a
	from	acc_trn_bordereaux_introducers a
			inner join @table_id t on t.id = a.trnbint_bordereau_id

	delete	a
	from	acc_trn_bordereaux a
			inner join @table_id t on t.id = a.trnb_bordereau_id



GO
