views:

106

answers:

2

Hi all,

Ich have a problem in postgres function:


 CREATE OR REPLACE FUNCTION getVar(id bigint) 
         RETURNS TABLE (repoid bigint, suf VARCHAR, nam VARCHAR)

         AS $$ 
        declare rec record;

        BEGIN
          FOR rec IN
         (WITH RECURSIVE children(repoobjectid,variant_of_object_fk, suffix, variantname) AS (
    SELECT repoobjectid, variant_of_object_fk, '' as suffix,variantname
    FROM b2m.repoobject_tab 
    WHERE repoobjectid = id
      UNION ALL
    SELECT repo.repoobjectid, repo.variant_of_object_fk, suffix || '..' , repo.variantname
    FROM b2m.repoobject_tab repo, children 
    WHERE children.repoobjectid = repo.variant_of_object_fk)   
         SELECT repoobjectid,suffix,variantname FROM children)
          LOOP

            RETURN next;

          END LOOP;

          RETURN;

         END;

 

It can be compiled, but if y try to call it

select * from getVar(18)

I got 8 empty rows with 3 columns.

If i execute the following part of procedure with hard-coded id parameter:

WITH RECURSIVE children(repoobjectid,variant_of_object_fk, suffix, variantname) AS (
    SELECT repoobjectid, variant_of_object_fk, '' as suffix,variantname
    FROM b2m.repoobject_tab 
    WHERE repoobjectid = 18
      UNION ALL
    SELECT repo.repoobjectid, repo.variant_of_object_fk, suffix || '..' , repo.variantname
    FROM b2m.repoobject_tab repo, children 
    WHERE children.repoobjectid = repo.variant_of_object_fk)   
         SELECT repoobjectid,suffix,variantname FROM children

I got exactly, what i need 8 rows with data:


repoobjectid  suffix variantname

18
19            ..     for IPhone
22            ..     for Nokia
23            ....   OS 1.0

and so on.

What is going wrong ? Please help.

Thanx in advance

A: 

I think if you're doing "return table", you need to assign to the "columns" of the table before doing "return next". So something like:

repoid := rec.repoid;
suf := rec.suf;
nam := rec.nam;

just before your "RETURN NEXT". Since you're not assigning these, they're being returned as null.

araqnid
A: 

Here is a sample code of a funcion that returns rows from a table, I think it might help.

First, a sample table with sample data:

CREATE TABLE sample_table (id smallint, description varchar, primary key (id));

INSERT INTO sample_table (id, description) VALUES (1, 'AAAA');
INSERT INTO sample_table (id, description) VALUES (2, 'BBBB');
INSERT INTO sample_table (id, description) VALUES (3, 'CCCC');
INSERT INTO sample_table (id, description) VALUES (4, 'DDDD');
INSERT INTO sample_table (id, description) VALUES (5, 'EEEE');

Then, a return type that describes the fields of the rows returned:

CREATE TYPE return_type AS
   (id smallint,
    description varchar);
ALTER TYPE return_type OWNER TO postgres;

Then the function itself:

CREATE OR REPLACE FUNCTION report(p_id integer)
  RETURNS SETOF return_type AS
$BODY$
DECLARE
  retorno return_type%ROWTYPE;
BEGIN
  FOR RETORNO IN SELECT * FROM sample_table WHERE id = p_id LOOP
    RETURN NEXT RETORNO;
  END LOOP;
  RETURN;
END
$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100
  ROWS 1000;
ALTER FUNCTION report(p_id integer) OWNER TO postgres;

And here goes the function call:

SELECT * FROM report(1);
Paulo Centeno