ALTER FUNCTION "dba"."get_asulist_nimi_dok"( in "a_asutnr" integer ) 
returns char(101)
begin
  declare "ls_return" char(101);
  declare "ls_nimi" char(100);
  declare "ls_enimi" char(50);
  declare "li_enimi_nimi" smallint;
  if "a_asutnr" > 0 then
    select if "asulist"."nimi" is null then '' else "asulist"."nimi" endif,
      if "asulist"."enimi" is null then '' else "asulist"."enimi" endif
      into "ls_nimi","ls_enimi" from "asulist" where "asulist"."asutnr" = "a_asutnr";
    select "tm_rmpt"."k_dok_enimi" into "li_enimi_nimi" from "tm_rmpt" where "tm_rmpt"."id" = 1;
    if "li_enimi_nimi" = 1 then
      set "ls_return"="Left"("Trim"("ls_enimi"+' '+"ls_nimi"),101)
    else
      set "ls_return"="Left"("Trim"("ls_nimi"+' '+"ls_enimi"),101)
    end if
  end if;
  if "ls_return" is null then
    set "ls_return" = ''
  end if;
  return "ls_return"
end