как обработать sql exception если все колонки пустые
Как оптимизировать ниже запрос?
begin
select t1.status, t1.curr, t1.amount, t1.serv
into stat,curr,amnt,serv
from table1 t1
where t1.id = 78;
exception when no_data_found then
result := 1;
stat := 'R';
when stat is null and curr is null and amnt is null and serv is null then
result := 1;
stat := 'R';
end;
Выборка возвращает не no_data_found а пустые поля то есть null
Нужно чтобы по данному блоку падал в no_data_found если все столбцы будут равны null
Добавил это блок:
when stat is null and curr is null and amnt is null and serv is null then
result := 1;
stat := 'R';
Ответы (2 шт):
Автор решения: Aziz Umarov
→ Ссылка
Почему предварительно не проверить по типу if exists then exception?
В этом варианте вы сможете проверить ваши поля на null.
if exists ( select * from table1 where "condition") begin exception ... end
... спокойной вставляете данные
Автор решения: 0xdb
→ Ссылка
Просто включите условие для пустых колонок в выборку:
create table t1 (id number, stat char (1), curr int, amnt int, serv int);
insert into t1 (id) values (78);
var rc refcursor
declare rec t1%rowtype; result int;
begin
begin
select * into rec
from t1
where t1.id = 78
and not (stat is null and curr is null and amnt is null and serv is null);
exception when no_data_found then
result := 1;
rec.stat := 'R';
end;
open :rc for select result result, rec.stat stat from dual;
end;
/
RESULT STAT
---------- --------------------------------
1 R