if(从表中选择count(列))> 0然后

use*_*913 29 oracle plsql

我需要检查一下情况.即:

if (condition)> 0 then
  update table
else do not update
end if
Run Code Online (Sandbox Code Playgroud)

我是否需要使用select into将结果存储到变量中?

例如:

declare valucount integer
begin
  select count(column) into valuecount from table
end
if valuecount > o then
  update table
else do 
  not update
Run Code Online (Sandbox Code Playgroud)

Jon*_*ler 41

是的,您必须使用变量.您不能在PL/SQL表达式中直接使用SQL语句:

SQL> begin
  2     if (select count(*) from dual) >= 1 then
  3        null;
  4     end if;
  5  end;
  6  /
        if (select count(*) from dual) >= 1 then
            *
ERROR at line 2:
ORA-06550: line 2, column 6:
PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:
...
...
Run Code Online (Sandbox Code Playgroud)

相反,你必须做这样的事情:

SQL> set serveroutput on
SQL>
SQL> declare
  2     v_count number;
  3  begin
  4     select count(*) into v_count from dual;
  5
  6     if v_count >= 1 then
  7             dbms_output.put_line('Pass');
  8     end if;
  9  end;
 10  /
Pass

PL/SQL procedure successfully completed.
Run Code Online (Sandbox Code Playgroud)

当然,您可以在SQL中完成所有事情:

update my_table
set x = y
where (select count(*) from other_table) >= 1;
Run Code Online (Sandbox Code Playgroud)

很难证明某些事情是不可能的.除了上述简单的测试用例,你可以看一下语法图IF说明; 你不会SELECT在任何一个分支机构看到声明.