oracle游标一 静态游标-显示游标-声明&初始化

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/changerzhuo_319/article/details/84928170

oracle静态游标-显示游标的使用

目录:

1. 游标语法

2. 游标的使用

2.1 声明游标并初始化

2.2 声明多个变量接收游标返回值

2.3 声明一个record变量接收游标返回值

2.4 打开/关闭游标

2.5 获取游标中的值

2.6 重复使用游标(需要先close, 才能二次open)

 

1. 游标语法

cursor 游标名称 [参数1, 参数2] is 游标数据来源
参数可有,可无
eg:
    --有参数
    cursor stu_cusor(v_scort number := 70) is select * from t_student t where t.scort >= v_scort ;
    --没有参数
     cursor stu_cusor is select * from t_student t where t.scort >= 80 ;

2. 游标的使用

create or replace procedure pro_student as
    --方式一:分别声明变量, 用来接收游标中的值
    v_stu_name varchar2(20);--自定义变量类型
    v_stu_bir t_student.birthday%type;--变量类型和t_student表中的birthday字段类型一致。
    --方式二:集中声明变量, 用来接收游标中的值
    -- 定义record类型
    type record_student is record (
        stu_name varchar2(20),
        stu_bir date
    );
    --声明record类型的的变量
    rec_stu record_student;

    -- 1. 声明游标,及其值得获取方式
    cursor cursor_student(v_score number := 70) is select t.name, t.birthday from t_student t where t.score >= v_score;
    begin 
        --2.打开游标
        if not cursor_student%isopen then
            open cursor_student(80);--获取分数大于80的数据列表
        end if;
        --3. 循环获取游标中的值
        loop
            --获取游标中的值, 并赋值给变量
            fetch cursor_student into v_stu_name, v_stu_bir;
            -- 游标中没有值得时候,退出loop
            exit when cursor_student%notfound;
            dbms_output.put_line('namex='||v_stu_name ||', birthday='||v_stu_bir);
        end loop;
        --4.关闭游标
        close cursor_student;
        
        --5. 获取这个游标其他条件值的数据列表。 二次使用前, 需要先关闭这个游标
        open cursor_student(85);--获取分数大于90的数据列表
        loop
             fetch cursor_student into rec_stu;
             exit when cursor_student%notfound;
             dbms_output.put_line('namey='||rec_stu.stu_name ||', birthday='||rec_stu.stu_bir);
        end loop;
        close cursor_student;
    end pro_student ;

猜你喜欢

转载自blog.csdn.net/changerzhuo_319/article/details/84928170