oracle的顯式遊標

來源:互聯網
上載者:User

標籤:oracle的顯式遊標

declare  cursor user_cur  is select *   from my_user;  user_row my_user%rowtype;  begin      open user_cur;      loop      fetch user_cur into user_row;      exit when user_cur%notfound;      dbms_output.put_line(user_row.user_id||'----'||user_row.name);      end loop;      close user_cur;  end;
declare  cursor user_cur  is select *   from my_user;  row_user my_user%rowtype;  begin      open user_cur;      loop        fetch user_cur into row_user;        exit when user_cur%notfound;        dbms_output.put_line(row_user.user_id||'----'||row_user.name||'----'||row_user.age);      end loop;      close user_cur;end;
declare  cursor row_user  is select *   from my_user;  type my_user_tab is table of my_user%rowtype;   /*   定義和表my_user行對象一致的集合類型cur_row_user,  用於存放批量得到的資料  */  cur_row_user my_user_tab;       begin      open row_user;      loop        /*從結果集中提取資料,每次提取兩行*/        fetch row_user bulk collect into cur_row_user limit 2;        /*遍曆集合cur_row_user中的資料*/        for i in 1..cur_row_user.count loop            dbms_output.put_line(cur_row_user(i).user_id||'----'||cur_row_user(i).name||'----'||cur_row_user(i).age);        end loop;        exit when row_user%notfound;             end loop;      close row_user;  end;
declare  cursor user_cur  is select *   from my_user;  type my_user_tab is table of my_user%rowtype;   /*   定義和表my_user行對象一致的集合類型cur_user_cur,  用於存放批量得到的資料  */  cur_user_cur my_user_tab;       begin      open user_cur;      loop        /*從結果集中提取資料,每次提取兩行*/        fetch user_cur bulk collect into cur_user_cur limit 2;        /*遍曆集合cur_user_cur中的資料*/        for i in 1..cur_user_cur.count loop            dbms_output.put_line(cur_user_cur(i).user_id||'----'||cur_user_cur(i).name||'----'||cur_user_cur(i).age);        end loop;        exit when user_cur%notfound;             end loop;      close user_cur;  end;
declare  cursor user_cur  is select *   from my_user;  begin             for cdr in user_cur          loop            dbms_output.put_line(cdr.user_id||'----'||cdr.name||'----'||cdr.age);          end loop;  end;  /*cursor for loop 不需要特別的申明變數,它可以提取出行物件類型資料*/
declare  cursor user_cur  is select *   from my_user;  cdr my_user%rowtype;  begin       if user_cur%isopen then         fetch user_cur into cdr;         dbms_output.put_line(cdr.user_id||'----'||cdr.name||'----'||cdr.age);       else dbms_output.put_line('遊標沒有開啟');       end if;  end;
declare  cursor user_cur  is select *   from my_user;  cdr my_user%rowtype;  begin    open user_cur;       if user_cur%isopen then         loop           fetch user_cur into cdr;           exit when user_cur%notfound;           dbms_output.put_line(cdr.user_id||'----'||cdr.name||'----'||cdr.age);          end loop;       else dbms_output.put_line('遊標沒有開啟');       end if;  end;
declare  cursor user_cur  is select *   from my_user;  cdr my_user%rowtype;  begin    open user_cur;     loop       fetch user_cur into cdr;             if user_cur%found then                dbms_output.put_line(cdr.user_id||'----'||cdr.name||'----'||cdr.age);             else                dbms_output.put_line('遊標沒有開啟');               exit;             end if;     end loop;    end;
declare  /*這裡的取值寫在declare和begin中都可以*/  v_user_id my_user.user_id%type:='&v_user_id';  /*這裡的v_user_id的類型寫number和my_user.user_id%type都可以*/  cursor c_my_user(v_user_id my_user.user_id%type)  is select *   from my_user  where user_id=v_user_id;  cdr my_user%rowtype;    begin       open c_my_user(v_user_id);     loop       fetch c_my_user into cdr;             if c_my_user%found then                dbms_output.put_line(cdr.user_id||'----'||cdr.name||'----'||cdr.age);             else                dbms_output.put_line('遊標沒有開啟');               exit;             end if;     end loop;    end;


oracle的顯式遊標

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.