CREATE OR REPLACE FUNCTION FC_GET_DJBH(v_djlx VARCHAR2,
v_table_name VARCHAR2,
v_ywrq VARCHAR2)
/*
* v_djlx: 单据类型
* v_table_name:编码所在表名
* v_number_col:编码所在列名
*/
RETURN VARCHAR2 IS
v_no VARCHAR2(50):=''; --原编码
v_num NUMBER; --原编码后三位编号
v_ywrqQF VARCHAR2(2);--判断v_ywrq是否含有'-'
v_sql VARCHAR2(4000);
v_ywrq1 VARCHAR2(10);--保存不含'-'的v_ywrq
begin
v_sql :='select nvl(max(reverse(substr(reverse(DJBH),1,2)))+1,1) from '|| v_table_name||
' where instr(DJBH,'''||v_djlx||''')>0 and to_char(YWRQ,''yyyy-mm-dd'') ='''||v_ywrq||'''';
--and instr(DJBH,V_DJLX)>0;
EXECUTE IMMEDIATE v_sql INTO v_num;
--v_ywrq1 := v_ywrq;
select translate(v_ywrq, '-', '') into v_ywrq1 from dual;
--v_ywrq如果包含字符串'-',要去换掉
select instr(v_ywrq,'-') into v_ywrqQF from dual;
if ascii(v_ywrqQF) > ascii('0') THEN
select TO_CHAR(TO_DATE(v_ywrq, 'YYYY-MM-DD'),'YYYYMMDD') into v_ywrq1 from dual;
-- select translate(v_ywrq, '-', '') into v_ywrq1 from dual;
end if;
--select translate( '00000921000001 ', '921', '924') from dual;
v_no := v_djlx||v_ywrq1||'-'||lpad(v_num, 2, '0');
RETURN v_no;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(SQLERRM);
END FC_GET_DJBH;