`

plsql学习:cursor游标使用例子(1)

 
阅读更多
--PL/SQL supports three kinds of records: table-based, cursor-based,and programmer-defined.

/**
    A table-based record is one whose structure is drawn from the list of columns in the table. 
    A cursor-based record is one whose structure matches the elements of a predefined cursor. 
    To create a table-based or cursor-based record, use the %ROWTYPE attribute:
    record_name table_name or cursor_name%ROWTYPE
*/
-----------example A:--------
set serveroutput on
declare 
    vr_student student%rowtype
begin
    select * 
      into vr_student
      from student
      where student_id = 156;
      dbms_output.put_line(vr_student.first_name||'  '||
         vr_student.lase_name||' has an ID of 156');
    exception
      when no_data_found
        then 
          reise_application_error(-2001,'the student '||
              'is not in the database');
end;

----------example B:----------
set serveroutput on 
declare 
    cursor c_zip is
        select * from zipcode;
    vr_zip c_zip%rowtype;

begin
  open c_zip;
  loop
    fetch c_zip into vr_zip;
    exit when c_zip%notfound;
    dbms_output.put_line(vr_zip.zip||'  '||vr_zip.city||'  '||vr_zip.state);
  end loop;
end;

----------example C:---------
set serveroutput on;
declare
    type instructor_info is record
         (first_name instructor.first_name%type,
          last_name instructor.last_name%type,
          sections number);
    rv_instructor instructor_info;
    begin
      select rtrim(i.first_name),rtrim(i.last_name),count(*)
      into rv_instructor
      from instructor i,section s
      where i.instructor_id = s.instructor_id
      and i.instructor_id = 102
      group by i.first_name,i.last_name;
      dbms_output.put_line('instructor, '||rv_instructor.first_name||
         '  '||rv_instructor.last_name||
         ',teaches '||rv_instructor.sections||
         'sections(s)');
exception 
  when no_data_found then
    dbms_output.put_line('there is no such instructor');
end;


#######################################################################################
#
# Explicit Cursor Attributes
#----------------------------------------------------------------------------------
# CURSOR ATTRIBUTE   SYNTAX                             DESCRIPTION
#----------------------------------------------------------------------------------
# A: %NOTFOUND       cursor_name%NOTFOUND   A Boolean attribute that returns TRUE if
#                                                                          the previous FETCH did not return a row
#                                                                          and FALSE if it did.
#----------------------------------------------------------------------------------
# B: %FOUND          cursor_name%FOUND             A Boolean attribute that returns TRUE if
#                                                                          the previous FETCH returned a row and
#                                                                          FALSE if it did not.
#----------------------------------------------------------------------------------
# C:%ROWCOUNT        cursor_name%ROWCOUNT  The number of records fetched from a
#                                                                           cursor at that point in time.
#----------------------------------------------------------------------------------
# D:%ISOPEN          cursor_name%ISOPEN              A Boolean attribute that returns TRUE if
#                                                                           the cursor is open and FALSE if it is not.
#---------------------------------------------------------------------------------- 
#######################################################################################


--example D:-----------
# Cursor attributes can be used with implicit cursors by using the prefix SQL, such as
# SQL%ROWCOUNT.
# If you use SELECT INTO syntax in your PL/SQL block, you will create an implicit cursor.You can
# then use these attributes on the implicit cursor.
set serveroutput on
declare
   v_city zipcode.city%type;
begin
  select city 
  into v_city
  from zipcode
  where zip = 07002;
  if SQL%rowcount = 0;
  then 
    dbms_output.put_line('******');
  else if sql%rowcount = 0
    then 
      dbms_output.put_line('******');
  else 
    dbms_output.put_line('*******');
  end if;
end;

--------example E:-------
declare
   v_sid student.student_id%type;
   cousor c_student is 
      select student_id
      from student
      where student_id < 110;
begin
  open c_student;
  loop
    fetch c_student into v_sid;
    exit when c_student%notfound;
      dbms_output.put_line('student ID:'||v_sid);
  end loop;
  close c_student;
exception
  when others
  then
    if c_student%isopen
    then 
      close c_student;
    end if;
end;


--------------example F:(Nest cusors)-------
set serveroutput on
declare
   v_zip zipcode.zip%Type;
   v_student_flag Char;
   cursor c_zip is
      select zip, city, state
      from zipcode
      where state = 'CT';
   cursor c_student is
      select first_name ,last_name
      from student
      where zip = v_zip;
bebin
   for r_zip in c_zip
   loop
     v_student_flag := 'N';
     v_zip := r_zip.zip;
     dbms_output.put_line(chr(10));
     dbms_output.put_line('students living in'||r_zip.city);
     for r_student in c_student
     loop
       dbms_output.put_line(r_student.first_name||
          '  '||r_student.last_name);
       v_student_flag := 'Y';
     end loop;
     if v_student_flag = 'N'
       then
       dbms_output.put_line('no student for this zipcode');
     end if
   end loop;
end ;

---------example G: using Current Of statement--------
declare
   cursor c_stud_zip is
      select s.student_id ,z.city
      from student s, zipcode z
      where z.city = 'Brookyn'
      and s.zip = z.zip
      for update of phone;
begin
  for c_stud_zip in c_stud_zip
  loop
    dbms_output.put_line(r_stud_zip.student_id);
    update student
       set phone = '718'||Substr(phone,4)
       where current of c_stud_zip;
  end loop;
end;

分享到:
评论

相关推荐

    Oralce PLSQL存储过程之游标实践!

    4. **提取数据**:使用`FETCH`语句从游标中提取数据: ```sql FETCH cursor_name INTO variable_list; ``` 5. **关闭游标**:完成数据提取后,应该关闭游标以释放资源: ```sql CLOSE cursor_name; ``` #### ...

    plsql.zip_PLSQL Developer_cursor

    在PL/SQL(Procedural Language/Structured Query Language)中,...通过阅读《PLSQL中显示Cursor、隐示Cursor、动态Ref_Cursor区别(有分支图解).pdf》这份资料,你可以更深入地了解这些概念,并结合实际例子进行学习。

    PLSQL游标编程培训内含实例

    建议按照PPT的讲解逐步学习,动手实践每个例子,以便更好地掌握PLSQL游标编程技巧。同时,解决实际问题时应考虑性能因素,因为频繁使用游标可能会对数据库性能产生影响,所以要谨慎选择何时使用游标。

    Oracle数据库的游标学习总结

    本文将详细介绍Oracle数据库中的游标概念、分类及其使用方法,并通过具体的例子进行说明。 #### 二、游标的分类 Oracle游标主要分为两大类:显示游标和隐式游标。其中,显示游标又可以根据定义方式的不同细分为...

    Oracle游标学习二

    【Oracle游标学习二】 在Oracle数据库中,游标(Cursor)是一种重要的编程工具,它允许程序员逐行处理查询结果集。游标对于那些需要多次交互处理数据的应用程序至关重要,尤其是在进行复杂的业务逻辑操作时。在本文...

    oracle游标使用大全3

    下面通过一个具体的例子来展示如何使用显式游标: ```plsql DECLARE CURSOR c_dept IS SELECT deptno, dname FROM dept ORDER BY deptno; CURSOR c_emp(p_dept VARCHAR2) IS SELECT ename, salary FROM emp...

    PLSQL笔记-从hello word到触发器,包,游标高级应用

    以下是如何使用游标的一个例子: ```sql DECLARE cursor_example CURSOR FOR SELECT id, name, salary FROM employees WHERE department_id = 100; emp_record employee_management.employee_rec; BEGIN OPEN ...

    ORACLE游标与异常处理

    在这个例子中,我们声明了一个名为`cursor_name`的游标,用于从`table_name`中选择`column1`和`column2`的值。然后,我们打开游标,进入一个循环,每次从游标中提取一行数据到变量`var1`和`var2`,并打印这些值。当...

    Oracle数据库实验-PLSQL游标、过程、函数、包的使用[文].pdf

    在本次实验中,我们将重点探讨PL/SQL中的游标、过程、函数和包的使用,这些都是数据库开发中不可或缺的部分。 首先,让我们了解PL/SQL中的游标。游标是一种在结果集中定位并移动的机制,它允许我们逐行处理查询结果...

    管理软件 PLSQL

    - 定义游标:`CURSOR cursor_name IS SELECT statement;` - 打开、提取和关闭游标:`OPEN cursor_name; FETCH cursor_name INTO variables; CLOSE cursor_name;` 5. **存储过程和函数**: - 存储过程是一组PLSQL...

    PLSQL适合初学者。

    另外,PLSQL中的游标(Cursor)用于逐行处理查询结果,这对于处理大量数据非常有用。游标可以与循环结构结合,实现数据的遍历和处理。 最后,DBMS_OUTPUT是PLSQL中用于调试的包,它可以打印出程序执行过程中的信息...

    Oracle PLSQL语法大全及实例讲解.pdf

    在上面的例子中,`c_scores`是游标,`FETCH`语句用于获取当前行,`%NOTFOUND`检查是否还有更多行。 7. **集合**: - PL/SQL支持多种类型的集合,如数组和关联数组,它们允许一次性处理多个值。例如,可以创建一个...

    PLSQL新手教程.rar

    以下是一个使用游标的例子: ```sql DECLARE cursor_name CURSOR FOR SELECT column1, column2 FROM table_name WHERE condition; var_column1 datatype1; var_column2 datatype2; BEGIN OPEN cursor_name; ...

    pl_sql基本语法例子.rar_SQL2569_oracle_pl/sql_plsql增删改查

    定义游标后,可以使用FETCH语句获取当前行数据,并通过%ROWTYPE属性创建变量存储这些数据。例如: ``` DECLARE cursor_name CURSOR FOR SELECT * FROM table_name; var_row table_name%ROWTYPE; BEGIN OPEN ...

    oracle游标

    1. **重命名表**:使用`sp_rename`存储过程进行表的重命名。 ```sql EXEC sp_rename 'oldname', 'newname'; ``` 2. **修改列属性**:通过`ALTER COLUMN`命令来改变列的数据类型或其他属性。 ```sql ALTER TABLE...

    plsql学习文件

    学习文件可能包含如何创建和使用这两者的例子。 十、事务管理 PL/SQL支持事务的概念,包括COMMIT、ROLLBACK和SAVEPOINT命令,用于确保数据的一致性和完整性。 通过这份"PLSQL笔记",初学者可以系统地了解和掌握PL/...

    Oracle PLSQL 从入门到精通

    同时,了解如何使用游标(CURSOR)处理结果集,对于进行数据库交互很有帮助。 PLSQL还支持包(PACKAGE),它将相关的函数、过程、变量和常量组合在一起,提供了一个模块化的编程环境。学习如何定义和使用包,有助于...

    PLSQL教程-从入门到精通

    - **模块化**:使用存储过程、函数和包等结构可以提高代码的复用性和组织性。 - **事务控制**:PL/SQL提供了强大的事务控制机制,确保数据的一致性和完整性。 **1.2.2 PL/SQL可用的SQL语句** PL/SQL支持几乎所有的...

    plsql的使用

    FETCH cursor_name INTO var1, var2; EXIT WHEN cursor_name%NOTFOUND; -- 执行基于var1和var2的逻辑 END LOOP; CLOSE cursor_name; END; ``` 接下来,我们谈谈子程序和程序包。在PL/SQL中,子程序包括函数和...

    利用PLSQL实现分页查询代码.rar

    本压缩包"利用PLSQL实现分页查询代码.rar"主要关注的是如何在Oracle环境中通过PL/SQL来执行分页查询,这对于处理大量数据时提升性能和用户体验至关重要。 分页查询是一种在大量数据中只显示一部分(例如,每页10条...

Global site tag (gtag.js) - Google Analytics