CREATE TABLE `tb_key_sequence` ( `seq_name` varchar(3) NOT NULL COMMENT '시퀀스 prefix', `seq_no` int NOT NULL COMMENT '시퀀스 no', PRIMARY KEY (`seq_name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='tb_key_sequence'; CREATE FUNCTION `fn_get_key_seq`(`p_seq_name` VARCHAR(3), `p_key_length` int) RETURNS varchar(20) CHARSET utf8 BEGIN DECLARE RTN_VAL VARCHAR(20); INSERT INTO tb_key_sequence (seq_name, seq_no) values (p_seq_name, LAST_INSERT_ID(1)) ON DUPLICATE KEY UPDATE seq_no=LAST_INSERT_ID(seq_no+1); set @ret = row_count(); if @ret = 0 then set RTN_VAL = '0'; else SET RTN_VAL = (SELECT TRIM(CONCAT(p_seq_name, LPAD(LAST_INSERT_ID(),p_key_length,'0')))); end if; RETURN RTN_VAL; END ;