原文地址:http://blog.csdn.net/jumtre/article/details/38092067
函数调用限制
1、SQL语句中只能调用存储函数(服务器端),而不能调用客户端的函数
2、SQL只能调用带有输入参数,不能带有输出,输入输出函数
3、SQL不能使用PL/SQL的特有数据类型(boolean,table,record等)
4、SQL语句中调用的函数不能包含INSERT,UPDATE和DELETE语句
1.function函数的语法如下:
- create or replace function function_name (
- argu1 [mode1] datatype1,
- argu2 [mode2] datatype2
- ) return datatype
- is
-
- begin
-
- 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;
- 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语句。
转载自:http://www.blogjava.net/stevenjohn/archive/2012/08/12/385324.html