oracle函数和存储过程
--1、自定义函数create or replace function 函数名(参数1 参数类型1,参数2 参数类型2,……) return 返回值类型 asbegin 函数定义end 函数名;--实例1select * from test1;create or replace function getMaxTage return number asbegin declare maxAge number; begin select max(tage) into maxAge from test1; return maxAge; end;end getMaxTage;--实例1调用begin dbms_output.put_line('表test1中年纪最大的是:'||getMaxTage());end;--执行动态拼接SQL语句:execute immediate sql语句……--为了提高数据库效率利用函数的确定性:在return 返回类型之后,as之前添加deterministic关键字,前提条件是函数传入参数--一样时返回相同,第二次调用函数是数据库就用前一个计算的值,而不需要再重新计算一次. --自定义函数+游标create or replace function row2column(sqlString varchar2)return varchar2 asbegin declare type cu_type is ref cursor; temp_cursor cu_type; temp_row1 varchar2(20); temp_row2 varchar2(10); v_result varchar2(200); begin open temp_cursor for sqlString; fetch temp_cursor into temp_row1,temp_row2; while temp_cursor%found loop v_result := v_result || temp_row1 || ':' || temp_row2 || ';'; fetch temp_cursor into temp_row1,temp_row2; end loop; return v_result; --rtrim(v_result,',') end;end row2column;--调用select row2column('select tname,tage from test1 where tage >30') v_result from dual;--2、存储过程create or replace procedure 存储过程名称(参数1 in/out 参数类型1,参数2 in/out 参数类型2,……) asbegin 存储过程定义end 存储过程名称;--参数类型:--in只进步出(表明in参数不能再存储过程内部进行修改)--out只出不进(out参数实际作为存储过程执行结果的出口,必须使用变量为参数,变量的作用相当于占位符,只是接收存储过程内部赋值)--in out 典型场景为交换两个变量值--存储过程添加调试测试create or replace procedure insertTest1(tname in varchar2,tage in number, ori_count out number,cur_count out number) asbegin declare v_max_id number; begin if(tname is null or length(tname)=0)then return; end if; if(tage<10 or tage>30)then return; end if; select count(1) into ori_count from test1; select max(tid) into v_max_id from test1; insert into test1 values(v_max_id+1,tname,tage,'test1'); select count(1) into cur_count from test1; end;end insertTest1;--程序包create or replace package pkg_test as function getMaxTage return number; procedure insertTest1(tname in varchar2,tage in number, ori_count out number,cur_count out number);end pkg_test;--create or replace function is_date(params varchar2) return varchar2 isd date;begin d:=to_date(nvl(params,''),'yyyy-mm-dd hh24:mi:ss'); return 'Y'; exception when others then return 'N';end;select is_date('20101010') from dual;
?
我的异常网推荐解决方案:oracle存储过程,http://www.myexception.cn/oracle-develop/177537.html