From: | Asche <asche(dot)public(at)mac(dot)com> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Cc: | Hendra <manusiatidakbiasa(at)gmail(dot)com> |
Subject: | Re: Hi there, new here and have question |
Date: | 2008-07-09 10:31:42 |
Message-ID: | FC7C67BE-6BEF-4EB5-BD49-F750416CC7B2@mac.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
> CREATE OR REPLACE FUNCTION listofemployeebasedondepartment(_id_dept
> int)
> RETURNS SETOF record AS
> $BODY$
> DECLARE
> empdata record;
> BEGIN
>
> RETURN QUERY
> SELECT
> e.*, d.department_name
> FROM
> employee e, dept d
> WHERE
> e.id_dept = d.id AND
> e.id_dept = _id_dept;
>
> RETURN;
> END;
> $BODY$
> LANGUAGE 'plpgsql';
>
> I can call it by
> SELECT listofemployeebasedondepartment(dept_id)
> and it gives me return value a set of record,
> but when I want to get just one field of those record,
> for example
> SELECT name FROM listofemployeebasedondepartment(dept_id)
> psql gives me error that I don't have column-list or something like
> that
> How to achieve such result?
>
hi hendry,
simple example:
CREATE OR REPLACE FUNCTION test1(_id int)
RETURNS SETOF record AS
$BODY$
DECLARE rec record;
BEGIN
FOR rec IN
SELECT
a.foo, b.bar
FROM
a, b
WHERE
a.id = _id
AND a.id = b.id
LOOP
RETURN NEXT rec;
END LOOP;
RETURN;
END;
$BODY$
LANGUAGE 'plpgsql'
you have to specify the columns when you call your function something
like this:
select * from test1(1) as (foo text, bar text);
Jan
From | Date | Subject | |
---|---|---|---|
Next Message | Tino Wildenhain | 2008-07-09 10:34:10 | Re: Getting source code for database objects |
Previous Message | Craig Ringer | 2008-07-09 08:58:26 | Re: SELECT Query returns empty |