Check a record IS NOT NULL in plsql

前端 未结 2 1438
生来不讨喜
生来不讨喜 2021-01-01 15:08

I have a function which would return a record with type my_table%ROWTYPE, and in the caller, I could check if the returned record is null, but PL/SQL complains

相关标签:
2条回答
  • 2021-01-01 15:49

    As far as I know, it's not possible. Checking the PRIMARY KEY or a NOT NULL column should be sufficient though.


    You can check for v_record.row_id IS NULL.

    Your function would throw a NO_DATA_FOUND exception though, when no record is found.

    0 讨论(0)
  • 2021-01-01 16:05

    You can't test for the non-existence of this variable so there are two ways to go about it. Check for the existence of a single element. I don't like this as it means if anything changes your code no longer works. Instead why not just raise an exception when there's no data there:

    I realise that the others in the exception is highly naughty but it'll only really catch my table disappearing when it shouldn't and nothing else.

    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;
    
    0 讨论(0)
提交回复
热议问题