Unix Technical Forum

Receive a record not a tuple - plpgsql

This is a discussion on Receive a record not a tuple - plpgsql within the pgsql Admins forums, part of the PostgreSQL category; --> Dear Friends How can I call a function into a function? My problem is: I'm trying to calculate a ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Admins

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-10-2008, 06:29 AM
=?ISO-8859-1?Q?Fl=E1vio?= Brito
 
Posts: n/a
Default Receive a record not a tuple - plpgsql

Dear Friends

How can I call a function into a function? My problem is: I'm trying to
calculate a tax(inss) over a employer 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();

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-10-2008, 06:30 AM
Thomas F. O'Connell
 
Posts: n/a
Default Re: Receive a record not a tuple - plpgsql


On Nov 16, 2005, at 2:21 PM, Flávio Brito wrote:

> How can I call a function into a function? My problem is: I'm
> trying to calculate a tax(inss) over a employer 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?


Have you looked into set-returning functions? It seems like that
might be what you're looking for:

http://www.postgresql.org/docs/8.1/s...pgsql-control-
structures.html#PLPGSQL-STATEMENTS-RETURNING

--
Thomas F. O'Connell
Database Architecture and Programming
Co-Founder
Sitening, LLC

http://www.sitening.com/
110 30th Avenue North, Suite 6
Nashville, TN 37203-6320
615-469-5150
615-469-5151 (fax)
---------------------------(end of broadcast)---------------------------
TIP 2: Don't 'kill -9' the postmaster

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 11:02 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com