Oracle PL/SQL複合資料型別
複合資料型別大致可以分為兩類。一類是記錄類型,適用於處理單行多列資料,有點類似於java中的VO;一類是集合類型,適用於處理單列多行的資料,類似java中的List,以下實驗在Oracle 11.2.0.1.0版本下做的。
1.記錄類型
drop table test purge;
create table test
(
id number(2),
name varchar2(60)
);
insert into test values(1,'aaa');
insert into test values(2,'bbb');
insert into test values(3,'ccc');
insert into test values(4,'ddd');
insert into test values(5,'eee');
commit;
--顯式定義記錄類型
declare
type t_record is record
(
id test.id%type,
name test.name%type
);
var_record t_record;
coun number := 0;
begin
for c_row in (select id,name from test) loop
coun := coun + 1;
dbms_output.put_line('第'||coun||'迴圈');
var_record.id := c_row.id;
var_record.name := c_row.name;
dbms_output.put_line('記錄:'||var_record.id||'---'||var_record.name);
dbms_output.put_line('遊標:'||c_row.id||'---'||c_row.name);
end loop;
exception when others then
dbms_output.put_line(sqlcode||sqlerrm);
end;
/
輸出結果:
第1迴圈
記錄:1---aaa
遊標:1---aaa
第2迴圈
記錄:2---bbb
遊標:2---bbb
第3迴圈
記錄:3---ccc
遊標:3---ccc
第4迴圈
記錄:4---ddd
遊標:4---ddd
第5迴圈
記錄:5---eee
遊標:5---eee
--隱式定義記錄類型
declare
t_record1 test%rowtype;
cursor c_row(v_id in varchar2) is select id,name from test where id <= v_id;
t_record2 c_row%rowtype;
begin
for row_test in c_row(3) loop
t_record1.id := row_test.id;
t_record1.name := row_test.name;
t_record2.id := row_test.id;
t_record2.name := row_test.name;
dbms_output.put_line('表的rowtype:'||t_record1.id||'---'||t_record1.name);
dbms_output.put_line('遊標的rowtype:'||t_record2.id||'---'||t_record2.name);
dbms_output.put_line('遊標:'||row_test.id||'---'||row_test.name);
end loop;
exception when others then
dbms_output.put_line(sqlcode||sqlerrm);
end;
/
輸出結果:
表的rowtype:1---aaa
遊標的rowtype:1---aaa
遊標:1---aaa
表的rowtype:2---bbb
遊標的rowtype:2---bbb
遊標:2---bbb
表的rowtype:3---ccc
遊標的rowtype:3---ccc
遊標:3---ccc
如果在顯式和隱式定義記錄中選擇,我傾向於選擇顯式的定義,因為讓邏輯更加清晰。
2.集合類型
--索引表
declare
cursor cur_test is select id,name from test;
type t_test1 is table of test%rowtype index by binary_integer;
var_test1 t_test1;
begin
SELECT id,name INTO var_test1(0) FROM test WHERE id=1;
dbms_output.put_line('var_test1(0):'||var_test1(0).id||'---'||var_test1(0).name);
SELECT id,name INTO var_test1(10) FROM test WHERE id=2;
dbms_output.put_line('var_test1(10):'||var_test1(10).id||'---'||var_test1(10).name);
end;
var_test1(0):1---aaa
var_test1(10):2---bbb
--巢狀表格
DECLARE
TYPE t_test1 IS TABLE OF test.id%TYPE;
var_test1 t_test1;
begin
var_test1 := t_test1(1,2,3);
dbms_output.put_line('var_test1: '||var_test1(1)||','||var_test1(2)||','||var_test1(3));
end;
var_test1: 1,2,3
--varray表
DECLARE
TYPE t_test1 IS VARRAY (10) OF test.id%TYPE;
var_test1 t_test1;
begin
var_test1 := t_test1(1,2,3);
dbms_output.put_line('var_test1: '||var_test1(1)||','||var_test1(2)||','||var_test1(3));
end;
var_test1: 1,2,3
索引表和巢狀表格集合中的元素的數量沒有限制,varray集合中是沒有限制的。
索引表不能儲存在資料庫中,巢狀表格和varray可以被儲存在資料庫中。
Oracle--plsql複合資料型別
--------------------------------------分割線 --------------------------------------
rlwrap - 解決Linux下SQLPLUS退格、上翻鍵亂碼問題
SQLPLUS spool 到動態記錄檔名
Oracle SQLPLUS提示符設定
通過設定SQLPLUS ARRAYSIZE(行預取)加快SQL返回速度
PL/SQL Developer實用技巧分享
--------------------------------------分割線 --------------------------------------