MySQL Generate serial number

Source: Internet
Author: User

Basic Rules Table Rul_sequence:

Data in the Rules table:

Secondary stored procedures:

CREATEDefiner=' Root ' @ '%`PROCEDURE' Proc_getseqence ' (inch' Seqcode 'VARCHAR( -), out ' Returnnum 'VARCHAR( +), out ' Messagecode 'VARCHAR( -)) COMMENT'Get serial number'BEGINDeclareSeqnownumstrVARCHAR( -);DeclareSeqnownumINT;DeclareNowyearCHAR(4);DeclareNowmonthCHAR(2);DeclareNowdayCHAR(2);DeclareNowlengthINT;DeclareDataFormatVARCHAR( -);DeclareInivalueINT;DeclareResettypeVARCHAR(Ten);DeclareLastdateCHAR(8);DeclareWorkflowstrVARCHAR( -);DeclareDatanowCHAR(8);DeclareIINT; /*Initialize Variables*/     SETMessagecode='999'; SETReturnnum= '0'; SETNowlength=0; SETSeqnownum=0; SETDatanow=Date_format (now (),'%y%m%d'); SETNowyear=SUBSTRING(Datanow,1,4); SETNowmonth=SUBSTRING(Datanow,5,2); SETNowday=SUBSTRING(Datanow,7,2); SetI=1; SelectValue_length,now_seqvalue,date_max,data_format,reset_type,init_value intoNowlength,seqnownum,lastdate,dataformat,resettype,inivalue fromRul_sequencewhereSeq_code=Seqcode; UpdateRul_sequenceSetIs_running='2' whereSeq_code=Seqcode andIs_running='1';If(Resettype=2  andDatanow<>Lastdate andInivalue>0)         OR(Resettype=3  andConcat (Nowyear,nowmonth)<>SUBSTR (Lastdate,1,6) andInivalue>0)        OR(Resettype=4  andNowyear<>SUBSTR (Lastdate,1,4) andInivalue>0) Then           SetSeqnownum=Inivalue;End if; SetI=Nowlength;--I represent the total length of the serial number at this timeSetWorkflowstr='<'; whileNowlength>0 DoSetWorkflowstr=Concat (WORKFLOWSTR,'X'); SetNowlength=Nowlength-1; End  while; SetWorkflowstr=Concat (WORKFLOWSTR,'>'); /*********** code format end*******/    SetSeqnownumstr=Concat (Seqnownum,"'); SetNowlength=I-length (SEQNOWNUMSTR); /*********** complement 0 operation start*******/                 whileNowlength>0 DoSetSeqnownumstr=Concat'0', SEQNOWNUMSTR); SetNowlength=Nowlength-1; End  while; /*********** complement 0 operation end*******/      SetReturnnum=REPLACE(DataFormat,'<YYYY>', nowyear);--Replace the rule <YYYY> with the corresponding year    SetReturnnum=REPLACE(Returnnum,'<MM>', Nowmonth);--Replace the <MM> in the rule with the corresponding month    SetReturnnum=REPLACE(Returnnum,'<DD>', Nowday);--Replace the <DD> in the rule with the corresponding day    SetReturnnum=REPLACE(RETURNNUM,WORKFLOWSTR,SEQNOWNUMSTR);--replace the shape in the rule with the corresponding serial number, such as <XXX>          /*********** Updates the current pipelining value to the maximum serial number, the last serial number generation time, and the run tag (run tag is set to "1" (not running)) start*******/     UPDATERul_sequenceSETNow_seqvalue=Seqnownum+1, Date_max=Datanow,is_running='1', Edit_time=Now ()WHEREIs_running='2'  andSeq_code=Seqcode; Commit; /*********** Updates the current pipelining value to the maximum serial number, the last serial number generation time, and the run tag (run tag is set to "1" (not running)) end*******/--set Messagecode =concat (' No this number sequence ', messagecode);END

MySQL Generate serial number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.