Delimiter //
/**
    In batchnum Int : num du lot, si 0 ou null traitement de l'ensembles des lignes
*/
Drop Procedure if exists LOADER_LINKEDACCOUNTS_INSERT//
Create Procedure LOADER_LINKEDACCOUNTS_INSERT (
    In batchnum int
)
Begin
	Insert into ACCOUNTS_has_ACCOUNTS (
		ACCOUNTS_ID_ACCOUNTS, ACCOUNTS_ID_ACCOUNTS_LINK, ACCOUNTS_has_ACCOUNTS_STATUS
	)
	Select 
		src.ACCOUNTS_ID_ACCOUNTS, src.ACCOUNTS_ID_ACCOUNTS_LINKED, src.ACCOUNTS_has_ACCOUNTS_STATUS
	From
		LOADER_LINKEDACCOUNTS src
		Inner Join LOADER_STATUS lst
			On src.LOADER_STATUS_ID_LOADER_STATUS = lst.ID_LOADER_STATUS
	Where
		lst.LOADER_STATUS_BATCH_NUM = batchnum
		And src.ID_ACCOUNTS_has_ACCOUNTS is null -- ne dois pas exister
		And lst.LOADER_STATUS_ERRCOUNT = 0
		And src.LOADER_LINKEDACCOUNTS_REMOVE = 0
	;
End //

Delimiter ;


