PL/SQL编程—游标

简介: PL/SQL编程—游标

一、游标的相关概念:


 定义:


  游标它是一个服务器端的存储区,这个区域提供给用户使用,在这个区域里


 存储的是用户通过一个查询语句得到的结果集,用户通过控制这个游标区域当中


 的指针 来提取游标中的数据,然后来进行操作。

b767dee657fd86a46c1f05fcdc25978d.png



 实质:

是用户在远程客户端上对服务器内存区域的操作,由数据库为用户提供这样的


 一个指针,使得用户能够去检索服务器内存区的数据。


  (1)、 指向上下文区域的句柄或指针


  (2)、上下文区域-用于SQL处理的内存区


  (3)、上下文区域的内容


  - 语句处理的行数


  -指向语句的语法分析表示的指针


 二、游标的类型


  a)静态游标


  -  隐式游标


  -  显式游标


  b)Ref游标


 三、游标具有的属性


  %notfound 询问是否没有结果集


  %found    询问是否存在结果集


  %rowcount 返回受影响的行数


  %isopen   询问游标是否已经打开


 四、隐式游标


  隐式游标 ---由Oracle数据库自动创建,名称是(SQL) ,主要用途是可以返回一个操作是否成功或失败.


  1.有Oracle在内部声明,由系统管理


  2.用于处理


  -DML语句   --注意只能用于DML语句哦。


  -返回单行的查询


  3.用于判断一个操作是否成功.


  SQL%notfound  --返回Boolean值  存在结果集返回 False


  SQL%found    --返回Boolean值   存在结果集返回 True


  SQL%rowcount  --用户成功提取数据的行数


  SQL%isopen   --在隐式游标里一般这个属性是自动打开和关闭的.且任何时候查询都返回False


 示例:向表中插入一行数据,询问是否插入成功.


declare
   iCount int:=0;
 begin
    insert into place values(2,'beijing');
    DBMS_output.put_line('游标所影响的行数:'||SQL%rowcount);
    if SQL%NotFount then
      DBMS_output.put_line('NotFount为真);
    else
      DBMS_output.put_line('NofFount为假');
    end if;
  
     if SQL%Fount then
      DBMS_output.put_line('Fount为真);
     else
      DBMS_output.put_line('Fount为假');
     end if;
      if SQL%isopen then
      DBMS_output.put_line('isOpen为真);
     else
      DBMS_output.put_line('isOpen为假');
     end if;
     
    isCount:=SQL%rowcount;
    DBMS_output.put_line('影响了'||isCount||'行');
  end;

fae94e0b4231888e0427de23b864270b.png

e8335d96274cd782c5fe829ec8972d3e.png


73b43d85cd1ee93ea74d76637029ffb2.png

显示游标:


e86d5c758ca9bdb15a8aea5e6563537b.png

SQL> declare
  2  --定义游标类型sp_test_cursor
  3  type sp_test_cursor is ref cursor;
  4  --定义一个游标变量
  5  test_cursor sp_test_cursor;
  6  --定义变量
  7  v_name mytest.name%type;
  8  v_passwd mytest.passwd%type;
  9  begin
 10  --执行  把cursor和一个select结合
 11  open test_cursor for select name,passwd from mytest;
 12  loop
 13  fetch test_cursor into v_name,v_passwd;
 14  --跳出循环的条件即判断当前游标是否为空
 15  exit when test_cursor%notfound;
 16  dbms_output.put_line('name:'||v_name||' passwd:'||v_passwd);
 17  end loop;
 18  --关闭游标
 19  close test_cursor;
 20  end;
 21  /
name:123 passwd:123
name:gaodingle!!!! passwd:123
name:gagaga passwd:123
name:125555 passwd:passwd
PL/SQL procedure successfully completed
SQL> declare
  2  type sp_cursor is ref cursor;
  3  test_cursor sp_cursor;
  4  v_id mytest.id%type;
  5  v_name mytest.name%type;
  6  v_passwd mytest.passwd%type;
  7  v_salary mytest.salary%type;
  8  begin
  9  open test_cursor for select id,name,passwd,salary from mytest;
 10  loop
 11  fetch test_cursor into v_id,v_name,v_passwd,v_salary;
 12  if v_salary=100 then
 13  update mytest set salary=v_salary+50 where id=v_id;
 14  end if;
 15  exit when test_cursor%notfound;
 16  end loop;
 17  close test_cursor;
 18  end;
 19  /
PL/SQL procedure successfully completed
SQL> select * from mytest;
ID    NAME                 PASSWD                                                                                         SALARY
----- -------------------- -------------------- --------------------------------------------------------------------------------
1     123                  123                                                                                               150
2     gaodingle!!!!        123                                                                                               150
3     gagaga               123                                                                                               150
4     125555               passwd                                                                                             50


目录
相关文章
|
25天前
|
SQL Perl
PL/SQL经典练习
PL/SQL经典练习
13 0
|
25天前
|
SQL Perl
PL/SQL编程基本概念
PL/SQL编程基本概念
13 0
|
30天前
|
SQL Perl
PL/SQL Developer 注册机+汉化包+用户指南
PL/SQL Developer 注册机+汉化包+用户指南
16 0
|
29天前
|
SQL Go
SQL编程
SQL编程
9 0
|
3月前
|
SQL 缓存 数据库
PL/SQL的性能优化
PL/SQL的性能优化
37 0
|
3月前
|
SQL Perl
PL/SQL的函数和包
PL/SQL的函数和包
27 1
|
3月前
|
SQL 存储 数据库
PL/SQL触发器的概述和用途
PL/SQL触发器的概述和用途
30 2
|
3月前
|
SQL 存储 Perl
PL/SQL的游标
PL/SQL的游标
26 2
|
3月前
|
存储 SQL Oracle
PL/SQL存储过程的使用
PL/SQL存储过程的使用
65 1
|
3月前
|
SQL Oracle 关系型数据库
Oracle PL/SQL基础知识及应用案例
Oracle PL/SQL基础知识及应用案例
33 0