1. 程式人生 > >Oracle中的loop迴圈實現方式

Oracle中的loop迴圈實現方式

--loop ...exit; when ...end loop; 大於2000的薪資
  --select * from emp where emp.sal>2000  ;  
                declare 
                    temp_salary emp.sal%type; --%type:通常是指某行某列資料型別 一個欄位
                    temp_emp emp%rowtype; --%rowtype:通配一行所有列的資料型別 如v_row_test%rowtype;匹配emp表中的一行資料
               cursor mycursor is ---遊標(cursor)
                      select * from emp where emp.sal>temp_salary;
                 begin
                   temp_salary:=2000;
                   open mycursor;
                   loop
                     fetch mycursor into temp_emp;
                     exit when mycursor%notfound;--找不到資料時
                     dbms_output.put_line('ename:'||temp_emp.ename||'薪資'||temp_emp.sal);
                    end loop;
                 end;   
      ----- for i in 1...n loop...end loop;
      declare 
             temp_emp  emp%rowtype;
             cursor cur is select * from emp    where emp.sal>2000;
             begin
               if cur%isopen=false then --判斷遊標是否開啟
                 open cur;
                 end if;
                 for i in 0..6 loop
                   fetch cur into temp_emp;
                   exit when cur%notfound;
                   dbms_output.put_line('ename:'||temp_emp.ename||' ,sal:'||temp_emp.sal);
                 end loop;
                 close cur;
             end;