CREATE PROCEDURE REGISTER_LOAD (
    npolice integer,
    family varchar(25),    
    name varchar(25),
    father varchar(25),
    bday date,
    smo_id integer,
    pers_num varchar(11),
    sex varchar(1),
    status integer,
    deleted integer,
    nreestr varchar(3))
returns (
    result integer)
as
declare variable v_bday date;
declare variable v_foms varchar(1);
declare variable v_father varchar(25);
declare variable v_name varchar(25);
declare variable v_family varchar(25);
declare variable v_id integer;
declare variable v_count integer;
declare variable v_sysdate timestamp;

begin
  result = 0;
  v_count = 0;
  for select id, family, name,  father, foms, bday from people where npolice = :npolice
        into v_id, v_family, v_name, v_father, v_foms, v_bday do
  begin
    if ((v_foms <> '1') and
        ((v_family <> :family)
        or (v_name <> :name)
        or (v_father <> :father)
        or (extract(year from v_bday) <> extract(year from :bday)))) then
    begin
      select current_timestamp from rdb$database into :v_sysdate;
      insert into log values(cast(:v_sysdate as varchar(30))||' Полис '||cast(:npolice as varchar (8)));
      insert into log values ('   база данных: '||:v_family||' '||:v_name||' '||:v_father||' '||cast(:v_bday as varchar(10)));
      insert into log values('   реестр ФОМС: '||:family||' '||:name||' '||:father||' '||cast(:bday as varchar(10)));
    end
    v_count = v_count + 1;
  end
  if (v_count = 0) then
      result = 1; 
  else if (v_count >= 1) then
  begin
    if (v_count > 1) then
    begin
      select current_timestamp from rdb$database into :v_sysdate;
      insert into log values(cast(:v_sysdate as varchar(30))||' Полис '||cast(:npolice as varchar (8))||' дублируется в базе данных');
    end
    update people
    set deleted = :deleted,
        status = :status,
        pers_num = :pers_num,
        sex = :sex,
        smo_id = :smo_id,
        nreestr = :nreestr
    where id = :v_id;
  end
  suspend;
end;
&^^&