CREATE PROCEDURE "dba"."mail_add_filelist"( in a_tyyp smallint, in a_kuup date, in a_asutnr integer, in a_name char(100), in a_address char(50), in a_subject char(200), in a_notetext long varchar, in a_content_type smallint, in a_skoop integer)
BEGIN
  declare ll_id integer;
  SELECT "filelist"."id" INTO ll_id FROM "filelist" WHERE "filelist"."tyyp"=a_tyyp AND date("filelist"."kell")=a_kuup AND "filelist"."asutnr"=a_asutnr AND "filelist"."skoop"=a_skoop AND "filelist"."id_user"=32767 AND "filelist"."staatus"<1;
  if ll_id>0 then
	UPDATE "filelist" SET mail_address=a_address,mail_subject=a_subject,mail_notetext=a_notetext WHERE "filelist"."id"=ll_id;
  else 
    INSERT INTO filelist(tyyp,asutnr,mail_name,mail_address,mail_subject,mail_notetext,content_type,skoop,id_user)
      VALUES (a_tyyp,a_asutnr,a_name,a_address,a_subject,a_notetext,a_content_type,a_skoop,32767);
  end if
END