Table of Contents
Preface
1. Scenario reproduction
2. How to avoid high concurrency situations where order numbers are not unique
3. The process of generating a unique order number under high concurrency conditions
4. Running results
5. Old rules
Home Database SQL SQL server high concurrency generates unique order number

SQL server high concurrency generates unique order number

Aug 29, 2020 pm 12:01 PM
sql server

Preface

A few days ago I wrote an article on how to generate unique order numbers for MySQL high concurrency , someone sent a private message to ask if there is a SQL server version. Today at noon, I wrote a high-concurrency implementation of the SQL server version to generate a unique order number. In fact, the principles of MySQL and SQL server are the same. The main reason is that some of their syntaxes are somewhat different, so you will find that I The text description of this article is almost the same, only the code and interface are different.

Recommended: "SQL Tutorial"

1. Scenario reproduction

In an ERP purchase and sales system or other systems such as 0A, if there are multiple If people perform the operation of generating order numbers at the same time, it is easy for multiple people to obtain the same order number, causing irreparable losses to the company's business

2. How to avoid high concurrency situations where order numbers are not unique

We can use stored procedures and data tables to build a table and create stored procedures. The stored procedure is responsible for generating order numbers, and the table is responsible for handling uniqueness issues.
SQL server high concurrency generates unique order number
When the stored procedure generates an order Number, first write the order number into the table, and then display the order number result. When writing the generated order number into the table, two situations will occur. Why? Because our table has a primary key (primary key uniqueness)

  • can be written: When there is no same order number in the table, the generated order number is written into the table

  • Cannot be written: When the same order number exists in the table, the generated order number cannot be written to the table, and the order number cannot be obtained, thereby ensuring that a unique order number is generated under high concurrency.

3. The process of generating a unique order number under high concurrency conditions

The following will explain the process of generating a unique order number using code and actual operations
Step 1: Create a data table and set the order number field as the primary key (the key to the unique order number)
SQL server high concurrency generates unique order number
Step 2: Create a stored procedure to generate the order number
The format of the generated order number is: custom prefix year, month and day suffix (001, 002, 003)
1. First create a stored procedure
The parameter is @BILL_TYPE

CREATE  PROCEDURE [dbo].[GetBillNO]
 @BILL_TYPE nvarchar(3)
Copy after login

2. Generate year, month, day and suffix
Year, month and day are the current system time, and the initial value of the suffix is ​​0

declare @BILL_NO nvarchar(15);declare @currentDate nvarchar(15);declare @lastno INT = 0;set @currentDate = Convert(varchar(10),Getdate(),112);
Copy after login

3. Query form , get the order number of the table
Query the table, get the latest order number with the prefix related to the custom content and assign it to @BILL_NO

select @BILL_NO = isnull(BILL_NO,0)
    FROM temp_bill 
    WHERE SUBSTRING(BILL_NO,1,3) =BILL_TYPE and
    SUBSTRING(BILL_NO,4,8) =@currentDate
Copy after login

4. Generate the order number

  • If the order number obtained in the previous step is not empty, the newly generated order number will be 1 on the original order number 1

    Example: Obtained order number: QNB20200615015
    That is, the generated order number is: QNB20200615016

  • If the order number obtained in the previous step is empty, the suffix of the newly generated order number is 001

    Example: Generated order number :QNB20200615001

IF @BILL_NO !='' begin
 SET @lastno = str(right(@BILL_NO, 3)+1) ; 
 set @BILL_NO = @BILL_TYPE+@currentDate+RIGHT('000'+CAST(@lastno as varchar),3);
 end;ELSE  begin
 set @BILL_NO = @BILL_TYPE+@currentDate+CAST('001' as varchar(20));END;
Copy after login

When I did this, since the SQL server did not have the LPAD function (it will automatically complete the number of digits less than the specified number), I used string splicing, and then used right to get the 3 on the right. The target is achieved (¯﹃¯), and it is dead

5. Insert the generated order number into the table
If the same order number exists in the table, the insertion into the table fails
If the same order number does not exist in the table, the insertion into the table is successful

insert into Temp_Bill(BILL_NO,BILL_TYPE) values(@BILL_NO ,@BILL_TYPE )
Copy after login

6. Return the unique order number
When the order is inserted into the table successfully, the unique order number can be returned (if the above If one step is unsuccessful, this step will not be run)

select @BILL_NO as BILL_NO
Copy after login

4. Running results

1. First of all, there is no data in my table, and the prefix will be generated (I entered: UIE) year and month Day (20200615) 001 (the first data, so it is 001)
That is: QNB20200615001

2 When I enter it for the second time, because there is data in the table, it will According to the latest order number, add 1
to the suffix: QNB20200615002
SQL server high concurrency generates unique order number

5. Old rules

I hope this article can help everyone
If you have enough points, just download it. If you don’t have points, type on the picture. The amount of code is not big anyway
If you are a student and don’t have points, you can message me privately and I will send you the source code for free
Code Download link: SQL server_getbillno.bak
Code screenshot:
SQL server high concurrency generates unique order number

The above is the detailed content of SQL server high concurrency generates unique order number. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What software is microsoft sql server? What software is microsoft sql server? Feb 28, 2023 pm 03:00 PM

Microsoft SQL Server is a relational database management system launched by Microsoft. It is a comprehensive database platform that uses integrated business intelligence (BI) tools to provide enterprise-level data management. It is easy to use, has good scalability, and has a high degree of integration with related software. High advantages. The SQL Server database engine provides more secure and reliable storage functions for relational data and structured data, allowing users to build and manage highly available and high-performance data applications for business.

SQL Server or MySQL? New research reveals the best database choices. SQL Server or MySQL? New research reveals the best database choices. Sep 08, 2023 pm 04:34 PM

SQLServer or MySQL? The latest research reveals the best database selection. In recent years, with the rapid development of the Internet and big data, database selection has become an important issue faced by enterprises and developers. Among many databases, SQL Server and MySQL, as the two most common and widely used relational databases, are highly controversial. So, between SQLServer and MySQL, which one should you choose? The latest research sheds light on this problem for us. First, let

How to connect to a Microsoft SQL Server database using PDO How to connect to a Microsoft SQL Server database using PDO Jul 29, 2023 pm 01:49 PM

Introduction to how to use PDO to connect to a Microsoft SQL Server database: PDO (PHPDataObjects) is a unified interface for accessing databases provided by PHP. It provides many advantages, such as implementing an abstraction layer of the database and making it easy to switch between different database types without modifying a large amount of code. This article will introduce how to use PDO to connect to a Microsoft SQL Server database and provide some related code examples. step

PHP and SQL Server database development PHP and SQL Server database development Jun 20, 2023 pm 10:38 PM

With the popularity of the Internet, website and application development has become the main business of many companies and individuals. PHP and SQLServer database are two very important tools. PHP is a server-side scripting language that can be used to develop dynamic websites; SQL Server is a relational database management system developed by Microsoft and has a wide range of application scenarios. In this article, we will discuss the development of PHP and SQL Server, as well as their advantages, disadvantages and application methods. First, let's

SQL Server vs. MySQL: Which database is more suitable for high availability architecture? SQL Server vs. MySQL: Which database is more suitable for high availability architecture? Sep 10, 2023 pm 01:39 PM

SQL Server vs. MySQL: Which database is more suitable for high availability architecture? In today's data-driven world, high availability is one of the necessities for building reliable and stable systems. As the core component of data storage and management, the database's high availability is crucial to the business operation of the enterprise. Among the many databases, SQLServer and MySQL are common choices. So in terms of high availability architecture, which database is more suitable? This article will compare the two and give some suggestions.

A brief analysis of five methods of connecting SQL Server with PHP A brief analysis of five methods of connecting SQL Server with PHP Mar 21, 2023 pm 04:32 PM

In web development, the combination of PHP and MySQL is very common. However, in some cases, we need to connect to other types of databases, such as SQL Server. In this article, we will cover five different ways to connect to SQL Server using PHP.

SQL Server vs. MySQL comparison: Which one is better for large-scale data processing? SQL Server vs. MySQL comparison: Which one is better for large-scale data processing? Sep 09, 2023 am 09:36 AM

SQLServer and MySQL are currently two very popular relational database management systems (RDBMS). They are both powerful tools for storing and managing large-scale data. However, they have some differences in handling large-scale data. This article will compare SQL Server and MySQL, focusing on their suitability for large-scale data processing. First, let us understand the basic characteristics of SQLServer and MySQL. SQLServer is developed by Microsoft

SQL Server and MySQL compete, how to choose the best database solution? SQL Server and MySQL compete, how to choose the best database solution? Sep 10, 2023 am 08:07 AM

With the continuous development of the Internet, database selection has become increasingly important. Among the many databases, SQLServer and MySQL are two high-profile options. SQLServer is a relational database management system developed by Microsoft, while MySQL is an open source relational database management system. So how to choose the best database solution between SQLServer and MySQL? First, we can compare these two databases in terms of performance. SQLServer is processing

See all articles