Re: Custom Field for a table row returned from stored procedure

From: "A(dot) Kretschmer" <andreas(dot)kretschmer(at)schollglas(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Custom Field for a table row returned from stored procedure
Date: 2010-01-11 09:11:51
Message-ID: 20100111091151.GA22848@a-kretschmer.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

In response to Yan Cheng Cheok :
> I have a "lot" table with 2 columns, with one of the column is current timestamp.
>
> I try to return another custom fields, which its calculation is based on timestamp.
>
> (For simplicity, I include only 1 field in following example)
>
> CREATE OR REPLACE FUNCTION create_lot(text)
> RETURNS lot AS
> $BODY$DECLARE
> configurationFile ALIAS FOR $1;
> _lot lot;
> BEGIN
> INSERT INTO lot(configuration_file)
> VALUES(configurationFile) RETURNING *, extract(epoch from timestamp) as timestampex INTO _lot;
> return _lot;
> END;$BODY$
> LANGUAGE 'plpgsql' VOLATILE
> COST 100;
> ALTER FUNCTION create_lot(text) OWNER TO postgres;
>
> I expect the table returns two field for a single row. 1 is configuration_file, 2nd is timestamp, 3rd is timestampex.
^^^^^^ ^^^^^^^^^^^^^^^^^

I think, you expected the function returns 3 fields, right?

>
> However, I only able to retrieve configuration_file and timestamp. How can I obtain timestampex too?

You have defined your function "RETURNS lot", and the data-type "lot"
contains only 2 fields.

I think, you have to rewrite your function, simple example:

test=# create table lot (a int, b int);
CREATE TABLE
test=*# create or replace function insert_lot(in _a int, in _b int, out x int,out y int,out z int) as $$begin
insert into lot values (_a, _b);
x:=_a;
y:=_b;
z:=_a+_b;
return;
end;$$ language plpgsql;
CREATE FUNCTION
test=*# select * from insert_lot(2,5);
x | y | z
---+---+---
2 | 5 | 7
(1 row)

HTH, Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431 2EB0 389D 1DC2 3172 0C99

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Vincenzo Romano 2010-01-11 10:14:10 Composite types questions
Previous Message Leif Biberg Kristensen 2010-01-11 08:43:36 Re: How to get DATE in server locale format