SQL 存储过程检查 table 中的值 yes/no 并执行 sql

SQL stored procedure to check value yes/no in a table and execute sql

检查table1中是否value=Y,然后立即执行sql_select

if
(select value1 from table1 where value_desc='Indicator' and value1='Y')
then 
    execute immediate sql_select_yes
else 
    execute immediate sql_select_no

您可以参考this SO answer to call other SP

并参考 this link 以正确使用 Select - EXISTS(似乎 IF 和 EXISTS 不是有效组合)

在 PL/SQL 语法中没有 if (cursor) 结构或任何 exists 运算符。您将需要执行以下操作:

declare
    somevar number;
begin
    select count(*) into somevar
    from   table1
    where  value_desc = 'Indicator'
    and    value1 = 'Y'
    and    rownum = 1;

    if somevar > 0 then
        execute immediate sql_select_yes
    else 
        execute immediate sql_select_no
    end;
end;

and rownum = 1 条件只是为了防止有大量行,因为您不需要它来计算所有行以进行存在性测试。 (如果它必须计算一百万行,它不会影响结果,当你只关心一行是否存在时,这只是浪费时间。)你同样可以使用这样的东西来检查是否存在:

select count(*) into somevar from dual
where  exists
       ( select 1
         from   table1
         where  value_desc = 'Indicator'
         and    value1 = 'Y'
         and    rownum = 1 );