【筆記】oracle 陣列實現
--一維陣列:
--巢狀表
--尺寸沒有限制。
--本質上是無序的
--VARRAY
--尺寸必須固定,所有的例項尺寸相同。
--在過程化語言中可以作為有序陣列進行檢索但在Oracle內部看成單個不能分割的單元。
--儲存效率高。
--多維陣列
--利用record 和record of
--建立測試表
drop table t_test_1;
create table t_test_1(
pid number(10),
pname varchar2(20),
birth date,
score number(3),
note varchar2(50)
);
--初始化,插入資料
--利用了nested table 來實現一維陣列
--不需要制定上下限,下表可以不連續,需初始化
--
declare
type type_test_pid is table of t_test_1.pid%type
index by binary_integer;
type type_test_pname is table of t_test_1.pname%type
index by binary_integer;
type type_test_birth is table of t_test_1.birth%type
index by binary_integer;
type type_test_score is table of t_test_1.score%type
index by binary_integer;
type type_test_note is table of t_test_1.note%type
index by binary_integer;
my_test_pid type_test_pid;
my_test_panme type_test_pname;
my_test_birth type_test_birth;
my_test_score type_test_score;
my_test_note type_test_note;
i number(10) := 0;
begin
for i in 1 .. 10000
loop
my_test_pid(i) := i;
my_test_panme(i) := 'names_' || to_char(i);
my_test_birth(i) := sysdate;
my_test_score(i) := to_number(nvl(substr(to_char(i), -2), to_char(i)));
my_test_note(i) := my_test_panme(i)|| ' score is ' ||
nvl(substr(to_char(i), -2), to_char(i));
end loop;
forall i in 1 .. 10000
insert into t_test_1
values(
my_test_pid(i),
my_test_panme(i),
my_test_birth(i),
my_test_score(i),
my_test_note(i)
);
commit;
exception
when others then
rollback;
end;
--使用varray實現陣列
--自定義一個TYPE使用VARRAY來得到一個陣列
--但只能對基本型別定義
--此種方式實現的陣列,需要制定上限,且下標連續,在使用之前須初始化
--適用於陣列長度不太大的情況
declare
type num_pid is varray(1000) of t_test_1.pid%type;
v_num_pid num_pid;
i number(8):=1;
begin
v_num_pid := num_pid();
v_num_pid.extend;
v_num_pid(1) := 1;
v_num_pid.EXTEND(999,1);--一次性新增999個第1個元素的副本
for i in 1 .. 1000
loop
--v_num_pid.extend; --每次擴充套件一個
v_num_pid(i) := i;
execute immediate 'update t_test_1 set score = trunc(score/2)
where pid=:1 '
using v_num_pid(i);
end loop;
end;
--使用record和table實現多維陣列
--Index-by-tables,nested table,varray的區別
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16179598/viewspace-539648/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- GO 筆記-陣列Go筆記陣列
- JS 陣列筆記JS陣列筆記
- 陣列筆記1陣列筆記
- IOS筆記之陣列iOS筆記陣列
- JavaScript筆記(6)陣列JavaScript筆記陣列
- 【筆記】字尾陣列筆記陣列
- C++ ——vector陣列筆記C++陣列筆記
- iOS筆記之陣列排序iOS筆記陣列排序
- JS陣列學習筆記JS陣列筆記
- 陣列分割——解題筆記陣列筆記
- 三,列表和陣列(筆記)陣列筆記
- Java 學習筆記 二維陣列和物件陣列Java筆記陣列物件
- c++筆記_多維陣列C++筆記陣列
- 字尾陣列學習筆記陣列筆記
- 學習筆記——陣列方法整理筆記陣列
- Java學習筆記之陣列Java筆記陣列
- 學習筆記----樹狀陣列筆記陣列
- swift 學習筆記之陣列Swift筆記陣列
- 學習筆記----字尾陣列筆記陣列
- [筆記] 解碼Nginx:陣列(Array)筆記Nginx陣列
- 字尾陣列 學習筆記陣列筆記
- 陣列排序的實現陣列排序
- golang實現稀疏陣列Golang陣列
- JavaScript 學習筆記 - 多維陣列變為一維陣列JavaScript筆記陣列
- JS常用陣列方法總結筆記JS陣列筆記
- 【C#學習筆記】陣列使用C#筆記陣列
- JS實現陣列去重JS陣列
- PHP 陣列底層實現PHP陣列
- 如何實現陣列去重?陣列
- JavaScript 中實現等分陣列JavaScript陣列
- 約瑟夫環(陣列實現)陣列
- javascript 偽陣列實現方法JavaScript陣列
- DELPHI也可以實現控制元件陣列,用定義陣列變數實現控制元件陣列 (轉)控制元件陣列變數
- Java學習筆記——陣列練習(七)Java筆記陣列
- Perl學習筆記(五)——關聯陣列筆記陣列
- Object C學習筆記11-陣列Object筆記陣列
- 靜態佇列,迴圈陣列實現佇列陣列
- PHP 實現按奇偶排序陣列PHP排序陣列