Oracle PL SQL 中如何使用Array

2021-04-20 00:51:55 字數 2175 閱讀 3992

因為在pl/sql中並沒有陣列,這是我查資料找的範例和自己寫的範例來解釋如何在pl/sql中使用陣列。也許很多人已知道,不過就是讓不知道的朋友們了解一下吧。

---------------------- 單維陣列 ------------------------

declare

type emp_ssn_array is table of number

index by binary_integer;

best_employees emp_ssn_array;

worst_employees emp_ssn_array;

begin

best_employees(1) := '123456';

best_employees(2) := '888888';

worst_employees(1) := '222222';

worst_employees(2) := '666666';

for i in 1..best_employees.count loop

dbms_output.put_line('i='|| i || ', best_employees= ' ||best_employees(i)

|| ', worst_employees= ' ||worst_employees(i));

end loop;

end;

---------------------- 多維陣列 ------------------------

declare

type emp_type is record

( emp_id employee_table.emp_id%type,

emp_name employee_table.emp_name%type,

emp_gender employee_table.emp_gender%type );

type emp_type_array is table of

emp_type index by binary_integer;

emp_rec_array emp_type_array;

emp_rec emp_type;

begin

emp_rec.emp_id := 300000000;

emp_rec.emp_name := 'barbara';

emp_rec.emp_gender := 'female';

emp_rec_array(1) := emp_rec;

emp_rec.emp_id := 300000008;

emp_rec.emp_name := 'rick';

emp_rec.emp_gender := 'male';

emp_rec_array(2) := emp_rec;

for i in 1..emp_rec_array.count loop

dbms_output.put_line('i='||i

||', emp_id ='||emp_rec_array(i).emp_id

||', emp_name ='||emp_rec_array(i).emp_name

||', emp_gender = '||emp_rec_array(i).emp_gender);

end loop;

end;

-------------- result --------------

i=1, emp_id =300000000, emp_name =barbara, emp_gender = female

i=2, emp_id =300000008, emp_name =rick, emp_gender = male

注:在pl/sql中是沒有陣列(array)概念的。但是如果程式設計師想用array的話,就得變通一下,用type 和table of record來代替多維陣列,一樣挺好用的。

emp_type 就好象乙個table 中的一條record 一樣,裡面有id, name,gender等。emp_type_array 象個table, 裡面含有一條條這樣的record (emp_type),就象多維陣列一樣。

Oracle PL SQL 中如何使用Array

因為在pl sql 中並沒有陣列.這是偶查資料找的範例和自己寫的範例來解釋如何在pl sql 中使用陣列.也許很多人已知道,不過就是讓不知道的朋友們了解一下吧。單維陣列 declare type emp ssn array is table of number index by binary int...

Oracle PL SQL 中如何使用Array

因為在pl sql中並沒有陣列,這是我查資料找的範例和自己寫的範例來解釋如何在pl sql中使用陣列。也許很多人已知道,不過就是讓不知道的朋友們了解一下吧。單維陣列 declare type emp ssn array is table of number index by binary integ...

Oracle PL SQL 中如何使用Array

因為在pl sql 中並沒有陣列.這是偶查資料找的範例和自己寫的範例來解釋如何在pl sql 中使用陣列.也許很多人已知道,不過就是讓不知道的朋友們了解一下吧。單維陣列 declare type emp ssn array is table of number index by binary int...