22

タイプ のレコードを返す関数がありmy_table%ROWTYPE、呼び出し元で、返されたレコードが null かどうかを確認できますが、PL/SQL は if ステートメントに不平を言います

PLS-00306: 'IS NOT NULL'のコールの引数の数またはタイプが正しくありません

これが私のコードです:

v_record my_table%ROWTYPE;
v_row_id my_table.row_id%TYPE := 123456;
begin
    v_record := myfunction(v_row_id)
    if (v_record is not null) then
        -- do something
    end if;
end;

function myfunction(p_row_id in my_table.row_id%TYPE) return my_table%ROWTYPE is
    v_record_out my_table%ROWTYPE := null;
begin
    select * into v_record_out from my_table
    where row_id = p_row_id;
    return v_record_out;
end myfunction;

ありがとう。

4

2 に答える 2

33

私の知る限り、それは不可能です。PRIMARY KEYただし、または列をチェックするNOT NULLだけで十分です。


を確認できますv_record.row_id IS NULL

NO_DATA_FOUNDただし、レコードが見つからない場合、関数は例外をスローします。

于 2011-08-26T16:42:11.237 に答える
3

この変数が存在しないことをテストすることはできないため、2 つの方法があります。単一の要素の存在を確認します。これは、コードを変更しても機能しなくなることを意味するため、好きではありません。代わりに、データがない場合に例外を発生させません:

othersは、例外が非常にいたずらであることを認識していますが、テーブルが消えるのを実際にキャッチするのは、そうすべきでないときにだけであり、他には何もありません。

v_record my_table%ROWTYPE;
v_row_id my_table.row_id%TYPE := 123456;

begin
    v_record := myfunction(v_row_id)
exception when others then
        -- do something
end;

function myfunction(p_row_id in my_table.row_id%TYPE) return my_table%ROWTYPE is
    v_record_out my_table%ROWTYPE := null;

cursor c_record_out(c_row_id char) is
 select * 
   from my_table
  where row_id = p_row_id;

begin
   open c_record_out(p_row_id);
   fetch c_record_out into v_record_out;

   if c_record_out%NOTFOUND then
      raise_application_error(-20001,'no data);
   end if;
   close c_record_out;
return v_record_out;
end myfunction;
于 2011-08-27T08:06:47.753 に答える