# Type
###### tags: `Oracle` `SQL`
一:Oracle中的類型有很多種,主要可以分為以下幾類:
1、字符串類型。如:char、nchar、varchar2、nvarchar2。
2、數值類型。如:int、number(p,s)、integer、smallint。
3、日期類型。如:date、interval、timestamp。
4、PL/SQL類型。如:pls_integer、binary_integer、binary_double(10g)、binary_float(10g)、boolean。
plsql類型是不能在sql環境中使用的,比如建表時。
5、自定義類型:type / create type。
二:type / create type 區別聯繫
相同:
可用用關鍵字create type 或者直接用type定義自定義類型,
區別是用 create 後面用 as , 若直接用 type 後面用 is
create 是創 object , 而 type 是創 record .
另 type用在語句塊中,而create 是的獨立的.
一般定義object的語法:
create type:
table類型
create type 自定義表類型A as table of 自定義Object類型A
object類型
create type 自定義Object類型A as object(
字段1 類型1,
字段2 類型2
);
type:
table類型
type 自定義表類型B is table of 類型
object類型
type 自定義Object類型B is record(
字段1 類型1,
字段2 類型2
);
自定義類型一般分為兩種,object類型和table類型.object類似於一個recored,可以表示一個表的一行數據,
自定義的table類型需要用的已經定義好的object類型
例子:使用%ROWTYPE向表中插入數據
DECLARE
vEmp empa%RowType;
Begin
Select * InTo vEmp From empa Where empa.EMPNO = '7369';
UpDate empa Set ROW = vEmp Where EMPNO = 1001;
End;
使用記錄刪除數據? 刪除記錄時,只能在delete語句的where子句中使用記錄成員。
注意:
Oracle中index by binary_integer的作用
如語句:type numbers is table of number index by binary_integer;其作用是,
加了”index by binary_integer ”後,numbers類型的下標就是自增長,numbers類型在插入元素時,
不需要初始化,不需要每次extend增加一個空間。
而如果沒有這句話“index by binary_integer”,那就得要顯示對初始化,
且每插入一個元素到numbers類型的table中時,都需要先extend.
示例:沒加“index by binary_integer”時:
declare
type numbers is table of number;
n numbers := numbers();
begin
n.extend;
n(1) := 2;
n.extend;
n(2) := 3;
for i in1 .. n.count loop
dbms_output.put_line(n(i));
end loop;
end;
--輸出:2,3
而如果加了“index by binary_integer”,代碼如下寫就可以達到上面的效果
declare
type numbers is table of number index by binary_integer;
n numbers;
begin
n(1) := 2;
n(2) := 3;
for i in1 .. n.count loop
dbms_output.put_line(n(i));
end loop;
end;
create type 用法 //深入用法可放入fn/sp 需要用到再查詢資料
CREATE [OR REPLACE] TYPE <typename> AS OBJECT
(attribute1 datatype,
:
attributeN datatype
MEMBER PROCEDURE <methodname> (parameter, mode, datatype),
MEMBER FUNCTION <methodname> (parameter, mode, datatype)
RETURN datatype,PRAGMA RESTRICT_REFERENCES
(<methodname>,WNDS/RNDS/WNPS/RNPS)
);
PRAGMA RESTRICT_REFERENCES指定MEMBER方法按以下模式之一 操作:
–WNDS (不能寫入數據庫狀態) 不能修改數據庫
–RNDS (不能讀出數據庫狀態) 不能執行查詢
–WNPS (不能寫入數據包狀態) 不能更改數據包變量的值
–RNPS (不能讀出數據包狀態) 不能引用數據包變量的值
varray
在 Oracle PL/SQL 中要使用 Array,
有兩種方式: table of 與 varray of,
我幾乎都是用 table of, 不過, 兩種方式還是都介紹, 如下 :
table of 陣列
語法: [Create or replace] Type typeName is Table of DataType [index by BINARY_INTEGER];
若是用 create type, 則不能加 index by BINARY_INTEGER.
若沒有加 index by BINARY_INTEGER, 則設定資料前, 須先有兩個動作 :
初始化, 即 變數名稱 typeName := typeName( );, 否則會有錯誤: ORA-06531: Reference to uninitialized collection.
指定陣列大小, 即 變數名稱.extend( arraySize );, 否則會有錯誤: ORA-06533: Subscript beyond count.
varray of 陣列
語法: [Create or replace] Type typeName is varray( arraySize1 ) of DataType;
設定資料前, 須先有兩個動作 :
初始化, 即 變數名稱 typeName := typeName( );, 否則會有錯誤: ORA-06531: Reference to uninitialized collection.
指定陣列大小, 即 變數名稱.extend( arraySize2 );, 且 arraySize2 <= arraySize1, 否則會有錯誤: ORA-06533: Subscript beyond count.
一維 array 相關函數
指定陣列大小: 變數名稱.extend( arraySize )
總筆數: 變數名稱.count
絕對位置第 n 筆的資料: 變數名稱( n )
相對位置第 n 筆的資料: 變數名稱.next( n )
第 1 筆資料: 變數名稱.first
最後 1 筆資料: 變數名稱.last
刪除第 n 筆資料: 變數名稱.delete( n )
刪除全部資料: 變數名稱.delete
第 n 筆的 Record 資料型態的『某欄位的資料』: 變數名稱( n ).ColumnName_of_Record
第 n 筆的 Record 資料型態的『某欄位陣列的第 m 個資料』: 變數名稱( n ).ColumnName_of_Record( m )
二維 array 相關函數
指定某列陣列大小: 變數名稱(i).extend( arraySize )
總筆數: 變數名稱.count
某列陣列 - 筆數: 變數名稱(i).count
某列陣列 - 絕對位置第 n 筆的資料: 變數名稱(i)( n )
某列陣列 - 相對位置第 n 筆的資料: 變數名稱(i).next( n )
某列陣列 - 第 1 筆資料: 變數名稱(i).first
某列陣列 - 最後 1 筆資料: 變數名稱(i).last
某列陣列 - 刪除第 n 筆資料: 名稱(i).delete( n )
刪除全部資料: 變數名稱.delete
某列陣列 - 刪除全部資料: 變數名稱(i).delete
某列陣列 - 第 n 筆的 Record 資料型態的『某欄位的資料』: 變數名稱(i)( n ).ColumnName_of_Record
某列陣列 - 第 n 筆的 Record 資料型態的『某欄位陣列的第 m 個資料』: 變數名稱(i)( n ).ColumnName_of_Record( m )
注意事項
Type 變數若是宣告在 Package 內, 被其他 Procedure / Package 使用時, 不能做為 "動態 SQL" 的參數.
Type 變數若是用 Create Type 建立, 被其他 Procedure / Package 使用時, 可以做為 "動態 SQL" 的參數.
Type 變數若是用 Create Type 建立, 且要給其他 Owner 使用時, 則必須 Grant EXECUTE 權限給 Owner.