`

Oracle 10g数据库游标的使用学习一

阅读更多
--使用游标
1)9i以前的使用方法,一次取一条数据
--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
    测试资源库:test001


用for循环访问游标中的记录时,可以不显示的打开或关闭游标,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
    测试,test
    收藏艺术品图鉴,
    Epub资源,
    专题订阅,ZJUSubjectSubscribe
    新书快读,newbook
    投稿指南,
    图书馆新书,Library's New Book
    新出版图书,BookSeller New Book
    手机听书,tingbook
    人文社科,renwensheke
    测试,
    测试资源库,test
    给对方,
    书友会,
    自助出版,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;


    电子图书
分享到:
评论

相关推荐

    Oracle10g基础教程---附数据库和sql

    oracle 游标、存储过程、函数、触发器、优化; 所有sql可以直接运行; 此教程针对oracle初学者(要求有一定的sql基础) 每一条sql都为自己整理,有问题的话也可以联系我! 请参考系列文章:...

    Oracle 10g 学习笔记

    │ Oracle 10G 数据库系统学习笔记.pdf │ Oracle DBA学习心得 --第0章 - ColorfulStar3399的专栏 - CSDN博客.mht │ Oracle DBA学习心得 --第1章 - ColorfulStar3399的专栏 - CSDN博客.mht │ Oracle Direct-Path ...

    Oracle11g从入门到精通2

     Oracle数据库系统是数据库领域最优秀的数据库之一,《Oracle11g从入门到精通》以Oracle最新版本Oracle 11g为蓝本,系统地讲述了Oracle数据库的概念、管理和应用开发等内容。  全书结构合理、内容翔实、示例丰富...

    Oracle 从入门到精通视频教程(11G版本)(ppt)

    第1章-Oracle 11g数据库简介 认识Oracle 11g 回忆Oracle的产品版本 学习Oracle 11g的新特性 第2章-Oracle 11g的安装与测试 能够使用Oracle 11g的基本条件 在Windows 2003上安装Oracle 11g 移除Oracle 11g ...

    大型数据库系统复习题.doc

    总共包括十五章的复习题: 第一章 Oracle 11g 介绍 第二章 ORACLE 11g 的体系结构 第三章 ORACLE 11g 的数据库...第十一章 存储过程与函数 第十二章 触发器 第十三章 游标 第十四章 安全管理 第十五章 数据库备份与恢复

    Oracle 11g SQL和PL SQL从入门到精通 pdf格式电子书 下载(一)

     第1章 在windows 平台上安装oracle database 11g  第2章 配置网络服务名  第3章 使用sql database  第4章 使用sql*plus 第二部分 sql  第5章 sql和pl/sql综述  第6章 简单查询  第7章 sql单行函数  第8章 ...

    Oracle11g从入门到精通

     Oracle数据库系统是数据库领域最优秀的数据库之一,《Oracle11g从入门到精通》以Oracle最新版本Oracle 11g为蓝本,系统地讲述了Oracle数据库的概念、管理和应用开发等内容。  全书结构合理、内容翔实、示例丰富...

    ORACLE11G宝典.rar 是光盘里面的内容,书太厚咧没法影印啊

     1.1 下载并展开Oracle11g数据库软件  1.2 安装前的准备工作  1.2.1 检查硬件、软件要求  1.2.2 设置IP地址  1.2.3 设置计算机全名  1.2.4 关闭Windows防火墙和某些杀毒软件  1.3 安装Oracle11g数据库...

    oracle数据库11G初学者指南.Oracle.Database.11g,.A.Beginner's.Guide

    《Oracle Database 11g初学者指南》带领读者循序渐进地学习数据库设置、管理、编程、备份和恢复。还深入介绍了SQL和PL/SQL。为了易于学习,这本独特的Oracle Press指南是这样组织的: 核心概念——Oracle Database ...

    Oracle 11g SQL和PL SQL从入门到精通 pdf格式电子书 下载(二)

     第1章 在windows 平台上安装oracle database 11g  第2章 配置网络服务名  第3章 使用sql database  第4章 使用sql*plus 第二部分 sql  第5章 sql和pl/sql综述  第6章 简单查询  第7章 sql单行函数  第8章 ...

    Oracle Database 11g初学者指南--详细书签版

     Michelle Malcher是一名高级数据库管理员,在数据库开发、设计和管理方面具有十多年经验.她是性能调优、安全、数据建模和超大型数据库环境数据库体系结构方面的专家.她是IOUG Best Practices Tip Booklet的特约作者...

    Oracle.11g.从入门到精通 (2/2)

    第1章 Oracle数据库概述 1.1 Oracle数据库产品结构及组成 1.1.1 企业版 1.1.2 标准版 1.1.3 标准版 1.1.4 个人版 1.2 数据库基本术语 1.2.1 数据库 1.2.2 数据库管理系统 1.2.3 数据库系统 1.2.4 数据库模式 1.2.5 ...

    Oracle.11g.从入门到精通 (1/2)

    第1章 Oracle数据库概述 1.1 Oracle数据库产品结构及组成 1.1.1 企业版 1.1.2 标准版 1.1.3 标准版 1.1.4 个人版 1.2 数据库基本术语 1.2.1 数据库 1.2.2 数据库管理系统 1.2.3 数据库系统 1.2.4 数据库模式 1.2.5 ...

    Oracle 11g SQL和PL SQL从入门到精通.part1

     第1章 在windows 平台上安装oracle database 11g  第2章 配置网络服务名  第3章 使用sql database  第4章 使用sql*plus 第二部分 sql  第5章 sql和pl/sql综述  第6章 简单查询  第7章 sql单行函数  第8章 ...

    Oracle 11g SQL和PL SQL从入门到精通〖送源代码〗

    通过学习本书,读者不仅可以掌握Oracle常用工具Oracle Universal Installer、Net Comfiguration Assistant、SQL Developer、SQL*Plus的作用及使用方法,而且可以掌握SQL语句和PL/SQL的各种基础知识和高级特征(记录...

    oracle数据库经典题目

    14. 每个Oracle 10g数据库在创建后都有4个默认的数据库用户:system、sys、sysman和DBcnmp 15. Oracle提供了两种类型的权限:系统权限和对象权限。系统权限提供了在Oracle数据库系统范围内执行某种任务的操作能力...

    Oracle 10g 开发与管理

    本文是由笔者2012年学习oracle数据库时编写的学习札记,其中的题目 多数为老师留下的思考题目。 我相信本文会对初学者使用oracle有一个初步的使用印象。右图为我所参 考的书籍。 目录 第一讲 Oacle关系数据库 ...

    oracle数据库全讲

    oracle10g增删改查 试图 索引 存储过程及游标的使用

Global site tag (gtag.js) - Google Analytics