oracle下巧用bulk collect實現cursor批量fetch的sql語句
更新時間:2008年03月05日 14:44:50 作者:
oracle下巧用bulk collect實現cursor批量fetch的sql語句,使用oracel的朋友可以試試了
在一般的情況下,使用批量fetch的幾率并不是很多,但是Oracle提供了這個功能我們最好能熟悉一下,說不定什么時候會用上它。
declare
cursor c1 is select * from t_depart;
v_depart t_depart%rowtype ;
type v_code_type is table of t_depart.depart_code%type ;
v_code v_code_type ;
type v_name_type is table of t_depart.depart_name%type ;
v_name v_name_type ;
begin
open c1;
fetch c1 bulk collect into v_code , v_name ;
for i in 1..v_code.count loop
dbms_output.put_line(v_code(i)||' '||v_name(i));
end loop;
close c1;
end;
通過上面的這個列子大家可以發(fā)現如果列很多的話,為每一列定義一個集合似乎有些繁瑣,可以把集合和%rowtype結合起來一起使用簡化程序!
declare
cursor c1 is select * from t_depart;
type v_depart_type is table of t_depart%rowtype ;
v_depart v_depart_type ;
begin
open c1;
fetch c1 bulk collect into v_depart ;
for i in 1..v_depart.count loop
dbms_output.put_line(v_depart(i).depart_code||' '||
v_depart(i).depart_name);
end loop;
close c1;
end;
在輸出結果時既可以使用集合的count屬性和可以使用first和last,在引用%rowtype類型的內容時還有一個需要注意的地方是v_depart(i).depart_code,而不是v_depart.depart_code(i),當然沒有這樣的寫法,即使有意義也并不一樣。
declare
cursor c1 is select * from t_depart;
type v_depart_type is table of t_depart%rowtype ;
v_depart v_depart_type ;
begin
open c1;
fetch c1 bulk collect into v_depart ;
for i in v_depart.first..v_depart.last loop
dbms_output.put_line(v_depart(i).depart_code||' '||
v_depart(i).depart_name);
end loop;
close c1;
end;
復制代碼 代碼如下:
declare
cursor c1 is select * from t_depart;
v_depart t_depart%rowtype ;
type v_code_type is table of t_depart.depart_code%type ;
v_code v_code_type ;
type v_name_type is table of t_depart.depart_name%type ;
v_name v_name_type ;
begin
open c1;
fetch c1 bulk collect into v_code , v_name ;
for i in 1..v_code.count loop
dbms_output.put_line(v_code(i)||' '||v_name(i));
end loop;
close c1;
end;
通過上面的這個列子大家可以發(fā)現如果列很多的話,為每一列定義一個集合似乎有些繁瑣,可以把集合和%rowtype結合起來一起使用簡化程序!
復制代碼 代碼如下:
declare
cursor c1 is select * from t_depart;
type v_depart_type is table of t_depart%rowtype ;
v_depart v_depart_type ;
begin
open c1;
fetch c1 bulk collect into v_depart ;
for i in 1..v_depart.count loop
dbms_output.put_line(v_depart(i).depart_code||' '||
v_depart(i).depart_name);
end loop;
close c1;
end;
在輸出結果時既可以使用集合的count屬性和可以使用first和last,在引用%rowtype類型的內容時還有一個需要注意的地方是v_depart(i).depart_code,而不是v_depart.depart_code(i),當然沒有這樣的寫法,即使有意義也并不一樣。
復制代碼 代碼如下:
declare
cursor c1 is select * from t_depart;
type v_depart_type is table of t_depart%rowtype ;
v_depart v_depart_type ;
begin
open c1;
fetch c1 bulk collect into v_depart ;
for i in v_depart.first..v_depart.last loop
dbms_output.put_line(v_depart(i).depart_code||' '||
v_depart(i).depart_name);
end loop;
close c1;
end;
相關文章
Oracle 實現類似SQL Server中自增字段的一個辦法
由于Oracle中沒有類似SQL Server中的自增字段,所以我們如果想要通過設定類似ID性質的唯一列的話,需要借助Oracle的sequence,先建立一個序列,然后在每次插入數據的時候,通過前觸發(fā)器來更新ID值,并將序列的序號加1,這樣的迂回方式來實現。2009-07-07