-
Returned more than one row error message in a FOR IN EXECUTE loop
I wrote a function that basically executes a dynamic query and returns
True if there is at least one record and false in not.
CREATE OR REPLACE FUNCTION RecordHasChild (table1, CHARACTER,
CHARACTER) RETURNS BOOLEAN AS '
DECLARE
childrec RECORD;
sqlquery text;
BEGIN
sqlquery = ''SELECT field1 FROM '' || $2 || '' WHERE ('' || $2
|| ''.account_no = '' || quote_literal(table1.account_no) || '') AND
('' || $3 || '') LIMIT 1'';
FOR childrec IN EXECUTE sqlquery LOOP
RETURN True;
END LOOP;
RETURN False;
END;
' LANGUAGE 'plpgsql';
I use this function in a where clause like RecordHasChild(table1,
'relatedtable', 'relatedtable.somefield = \'some value\'')
The problem I have is I get this error "query "SELECT ..." returned
more than one row". Why is there a problem if the query returns more
than one row ? That's why there is a loop. Any ideas ?
Thanks,
George