Copy codeThe Code is as follows:
Create or replace function fn_no_make (v_prefix VARCHAR2,
V_table_name VARCHAR2,
V_number_col VARCHAR2)
/*
* V_prefix: Two encoding prefixes
* V_table_name: name of the table to which the encoding belongs
* V_number_col: name of the encoding Column
*/
RETURN VARCHAR2 IS
V_old_no VARCHAR2 (50); -- original Encoding
V_new_no VARCHAR2 (50); -- New Encoding
V_old_num NUMBER; -- the last three digits of the original Encoding
V_new_num VARCHAR2 (10); -- the last three digits after the new Encoding
V_date_no VARCHAR2 (10); -- current date number
V_ SQL VARCHAR2 (4000 );
BEGIN
V_ SQL: = 'select MAX ('| v_number_col |') from' | v_table_name;
Execute immediate v_ SQL INTO v_old_no;
V_ SQL: = 'select TO_CHAR (SYSDATE, ''yyymmdd') FROM dual ';
Execute immediate v_ SQL INTO v_date_no;
V_old_num: = to_number (substr (v_old_no, 12, 3 ));
V_new_num: = lpad (to_char (v_old_num + 1), 3, '0 ');
IF v_old_no is null or substr (v_old_no, 3, 8) <> v_date_no THEN
V_new_no: = v_prefix | v_date_no | '-' | '001 ';
ELSE
V_new_no: = v_prefix | v_date_no | '-' | v_new_num;
End if;
RETURN v_new_no;
EXCEPTION
WHEN OTHERS THEN
Dbms_output.put_line (SQLERRM );
END fn_no_make;