Re: writing a plpgsql query for the first time - Mailing list pgsql-general

From Gabriele Bartolini
Subject Re: writing a plpgsql query for the first time
Date
Msg-id eb0f5bf2bd0c3743f1036e3bc15b6a33@2ndquadrant.it
Whole thread Raw
In response to writing a plpgsql query for the first time  ("maxxedit@gmail.com" <maxxedit@gmail.com>)
List 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.Bartolini@2ndQuadrant.it - www.2ndQuadrant.it

pgsql-general by date:

Previous
From: "maxxedit@gmail.com"
Date:
Subject: writing a plpgsql query for the first time
Next
From: Merlin Moncure
Date:
Subject: Re: Postgres 8.3 vs. 8.4 - Query plans and performance