Initial contact for MySQL database stored procedure

Source: Internet
Author: User
Tags time and seconds

have been engaged in Oracle, after coming to the new company, although it feels similar to MySQL, but MySQL is still very inconvenient, such as the following functions do not support TURNC such a simple function.

A stored procedure that is intended to write an order number, similar to the letter + Month Day number

BEGIN
DECLARE currentdate VarCHAR (15);--current date, may contain time and seconds
DECLARE maxno INT DEFAULT 0; --the last 5 digits of the serial number of the order number of the nearest satisfying condition, such as: SH2013011000002 's maxno=2

SELECT Date_format (now (), '%y%m%d ') into currentdate;
Select MAX (substring (t.order_no,12,4)) into Maxno from Mmd_po_order t where substring (t.order_no,4,8) =date_format (now ( ), '%y%m%d ');
If maxno!= ' Then
SELECT CONCAT (' MMD ', currentdate, Lpad ((maxno + 1), 4, ' 0 ')) into OrderNo; --Lpad ((maxno + 1), 5, ' 0 '): if less than 5 bits, the left is filled with 0
ELSE
SELECT CONCAT (' MMD ', currentdate, ' 0001 ') into OrderNo;
END IF;

Select OrderNo;

COMMIT;
END

Initial contact for MySQL database stored procedure

Related Article

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.