Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2008
    Posts
    7

    Question Unanswered: Function not working in a different version of PostgreSQL

    Hello everyone,

    I've created this function:
    Code:
    CREATE OR REPLACE FUNCTION subcategories(int) RETURNS setof int AS '
    DECLARE
    	-- declarations
    	category		ALIAS FOR $1;
    	category_current	record;
    BEGIN
    	-- function body
    	RETURN QUERY SELECT category as my_output;
    	FOR category_current IN SELECT id FROM is_categories WHERE id_parent=category AND status=''1'' LOOP
    		RETURN QUERY SELECT category_current.id as my_output UNION SELECT * FROM subcategories(category_current.id);
    	END LOOP;
    END;
    ' LANGUAGE plpgsql;
    It works well on my development machine with PostgreSQL 8.3.6, but on the server where there's 7.4.8 installed, it returns no results and no errors either.

    That I don't quite understand, as this query:
    Code:
    SELECT id FROM is_categories WHERE id_parent=1 AND status='1';
    does return two rows (as it should), but calling:
    Code:
    SELECT * FROM subcategories(1);
    returns none and there's no error message either.

    It's my second function in pg/plsql I've created and I don't really know why there are no results being returned in 7.4.8.

    Any ideas are appreciated.

    Thanks in advance.

  2. #2
    Join Date
    Nov 2008
    Posts
    7
    Got this answered on the PostgreSQL Novice mailing list:
    Re: Function working in PostgreSQL 8.3.6 and not in 7.4.8

    The working function looks like this, if anybody would like to know:
    Code:
    CREATE OR REPLACE FUNCTION subcategories(int) RETURNS setof int AS '
    DECLARE
    	-- declarations
    	category		ALIAS FOR $1;
    	category_current	record;
    	sub_category_current	record;
    BEGIN
    	-- function body
    	SELECT category as my_output INTO category_current;
    	RETURN NEXT category_current.my_output;
    	FOR category_current IN SELECT id FROM is_categories WHERE id_parent=category AND status=''1'' LOOP
    		FOR sub_category_current IN SELECT subcategories FROM subcategories(category_current.id) LOOP
    			RETURN NEXT sub_category_current.subcategories;
    		END LOOP;
    	END LOOP;
    	RETURN;
    END;
    ' LANGUAGE plpgsql;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •