CREATE OR REPLACE FUNCTION public.tr_tetel_raktar_valtozas()
  RETURNS trigger AS
'
declare
  v_hasznalatban tetel.hasznalatban%type;
begin
   if (new.keszlet > 0) then 
      select hasznalatban
        into v_hasznalatban
        from tetel
       where kod = new.tetel_kod;
       if (not (v_hasznalatban = 1)) then
          UPDATE tetel SET hasznalatban = 1 WHERE kod = new.tetel_kod; 
       end if;
   end if;       
   return new;
end;'
  LANGUAGE plpgsql VOLATILE
  COST 100;