Re: PL/pgSQL: SELECT INTO only if result count = 1

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Tarlika Elisabeth Schmitz <postgresql6(at)numerixtechnology(dot)de>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: PL/pgSQL: SELECT INTO only if result count = 1
Date: 2011-08-27 22:36:14
Message-ID: 15694.1314484574@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Tarlika Elisabeth Schmitz <postgresql6(at)numerixtechnology(dot)de> writes:
> SELECT INTO
> country_id, region_id, town_id
> country_fk, region_fk, id
> FROM town
> WHERE ...;

> GET DIAGNOSTICS cnt = ROW_COUNT;
> RAISE DEBUG 'COUNT %', cnt;

> always returns 1

Yeah. By default, SELECT INTO just fetches one row and stops;
it doesn't look to see if there are more.

You could possibly use SELECT INTO STRICT and catch the error if
there's more than one row. I suspect though that it'd be more efficient
to use a FOR loop and just note for yourself how many rows you get.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tarlika Elisabeth Schmitz 2011-08-27 23:20:28 Re: PL/pgSQL: SELECT INTO only if result count = 1
Previous Message Darren Duncan 2011-08-27 22:26:59 Re: Still no way to install PostGres on os x Lion?