oracle 三種複合類型變數分析

來源:互聯網
上載者:User

標籤:oracle   number   record   字串   記錄   記錄類型   

實際項目中,經常遇到的三種複合類型變數。結合部分項目執行個體做個整理,具體如下:

  1. 記錄類型:記錄類型可以包含一個或多個成員,而每個成員的類型可以不同,成員可以是標量類型。也可以引用其他的變數類型。這種類型的特點是比較適合處理查詢語句中有多個列的情況,最常用的情況就如在調用某一張表中的一行記錄。

  2. 索引表類型(關聯陣列):索引表類型和數組相似,他利用索引值尋找對應的值,這裡的索引值同真正數組的下標不同,索引表中下標允許使用字串。數組的長度不是固定值,可以根據需要自動成長。其中的索引值是整數或是字串,其中的值就是普通的標量類型,也可以是記錄類型。(下標從1開始)

  3. VARRAY 變長數組:變長數組的元素個數需要限制,他是一個儲存有序元素的集合,數組的下標從1開始,適合較少資料時使用。


eg1:記錄類型-declare type pro_x is record (v_id product_info.id%type, v_name varchar2(10), v_price number(8,2));  v_pro pro_x;//聲明變數 v_pro,v_pro的資料類型是pro_x類型。  begin   select id,name,price into v_pro from product_info where id=‘11011211410086‘;   dbms_output.put_line(‘id:‘||v_pro.v_id||‘,‘||‘name:‘||v_pro.v_name||‘,‘||‘price:‘||v_pro.v_price);   end;      記錄類型聲明方式:type type_name is record(field_name datatype[not null]{:=|default}expression) 說明:not null 可以約束記錄成員非空。   可以使用%rowtype進行代替,實現上述功能。即: declarev_pro product_info%rowtype; begin   select id,name,price into v_pro from product_info where id=‘11011211410086‘;   dbms_output.put_line(‘id:‘||v_pro.id||‘,‘||‘name:‘||v_pro.name||‘,‘||‘price:‘||v_pro.price);  end;  eg2:索引表類型(關聯陣列)---身份證校正  declare  type TiArray is table of integer;  type TcArray is table of varchar2(1);  type id_emp is table of qlr_info%type  index by binary_integer;  rst id_emp;  W TiArray;  A TcArray;  S integer;  tab varchar2(200);  zuihyw varchar2(1);  jieguo number;  shenfzh varchar2(20);  cursor c is   select zjbh from qlr_info where length(zjbh)=18 and zjzl=‘居民身份證‘ and regexp_like(substr(zjbh,1,17),‘^[0-9]*$‘);//regexp_like(substr(zjbh,1,17),‘^[0-9]*$‘) 表示尋找截取的前17為字串為數字。^:匹配開始位置;$:匹配結束位置;*:匹配零次或多次。 begin W:=TiArray(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2); A:=TcArray(‘1‘,‘0‘,‘X‘,‘9‘,‘8‘,‘7‘,‘6‘,‘5‘,‘4‘,‘3‘,‘2‘);for emc in c loopS:=0;shenfzh:=emp.zjbh;for i in 1..17 loopS:=S+to_number(substr(shenfzh,i,1)*W(i));end loop;jieguo:=S mod 11;zuihyw:=A(jieguo+1);zuihyw2:=substr(shenfzh,18,1);if (zuihyw<>zuihyw2) thendbms_output_line(‘證件編號:‘emp.zjbh||‘錯誤!‘)end if;end loop;end;eg2.1使用字串為索引值的索引表declaretype pro is table of number(8) index by varchar2(20);v_pro pro;beginv_pro(‘test‘):=253;v_pro(‘test1‘):=256;dbms_out.put_line(v_pro.first ||‘,‘||v_pro(v_pro.first));end;索引表型別宣告:type type_name is table of {column_type|variable_name%type|table_name%rowtype}[not null] index by {pls_integer|binary_integer|varchar2(v_size)}eg3,變長數組declaretype varr is  varray(10) of varchar2(10);//定義數組長度10v_pro varr:=varr(‘1‘,‘2‘);//初始化了兩個元素(最多可以初始化10個)beginv_pro(1):=‘haha‘;v_pro(2):=‘ouou‘;dbms_output.put_line(v_pro(1)||‘,‘||v_pro(2));end;變長數組聲明:type type_name is {varray|varying array}(size_limit)of element_type[not null]                                                                    2016.05.11


本文出自 “MST” 部落格,轉載請與作者聯絡!

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.