NO DATA FOUND Exception - Mailing list pgsql-sql

From Fernando Hevia
Subject NO DATA FOUND Exception
Date
Msg-id 04e701c7b75d$e9e5c6e0$8f01010a@iptel.com.ar
Whole thread Raw
In response to Re: [GENERAL] yet another simple SQL question  (Erik Jones <erik@myemma.com>)
Responses Re: NO DATA FOUND Exception  (Andrew Sullivan <ajs@crankycanuck.ca>)
Re: NO DATA FOUND Exception  (Michael Glaesemann <grzm@seespotcode.net>)
List pgsql-sql
Hi.

Im taking my first steps with plpgsql.
I want my function to react to the result of a query in the following way:

begin select column into variable from table where condition;
exception when <<data_found>> then return variable; when <<no_data_found>> then <<do nothing/continue>> ; when
<<any_other_exception>>then <<raise some error message>> ;
 
end ;

Is something like this possible en plpgsql without recurring to a select
count(*) to check how many results I will get?


Actual code is:

CREATE OR REPLACE FUNCTION test(p_line text) RETURNS text AS
$body$
DECLARE v_len integer DEFAULT 8; v_search varchar; v_register num_geo%ROWTYPE;
BEGIN
 -- Search loop WHILE v_len > 0 LOOP   v_search := substring(p_line, 1, v_len);   begin     SELECT * INTO v_register
WHEREprefix = v_search;   exceptionwhen no_data then        -- Getting error here    continue;       when others then
return v_register.prefix;   end;       v_len := v_len - 1; END LOOP;
 
 raise 'Not found';
END;
$body$
LANGUAGE 'plpgsql' VOLATILE ;


ERROR: unrecognized exception condition "no_data"
SQL state: 42704
Context: compile of PL/pgSQL function "test" near line 14


Thanks,
Fernando.




pgsql-sql by date:

Previous
From: Erik Jones
Date:
Subject: Re: [GENERAL] yet another simple SQL question
Next
From: Andrew Sullivan
Date:
Subject: Re: NO DATA FOUND Exception