Oracle-day04 中

來源:互聯網
上載者:User

標籤:Oracle

(六)迴圈
1.無條件迴圈
文法結構

loop--迴圈語句end loop;

範例:輸出從1開始的100個數

declarev_num number:=1;beginloopdbms_output.put_line(v_num);v_num:=v_num+1;exit when v_num>100;end loop;end ;

2、條件迴圈
文法結構

while 條件loopend loop;

範例:輸出從1開始的100個數

declarev_num number:=1;beginwhile v_num<=100loopdbms_output.put_line(v_num);v_num:=v_num+1;end loop;end ;

3、for迴圈
基本文法

for 變數 in 起始值..終止值loopend loop;

範例:輸出從1開始的100個數

beginfor v_num in 1..100loopdbms_output.put_line(v_num);end loop;end;

(七)遊標
1.什麼是遊標
遊標是系統為使用者開設的一個資料緩衝區,存放 SQL 陳述式的執行結果。我們
可以把遊標理解為 PL/SQL 中的結果集。

2.文法結構及樣本
在聲明區聲明遊標,文法如下:

cursor 遊標名稱 is SQL 陳述式;
使用遊標文法

open 遊標名稱loopfetch 遊標名稱 into 變數exit when 遊標名稱%notfoundend loop;close 遊標名稱

需求:列印業主類型為 1 的價格表
代碼:

declarev_pricetable T_PRICETABLE%rowtype;--價格行對象cursor cur_pricetable is select * from T_PRICETABLE whereownertypeid=1;--定義遊標beginopen cur_pricetable;--開啟遊標loopfetch cur_pricetable into v_pricetable;--提取遊標到變數exit when cur_pricetable%notfound;--當遊標到最後一行下面退出迴圈dbms_output.put_line( ‘價格:‘||v_pricetable.price ||‘噸位:‘||v_pricetable.minnum||‘-‘||v_pricetable.maxnum );end loop;close cur_pricetable;--關閉遊標end ;

運行結果如下:

3.帶參數的遊標
我們的查詢語句的條件值有可能是在運行時才能決定的,比如性業主類型,
可能是運行時才可以決定,那如何?呢?我們接下來學習帶參數的遊標,修改
上述案例

declarev_pricetable T_PRICETABLE%rowtype;--價格行對象cursor cur_pricetable(v_ownertypeid number) is select *from T_PRICETABLE where ownertypeid=v_ownertypeid;--定義遊標beginopen cur_pricetable(2);--開啟遊標loopfetch cur_pricetable into v_pricetable;--提取遊標到變數exit when cur_pricetable%notfound;--當遊標到最後一行下面退出迴圈dbms_output.put_line(‘價格:‘||v_pricetable.price ||‘噸位:‘||v_pricetable.minnum||‘-‘||v_pricetable.maxnum );end loop;close cur_pricetable;--關閉遊標end ;

4.for迴圈提取遊標值
我們每次提取遊標,需要開啟遊標 關閉遊標 迴圈遊標 提取遊標 控制迴圈的
退出等等,好麻煩!有沒有更簡單的寫法呢?有!用 for 迴圈一切都那麼簡單,
上例的代碼可以改造為下列形式

declarecursor cur_pricetable(v_ownertypeid number) is select *from T_PRICETABLE where ownertypeid=v_ownertypeid;--定義遊標beginfor v_pricetable in cur_pricetable(3)loopdbms_output.put_line(‘價格:‘||v_pricetable.price ||‘噸位:‘||v_pricetable.minnum||‘-‘||v_pricetable.maxnum );end loop;end ;

二、儲存函數
(一)什麼是儲存函數
儲存函數又稱為自訂函數。可以接收一個或多個參數,返回一個結果。
在函數中我們可以使用 P/SQL 進行邏輯的處理。
(二)儲存函數文法結構
建立或修改預存程序的文法如下:

CREATE [ OR REPLACE ] FUNCTION 函數名稱(參數名稱 參數類型, 參數名稱 參數類型, ...)RETURN 結果VARIANT 資料型別IS變數聲明部分;BEGIN邏輯部分;RETURN 結果變數;[EXCEPTION異常處理部分]END;

(三)案例
需求: 建立儲存函數,根據地址 ID 查詢位址名稱。
語句:

create function fn_getaddress(v_id number)return varchar2isv_name varchar2(30);beginselect name into v_name from t_address where id=v_id;return v_name;end;

測試此函數:

select fn_getaddress(3) from dual
輸出內容

需求:查詢業主 ID,業主名稱,業主地址,業主地址使用剛才我們建立的函數
來實現。

select id 編號,name 業主名稱,fn_getaddress(addressid) 地址from t_owners

查詢結果如下:

Oracle-day04 中

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.