SQL存储过程检查表中的值是/否并执行sql

2024-03-19

检查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

没有if (cursor)建筑或实际上任何existsPL/SQL 语法中的运算符。你需要做这样的事情:

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;

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

select count(*) into somevar from dual
where  exists
       ( select 1
         from   table1
         where  value_desc = 'Indicator'
         and    value1 = 'Y'
         and    rownum = 1 );
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SQL存储过程检查表中的值是/否并执行sql 的相关文章

随机推荐