38

状態を確認する必要があります。すなわち:

if (condition)> 0 then
  update table
else do not update
end if

select into を使用して結果を変数に格納する必要がありますか?

例えば:

declare valucount integer
begin
  select count(column) into valuecount from table
end
if valuecount > o then
  update table
else do 
  not update
4

3 に答える 3

52

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:
...
...

代わりに変数を使用する必要があります。

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.

もちろん、すべてを SQL で実行できる場合もあります。

update my_table
set x = y
where (select count(*) from other_table) >= 1;

何かが不可能であることを証明することは困難です。上記の単純なテスト ケースの他に、ステートメントの構文図を確認できます。どのブランチにもステートメントはIFありません。SELECT

于 2012-04-18T05:06:18.673 に答える
3

編集:

この回答が提供されたとき、oracleタグは質問にありませんでした。明らかにoracleでは機能しませんが、少なくともpostgresとmysqlでは機能します

いいえ、値を直接使用してください。

begin
  if (select count(*) from table) > 0 then
     update table
  end if;
end;

「else」は必要ないことに注意してください。

編集済み

update ステートメントですべてを簡単に行うことができます(つまり、if構成要素はありません)。

update table
set ...
where ...
and exists (select 'x' from table where ...)
于 2012-04-17T22:56:42.047 に答える