Oracle/PLSQL: FETCH Statement

本文详细介绍了在数据库操作中使用游标的目的和基本语法,通过实例展示了如何定义及从游标中FETCH数据行,并提供了一个使用FETCH语句的函数示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

None.gif The purpose of using a cursor, in most cases, is to retrieve the rows from your cursor so that some type of operation can be performed on the data. After declaring and opening your cursor, the next step is to FETCH the rows from your cursor.
None.gif
None.gifThe basic syntax for a FETCH statement is:
None.gif
None.gifFETCH cursor_name INTO 
< list  of variables > ;
None.gif
None.gif
None.gif
None.gifFor example, you could have a cursor defined as:
None.gif
None.gifCURSOR c1
None.gifIS
None.gif    SELECT course_number
None.gif      from courses_tbl
None.gif      where course_name = name_in;
None.gif
None.gifThe command that would be used to fetch the data from this cursor is:
None.gif
None.gifFETCH c1 into cnumber;
None.gif
None.gifThis would fetch the first course_number into the variable called cnumber;
None.gif
None.gif
None.gif
None.gifBelow is a function that demonstrates how to use the FETCH statement.
None.gif
None.gifCREATE OR REPLACE Function FindCourse
None.gif   ( name_in IN varchar2 )
None.gif   RETURN number
None.gifIS
None.gif    cnumber number;
None.gif
None.gif    CURSOR c1
None.gif    IS
None.gif       SELECT course_number
None.gif        from courses_tbl
None.gif        where course_name = name_in;
None.gif
None.gifBEGIN
None.gif
None.gifopen c1;
None.giffetch c1 into cnumber;
None.gif
None.gifif c1%notfound then
None.gif     cnumber := 9999;
None.gifend if;
None.gif
None.gifclose c1;
None.gif
None.gifRETURN cnumber;
None.gif
None.gifEND;
None.gif

转载于:https://www.cnblogs.com/timsoft/articles/412755.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值