執行結果如圖9.6所示。 ――――――――――――――――――――――――――――――――――――― declare pi constant number(9):=3.1415926; begin commit; end; ――――――――――――――――――――――――――――――――――――― 【配套程序位置】:第9章/constantdefine.sql。
基本數據類型變量
1. 基本數據類型
PL/SQL中常用的基本數據類型如表9.2所示。
表9.2 常見的數據基本類型
類型標識符 說明
Number 數字型 Int 整數型 Pls_integer 整數型,產生溢出時出現錯誤 Binary_integer 整數型,表示帶符號的整數 Char 定長字符型,最大255個字符 Varchar2 變長字符型,最大2000個字符 Long 變長字符型,最長2GB Date 日期型 Boolean 布爾型(TRUE、FALSE、NULL三者取一)
――――――――――――――――――――――――――――――――――――― declare age number(3):=26; begin commit; end; ――――――――――――――――――――――――――――――――――――― 【配套程序位置】:第9章/basicdatatypedefine.sql。
――――――――――――――――――――――――――――――――――――― set serveroutput on declare type myrecord is record( myrecordnumber int, mycurrentdate date); srecord myrecord; begin select * into srecord from tempuser.testtable where recordnumber=68; dbms_output.put_line(srecord.mycurrentdate); end; ――――――――――――――――――――――――――――――――――――― 【配套程序位置】:第9章/ recordtypedefine.sql。
在【SQLPlus Worksheet】中執行下列PL/SQL程序,該程序定義了名為mytable的復合類型變量,與testtable數據表結構相同,執行結果如圖9.10所示。 ――――――――――――――――――――――――――――――――――――― Declare mytable testtable%rowtype; begin select * into mytable from tempuser.testtable where recordnumber=88; dbms_output.put_line(mytable.currentdate); end; ―――――――――――――――――――――――――――――――――――――
【配套程序位置】:第9章/ rowtypedefine.sql。
4. 定義一維表類型變量
表類型變量和數據表是有區別的,定義表類型變量的語法如下: ――――――――――――――――――――――――――――――――――――― type 表類型 is table of 類型 index by binary_integer;
表變量名 表類型; ――――――――――――――――――――――――――――――――――――― 類型可以是前面的類型定義,index by binary_integer子句代表以符號整數為索引,這樣訪問表類型變量中的數據方法就是“表變量名(索引符號整數)”。
執行結果如圖9.11所示。 ――――――――――――――――――――――――――――――――――――― Declare type tabletype1 is table of varchar2(4) index by binary_integer; type tabletype2 is table of tempuser.testtable.recordnumber%type index by binary_integer; table1 tabletype1; table2 tabletype2; begin table1(1):='大學'; table1(2):='大專'; table2(1):=88; table2(2):=55; dbms_output.put_line(table1(1)table2(1)); dbms_output.put_line(table1(2)table2(2)); end; ――――――――――――――――――――――――――――――――――――― 【配套程序位置】:第9章/ tabletypedefine1.sql。
“”是連接字符串的運算符。
5. 定義多維表類型變量
在【SQLPlus Worksheet】中執行下列PL/SQL程序,該程序定義了名為tabletype1的多維表類型,相當于多維數組,table1是多維表類型變量,將數據表tempuser.testtable中recordnumber為60的記錄提取出來存放在table1中并顯示。執行結果如圖9.12所示。 ――――――――――――――――――――――――――――――――――――― 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).recordnumbertable1(60).currentdate); end; ――――――――――――――――――――――――――――――――――――― 【配套程序位置】:第9章/ tabletypedefine2.sql。