Thread: Receive a record not a tuple - plpgsql

Receive a record not a tuple - plpgsql

From
Flávio Brito
Date:
Dear Friends

How can I call a function into a function? My problem is: I'm trying to calculate a tax(inss) over a employee salary. I created a function called inss that do it correctly, but when I create another one to show more attributes (inss is not a attribute, it is calculate over a salary) I receive a record (like {1,Mary,32.45} not a tuple. How can I solve it?

CREATE TABLE emp
(
  cod numeric
  name text NOT NULL,
  last_date timestamp,
  last_user text,
  salary numeric,
  CONSTRAINT pkey PRIMARY KEY (cod)
)

CREATE FUNCTION inss() RETURNS numeric AS '
SELECT salary*0.11 FROM emp;
'LANGUAGE 'sql';
---------------------------------------------------------------------------------------------
It is OK
---------------------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION mostra_inss() RETURNS setof record AS '

select name, inss(), salary from emp;

'LANGUAGE 'sql';
---------------------------------------------------------------------------------------------
--- Only for test --- DROP FUNCTION show_inss();
--- Only for test --- select inss();
---------------------------------------------------------------------------------------------
--- Another Test - Not OK

CREATE OR REPLACE FUNCTION show_inss() RETURNS VARCHAR AS '
DECLARE
inss float;
BEGIN
SELECT INTO inss inss() FROM emp;
RETURN inss;
END;
' LANGUAGE 'plpgsql';
---------------------------------------------------------------------------------------------
--- Another Test (NOT OK)
---------------------------------------------------------------------------------------------
create or replace function test_inss() returns record as '
      DECLARE
         d record;
      BEGIN
         for d in select name,salary,inss() from emp
         loop     
         end loop;
      RETURN;
   END;
   ' language 'plpgsql';

select test_inss();

Re: Receive a record not a tuple - plpgsql

From
Michael Fuhr
Date:
On Wed, Nov 16, 2005 at 06:41:36PM -0200, Flvio Brito wrote:
> How can I call a function into a function? My problem is: I'm trying to
> calculate a tax(inss) over a employee salary. I created a function
> called inss that do it correctly, but when I create another one to show
> more attributes (inss is not a attribute, it is calculate over a salary)
> I receive a record (like {1,Mary,32.45} not a tuple. How can I solve it?

You can put a function in the FROM clause and use a column definition
list:

test=> SELECT test();
      test
----------------
 (1,Mary,32.45)
(1 row)

test=> SELECT * FROM test() AS (id integer, name text, salary numeric);
 id | name | salary
----+------+--------
  1 | Mary |  32.45
(1 row)

Another possibility is to create a type and have the function return
that type instead of record:

CREATE TYPE person_info AS (
    id      integer,
    name    text,
    salary  numeric
);

CREATE FUNCTION test() RETURNS person_info AS ...

test=> SELECT * FROM test();
 id | name | salary
----+------+--------
  1 | Mary |  32.45
(1 row)

test=> SELECT (test()).*;
 id | name | salary
----+------+--------
  1 | Mary |  32.45
(1 row)

Is one of these examples what you're looking for?

--
Michael Fuhr