咖啡仔

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
View Code
  1 Oracle数组一般可以分为固定数组和可变数组 
2 固定数组
3
4 declare
5 type v_ar is varray(10) of varchar2(30);
6 my_ar v_ar:=v_ar('g','m','d','','');
7 begin
8 for i in 1..my_ar.count
9 loop
10 dbms_output.put_line(my_ar(i));
11 end loop;
12 end;
13 declare
14 type v_ar is varray(10) of varchar2(30);
15 my_ar v_ar:=v_ar('g','m','d','','');
16 begin
17 for i in 1..my_ar.count
18 loop
19 dbms_output.put_line(my_ar(i));
20 end loop;
21 end;
22
23 --可变数组
24 --一维数组
25
26 declare
27 type v_table is table of varchar2(30) index by binary_integer;
28 --类型可以是前面的类型定义,index by binary_integer子句代表以符号整数为索引,
29 --这样访问表类型变量中的数据方法就是“表变量名(索引符号整数)”。
30 my_table v_table;
31 begin
32 for i in 1..20
33 loop
34 my_table(i):=i;
35 dbms_output.put_line(my_table(i));
36 end loop;
37 end;
38 declare
39 type v_table is table of varchar2(30) index by binary_integer;
40 --类型可以是前面的类型定义,index by binary_integer子句代表以符号整数为索引,
41 --这样访问表类型变量中的数据方法就是“表变量名(索引符号整数)”。
42 my_table v_table;
43 begin
44 for i in 1..20
45 loop
46 my_table(i):=i;
47 dbms_output.put_line(my_table(i));
48 end loop;
49 end;
50
51 --多维数组--多条记录
52
53
54 declare
55 type v_table is table of t_user%rowtype index by binary_integer;
56 my_table v_table;
57 begin
58 select * bulk collect into my_table from t_user;
59 for i in 1..my_table.count/10 --my_table.count/10取到的值为四舍五入值
60 loop
61 dbms_output.put_line('suser--'||my_table(i).suser);
62 dbms_output.put_line('name---'||my_table(i).name);
63 dbms_output.put_line('sex----'||my_table(i).sex);
64 end loop;
65 end;
66 declare
67 type v_table is table of t_user%rowtype index by binary_integer;
68 my_table v_table;
69 begin
70 select * bulk collect into my_table from t_user;
71 for i in 1..my_table.count/10 --my_table.count/10取到的值为四舍五入值
72 loop
73 dbms_output.put_line('suser--'||my_table(i).suser);
74 dbms_output.put_line('name---'||my_table(i).name);
75 dbms_output.put_line('sex----'||my_table(i).sex);
76 end loop;
77 end;
78
79 多维数组--单条记录
80
81
82 declare
83 type v_table is table of t_user%rowtype index by binary_integer;
84 my_table v_table;
85 begin
86 select * into my_table(9) from t_user where suser='admin';
87 --my_table(i) i可以为任意整数,但取值时必须保持以i一致;
88 dbms_output.put_line('--suser--'||my_table(9).suser||'--name--'||my_table(9).name);
89 end;
90 declare
91 type v_table is table of t_user%rowtype index by binary_integer;
92 my_table v_table;
93 begin
94 select * into my_table(9) from t_user where suser='admin';
95 --my_table(i) i可以为任意整数,但取值时必须保持以i一致;
96 dbms_output.put_line('--suser--'||my_table(9).suser||'--name--'||my_table(9).name);
97 end;
98
99 --自定义数组
100
101 create or replace type varray_list as varray(30) of varchar2(50);
102 --使用自定义数组
103 create or replace procedure show_list(p_varlist in varray_list)
104 is
105 v_str varchar2(50);
106 begin
107 for i in 1..p_varlist.count
108 loop
109 v_str:=p_varlist(i);
110 dbms_output.put_line('v_str='||v_str);
111 dbms_output.put_line('p_varlist('||i||')='||p_varlist(i));
112 end loop;
113 end;
114
115 declare
116 my_var varray_list:=varray_list('g','m','d','','');
117 begin
118 show_list(my_var);
119 end;


 

posted on 2011-12-15 12:42  咖啡仔  阅读(466)  评论(0编辑  收藏  举报