Страницы

Поиск по вопросам

воскресенье, 29 декабря 2019 г.

Select в условии if на pl/sql

#oracle #plsql


Пишу скрипт на PL/SQL с условием:

if ((select *
     from table1 
     where quant>1000) is not null) 
then ... 
else RAISE error$e; 
end if;


При компиляции выдает следующую ошибку: 


  PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:


Можно ли подставить запрос прямо в условие, а не создавать новую переменную?
    


Ответы

Ответ 1



Видимо, подразумевается if exists (select * from table1 where quant>1000)

Ответ 2



Нельзя. Выражение select ... это указатель на курсор, IF ожидает логическое выражение. Если было бы даже и можно я лучше бы завернул запрос в функцию. Где то так: create table table1 (quant number); insert into table1 values (0); declare function existsHighQuant return boolean is begin for r in (select 1 from table1 where quant>1000) loop return true; end loop; return false; end; -- или так - не очень изящный по моему мнению вариант function existsRow(stmt varchar2) return boolean is curs sys_refcursor; dummy number; begin open curs for stmt; fetch curs into dummy; return curs%found; end; begin if existsHighQuant then null; --do something else raise_application_error(-20000, 'quant gt 1000 not found'); end if; if existsRow('select 1 from table1') then dbms_output.put_line('at least 1 row exists'); end if; end; / ORA-20000: quant gt 1000 not found insert into table1 values (2000); -- повторить вызов блока PL/SQL procedure successfully completed. at least 1 row exists

Комментариев нет:

Отправить комментарий