PL/SQL基本結構---PLSQL複合類型---表類型變數table

來源:互聯網
上載者:User

標籤:

表類型變數table 
文法如下: 
     type 表類型  is table of  類型  index by binary_integer; 
     表變數名  表類型; 
類型可以是前面的類型定義,index by binary_integer 子句代表以符號整數為索引,這樣訪問表
類型變數中的資料方法就是“表變數名(索引符號整數)”。table類型,相當於java中的Map容器,
就是一個可變長的數組,key(符號整數索引)必須是整數,可以是負數,value(類型)可以是
標量,也可以是record類型。可以不按順序賦值,但必須先賦值後使用。 
1. 定義一維表類型變數 
    ――――――――――――――――――――――――――――――――――――― 
    declare 
         type t_tb is table of varchar2(20) index by binary_integer; 
         v_tb t_tb; 
    begin 
       v_tb(100):=‘hello‘; 
       v_tb(98):=‘world‘; 
       dbms_output.put_line(v_tb(100)); 
       dbms_output.put_line(v_tb(98)); 
    end;     
    類型為record的表類型變數  
    declare 
         type t_rd is record(id number,name varchar2(20)); 
         type t_tb is table of t_rd index by binary_integer; 
         v_tb2 t_tb; 
    begin 
         v_tb2(100).id:=1; 
         v_tb2(100).name:=‘hello‘; 
         --dbms_output.put_line(v_tb2(100).id); 
         --dbms_output.put_line(v_tb2(100).name); 
         dbms_output.put_line(v_tb2(100).id||‘     ‘||v_tb2(100).name); 
    end; 
    ――――――――――――――――――――――――――――――――――――― 

2. 定義多維表類型變數 
該程式定義了名為tabletype1的多維表類型,相當於多維陣列,table1是多維表類型變數,將數
據表tempuser.testtable中recordnumber為60的記錄提取出來 
存放在table1中並顯示。 
    ――――――――――――――――――――――――――――――――――――― 
    declare 
       type tabletype1 is table of testtable%rowtype index by binary_integer; 
       table1 tabletype1; 
    begin 
        select * into table1(60) from tempuser.testtable where recordnumber=60; 
        dbms_output.put_line(table1(60).recordnumber||table1(60).currentdate); 
    end; 
    
   備忘:在定義好的表類型變數裡,可以使用count、delete、first、last、next、exists和prior
等屬性進行操作,使用方法為“表變數名.屬性”,返回的是數字。 
     
    set serveroutput on 
    declare 
         type tabletype1 is table of varchar2(9) index by binary_integer; 
         table1 tabletype1; 
    begin 
         table1(1):=‘成都市‘; 
         table1(2):=‘北京市‘; 
         table1(3):=‘青島市‘; 
         dbms_output.put_line(‘總記錄數:‘||to_char(table1.count)); 
         dbms_output.put_line(‘第一條記錄:‘||table1.first); 
         dbms_output.put_line(‘最後條記錄:‘||table1.last); 
         dbms_output.put_line(‘第二條的前一條記錄:‘||table1.prior(2)); 
         dbms_output.put_line(‘第二條的後一條記錄:‘||table1.next(2)); 
     end; 
     ――――――――――――――――――――――――――――――――――――― 
***************************************** 
      %type和%rowtype 
***************************************** 
使用%type定義變數,為了讓PL/SQL中變數的類型和資料表中的欄位的資料類型一致,Oracle 
9i提供了%type定義方法。這樣當資料表的欄位類型修改後,PL/SQL程式中相應變數的類型也
自動修改。 
     ――――――――――――――――――――――――――――――――――――― 
     create table student( 
        id number, 
        name varchar2(20), 
        age number(3,0) 
     ); 
     insert into student(id,name,age) values(1,‘susu‘,23); 
     --尋找一個欄位的變數 
     declare 
        v_name varchar2(20); 
        v_name2 student.name%type; 
     begin 
        select name into v_name2 from student where rownum=1; 
        dbms_output.put_line(v_name2); 
     end; 
     --尋找多個欄位的變數 
     declare 
         v_id student.id%type; 
         v_name student.name%type; 
         v_age student.age%type; 
     begin 
       select id,name,age into v_id,v_name,v_age from student where rownum=1; 
       dbms_output.put_line(v_id||‘   ‘||v_name||‘   ‘||v_age); 
     end; 
     --尋找一個類型的變數,推薦用* 
     declare 
        v_student student%rowtype; 
     begin 
        select * into v_student from student where rownum=1; 
        dbms_output.put_line(v_student.id||‘   ‘||v_student.name||‘   ‘||v_student.age); 
     end; 
     --也可以按欄位尋找,但是欄位順序必須一樣,不推薦這樣做 
     declare 
        v_student student%rowtype; 
     begin 
      select id,name,age into v_student from student where rownum=1; 
      dbms_output.put_line(v_student.id||‘   ‘||v_student.name||‘   ‘||v_student.age); 
     end; 
     declare 
        v_student student%rowtype; 
     begin 
      select id,name,age into v_student.id,v_student.name,v_student.age from student where 
id=1; 
      --select * into v_student.id,v_student.name,v_student.age from student where id=1; 
      dbms_output.put_line(); 
     end; 
     ――――――――――――――――――――――――――――――――――――― 
    備忘:insert,update,delete,select都可以,create table,drop table不行。DPL,DML,
和流程式控制制語句可以在pl/sql裡用,但DDL語句不行。 
     
     declare 
        v_name student.name%type:=‘wang‘; 
     begin 
        insert into student(id,name,age) values(2,v_name,26); 
     end; 
     
     begin 
        insert into student(id,name,age) values(5,‘hehe‘,25); 
     end; 
     declare 
        v_name student.name%type:=‘hexian‘; 
     begin 
        update student set name=v_name where id=1; 
     end; 
     begin 
        update student set name=‘qinaide‘ where id=2; 
     end; 
     ――――――――――――――――――――――――――――――――――――― 

PL/SQL基本結構---PLSQL複合類型---表類型變數table

聯繫我們

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