Skip to content

Instantly share code, notes, and snippets.

@qlong8807
Created October 17, 2019 07:03
Show Gist options
  • Save qlong8807/6887443991a59ab571ee5f9fe5a5ce3b to your computer and use it in GitHub Desktop.
Save qlong8807/6887443991a59ab571ee5f9fe5a5ce3b to your computer and use it in GitHub Desktop.
时间+4位随机数
使用Oracle函数生成主键,时间+4位随机
CREATE OR REPLACE PACKAGE BODY sys_helper_pkg IS
FUNCTION get_key_id RETURN INTEGER IS
v_key_id INTEGER;
v_unix_timestamp INTEGER;
v_random_value CHAR(4);
BEGIN
v_unix_timestamp := (SYSDATE - to_date('2019-10-01', 'yyyy-mm-dd')) *
86400;
v_random_value := lpad(seq_random_value.nextval, 4, 0);
v_key_id := to_number(v_unix_timestamp || v_random_value);
RETURN v_key_id;
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20003, SQLERRM);
END get_key_id;
END abims_helper_pkg;
使用方式:
select sys_helper_pkg.get_key_id from dual;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment