CREATE FUNCTION "dba"."getid_pa_kaader_pens"( ) 
returns integer
begin
  declare li_return integer;
  select max(asutpensnr) + 1 into li_return from pa_kaader_pens;
  if li_return is null or li_return < 1 then
    set li_return=1;
    while exists(select 1 from pa_kaader_pens where asutpensnr = li_return) loop
      set li_return=li_return+1
    end loop
  end if;
  return(li_return)
end