Oracle/PLSQL: Creating Procedures

None.gif In Oracle, you can create your own procedures.
None.gif
None.gifThe syntax for a procedure is:
None.gif
None.gifCREATE [OR REPLACE] PROCEDURE procedure_name
None.gif    [ (parameter [,parameter]) ]
None.gifIS
None.gif    [declaration_section]
None.gifBEGIN
None.gif    executable_section
None.gif[EXCEPTION
None.gif    exception_section]
None.gifEND [procedure_name];
None.gif
None.gif
None.gif
None.gifWhen you create a procedure or function, you may define parameters. There are three types of parameters that can be declared:
None.gif
None.gifIN - The parameter can be referenced by the procedure or function. The value of the parameter can not be overwritten by the procedure or function. 
None.gifOUT - The parameter can not be referenced by the procedure or function, but the value of the parameter can be overwritten by the procedure or function. 
None.gifIN OUT - The parameter can be referenced by the procedure or function and the value of the parameter can be overwritten by the procedure or function. 
None.gif
None.gif
None.gifThe following is a simple example of a procedure: 
None.gif
None.gifCREATE OR REPLACE Procedure UpdateCourse
None.gif   ( name_in IN varchar2 )
None.gifIS
None.gif    cnumber number;
None.gif
None.gif    cursor c1 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.gifinsert into student_courses
None.gif( course_name,
None.gif  course_number)
None.gifvalues ( name_in,
None.gif                cnumber );
None.gif
None.gifcommit;
None.gif
None.gifclose c1;
None.gif
None.gifEXCEPTION
None.gifWHEN OTHERS THEN
None.gif      raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
None.gifEND;
None.gif
None.gifThis procedure is called UpdateCourse. It has one parameter called name_in. The procedure will lookup the course_number based on course name. If it does not find a match, it defaults the course number to 99999. It then inserts a new record into the student_courses table.
None.gif

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值