functions 示例
示例1: “,”字符串截取
1 CREATE OR REPLACE FUNCTION splitstr(p_string IN VARCHAR2, p_delimiter IN VARCHAR2, p_INDEX IN NUMBER) 2 RETURN varchar2 3 AS 4 v_length NUMBER := LENGTH(p_string); 5 v_start NUMBER := 1; 6 v_index NUMBER; 7 v_ret VARCHAR2(20); 8 v_count NUMBER := 1; 9 BEGIN 10 WHILE(v_start <= v_length) 11 LOOP 12 v_index := INSTR(p_string, p_delimiter, v_start); 13 v_ret := substr(p_string,v_start,v_index-1); 14 v_start := v_index+1; 15 IF v_count = p_index THEN 16 RETURN v_ret; 17 END IF; 18 v_count := v_count + 1; 19 END LOOP; 20 21 RETURN v_ret; 22 END splitstr;
示例2:解析分组函数
1 create or replace function resolve_group_sql(v_sql varchar2,in_flowCode varchar2, 2 in_nodesCode varchar2, in_workId varchar2, in_userId varchar2) return varchar2 is 3 /****************************************** 4 解析分组函数 5 ***************************************/ 6 Result varchar2(3000); 7 v_start INT; 8 v_start2 INT; 9 v_count INT; 10 v_count2 INT; 11 i_count INT; 12 v_tm_sql varchar2(3000); 13 v_tm_str varchar2(3000); 14 v_tm_code varchar2(200); 15 v_tm_code2 varchar2(200); 16 v_value varchar2(30); 17 begin 18 v_tm_sql:=v_sql; 19 /* i_count:=0; 20 while (instr(v_tm_sql,'{')>0) loop 21 v_start:=instr(v_tm_sql,'{')+1; 22 v_count:=instr(v_tm_sql,'}')- v_start; 23 v_tm_code:=substr(v_tm_sql,v_start,v_count); 24 i_count:=i_count+1; 25 26 v_tm_str:=v_tm_code; 27 while (instr(v_tm_str,'%[')>0) loop 28 v_start2:=instr(v_tm_str,'%[')+2; 29 v_count2:=instr(v_tm_str,']')- v_start2; 30 v_tm_code2:=substr(v_tm_str,v_start2,v_count2); 31 v_value:=get_constant_val(v_tm_code2); 32 v_tm_str:=replace(v_tm_str,'%['||v_tm_code2||']',v_value); 33 end loop; 34 select v_tm_str into v_value from dual; 35 36 v_tm_sql:=replace(v_tm_sql,'{'||v_tm_code||'}',v_value); 37 38 end loop;*/ 39 40 --解析常量 41 i_count:=0; 42 while (instr(v_tm_sql,'%[')>0) loop 43 v_start:=instr(v_tm_sql,'%[')+2; 44 v_count:=get_end_pos(v_tm_sql,v_start)- v_start; 45 v_tm_code:=substr(v_tm_sql,v_start,v_count); 46 v_value:=get_constant_val(v_tm_code); 47 v_tm_sql:=replace(v_tm_sql,'%['||v_tm_code||']',v_value); 48 end loop; 49 50 --解析接口参数 51 i_count:=0; 52 while (instr(v_tm_sql,'&[')>0) loop 53 v_start:=instr(v_tm_sql,'&[')+2; 54 v_count:=get_end_pos(v_tm_sql,v_start)- v_start; 55 v_tm_code:=substr(v_tm_sql,v_start,v_count); 56 v_value:=get_param_val(v_tm_code,in_flowCode,in_nodesCode,in_workId,in_userId); 57 v_tm_sql:=replace(v_tm_sql,'&['||v_tm_code||']',v_value); 58 end loop; 59 60 --去掉{}符号 61 v_tm_sql:=replace(v_tm_sql,'{',''); 62 v_tm_sql:=replace(v_tm_sql,'}',''); 63 64 Result:=v_tm_sql; 65 return(Result); 66 end resolve_group_sql; 67
示例3:获取主键id
1 CREATE OR REPLACE FUNCTION NEXTID(NOID_ IN VARCHAR2) RETURN NUMBER AS 2 PRAGMA AUTONOMOUS_TRANSACTION; 3 RESULT NUMBER; --返回结果 4 IS_EXIST NUMBER; --是否存在,0 不尊在 1 存在 5 BEGIN 6 -- 判断非空 7 IF NOID_ IS NULL OR NOID_ = '' THEN 8 RETURN NULL; 9 END IF; 10 --判断是否存在序列 NOID 11 12 SELECT COUNT(*) INTO IS_EXIST FROM PWP_NO WHERE NOID = NOID_; 13 IF IS_EXIST = 1 THEN 14 -- 存在主表信息 15 SELECT COUNT(*) INTO IS_EXIST FROM PWP_NODTL WHERE NOID = NOID_; 16 IF IS_EXIST = 1 THEN 17 -- 存在明细表信息,查询需要的结果 18 SELECT NEXTID INTO RESULT FROM PWP_NODTL WHERE NOID = NOID_; 19 -- 更新nextid为下一个id 20 UPDATE PWP_NODTL SET NEXTID = NEXTID + 1 WHERE NOID = NOID_; 21 ELSIF IS_EXIST = 0 THEN 22 --不存在明细表信息,明细表插入数据,nextid设置为2,返回1 23 INSERT INTO PWP_NODTL 24 (NOID, 25 PREFIX, 26 NODAY, 27 NEXTID, 28 POSTFIX, 29 NOINCREMENT, 30 NOYEAR, 31 NOMONTH) 32 VALUES 33 (NOID_, 34 NULL, 35 0, 36 2, 37 NULL, 38 1, 39 TO_CHAR(SYSDATE, 'yyyy'), 40 TO_CHAR(SYSDATE, 'MM')); 41 42 RESULT := 1; 43 END IF; 44 END IF; 45 46 -- 提交 47 COMMIT; 48 --返回 49 RETURN(RESULT); 50 END NEXTID; 51
示例4:拼接字符串“,”
1 create or replace function GET_YKJF_YKJHID( v_zbz_bal_id in number, 2 v_gk_ykjhsphz_id in number) 3 return varchar2 is 4 result varchar2(4000); 5 begin 6 7 if v_zbz_bal_id is not null and v_gk_ykjhsphz_id is not null then 8 for temp_cursor in (select distinct zc.id 9 from gk_ykjh zc, gk_ykjh_mx zcm,gk_ykjh_sphz_mx zsm 10 where zc.id = zcm.gk_ykjh_id 11 and zsm.gk_ykjh_id=zc.id 12 and zcm.zbz_bal_id = v_zbz_bal_id 13 and zsm.gk_ykjhsphz_id = v_gk_ykjhsphz_id 14 order by zc.id) loop 15 result := result || temp_cursor.id || ','; 16 end loop; 17 result := rtrim(result, ','); 18 19 20 ----------------------------------------------------------------------------------------------- 21 22 else 23 result := ''; 24 end if; 25 dbms_output.put_line(result); 26 return result; 27 end; 28
1 create or replace function get_var_val(in_code varchar2, in_pid VARCHAR2, v_sql varchar2, in_group_id varchar2, 2 vartype in number, null_value in varchar2,in_flowCode varchar2, in_nodesCode varchar2, 3 in_workId varchar2, in_userId varchar2) return varchar2 is 4 /****************************************** 5 获取取数函数 6 ***************************************/ 7 Result varchar2(200); 8 type emp_array is Table OF varchar2(200) INDEX BY BINARY_INTEGER; 9 TYPE CURSOR_TYPE IS REF CURSOR; --声明动态游标 10 my_cursor CURSOR_TYPE; --声明游标变量 11 a_field_params emp_array; -- 取数sql中的分组字段数组 12 a_group_name emp_array; -- 取数sql中的分组字段数组 13 a_group_type emp_array; -- 取数sql中的分组字段类型 14 a_group_value emp_array; -- 取数sql中的分组字段数组 15 a_var_params emp_array; --取数代码数组 16 a_var_values emp_array; --取数代码值数组 17 cursor_handle INTEGER; 18 tmp_return INTEGER; 19 v_start INT; 20 v_count INT; 21 i_count INT; 22 v_tm_sql varchar2(1000); 23 v_tm_code varchar2(30); 24 v_value varchar2(200); 25 v_error_msg varchar2(4000); 26 begin 27 v_tm_sql:=v_sql; 28 i_count:=0; 29 while (instr(v_tm_sql,'#[')>0) loop 30 v_start:=instr(v_tm_sql,'#[')+2; 31 v_count:=get_end_pos(v_tm_sql,v_start)- v_start; 32 v_tm_code:=substr(v_tm_sql,v_start,v_count); 33 34 /* open my_cursor for 'SELECT T.GVALUE FROM zz_tmp_variable T WHERE T.group_id = '''||in_group_id||'''' ||' and T.CODE = '''||v_tm_code||''''; 35 loop 36 fetch my_cursor into v_value; 37 exit when my_cursor%notfound; 38 end loop; 39 close my_cursor;*/ 40 41 SELECT T.GVALUE into v_value FROM zz_tmp_variable T 42 WHERE T.group_id =in_group_id 43 and T.CODE = v_tm_code and rownum=1 44 AND T.PID=in_pid ; 45 46 if v_value='null' then 47 Result:='null'; 48 return(Result); 49 end if; 50 v_tm_sql:=replace(v_tm_sql,'#['||v_tm_code||']',':'||v_tm_code); 51 i_count:=i_count+1; 52 a_var_params(i_count):=v_tm_code; 53 a_var_values(i_count):=v_value; 54 end loop; 55 56 --解析SQL中的分组字段start 57 --去掉'.'符号,若不去掉绑定变量的时候会找不到变量start 58 i_count:=0; 59 while (instr(v_tm_sql,'$[')>0) loop 60 v_start:=instr(v_tm_sql,'$[')+2; 61 v_count:=get_end_pos(v_tm_sql,v_start)- v_start; 62 v_tm_code:=substr(v_tm_sql,v_start,v_count); 63 i_count:=i_count+1; 64 v_tm_sql:=replace(v_tm_sql,'$['||v_tm_code||']',':'||replace(v_tm_code,'.','')); 65 a_field_params(i_count):=replace(v_tm_code,'.',''); 66 end loop; 67 --去掉'.'符号,若不去掉绑定变量的时候会找不到变量end 68 69 select t.value1,t.value2,t.value3,t.value4,t.value5,t.value6,t.value7,t.value8,t.value9,t.value10 70 into a_group_value(1),a_group_value(2),a_group_value(3),a_group_value(4),a_group_value(5) 71 ,a_group_value(6),a_group_value(7),a_group_value(8),a_group_value(9),a_group_value(10) 72 from zz_tmp_group t where t.id=in_group_id and t.pid=in_pid; 73 74 select t.name1,t.name2 ,t.name3,t.name4,t.name5,t.name6,t.name7,t.name8,t.name9,t.name10 75 into a_group_name(1),a_group_name(2),a_group_name(3),a_group_name(4),a_group_name(5) 76 ,a_group_name(6),a_group_name(7),a_group_name(8),a_group_name(9),a_group_name(10) 77 from zz_tmp_group t where t.id=in_group_id and t.pid=in_pid; 78 79 select t.type1,t.type2 ,t.type3,t.type4,t.type5,t.type6,t.type7,t.type8,t.type9,t.type10 80 into a_group_type(1),a_group_type(2),a_group_type(3),a_group_type(4),a_group_type(5) 81 ,a_group_type(6),a_group_type(7),a_group_type(8),a_group_type(9),a_group_type(10) 82 from zz_tmp_group t where t.id=in_group_id and t.pid=in_pid; 83 84 --解析SQL中的分组字段end 85 86 --解析常量 87 i_count:=0; 88 while (instr(v_tm_sql,'%[')>0) loop 89 v_start:=instr(v_tm_sql,'%[')+2; 90 v_count:=get_end_pos(v_tm_sql,v_start)- v_start; 91 v_tm_code:=substr(v_tm_sql,v_start,v_count); 92 v_value:=get_constant_val(v_tm_code); 93 v_tm_sql:=replace(v_tm_sql,'%['||v_tm_code||']',v_value); 94 end loop; 95 96 --去掉{}符号 97 v_tm_sql:=replace(v_tm_sql,'{',''); 98 v_tm_sql:=replace(v_tm_sql,'}',''); 99 100 --解析接口参数 101 i_count:=0; 102 while (instr(v_tm_sql,'&[')>0) loop 103 v_start:=instr(v_tm_sql,'&[')+2; 104 v_count:=get_end_pos(v_tm_sql,v_start)- v_start; 105 v_tm_code:=substr(v_tm_sql,v_start,v_count); 106 v_value:=get_param_val(v_tm_code,in_flowCode,in_nodesCode,in_workId,in_userId); 107 v_tm_sql:=replace(v_tm_sql,'&['||v_tm_code||']',v_value); 108 end loop; 109 110 --执行取数sql开始 111 cursor_handle := DBMS_SQL.OPEN_CURSOR; 112 dbms_sql.parse(cursor_handle,v_tm_sql,dbms_sql.native); 113 114 --替换绑定取数值start 115 for i in 1..a_var_params.count loop 116 dbms_sql.bind_variable(cursor_handle,a_var_params(i),a_var_values(i)) ; 117 end loop; 118 --替换绑定取数值end 119 120 i_count:=0; 121 for i in 1..a_group_name.count loop 122 if a_group_name(i) is not null then 123 i_count:=i_count+1; 124 end if; 125 end loop; 126 127 --绑定分组字段start 128 for i in 1..a_field_params.count loop 129 for j in 1..i_count loop 130 if a_field_params(i)=replace(a_group_name(j),'.','') then 131 if a_group_type(j)='date' then 132 dbms_sql.bind_variable(cursor_handle,a_field_params(i),to_date(a_group_value(j))); 133 else 134 dbms_sql.bind_variable(cursor_handle,a_field_params(i),a_group_value(j)); 135 end if; 136 137 end if; 138 end loop; 139 end loop; 140 --绑定分组字段end 141 142 143 144 --绑定常量值start 145 /* for i in 1..a_constant.count loop 146 dbms_sql.bind_variable(cursor_handle,a_constant(i),get_constant_val(a_constant(i),v_constant_type)) ; 147 end loop;*/ 148 --绑定常量值end 149 150 dbms_sql.define_column(cursor_handle, 1, Result,200); 151 tmp_return :=dbms_sql.EXECUTE(cursor_handle); 152 if dbms_sql.fetch_rows(cursor_handle)>0 then 153 dbms_sql.column_value(cursor_handle,1,Result); 154 end if; 155 if Result is null then 156 if null_value='1' then 157 Result:='null'; 158 elsif null_value='2' then 159 Result:='0'; 160 elsif null_value='3' then 161 Result:=''; 162 end if; 163 end if; 164 if (vartype=3 ) then Result:=to_char(to_date(Result),'YYYY-MM-DD'); end if; 165 Result:=TO_CHAR(Result); 166 dbms_sql.close_cursor(cursor_handle); 167 --执行取数sql结束 168 return(Result); 169 exception when others then 170 v_error_msg:='取数编码:'||in_code||'解析出错 sqlerrm:'||substr(sqlerrm,1,3000); 171 DBMS_OUTPUT.PUT_LINE(v_error_msg); 172 Result:='null'; 173 return result; 174 175 end get_var_val; 176
示例5:blob clob 之间的转化
1 CREATE OR REPLACE FUNCTION blob_to_varchar( 2 blob_in IN BLOB) 3 RETURN VARCHAR2 4 IS 5 v_varchar VARCHAR2(4000); 6 v_start Pls_Integer := 1; 7 v_buffer Pls_Integer := 4000; 8 BEGIN 9 IF Dbms_Lob.Getlength(blob_in) IS NULL THEN 10 RETURN ''; 11 END IF; 12 FOR I IN 1..Ceil(Dbms_Lob.Getlength(blob_in) / V_Buffer) 13 LOOP 14 --当转换出来的字符串乱码时,可尝试用注释掉的函数 15 --v_varchar := Utl_Raw.Cast_To_Varchar2(Utl_Raw.Convert(Dbms_Lob.Substr(blob_in, v_buffer, v_start),'SIMPLIFIED CHINESE_CHINA.ZHS16GBK', 'AMERICAN_THE NETHERLANDS.UTF8')); 16 v_varchar := Utl_Raw.Cast_To_Varchar2(Dbms_Lob.Substr(blob_in, v_buffer, v_start)); 17 v_start := v_start + v_buffer; 18 END LOOP; 19 RETURN v_varchar; 20 END ; 21
1 CREATE OR REPLACE FUNCTION B2C(P_BLOB BLOB) RETURN CLOB AS 2 L_CLOB CLOB; 3 AMOUNT NUMBER; 4 DEST_OFFSET NUMBER; 5 SRC_OFFSET NUMBER; 6 BLOB_CSID NUMBER; 7 LANG_CONTEXT NUMBER; 8 WARNING NUMBER; 9 BEGIN 10 DBMS_LOB.CREATETEMPORARY(L_CLOB, TRUE); 11 AMOUNT := DBMS_LOB.GETLENGTH(P_BLOB); 12 DEST_OFFSET := 1; 13 SRC_OFFSET := 1; 14 BLOB_CSID := NLS_CHARSET_ID('UTF8'); 15 LANG_CONTEXT := DBMS_LOB.DEFAULT_LANG_CTX; 16 DBMS_LOB.CONVERTTOCLOB(L_CLOB, 17 P_BLOB, 18 AMOUNT, 19 DEST_OFFSET, 20 SRC_OFFSET, 21 BLOB_CSID, 22 LANG_CONTEXT, 23 WARNING); 24 RETURN L_CLOB; 25 EXCEPTION 26 WHEN OTHERS THEN 27 RETURN NULL; 28 END; 29
1 CREATE OR REPLACE FUNCTION C2B(L_CLOB CLOB) RETURN BLOB IS 2 P_BLOB BLOB; 3 AMOUNT NUMBER; 4 DEST_OFFSET NUMBER; 5 SRC_OFFSET NUMBER; 6 BLOB_CSID NUMBER; 7 LANG_CONTEXT NUMBER; 8 WARNING NUMBER; 9 BEGIN 10 11 DBMS_LOB.CREATETEMPORARY(P_BLOB, TRUE); 12 AMOUNT := DBMS_LOB.GETLENGTH(L_CLOB); 13 DEST_OFFSET := 1; 14 SRC_OFFSET := 1; 15 BLOB_CSID := NLS_CHARSET_ID('UTF8'); 16 LANG_CONTEXT := DBMS_LOB.DEFAULT_LANG_CTX; 17 DBMS_LOB.CONVERTTOBLOB(P_BLOB, 18 L_CLOB, 19 AMOUNT, 20 DEST_OFFSET, 21 SRC_OFFSET, 22 BLOB_CSID, 23 LANG_CONTEXT, 24 WARNING); 25 RETURN P_BLOB; 26 END C2B; 27
为人:谦逊、激情、博学、审问、慎思、明辨、 笃行
学问:纸上得来终觉浅,绝知此事要躬行
为事:工欲善其事,必先利其器。
态度:道阻且长,行则将至;行而不辍,未来可期
.....................................................................
------- 桃之夭夭,灼灼其华。之子于归,宜其室家。 ---------------
------- 桃之夭夭,有蕡其实。之子于归,宜其家室。 ---------------
------- 桃之夭夭,其叶蓁蓁。之子于归,宜其家人。 ---------------
=====================================================================
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。 * @author Alan -liu * @Email no008@foxmail.com
转载请标注出处! ✧*꧁一品堂.技术学习笔记꧂*✧. ---> https://www.cnblogs.com/ios9/
学问:纸上得来终觉浅,绝知此事要躬行
为事:工欲善其事,必先利其器。
态度:道阻且长,行则将至;行而不辍,未来可期
.....................................................................
------- 桃之夭夭,灼灼其华。之子于归,宜其室家。 ---------------
------- 桃之夭夭,有蕡其实。之子于归,宜其家室。 ---------------
------- 桃之夭夭,其叶蓁蓁。之子于归,宜其家人。 ---------------
=====================================================================
* 博客文章部分截图及内容来自于学习的书本及相应培训课程以及网络其他博客,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。 * @author Alan -liu * @Email no008@foxmail.com
转载请标注出处! ✧*꧁一品堂.技术学习笔记꧂*✧. ---> https://www.cnblogs.com/ios9/