CREATE FUNCTION "dba"."get_pa_haig_alates"( in a_skoop integer default 0, in a_id_user integer default 32767)
RETURNS INTEGER
BEGIN
  declare li_alates integer;
  select if yld_haig_alates is null then 0 else yld_haig_alates endif into li_alates from t_user where t_user.id_user=a_id_user;
  if li_alates=0 then
    select if yld_haig_alates is null then 0 else yld_haig_alates endif into li_alates from skoobid where skoobid.skoop=a_skoop;
  end if;
  if li_alates=0 then
    select if yld_haig_alates is null OR yld_haig_alates = 0 then 1 else yld_haig_alates endif into li_alates from pa_konfm WHERE "id"=1;
  end if;
  return li_alates;
END