Can't catch null_value_not_allowed exception in postgresql 9.3 -


i wondering if missing obvious here. have function defines table name , goes query table in execute clause. if there no data in main table (one_min, fifteen_min etc) null_value_not_allowed exception code 22004. when try handler around exception, seems pass , still die. tried wrapping bigger if condition around out still no luck.

create or replace function loadresults(force_update boolean)   returns date $body$  declare  inter int; startdate date; table_name varchar(50);  begin  select 1440 / avg(array_length(intervals,1))::int inter temptable;  case inter     when 1      table_name := 'one_min';     when 15      table_name := 'fifteen_mins';     when 30      table_name := 'half_hour';     when 60      table_name := 'one_hour';     else         raise exception 'i not recognise interval %', inter ; end case;  set constraints deferred ;   if force_update true      select min(sday) startdate temptable ; else     begin         execute ' select max(sday) ' || table_name              || ' (orgid,householdid) in                  (select orgid, householdid temptable limit 1 )'         startdate ;     exception when null_value_not_allowed          select min(sday) startdate temptable;      end; end if; return startdate; end; $body$ language plpgsql volatile; 

given tables exist , work fine - function carries on load data - , works fine when force_flag true.
when force_update flag false , there no data in one_min table, error back:

error: query string argument of execute null sql state: 22004 context: pl/pgsql function loadresults(boolean) line 39 @ execute statement 

this points execute statement query not return values. ideas why might happen? i'd rather keep error handling within postgres rather remaining code.

update
have updated query in execute clause one:

execute ' select coalesce(res, tem) ' ||  ' (select max(sday) res ' || table_name  || '  (orgid,householdid) in (select orgid, householdid temptable limit 1 )) t1,  (select min(sday) tem temptable) m ' startdate ; 

this seems trick exception not raised. still understand why exception can't caught.

weird, seems, there 2 null_value_not_allowed exceptions (22004 , 39004).

try catch sqlstate, like:

begin     -- ... exception when sqlstate '22004'     -- ... end; 

or, can achieve same results, additional condition:

if force_update or table_name null     select min(sday) startdate temptable; else     execute 'select max(sday) '          || table_name          || ' (orgid,householdid) in (select orgid, householdid temptable limit 1 )'        startdate; end if; 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -