SQL sequential account generation

Source: Internet
Author: User
1 -- The followingCodeGenerate an 8-digit serial number. The serial number starts with BH and the remaining six digits are serial numbers.
2 -- Function with the new number
3 Create   Function F_nextbh ()
4 Returns   Char ( 8 )
5 As
6 Begin
7 Return ( Select   ' BH ' + Right ( 1000001 + Isnull ( Right ( Max (BH ), 6 ), 0 ), 6 ) From TB With (Xlock, paglock ))
8 End
9 Go
10
11 -- Apply functions in the table
12 Create   Table TB (
13 BH Char ( 8 ) Primary   Key   Default DBO. f_nextbh (),
14 Col Int )
15
16 -- Insert data
17 Begin   Tran
18 Insert TB (COL) Values ( 1 )
19 Insert TB (COL) Values ( 2 )
20 Insert TB (COL) Values ( 3 )
21 Delete TB Where Col = 3
22 Insert TB (COL) Values ( 4 )
23 Insert TB (BH, col) Values (DBO. f_nextbh (), 14 )
24 Commit   Tran
25
26 -- Display result
27 Select   *   From TB
28 /* -- Result
29 BH col
30 ---------------------------
31 Bh120001 1
32 Bh120002 2
33 Bh120003 4
34 Bh000004 14
35 -- */

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.