PLSQL基础语法四-游标

1.游标的声明:

CURSOR <游标名> IS SELECT<语句>;其中<游标名>就是游标名,<语句>是将要处理的查询。

声明游标例子:

declare
 student_id number(5);  --定义3个变量存放student表中的内容
 student_name varchar2(30);
 student_score number(30);
 cursor stu_cur is  --定义游标stu_cur
  select id,name,score from student where id<5;  --选出id小于5的学生
  end;

2.打开游标语法:

OPEN <游标名>;其中,<游标名>标识了一个已经被声明的游标。

打开游标例子:

declare
 student_id number(5);  --定义3个变量存放student表中的内容
 student_name varchar2(30);
 student_score number(30);
 cursor stu_cur is  --定义游标stu_cur
  select id,name,score from student where id<5;  --选出id小于5的学生
  begin
    open stu_cur;  --打开游标
    
    end;
注意:打开一个已经打开的游标是合法的,在第二次执行open语句以前,PL/SQL将在重新打开该游标之前隐式的执行一条close语句,一次也可以同时打开多个游标。

3.提取游标:

FETCH <游标名>INTO<变量列表>;或FETCH<游标名> INTO PL/SQL记录;其中<游标名>标识了一个已经被声明的游标,<变量列表>是已经声明的PL/SQL变量的列表(变量之间用逗号隔开);

提取游标例子:

declare
 student_id number(5);  --定义3个变量存放student表中的内容
 student_name varchar2(30);
 student_score number(30);
 cursor stu_cur is  --定义游标stu_cur
  select id,name,score from student where id<5;  --选出id小于5的学生
  begin
    open stu_cur;  --打开游标
    fetch stu_cur into student_id,student_name,student_score;--将第一行数据放入变量中,游标后移
    end;

4.关闭游标:

CLOSE <游标名>;其中, <游标名>给出了原来被打开的游标。一旦关闭了游标,也就关闭了select操作,释放了占用的内存,如果再从游标提取数据是非法的。类似的关闭一个已经被关闭的游标也是非法的。

关闭游标例子:

declare
 student_id number(5);  --定义3个变量存放student表中的内容
 student_name varchar2(30);
 student_score number(30);
 cursor stu_cur is  --定义游标stu_cur
  select id,name,score from student where id<5;  --选出id小于5的学生
  begin
    open stu_cur;  --打开游标
    fetch stu_cur into student_id,student_name,student_score;--将第一行数据放入变量中,游标后移
    
    loop
      exit when stu_cur%notfound;--如果游标到尾则结束
      if student_score<60  then--分数小于60的时候
        --一些操作(我这里采用null操作了)
        null;
        else 
          --一些操作(我这里采用null操作了)
          null;
          end if;
        fetch stu_cur into student_id,student_name,student_score;
    end loop;
    close stu_cur; --关闭游标
    end;

游标综合例子:


--游标
declare
  /*1.定义游标,将student表里面的所有数据都提取出来存储在游标中*/
  cursor cur_student is 
   select * from student;
   r_student student%rowtype;
begin
  --2.打开游标
  open cur_student;
  --3.提取数据
  fetch cur_student into r_student;
  dbms_output.put_line('id:'||r_student.id||',name:'||r_student.name);
  --4.关闭游标
  close cur_student;
  end;
--练习2:使用循环提取游标所有记录
declare
  /*1.定义游标,将student表里面的所有数据都提取出来存储在游标中*/
  cursor cur_student is 
   select * from student;
begin
  --3.利用loop循环来提取数据,
  for stu_record in cur_student loop
  dbms_output.put_line('id:'||stu_record.id||',name:'||stu_record.name);
  end loop;
  end;
--练习3:定义带参数的游标(提取每个班级信息,包括每个班级的学生信息)
declare
  /*1.定义游标*/
  cursor cur_grade is --保存班级的游标
  select * from grade;
  cursor cur_student(v_gradeid number) is --保存学生信息的游标
   select * from student s where s.gradeid = v_gradeid;
   r_student student%rowtype;
   r_grade grade%rowtype;
begin
  --2.打开游标
  open cur_grade;
  --3.利用loop循环来提取数据,
  loop
  fetch cur_grade into r_grade;
   exit when cur_grade%notfound;
   dbms_output.put_line('班级id:'||r_grade.id||',班级name:'||r_grade.name);
   
   /************内循环取学生信息*****************/
   open cur_student(r_grade.id);
   loop
     fetch cur_student into r_student;
      exit when cur_student%notfound;
     dbms_output.put_line('学生的姓名:'||r_student.name);
     end loop;
   close cur_student;
   /************内循环取学生信息*****************/
   
  end loop;
  --4.关闭游标
  close cur_grade;
  end;


你可能感兴趣的:(oracle,plsql)