P001-根據編碼規則自動生成ID的函式

ysloong發表於2024-03-07
DROP FUNCTION IF EXISTS dm.generate_dm_data_auto_id(bigint);

CREATE OR REPLACE FUNCTION dm.generate_dm_data_auto_id(num_rule_id bigint)
 RETURNS character varying
 LANGUAGE plpgsql
AS $function$
DECLARE
    maxVal bigint;
    namePre varchar;
    nameSuf varchar;
    numLen integer;
BEGIN
   select name_pre,name_suf,num_len,nextval(concat('dm_data.seq_',num_rule_id)) into namePre,nameSuf,numLen,maxVal
   FROM dm_id_num_rule where id_num_rule_id = num_rule_id;

  RETURN concat(namePre,LPAD(maxVal::varchar ,numLen,'0'),nameSuf);
END;
$function$
;

日期:2024-03-07

相關文章