
| 類型標識符 | 說明 |
| number | 數字型 |
| int | 整數型 |
| pls_integer | 整數型,產生溢出時出現錯誤 |
| binary_integer | 整數型,表示帶符號的整數 |
| char | 定長字符型,最大255個字符 |
| varchar2 | 變長字符型,最大2000個字符 |
| long | 變長字符型,最長2gb |
| date | 日期型 |
| boolean | 布爾型(true、false、null三者取一) |
在pl/sql中使用的數據類型和oracle數據庫中使用的數據類型,有的含義是完全一致的,有的是有不同的含義的。
2. 基本數據類型變量的定義方法
變量名 類型標識符 [not null]:=值;
3. 實例
在【sqlplus worksheet】中執行下列pl/sql程序,該程序定義了名為age的數字型變量,長度為3,初始值為26。執行結果如圖9.7所示。
―――――――――――――――――――――――――――――――――――――
declare
age number(3):=26;
begin
commit;
end;
―――――――――――――――――――――――――――――――――――――
【配套程序位置】:第9章/basicdatatypedefine.sql。
復合數據類型變量
下面介紹常見的幾種復合數據類型變量的定義。
1. 使用%type定義變量
為了讓pl/sql中變量的類型和數據表中的字段的數據類型一致,oracle 9i提供了%type定義方法。這樣當數據表的字段類型修改后,pl/sql程序中相應變量的類型也自動修改。
在【sqlplus worksheet】中執行下列pl/sql程序,該程序定義了名為mydate的變量,其類型和tempuser.testtable數據表中的currentdate字段類型是一致的。
執行結果如圖9.8所示。
―――――――――――――――――――――――――――――――――――――
declare
mydate tempuser.testtable.currentdate%type;
begin
commit;
end;
―――――――――――――――――――――――――――――――――――――
【配套程序位置】:第9章/typedefine.sql。
2. 定義記錄類型變量
很多結構化程序設計語言都提供了記錄類型的數據類型,在pl/sql中,也支持將多個基本數據類型捆綁在一起的記錄數據類型。
下面的程序代碼定義了名為myrecord的記錄類型,該記錄類型由整數型的myrecordnumber和日期型的mycurrentdate基本類型變量組成,srecord是該類型的變量,引用記錄型變量的方法是“記錄變量名.基本類型變量名”。
程序的執行部分從tempuser.testtable數據表中提取recordnumber字段為68的記錄的內容,存放在srecord復合變量里,然后輸出srecord.mycurrentdate的值,實際上就是數據表中相應記錄的currentdate的值。
在【sqlplus worksheet】中執行下列pl/sql程序,執行結果如圖9.9所示。
―――――――――――――――――――――――――――――――――――――
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。
在pl/sql程序中,select語句總是和into配合使用,into子句后面就是要被賦值的變量。
3. 使用%rowtype定義變量
使用%type可以使變量獲得字段的數據類型,使用%rowtype可以使變量獲得整個記錄的數據類型。比較兩者定義的不同:變量名 數據表.列名%type,變量名 數據表%rowtype。
在【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子句代表以符號整數為索引,這樣訪問表類型變量中的數據方法就是“表變量名(索引符號整數)”。
在【sqlplus worksheet】中執行下列pl/sql程序,該程序定義了名為tabletype1和tabletype2的兩個一維表類型,相當于一維數組。table1和table2分別是兩種表類型變量。
執行結果如圖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).recordnumber||table1(60).currentdate);
end;
―――――――――――――――――――――――――――――――――――――
【配套程序位置】:第9章/ tabletypedefine2.sql。
在定義好的表類型變量里,可以使用count、delete、first、last、next、exists和prior等屬性進行操作,使用方法為“表變量名.屬性”,返回的是數字。
在【sqlplus worksheet】中執行下列pl/sql程序,該程序定義了名為tabletype1的一維表類型,table1是一維表類型變量,變量中插入3個數據,綜合使用了表變量屬性。
執行結果如圖9.13所示。
―――――――――――――――――――――――――――――――――――――
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;
―――――――――――――――――――――――――――――――――――――
【配套程序位置】:第9章/ tabletypedefine3.sql。
表達式
變量、常量經常需要組成各種表達式來進行運算,下面介紹在pl/sql中常見表達式的運算規則。
1. 數值表達式
pl/sql程序中的數值表達式是由數值型常數、變量、函數和算術運算符組成的,可以使用的算術運算符包括+(加法)、-(減法)、*(乘法)、/(除法)和**(乘方)等。
在【sqlplus worksheet】中執行下列pl/sql程序,該程序定義了名為result的整數型變量,計算的是10+3*4-20+5**2的值,理論結果應該是27。執行結果如圖9.14所示。
―――――――――――――――――――――――――――――――――――――
set serveroutput on
declare
result integer;
begin
result:=10+3*4-20+5**2;
dbms_output.put_line('運算結果是:'||to_char(result));
end;
―――――――――――――――――――――――――――――――――――――
【配套程序位置】:第9章/ datacompute.sql。
dbms_output.put_line函數輸出只能是字符串,因此利用to_char函數將數值型結果轉換為字符型。
2. 字符表達式
字符表達式由字符型常數、變量、函數和字符運算符組成,唯一可以使用的字符運算符就是連接運算符“||”。
3. 關系表達式
關系表達式由字符表達式或數值表達式與關系運算符組成,可以使用的關系運算符包括以下9種。
< 小于
> 大于
= 等于(不是賦值運算符:=)
like 類似于
in 在……之中
<= 小于等于
>= 大于等于
!= 不等于
between 在……之間
關系型表達式運算符兩邊的表達式的數據類型必須一致。
4. 邏輯表達式
邏輯表達式由邏輯常數、變量、函數和邏輯運算符組成,常見的邏輯運算符包括以下3種。
not:邏輯非
or:邏輯或
and:邏輯與
運算的優先次序為not、and和or。
函數
pl/sql程序中提供了很多函數供擴展功能,除了標準sql語言的函數可以使用外,最常見的數據類型轉換函數有以下3個。
to_char:將其他類型數據轉換為字符型。
to_date:將其他類型數據轉換為日期型。
to_number:將其他類型數據轉換為數值型。
以上介紹了pl/sql中最基本的語法要素,下面介紹體現pl/sql過程化編程思想的流程控制語句。
新聞熱點
疑難解答