CREATE FUNCTION "DBA"."getid_klassiflist"( IN a_table char(20) DEFAULT '', IN a_id char(20) DEFAULT '' )
returns integer
begin
  declare li_return integer;
  declare ll_id integer;
  declare ls_sql char(100);
  if Length(a_id)>0 and Length(a_table)>0 then
    SELECT "table_id" INTO ll_id FROM "SYSTABLE" WHERE "table_name"=a_table;
    if ll_id>0 then
      if exists(select 1 from "SYSCOLUMN" where "table_id"=ll_id and "column_name"=a_id) then
        set ls_sql='select max('+a_id+')+1 into li_return from '+a_table;
        execute immediate ls_sql;
        if li_return is null or li_return < 1 then
          set li_return=1;
          id_loop: loop
            set ll_id=null;
            set ls_sql='select '+a_id+' into ll_id from '+a_table+' where '+a_id+' = li_return';
            execute immediate ls_sql;
            if ll_id is null then
              leave id_loop
            end if;
            set li_return=li_return+1;
          end loop id_loop;
        end if;
      end if;
    end if;
  end if;
  return(li_return)
end