Oracle 9i 游标

2016-02-19 17:32 2 1 收藏

下面是个简单易学的Oracle 9i 游标教程,图老师小编详细图解介绍包你轻松学会,喜欢的朋友赶紧get起来吧!

【 tulaoshi.com - 编程语言 】

  游标是从数据表中提取出来的数据,以临时表的形式存放在内存中,在游标中有一个数据指针,在初始状态下指向的是首记录,利用fetch语句可以移动该指针,从而对游标中的数据进行各种操作,然后将操作结果写回数据表中。

  定义游标

  游标作为一种数据类型,首先必须进行定义,其语法如下。

  cursor 游标名 is select 语句;

  cursor是定义游标的关键词,select是建立游标的数据表查询命令。

  以scott用户连接数据库,在中执行下列PL/SQL程序,该程序定义tempsal为与scott.emps数据表中的sal字段类型相同的变量,mycursor为从scott.emp数据表中提取的sal大于tempsal的数据构成的游标。

  执行结果如图9.35所示。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  

  ―――――――――――――――――――――――――――――――――――――  set serveroutput on  declare    tempsal scott.emp.sal%type;    cursor mycursor is      select * from scott.emp      where saltempsal;  begin    tempsal:=800;    open mycursor;  end;  ―――――――――――――――――――――――――――――――――――――

  :第9章 cursordefine.sql。

  打开游标

  要使用创建好的游标,接下来要打开游标,语法结构如下:

  open 游标名;

  打开游标的过程有以下两个步骤:

  (1)将符合条件的记录送入内存。

  (2)将指针指向第一条记录。

  提取游标数据

  要提取游标中的数据,使用fetch命令,语法形式如下。

  fetch 游标名 into 变量名1, 变量名2,;

  或

  fetch 游标名 into 记录型变量名;

  在中执行下列PL/SQL程序,该程序定义cursorrecord变量是游标mycursor的记录行变量,在游标mycursor的结果中找到sal字段大于800的第一个记录,显示deptno字段的内容。

  执行结果如图9.36所示。

  

  ―――――――――――――――――――――――――――――――――――――  set serveroutput on  declare    tempsal scott.emp.sal%type;    cursor mycursor is      select * from scott.emp      where saltempsal;    cursorrecord mycursor%rowtype;  begin    tempsal:=800;    open mycursor;    fetch mycursor into cursorrecord;    dbms_output.put_line(to_char(cursorrecord.deptno));  end;  ―――――――――――――――――――――――――――――――――――――

  :第9章 cursorfetch.sql。

  关闭游标

  使用完游标后,要关闭游标,使用close命令,语法形式如下:

  close 游标名;

  游标的属性

  游标提供的一些属性可以帮助编写PL/SQL程序,游标属性的使用方法为:游标名[属性],例如mycursor%isopen,主要的游标属性如下。

  1. %isopen属性

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  该属性功能是测试游标是否打开,如果没有打开游标就使用fetch语句将提示错误。

  在中执行下列PL/SQL程序,该程序利用%isopen属性判断游标是否打开。执行结果如图9.37所示。

  

  ―――――――――――――――――――――――――――――――――――――  set serveroutput on  declare    tempsal scott.emp.sal%type;    cursor mycursor is      select * from scott.emp      where saltempsal;    cursorrecord mycursor%rowtype;   begin    tempsal:=800;    if mycursor%isopen then      fetch mycursor into cursorrecord;      dbms_output.put_line(to_char(cursorrecord.deptno));    else      dbms_output.put_line('游标没有打开!');    end if;  end;  ―――――――――――――――――――――――――――――――――――――

  :第9章 isopenattribute.sql。

  2. %found属性

  该属性功能是测试前一个fetch语句是否有值,有值将返回true,否则为false。

  在中执行下列PL/SQL程序。该程序利用%found属性判断游标是否有数据。

  执行结果如图9.38所示。

  

  ―――――――――――――――――――――――――――――――――――――  set serveroutput on  declare    tempsal scott.emp.sal%type;    cursor mycursor is      select * from scott.emp      where saltempsal;    cursorrecord mycursor%rowtype;  begin    tempsal:=800;    open mycursor;      fetch mycursor into cursorrecord;      if mycursor%found then        dbms_output.put_line(to_char(cursorrecord.deptno));      else        dbms_output.put_line('没有数据!');      end if;  end;  ―――――――――――――――――――――――――――――――――――――

  :第9章 foundattribute.sql。

  3. %notfound属性

  该属性是%found属性的反逻辑,常被用于退出循环。

  在中执行下列PL/SQL程序。该程序利用%notfound属性判断游标是否没有数据。

  执行结果如图9.39所示。

  :第9章 notfoundattribute.sql。

  4. %rowcount属性

  该属性用于返回游标的数据行数。

  在SQLPlus Worksheet的执行下列PL/SQL程序,该程序利用%rowcount属性判断游标数据行数。

  执行结果如图9.40所示。

  :第9章 rowcountattribute.sql。

  若返回值为0,表明游标已经打开,但没有提取出数据。

来源:https://www.tulaoshi.com/n/20160219/1615447.html

延伸阅读
Oracle 9i默认的用户 表8.1 Oracle 9i默认的主要用户 用户名口令登录身份及说明syschange_on_installSYSDBA或SYSOPER,但不能以NORMAL登录,可作为默认的系统管理员systemManagerSYSDBA或NORMAL,但不能以SYSOPER登录,可作为默认的系统管理员scottTigerNORMAL,普通用户aqadmaqadmSYSDBA或NORMAL,高级队列管理员。DbsnmpdbsnmpSYSDB...
3.3.1 用户 1. 用户密码 用户默认的密码为manager。 2. 用户权限 用户具有SYSDBA权限,即数据库管理员权限,包括。 打开数据库服务器 关闭数据库服务器 备份数据库 恢复数据库 日志归档 会话限制 管理功能 创建数据库 3.3.2 用户 1. 用户密码 用户默认的密码为change_on_install。...
约束条件就是Oracle数据库系统提供的对数据的完整性进行制约的机制。Oracle 9i允许创建5种约束条件。参见表7.8。 创建检查约束条件 (1)在中按照7.6节修改数据表结构的步骤进行操作。 (2)切换到图7.61所示的编辑表的选项卡。 (3)上述创建检查约束条件的SQL代码如下。 ――――――――――――――――...
Oracle 背景资料 在介绍 Oracle9i 之前我们先介绍一些关于 Oracle 公司的资料,让各位朋友更多了解 Oracle。 1977 年,拉里艾里森和Bob Miner、Ed Oates一起创建一家软件开发实验室(Software Development Laboratories)。开发当时新型的数据库技术--关系型数据库系统,并将第一个产品命名为Oracle,意为智慧之源。1978年软件...
截断操作的SQL语法如下。 ――――――――――――――――――――――――――――――――――――― TRUNCATE TABLE 用户名.表名 [DROP|REUSE STORAGE] ――――――――――――――――――――――――――――――――――――― 其中,若使用DROP STORAGE子句,显式指明释放数据表和索引的空间。若使用REUSE...

经验教程

306

收藏

60
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部