DELIMITER //
DROP PROCEDURE IF EXISTS `MonTest`;
CREATE PROCEDURE `MonTest`()
BEGIN
  DECLARE countTmp INT DEFAULT (SELECT EXISTS (SELECT 1 FROM owner));
  IF countTmp != 0 THEN
    INSERT INTO owner(siren, type, qualite, lastname, firstname, birth_day, birth_place, country)
    SELECT tmp.siren, tmp.type, tmp.qualite, tmp.lastname, tmp.firstname, tmp.birth_day, tmp.birth_place, tmp.country FROM tmp
    WHERE tmp.siren NOT IN (SELECT siren FROM owner);

    INSERT INTO owner(siren, type, qualite, lastname, firstname, birth_day, birth_place, country)
    SELECT tmp.siren, tmp.type, tmp.qualite, tmp.lastname, tmp.firstname, tmp.birth_day, tmp.birth_place, tmp.country FROM tmp
    WHERE tmp.siren IN (SELECT siren FROM owner) AND tmp.lastname NOT IN (SELECT lastname FROM owner) AND tmp.firstname NOT IN (SELECT firstname FROM owner);
  ELSE
    INSERT INTO owner SELECT * FROM tmp;
  END IF;
END
//

DELIMITER ;
CALL MonTest;
