MySql根據配置實現資料表唯一編號自動生成
阿新 • • 發佈:2019-02-14
建立配置表:
CREATE TABLE `sys_seq_ctrl` (
`seq_type` varchar(50) NOT NULL COMMENT '型別',`seq_title` varchar(50) NOT NULL COMMENT '描述',
`seq_by` char(1) NOT NULL COMMENT '編號產生方式:S順序號,Y按年,M按年月,D按年月日,W按年周',
`seq_prefix` varchar(5) NOT NULL COMMENT '編碼字首',
`seq_midfix` varchar(10) NOT NULL COMMENT '編碼中綴',
`seq_year_len` tinyint(4) NOT NULL COMMENT '年顯示長度',
`seq_seq` int(11) NOT NULL COMMENT '序號',
`seq_len` tinyint(4) NOT NULL COMMENT '序號顯示的最短長度',
`seq_max_len` tinyint(4) NOT NULL COMMENT '編碼長度',
`seq_suffix` varchar(50) NOT NULL COMMENT '編碼字尾',
`seq_mtable` varchar(50) NOT NULL COMMENT '資料表名稱',
`seq_mkey` varchar(50) NOT NULL COMMENT '資料表主鍵欄位名稱', -- 可忽略
`seq_mseq` varchar(50) DEFAULT NULL COMMENT '資料表狀態名稱', -- 可忽略
`seq_mstatus` varchar(50) DEFAULT NULL COMMENT '資料表狀態描述名稱', -- 可忽略
PRIMARY KEY (`seq_type`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系統編號生成規則';
----------------------------------------------------------------------
儲存過程
CREATE PROCEDURE `p_getNbr`(in type varchar(50),out nbr varchar(50),out iret int)
leave_return: -- 定義跳出儲存過程標記
begin
declare flag char(1);
declare prefix char(5);
declare suffix char(5);
declare midfix char(10);
declare yearlen int;
declare maxlen int;
declare seqlen int;
declare seqtable char(50);
declare currMidfix char(10);
declare seqmonth char(2);
declare ww char(2);
declare seq int;
select seq_mtable,seq_by,seq_prefix,seq_len,seq_midfix,seq_year_len,seq_seq,seq_suffix,seq_max_len INTO
seqtable,flag,prefix,seqlen,midfix,yearlen,seq,suffix,maxlen from sys_seq_ctrl where seq_type = type;
if seqtable is null then
set nbr='';
set iret = 70;
LEAVE leave_return;
end if;
if not EXISTS( SELECT table_name FROM information_schema.TABLES WHERE table_name =seqtable) then
set iret = 72;
LEAVE leave_return;
end if;
if flag = '' then
set flag = 'S';
end if;
if flag = 'S' then
set currMidFix = '';
end if;
if yearlen = 0 then
set yearlen = 2 ;
end if;
if UCASE(flag) = 'Y' then
set currMidFix = RIGHT( year( CURRENT_DATE ( ) ),yearlen);
end if;
if UCASE(flag) = 'M' then
set currMidFix = CONCAT(RIGHT(year( CURRENT_DATE( )),yearlen),
right( LPAD(month( CURRENT_DATE( )),2,'0'),2));
end if;
if UCASE(flag)= 'D' then
set currMidFix = CONCAT(RIGHT(year( CURRENT_DATE( )),yearlen),
right( LPAD(month( CURRENT_DATE( )),2,'0'),2),
right( LPAD(day( CURRENT_DATE( )),2,'0'),2));
end if;
if UCASE(flag) = 'W' then
set currMidFix =CONCAT(RIGHT(year( CURRENT_DATE( )),yearlen),
right( LPAD(weekofyear( CURRENT_DATE( )),2,'0'),2));
end if;
if seq is null THEN
set seq = 0;
end if;
if currMidFix <> midfix then
set seq = 0;
end if;
set seq = seq + 1;
START TRANSACTION;
update sys_seq_ctrl set seq_midfix = currMidFix,seq_seq = seq where seq_type = type;
if(row_count()<=0) THEN -- row_count()
rollback;
set iret = 71;
LEAVE leave_return;
end if;
if LENGTH (RTRIM(LTRIM(seq))) <= seqlen then
set nbr =CONCAT( ltrim(rtrim(prefix)), ltrim(rtrim(currMidFix)),LPAD(seq,seqlen,'0'),ltrim(rtrim(suffix)));
else
set nbr =CONCAT( ltrim(rtrim(prefix)), ltrim(rtrim(currMidFix)), rtrim(ltrim(seq)),ltrim(rtrim(suffix)));
end if;
if LENGTH(ltrim(rtrim(nbr))) > maxlen then
rollback;
set iret = 73;
LEAVE leave_return;
end if;
commit;
set iret = 0;
end