Oracle学习笔记 part3

游标

今天我们来聊聊游标,这是一个令人头疼的东西,因为这玩意挺像指针的,而且写起来比较繁琐。

"简单"的游标

declare
    v_age hls_test_sora.test_age%type;
    
    --定义游标
    cursor sora_age_cursor is select test_age from hls_test_sora where test_gender = 'male';
begin
    --打开游标
    open sora_age_cursor;
    
    --提取游标
    fetch sora_age_cursor into v_age;
    
    --循环判断是否能找到游标,能提取就输出并在提取,不能就跳出循环
    while sora_age_cursor%found loop
        dbms_output.put_line(v_age);
        fetch sora_age_cursor into v_age;
    end loop;
    
    --关闭游标
    close sora_age_cursor;
end;

 

通过这个例子我们可以了解到,游标就是将一个单列拆成一个一个小数据,我们可以通过游标来遍历这一个小数据。那么为什么必须是单列呢?下面来试试Record+Cursor

for版游标

declare
    --定义游标
    cursor sora_cursor is select * from hls_test_sora where test_gender = 'female';
begin
    for c in sora_cursor loop
        dbms_output.put_line
        ('ID:'||c.test_id||' 姓名:'||c.test_name||' 年龄:'||c.test_age);
        dbms_output.put_line
        ('性别:'||c.test_gender||' 出生年月:'||c.test_birthday);
    end loop;
end;

 

在我看来这无疑和Java中的List<Map>很像,之后估计需要用这个遍历数据处理。

游标API

%found 布尔属性,当最近一次记录时成功返回,则值为true;
%notfound 与found相反
%isopen 布尔,判断是否打开游标
%rowcount 数字,返回已从游标中读取的记录数(类似Index)

 

posted @ 2021-12-08 11:13  二色莲华  阅读(39)  评论(0)    收藏  举报