oracle function用法

函数调用限制
1、SQL语句中只能调用存储函数(服务器端),而不能调用客户端的函数
2、SQL只能调用带有输入参数,不能带有输出,输入输出函数
3、SQL不能使用PL/SQL的特有数据类型(boolean,table,record等)
4、SQL语句中调用的函数不能包含INSERT,UPDATE和DELETE语句

1.function函数的语法如下:

1 create or replace function function_name (  
2      argu1 [mode1] datatype1, --定义参数变量  
3      argu2 [mode2] datatype2 --定义参数变量  
4  ) return datatype --定义返回的数据类型  
5 is   
6   
7 begin  
8   
9 end;  

执行:

var v1 varchar2(100)   
exec :v1:=function_name 

2.不带任何参数的定义

create or replace function get_user   
return varchar2   
is   
Result varchar2(50); --定义变量  
begin   
select username into Result from user_users;   
return(Result); --返回值  
end get_user;  

3.带有in参数的

create or replace function get_sal(  
empname in varchar2  
) return number   
is   
Result number;   
begin   
select sal into Result from emp where ename=empname;   
return(Result);   
end;  

执行:

SQL> var sal number   
SQL> exec :sal:=get_sal('scott');  

4.带out参数的

create or replace function get_info(  
e_name varchar2,  
job out varchar2  
) return number   
Is  
Result number;   
begin   
select sal,job into Result,job from emp where ename=e_name;   
return(Result);   
end;  

执行:

SQL> var job varchar2(20)   
SQL> var dname varchar2(20)   
SQL> exec :dname:=get_info('SCOTT',:job)  

5.带in out参数的

6.函数调用举例

create or replace function f_sys_getseqid(  
    v_seqname           IN VARCHAR2,  
    v_provincecode      IN VARCHAR2    --省编码  
) return Varchar2  
IS  
    iv_date             VARCHAR2(8);  
    iv_seqname          VARCHAR2(50);  
    iv_sqlstr           VARCHAR2(200);  
    iv_seq              VARCHAR2(8);  
    iv_seqid            VARCHAR2(16);  
BEGIN  
    iv_seqname := LOWER(TRIM(v_seqname));  
    iv_sqlstr := 'SELECT '||iv_seqname||'.nextval FROM DUAL';  
    EXECUTE IMMEDIATE iv_sqlstr INTO iv_seq;--执行动态的sql语句,执行相似的一组语句  
    IF v_seqname = 'SEQ_FUNCROLE_ID' THEN  
      iv_seqid:= 'ESS' || LPAD(iv_seq,5,'0');  
    ELSE  
      SELECT substrb(v_provincecode,1,2)||TO_CHAR(SYSDATE,'yymmdd') INTO iv_date FROM DUAL;  
      iv_seqid:= iv_date || LPAD(iv_seq,8,'0');  
    END IF;  
    RETURN iv_seqid;  
EXCEPTION  
    WHEN OTHERS THEN  
    RETURN NULL;  
END;  

调用方式如下:

SELECT TO_NUMBER(F_SYS_GETSEQID('SEQ_TERMTRADE_ID', V_PROVINCE_CODE)) INTO V_BATCH_ID FROM DUAL;  

EXECUTE IMMEDIATE的说明:执行动态的sql语句。

函数中使用游标

create or replace function getcustprodinstaddr(in_CustId in number,in_area_code in number)   
return varchar2   
is  
  Result varchar2(4000);  
  v_acc_nbr varchar2(400);  
  tempCount number:=1;  
  type ref_cursor is ref cursor;  
  v_cursor ref_cursor;  
begin  
  Result:='';  
  open v_cursor for   
  'select install_addr from tb_prd_prd_inst_'||to_char(in_area_code)||  
  ' where PRD_INST_STAS_ID not in(''1003'',''1101'',''1401'',''1102'')   
  and own_cust_id='||to_char(in_CustId)||' order by install_date desc';  
  loop  
    fetch v_cursor into v_acc_nbr;  
    exit when v_cursor%notfound;  
    if(tempCount>8) then  
        goto label_end;  
    end if;  
    Result:=v_acc_nbr||','||Result;  
    tempCount := tempCount +1;  
  end loop;  
  <<label_end>>  
  close v_cursor;  
  return(Result);  
  exception   
    when others then  
    if(v_cursor%isopen) then  
        close v_cursor;  
    end if;  
    return '';  
end getcustprodinstaddr;  

 

posted @ 2018-06-21 16:28  Anan_Zou  阅读(4078)  评论(0编辑  收藏  举报