天天看點

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  調用示例