PL/SQL Selectinto異常処理

6153 ワード

select intoを使用して変数に値を割り当てると、変数がコレクションタイプであれば異常は発生せず、基本タイプまたはレコードタイプであれば異常が報告されます.
異常が発生したらどうしますか?もちろん捕獲して処理しました.
通常のコードブロックでは、コードブロックの末尾で処理すればよい.
でもサイクルの中では?異常の伝播により、異常発生プログラムは中止されます.しかし、異常が発生した後、continueの効果を達成するには、最後のexceptionにwhen others then continueを追加することはできません.の文を入力します.そうしないと、エラーが発生します.
ループに到達するには、データが見つからない場合にcontinueを設定し、値を割り当てるときにno_data_foundの場合は異常処理を行います.すなわち,付与文をbegin exception endの構造に含める.
また、記録タイプは全体的に空と判断することができず、ある列の値を修正して判断したり、1つのフラグ変数を用いて判断したりすることができる.
次は小さな例ですが、一緒に感じてみましょう.
 1 declare

 2   v_test2 studentRecord;--    

 3   v_test3 studentRecord;--       

 4   cursor c_test is 

 5   select *

 6     from student_head h

 7    where h.student_key in (3285, 3286, 3287, 3288);

 8   flag boolean;

 9 begin

10   open c_test;

11   loop

12     fetch c_test into v_test2;

13     exit when c_test%notfound;

14     flag := true;

15     begin

16       select *

17         into v_test3

18         from student_head

19        where student_key = v_test2.student_key

20          and student_status <> 'CNX';

21     exception

22       when no_data_found then--select into      ,             ,       

23         flag := false;

24     end;

25     /*if v_test3 is null*/--            

26     if flag then--         

27        dbms_output.put_line(v_test3.student_key || ' ' || v_test3.student_no);

28     end if;

29    end loop;

30   close c_test;

31 /*exception

32   when no_data_found then continue ;*/ --         continue

33                                        --         ,       ,   continue   ,            

34 end;