Oracle/PLSQL: Declare a Cursor

None.gif A cursor is a SELECT statement that is defined within the declaration section of your PLSQL code. We'll take a look at three different syntaxes for cursors.
None.gif
None.gifCursor without parameters (simplest)
None.gifThe basic syntax for a cursor without parameters is:
None.gif
None.gifCURSOR cursor_name
None.gifIS
None.gif    SELECT_statement;
None.gif
None.gif
None.gif
None.gifFor example, you could define a cursor called c1 as below.
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 result set of this cursor is all course_numbers whose course_name matches the variable called name_in.
None.gif
None.gif
None.gif
None.gifBelow is a function that uses this cursor.
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
None.gif
None.gif
None.gifCursor with parameters
None.gifThe basic syntax for a cursor with parameters is:
None.gif
None.gifCURSOR cursor_name (parameter_list)
None.gifIS
None.gif    SELECT_statement;
None.gif
None.gif
None.gif
None.gifFor example, you could define a cursor called c2 as below.
None.gif
None.gifCURSOR c2 (subject_id_in IN varchar2)
None.gifIS
None.gif    SELECT course_number
None.gif      from courses_tbl
None.gif      where subject_id = subject_id_in;
None.gif
None.gifThe result set of this cursor is all course_numbers whose subject_id matches the subject_id passed to the cursor via the parameter.
None.gif
None.gif
None.gif
None.gifCursor with return clause
None.gifThe basic syntax for a cursor with a return clause is:
None.gif
None.gifCURSOR cursor_name
None.gifRETURN field%ROWTYPE
None.gifIS
None.gif    SELECT_statement;
None.gif
None.gifFor example, you could define a cursor called c3 as below.
None.gif
None.gifCURSOR c3
None.gifRETURN courses_tbl%ROWTYPE
None.gifIS
None.gif    SELECT *
None.gif      from courses_tbl
None.gif      where subject = 'Mathematics'; 
None.gif
None.gifThe result set of this cursor is all columns from the course_tbl where the subject is Mathematics. 
None.gif

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值