架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 8700|回复: 1

[资料] oracle普通游标隐式游标和带参数的游标

[复制链接]
发表于 2015-3-19 10:53:06 | 显示全部楼层 |阅读模式
select * from stuinfo;
--隐式游标
begin
    dbms_output.put_line('查询前:'||sql%rowcount);
    update stuinfo set stusex = '女';
    dbms_output.put_line('查询后:'||sql%rowcount);
end;

--显式游标
--1.普通的
declare
    cursor mycur1 is select * from stuinfo;--定义游标
    --v_id stuinfo.stuid%type;
    --v_name stuinfo.stuname%type;
    --v_sex stuinfo.stusex%type;
    --v_bir stuinfo.stubir%type;
    v_row stuinfo%rowtype;
begin
    open mycur1;--打开游标
    loop
    --fetch mycur1 into v_id,v_name,v_sex,v_bir;--向下移动游标,把读取出的值放入到变量里
    fetch mycur1 into v_row;--读取游标
    exit when mycur1%notfound;
    --dbms_output.put_line('id:'||v_id||',姓名:'||v_name||',性别:'||v_sex||'生日:'||v_bir);--输出结果
    dbms_output.put_line('id:'||v_row.stuid||',姓名:'||v_row.stuname||',性别:'||v_row.stusex||'生日:'||v_row.stubir);
    end loop;
    close mycur1;--关闭游标
end;

--2.带参数的游标
declare
    cursor mycur1(v_sex stuinfo.stusex%type) is select * from stuinfo where stusex = v_sex;--定义游标
    v_row stuinfo%rowtype;
begin
    open mycur1('&性别');--打开游标
    loop
    fetch mycur1 into v_row;--读取游标
    exit when mycur1%notfound;
    dbms_output.put_line('id:'||v_row.stuid||',姓名:'||v_row.stuname||',性别:'||v_row.stusex||'生日:'||v_row.stubir);
    end loop;
    close mycur1;--关闭游标
end;

--3.循环游标
declare
    cursor mycur1 is select * from stuinfo;--定义游标
begin
    for v_row in mycur1
    loop
        dbms_output.put_line('id:'||v_row.stuid||',姓名:'||v_row.stuname||',性别:'||v_row.stusex||'生日:'||v_row.stubir);
    end loop;
end;

--4.用游标更新数据
declare
    cursor mycur1 is select * from stuinfo for update;--定义游标
begin
    for v_row in mycur1
    loop
        if v_row.stuid = '003' then
           delete from stuinfo where current of mycur1;
        else
           update stuinfo set stusex = '男' where current of mycur1;
        end if;
    end loop;
end;

select * from stuinfo;
update stuinfo set stusex = '女' where stuid = '002';

--引用游标
--1.弱类型
declare
    type curname is ref cursor;--定义一个游标类型(curname是类型名称)
    mycur1 curname;--
    v_sex stuinfo.stusex%type;
    v_row stuinfo%rowtype;
    v_row2 subinfo%rowtype;
begin
    v_sex := '&性别';
    if v_sex = '男' then
       open mycur1 for select * from stuinfo where stusex = '男';
        loop
        fetch mycur1 into v_row;--读取游标
        exit when mycur1%notfound;
        dbms_output.put_line('id:'||v_row.stuid||',姓名:'||v_row.stuname||',性别:'||v_row.stusex||'生日:'||v_row.stubir);
        end loop;
    else
       open mycur1 for select * from subinfo;
       loop
        fetch mycur1 into v_row2;--读取游标
        exit when mycur1%notfound;
        end loop;
    end if;
    close mycur1;--关闭游标
end;
select * from subinfo;

--强类型
declare
    type curname is ref cursor return stuinfo%rowtype;
    mycur1 curname;
    v_sex stuinfo.stusex%type;
    v_row stuinfo%rowtype;
begin
    v_sex := '&性别';
    if v_sex = '男' then
       open mycur1 for select * from stuinfo where stusex = '男';
    else
       open mycur1 for select * from stuinfo where stusex = '女';
    end if;

    loop
        fetch mycur1 into v_row;--读取游标
        exit when mycur1%notfound;
        dbms_output.put_line('id:'||v_row.stuid||',姓名:'||v_row.stuname||',性别:'||v_row.stusex||'生日:'||v_row.stubir);
    end loop;
    close mycur1;--关闭游标
end;







上一篇:《财色棋局》 --揭密恋爱,职场,考研内幕
下一篇:php5.2.17在安装的时候需要把cgi.force_redirect值1改成0
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
发表于 2015-3-19 11:21:08 | 显示全部楼层
备用 lesson6.rar (920 Bytes, 下载次数: 0)
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2024-4-23 13:30

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表