天天看点

Oracle自定义函数示例

create or replace function getStu_name(s_stu_id varchar2) return varchar2 is

  --声明变量
  Result     varchar2();
  stu_cursor my_cursor;
  sex        int;
  stu_name   varchar2();
  --定义游标
  type my_cursor is ref cursor;

begin
  --打开游标
  open stu_cursor for
    select stu_name, sex from tb_gyf_students where stu_id = s_stu_id;

  loop
    --循环
    fetch stu_cursor
      into stu_name, sex; --读取游标存入变量

    exit when stu_cursor%notfound; --循环结束条件
    --if判断
    if sex =  then   
      Result := stu_name || ',' || Result; --业务逻辑

    end if; --结束判断

  end loop; --结束循环

  return(Result); --返回业务逻辑处理结果
end getStu_name; --结束函数

--  select stu_name,getstu_name(stu_id) from TB_GYF_STUDENTS  调用示例