当前位置:文章写作网 >日记 >日记 >游标遍历%rowtype中的记录

游标遍历%rowtype中的记录

2008-05-09 12:01 作者:tshfang 阅读量:18302 推荐30次 | 我要投稿

上篇文章中讲到%rowtype,那么如何遍历所有记录,不用每次输入特定的值去查询。那么我们使用Oracle游标

游标分为:静态游标和引用游标(动态游标)

静态游标:由用户定义(隐式游标、显示游标)结果集不变

引用游标游标:结果集变化

隐式游标:用DML操作时,自动使用隐式游标。我们可以使用隐式游标判断SQL语句执行结果

自动声明和处理的。在Session会话区,开启游标。处理后自动关闭。可以返回单行查询。

隐式游标使用:

declare

%NOTFOUND -- 执行行没有找到。

%FOUND --执行行找到

%ROWCOUNT --游标影响行数

%ISOPEN -- 当前游标是否打开

我们现在通过游标来看看上篇文章的例子

通过循环来遍历数据:

1、loop when循环

declare

cursor myCur is select * from hr.jobs;

oneRow hr.jobs%rowtype;

begin

open myCur;

loop

fetch myCur into oneRow;

dbms_output.put_line(oneRow.job_id ||' ' ||onerow.job_title);

exit when myCur%notFound;

end loop;

close myCur;

end;

2、while 循环

declare

cursor myCur is select * from hr.jobs;

oneRow hr.jobs%rowtype;

begin

open myCur;

fetch myCur into oneRow;

while (myCur%found)

loop

dbms_output.put_line(oneRow.job_id ||' ' ||onerow.job_title);

fetch myCur into oneRow;

end loop;

close myCur;

end;

3、for 循环

declare

cursor myCur is select * from hr.jobs;

oneRow hr.jobs%rowtype;

begin

for oneRow in myCur loop

dbms_output.put_line(oneRow.job_id ||' ' ||onerow.job_title);

end loop;

end;

结果如下:

AD_PRES President

AD_VP Administration Vice President

AD_ASST Administration Assistant

FI_MGR Finance Manager

FI_ACCOUNT Accountant

AC_MGR Accounting Manager

AC_ACCOUNT Public Accountant

SA_MAN Sales Manager

SA_REP Sales Representative

PU_MAN Purchasing Manager

PU_CLERK Purchasing Clerk

ST_MAN Stock Manager

ST_CLERK Stock Clerk

SH_CLERK Shipping Clerk

IT_PROG Programmer

MK_MAN Marketing Manager

MK_REP Marketing Representative

HR_REP Human Resources Representative

PR_REP Public Relations Representative

其他人在看啥

    《游标遍历%rowtype中的记录》的评论 (共 0 条)

    • 阿刚:tshfang先生,您好。我叫阿刚,湖南人,目前在尼日利亚工作,我用的erp是excel sever,想和您交流,您方便的话可以qq联系啊,我的号码214936390,谢谢
    • Guest::ding