oracle-自定义函数

  在Oracle中,自定义函数格式如下:

CREATE OR REPLACE FUNCTION 函数名 (   argu1 [mode1] datatype1) --  定义参数变量
RETURN 返回值类型 
IS 
    声明部分; 
BEGIN 
    函数体; 
  RETURN 变量; 
END;

函数解析:

函数的参数有3种类型:

(1)in参数类型:表示输入给函数的参数,该参数只能用于传值,不能被赋值。

(2)out参数类型:表示参数在函数中被赋值,可以传给函数调用程序,该参数只能用于赋值,不能用于传值。

(3)in out参数类型:表示参数既可以传值,也可以被赋值。

示例:

create or replace function sf_score_pm(
p_in_stuid in varchar2,--学号
p_in_courseid in  varchar2 --课程ID
)
return number
is
ls_pm number:=0;
ls_score number:=0;
begin
  --获取该学生的成绩
  select t.score into ls_score from score t
   where t.stuid = p_in_stuid
     and t.courseid = p_in_courseid;
  --获取成绩比该学生高的人数
  select count(1) into ls_pm from score t
   where t.courseid = p_in_courseid
   and  t.score>ls_score;
   --得到该学生的成绩排名
   ls_pm:=ls_pm+1;
   return ls_pm;
exception
  when no_data_found  then
     dbms_output.put_line('该学生的课程:'||p_in_courseid|| '的成绩在成绩表中找不到');
end;

函数查看语法:

select * from user_source t where t.name='SF_SCORE_PM';

删除自定义函数语法:

drop function 函数名;

转载于:https://my.oschina.net/lyleluo/blog/3058157

猜你喜欢

转载自blog.csdn.net/weixin_34148340/article/details/91907701