The stored procedure realizes the order number, the detailed thought of the water series (8 digits) _mssql

Source: Internet
Author: User
This article is to learn from the garden of the great God, I just slightly modified. There is a bad place, welcome to spit Groove.
write the implementation idea first and then post the code
1. Set up stored procedure, declare output parameter @indentnum, output order number
2. The general order number consists of two parts, the date and the subsequent order number, according to the need to see the date after the number of digits, this article is 8.
3. Define function @date As date part, assign value.
4. The existence of the day's data in the table, according to the date of the query, the General order form will have a date.
5. If the table exists, the largest order in its table is +1, and if not, add the first order number of the day.
Note: It uses a function replicate that is not normally used, because it needs to be up to 0 of the highest order at +1.
Grammar: Print replicate (' 0 ', 3) ———— output 3 0
Copy Code code as follows:

Use Mybookshop
Go
--Delete If there is a stored procedure
IF object_id (' proc_numindent ') is not NULL
DROP PROC proc_numindent
Go
--Create a new stored procedure for the production of serial numbers
CREATE PROC proc_numindent
@indentNum nvarchar (m) output--water waybill
As
--The date part is the current date.
DECLARE @date nvarchar (20)
Set @date = CONVERT (varchar), GETDATE (), 112)--format 20130117
--Determine if the data for the day is present in the table
DECLARE @CountMax nvarchar (20)
Select @CountMax = max (id) from TESTST where convert (varchar), RTrim (LTrim (date)) = CONVERT (varchar (), RTrim (LTrim ( GETDATE ()))
--If the @countmax is not equal to NULL, then the data for the day in the table
IF (@CountMax <> ")
BEGIN
declare @num nvarchar (20)
--on the largest order taken today, take the rightmost (back) 8 digits to int type plus one
Set @num = CONVERT (varchar), CONVERT (Int,right (@CountMax, 8)) +1)
--Using the Replicate function, repeat the assignment ' 0 ', to fill up the high
Set @num = Replicate (' 0 ', 8-len (@num)) + @num
Set @indentNum = @date + @num
Print @indentNum
End
--No existing on date plus ' 00000001 ' for today's first order
ELSE
BEGIN
Set @indentNum = @date + ' 00000001 '
PRINT @indentNum
End
Go
--The following is the execution of the stored procedure
DECLARE @indentNum nvarchar (20)
EXEC proc_numindent @indentNum

There is also hope that the great God can provide a better way to compare dates.
is to determine whether 2 dates are equal, the database is datatime format, but the judgment of the comparison of eggs pain.
I also want to take the date part and not take the time part to judge. I hope everyone will come and spit in the groove.

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.