SELECT INTO returning more than one row

From: Zac <zaccheob(at)inwind(dot)it>
To: pgsql-sql(at)postgresql(dot)org
Subject: SELECT INTO returning more than one row
Date: 2005-06-21 09:54:41
Message-ID: d98o6p$i1p$1@news.hub.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi.

I have this problem in a plpgsql function:

SELECT INTO myvar col FROM table WHERE ...;

IF <query returns more than one row> THEN
do something
ELSE IF <query returns no rows> THEN
do something else
ELSE
do other things

If <query returns no rows> I know that myvar IS NULL OR GET DIAGNOSTICS
ROW_COUNT is zero.

Is there a way to know if the query returned more than one row?
In Oracle PL/SQL I can catch the TOO_MANY_ROWS exception but in Postgres
I found no way for doing it:

- myvar is correctly valued with the first occurrence of the result
- FOUND is TRUE
- GET DIAGNOSTICS ROW_COUNT is 1 (it counts only fetched rows)

I tried to use a cursor but the only way to know how many rows it
returned is to fetch them all. (And I don't like this solution)

Thanks in advance.

Browse pgsql-sql by date

  From Date Subject
Next Message Kenneth Gonsalves 2005-06-21 10:01:28 customising serial type
Previous Message Michael Fuhr 2005-06-21 00:12:40 Re: SQL repr of bytea val