SQL serial number

Source: Internet
Author: User

Effect: The serial number consists of three parts: "serial number Header" + "date" + "generated serial number"
The serial number is generated every day for one cycle, that is, the serial number of each day starts from 0.
Design Principle: I use a special table to record the relevant information of the serial number, such as the serial number header, the serial number name, and the serial number of the current serial number, as follows:

Create table tb_BH <br/> (<br/> [ID] int identity (1, 1), <br/> MingChen nvarchar (50 ), -- Name of the numbered table <br/> TouBu char (2) not null, -- header of the sequential number <br/> RiQi datetime default getdate (), -- save the current date <br/> XuHao int default 0, -- maximum value of the current date serial number <br/> MiaoShu nvarchar (100), <br/> C_Tb nvarchar (50 ), -- table corresponding to the current serial number <br/> C_Tb_Field nvarchar (50), -- field of the table corresponding to the current serial number <br/> primary key (TouBu) <br/>) <br/> Go <br/>
 

Entity table corresponding to the numbered table:
Create table demo (bh char (17) primary key (bh )) <br/> insert demo select 'cc20100429000028' <br/> union all select 'cc20100429000029' <br/> union all select 'cc20100429000030' <br/> union all select 'cc20100429000031' <br/>

If tb_BH saves the same date as the current date, it reads data from XuHao of tb_BH. If the read date is earlier than the current date, it is obtained from the corresponding table, for example, obtain the maximum value of the specified date serial number from the demo table.
 Create proc pro_bh <br/> @ toubu nvarchar (2), <br/> @ riqi datetime = NULL, <br/> @ BH char (17) = Null Output <br/> as <br/> If @ riqi is null set @ riqi = convert (char (8), getdate (), 112) <br/> begin Tran <br/> declare @ s nvarchar (4000) <br/> declare @ c_tb_field varchar (50) <br/> declare @ c_tb varchar (50) <br/> Update tb_bh with (rowlock) set <br/> @ bH = case when convert (char (8), riqi, 112)> @ riqi then '0' <br/> else toubu + convert (char (8), getdate (), 112) + right ('100' + Cast (xuhao as nvarchar ), 6) End, <br/> xuhao = case when convert (char (8), riqi, 112) <@ riqi then 0 <br/> when convert (char (8 ), riqi, 112) = @ riqi then xuhao + 1 end, <br/> riqi = getdate (), <br/> @ c_tb_field = c_tb_field, <br/> @ c_tb = c_tb <br/> where toubu = @ toubu <br/> If @ bH = '0' <br/> begin <br/> select @ s = n' select top 1 @ bH = '+ @ c_tb_field + 'from' + @ c_tb +' with (xlock, paglock) Where '+ @ c_tb_field + 'like ''' + @ toubu + convert (char (8), @ riqi, 112) + '% ''order by' + @ c_tb_field + 'desc' <br/> exec sp_executesql @ s, n' @ BH char (17) output ', @ BH output <br/> set @ bH = left (@ BH, 10) + right ('000000' + Cast (cast (right (@ BH, 7) as INT) + 1 as nvarchar), 6) <br/> end <br/> Print @ BH <br/> commit Tran <br/> go <br/>

Next, perform the test in two ways:
1. Obtain the current ID: exec Pro_BH 'cc '. Read from the numbered table

2. Get the serial number of the specified date: exec Pro_BH 'cc', '2017-4-29 '. In this case, obtain from the inserted Table:

Download: 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.