Oracle 10 gデータベース・カーソルの使用学習1


--カーソルの使用
1)9 i従来の使い方では、一度に1つのデータをとる
--1、カーソルの表示

declare
--    
cursor temp_cursor is
select t.name,t.english_name from communitytype t;
--where t.community_type_id = 'ebook';
--    
v_name communitytype.name%type;
v_english_name communitytype.english_name%type;
begin
--    
open temp_cursor;
--     
loop
--  fetch into     ,         
fetch temp_cursor into v_name,v_english_name;
--         ,  
exit when temp_cursor%notfound;
dbms_output.put_line(v_name||':'||v_english_name);
end loop;
--    
close temp_cursor;
end;

オンデマンド出版:test
機構コレクション:333
電子図書:ebook
学術ジャーナル:ZJUPLink
学位論文:ZJUDissertation
カリキュラム:ZJUEducation
コレクション芸術品図鑑:
Epubリソース:
特集購読:ZJUSubjectSubscribe
新刊書速読:newbook
セルフ出版:wlcb
図書館新刊書:Library's New Book
新刊図書:BookSeller New Book
携帯電話の本を聞く:tingbook
人文社会科:renwensheke
テストリポジトリ:test
相手へ:
liangCC:
0524teste:
test01:
チームリポジトリ:Research Group
0525test:
test07:
科学教育興国:
アーカイブリソース:archive
マルチメディア教案:dmtja
0524test:0524test
qlltest:
qlltest:
05242:
asd:asd
ddd:ddd
qq:qqq
テストリポジトリ:test 001
forループでカーソル内のレコードにアクセスすると、カーソルを開いたり閉じたりしないで自動的に実行できます.

-- for           ,             ,for            
declare
--    
cursor temp_cursor is
select t.name,t.english_name from communitytype t;
begin
--     
for v_comtype in temp_cursor loop
dbms_output.put_line(v_comtype.name||','||v_comtype.english_name);
end loop;
end;

中学校のマルチメディア教案
チームリポジトリ、
アーカイブリソース、
オンデマンドで出版され、
機構の典蔵、
電子図書、ebook
学術ジャーナル、ZJUPLink
学位論文、ZJUDissertation
カリキュラム、ZJUEducation
テスト
芸術品図鑑を所蔵し、
Epubリソース、
特集購読、ZJUSubjectSubscribe
新刊書早く読んでnewbook
投稿ガイド
図書館の新刊書、Library's New Book
新刊図書、BookSeller New Book
携帯電話の本を聞いてtingbook
人文社会科、renwensheke
テスト、
リポジトリのテスト
相手に、
書友会、
セルフ出版、wlcb
2)集合で一度に全てのデータを取る

--    
--1、    
declare
--    
cursor temp_cursor is
select t.name from communitytype t;
--       
type name_table_type is table of communitytype.name%type;
name_table name_table_type;
begin
--    
open temp_cursor;
--  bulk collect into        
fetch temp_cursor bulk collect into name_table;
for i in 1..name_table.count loop
dbms_output.put_line(name_table(i));
end loop;
--    
close temp_cursor;
end;

オンデマンド出版
メカニズムライブラリ
電子図書
学術ジャーナル.
学位論文
カリキュラム
芸術品図鑑を収蔵する.
Epubリソース
特集購読
新刊書は早く読みなさい
セルフ出版
図書館の新刊書
新刊図書
携帯電話で本を聞く
人文社会科
リポジトリのテスト
相手にあげる
liangCC
0524teste
test01
チームリポジトリ
0525test
test07
科学教育が国を興す.
アーカイブリソース
マルチメディア教案
0524test
qlltest
qlltest
05242
asd
ddd
qq
リポジトリのテスト
3)集合変数を利用して一度に一部のデータを取り出す

--1、    
declare
--    
cursor temp_cursor is
select t.name from communitytype t;
--        
type name_array_type is varray(5) of communitytype.name%type;
name_array name_array_type;
begin
--    
open temp_cursor;
--     
loop
--  fetch into        ,   5 
fetch temp_cursor bulk collect into name_array limit 5;
dbms_output.put_line('     :');
for i in 1..name_array.count loop
dbms_output.put_line(name_array(i));
end loop;
dbms_output.new_line;
--         ,  
exit when temp_cursor%notfound;
end loop;
--    
close temp_cursor;
end;

リポジトリ名:
オンデマンド出版
メカニズムライブラリ
電子図書
学術ジャーナル.
学位論文
リポジトリ名:
カリキュラム
芸術品図鑑を収蔵する.
Epubリソース
特集購読
新刊書は早く読みなさい
リポジトリ名:
セルフ出版
図書館の新刊書
新刊図書
携帯電話で本を聞く
人文社会科
リポジトリ名:
リポジトリのテスト
相手にあげる
liangCC
0524teste
test01
リポジトリ名:
チームリポジトリ
0525test
test07
科学教育が国を興す.
アーカイブリソース
リポジトリ名:
マルチメディア教案
0524test
qlltest
qlltest
05242
リポジトリ名:
asd
ddd
qq
リポジトリのテスト
4)、カーソル属性isopen rowcountの使用

--4、       isopen rowcount
declare
--    
cursor temp_cursor is
select t.name from communitytype t;
--    
type name_table_type is table of communitytype.name%type;
name_table name_table_type;
begin
--    
if not temp_cursor%isopen 
then open temp_cursor;
end if;
--   
--  fetch into        ,   5 
fetch temp_cursor bulk collect into name_table;
dbms_output.put_line('     :'||temp_cursor%rowcount);
--    
close temp_cursor;
end;

クエリの合計ロー数:34
5)、カーソル定義に基づく記録変数

--5、          
declare
cursor emp_cursor is
select ct.community_type_id,ct.name 
from communitytype ct
where community_type_id = 'ebook';
--           
emp_record emp_cursor%rowtype;
begin
open emp_cursor;
loop
fetch emp_cursor into emp_record;
exit when emp_cursor%notfound;
end loop;
dbms_output.put_line(emp_record.name);  
close emp_cursor;
end;

電子図書
6)パラメータ付きカーソルの使用

--        ,            community_type_id        
declare
cursor emp_cursor(id communitytype.community_type_id%type) is
select name from communitytype
where community_type_id = id;
v_name communitytype.name%type;
begin
open emp_cursor('ebook');
loop
fetch emp_cursor into v_name;
exit when emp_cursor%notfound;
dbms_output.put_line(v_name); 
end loop;
close emp_cursor;
end;

電子図書