Re: writing a plpgsql query for the first time

From: Gabriele Bartolini <Gabriele(dot)Bartolini(at)2ndQuadrant(dot)it>
To: <maxxedit(at)gmail(dot)com>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: writing a plpgsql query for the first time
Date: 2011-03-16 11:02:05
Message-ID: eb0f5bf2bd0c3743f1036e3bc15b6a33@2ndquadrant.it
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi,

> test=# create function testfunc()
> test-# returns table (id int, code char(1)) as $$
> test$# BEGIN
> test$# return query select id, code from
> record_table where id > 2;
> test$# END;
> test$# $$ language plpgsql;

That's due to a clash in the identifiers' names. As you see you have an
'id' as output parameter, then 'id' in the query as part of the select
and an 'id' as part of the WHERE condition.

I suggest that you change the query and specify directly the name of
the fields, as follows:

return query select r.id, r.code from record_table r where r.id > 2;

Have a look on the documentation about this kind of issues, which has
been improved from version 9
(http://www.postgresql.org/docs/9.0/interactive/plpgsql-implementation.html#PLPGSQL-VAR-SUBST).

Cheers,
Gabriele

--
Gabriele Bartolini - 2ndQuadrant Italia
PostgreSQL Training, Services and Support
Gabriele(dot)Bartolini(at)2ndQuadrant(dot)it - www.2ndQuadrant.it

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Merlin Moncure 2011-03-16 13:22:03 Re: Postgres 8.3 vs. 8.4 - Query plans and performance
Previous Message maxxedit@gmail.com 2011-03-16 10:43:37 writing a plpgsql query for the first time