CREATE OR REPLACE FUNCTION food600.semafor_stav_all(
	ip_hod character,
	OUT op_hod character)
    RETURNS character
    LANGUAGE 'plpgsql'
    COST 100
    VOLATILE
AS $BODY$
DECLARE li_semafor INT; 
        ln_semafor CHARACTER(50);
        lkto CHARACTER(30);
	pocetriadkov INTEGER;
	li INT DEFAULT 0;
	lzamknuty INTEGER DEFAULT 0;
	lhod CHARACTER(50);
	prepbufout CURSOR FOR SELECT i_semafor,n_semafor,kto FROM food600.semafor WHERE n_semafor LIKE lhod;
BEGIN
op_hod = '';
lhod = '%' || ip_hod || '%';
SELECT count(*)  INTO pocetriadkov FROM food600.semafor WHERE n_semafor LIKE lhod;

OPEN prepbufout;
WHILE li<=pocetriadkov LOOP
	
        FETCH prepbufout INTO li_semafor, ln_semafor, lkto;
        li:=li + 1;

	lkto:= food600.semafor_stav(ln_semafor);
	
	IF lkto<>'' THEN 
         op_hod = lkto;
         li:=pocetriadkov;
	END IF;

END LOOP;

CLOSE prepbufout;

END
$BODY$;
