SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE procedure [dbo].[p_identity_init]
as
	/*
	Cette procédure sert à initialiser les compteurs "identity" au moment du démarrage du BS (business server).


	Pour identifier les tables qui ont des champs de type IDENTITY on utilise la requête suivante:
	SELECT	[schema] = s.name,
			[table] = t.name
	FROM	sys.schemas AS s
			INNER JOIN sys.tables AS t ON s.[schema_id] = t.[schema_id]
	WHERE	EXISTS	(
					SELECT	1 
					FROM	sys.identity_columns
					WHERE	[object_id] = t.[object_id]
					)
	and		t.name != 'sysdiagrams'
	order by t.name

	Après la mise en place de la gestion des compteurs automatiques dans le BS, nous allons interdire la création des champs
	de type IDENTITY dans la base de données. La requête ci-dessus sera utilisée dans la procédure stockée p_sys_md_create
	pour bloquer la validation des modifications de la base si un champ identity est présent.

	*/

	declare @max_id int


	select @max_id = opt_seed + 50000000 from ais_options

	select opt_seed from ais_options

	select a_account_id = max(a_account_id) from acc_accounts where a_account_id < @max_id
	
	select ctsb_bordereau_id = max(ctsb_bordereau_id) from acc_cts_bordereaux where ctsb_bordereau_id < @max_id

	select acctax_tax_id = max(acctax_tax_id) from acc_taxes where acctax_tax_id < @max_id

	select act_actor_id = max(act_actor_id) from act_actors where act_actor_id < @max_id

	select adr_address_id = max(adr_address_id) from adr_addresses where adr_address_id < @max_id

	select ba_batch_id = max(ba_batch_id) from ba_batches where ba_batch_id < @max_id

	select cl_claim_id = max(cl_claim_id) from claim_claims where cl_claim_id < @max_id

	select clacc_account_id = max(clacc_account_id) from claim_variables_accounts where clacc_account_id < @max_id

	select clsc_class_id = max(clsc_class_id) from grpcls_classes where clsc_class_id < @max_id

	select usgu_usage_id = max(usgu_usage_id) from grpusg_usages where usgu_usage_id < @max_id

	select zonz_zone_id = max(zonz_zone_id) from grpzon_zones where zonz_zone_id < @max_id

	select pol_policy_id = max(pol_policy_id) from pol_policies where pol_policy_id < @max_id

	select pols_policy_situation_id = max(pols_policy_situation_id) from pol_policies_situations where pols_policy_situation_id < @max_id

	select prdv_variable_id = max (prdv_variable_id) from pr_products_variables where prdv_variable_id < @max_id

	select ris_risk_id = max(ris_risk_id) from ris_risks where ris_risk_id < @max_id

	select riss_risk_situation_id = max(riss_risk_situation_id) from ris_risks_situations where riss_risk_situation_id < @max_id

	select pare_id = max(pare_id) from parameters_export where pare_id < @max_id

	select pari_id = max(pari_id) from parameters_import where pari_id < @max_id

	select trnb_bordereau_id = max(trnb_bordereau_id) from acc_trn_bordereaux where trnb_bordereau_id < @max_id

	select iddh_header_id = max(iddh_header_id) from input_data_dump_header where iddh_header_id < @max_id

	select iddhh_history_id = max(iddhh_history_id) from input_data_dump_history where iddhh_history_id < @max_id

	select obj_object_id = max(obj_object_id) from vw__obj_objects_select where obj_object_id < @max_id

	SELECT dc_collection_id = max(dc_collection_id) FROM doc_collections WHERE dc_collection_id < @max_id

	SELECT dcd_document_id = max(dcd_document_id) FROM dbo.doc_collections_docs dcd WHERE dcd.dcd_document_id < @max_id

	select clb_bordereau_id = max(clb_bordereau_id) from acc_cl_bordereaux a where a.clb_bordereau_id < @max_id

	select mpayb_bordereau_id = max(mpayb_bordereau_id) from acc_mpay_bordereaux a where a.mpayb_bordereau_id < @max_id


GO
