【问题标题】:SQL stored procedure to check value yes/no in a table and execute sqlSQL存储过程检查表中的值是/否并执行sql
【发布时间】:2021-08-25 16:54:11
【问题描述】:

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

【问题讨论】:

  • 那是你的实际代码吗?还是伪代码?

标签: sql oracle stored-procedures


【解决方案1】:

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

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

【讨论】:

  • 这不是有效的 oracle 语法。它返回“PLS-00204:函数或伪列 'EXISTS' 只能在 SQL 语句中使用”
  • 同意,已更新我的答案和链接。感谢您指出这一点!
【解决方案2】:

在 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 );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    相关资源
    最近更新 更多