SQL serial number acquisition code example

SQL serial number acquisition code example

This article mainly introduces the sql serial number acquisition code example. The example code is introduced in great detail in the article, which has a certain reference value for everyone's study or work. Friends in need can refer to it.

It is often used to generate order numbers and other serial numbers. SQL Server implements serial numbers as follows:

Table tb_b_Seq (serial number table):

CREATE TABLE tb_b_Seq( 
Year int, -- year Month int, -- month Attr varchar(50), -- attribute Seq int -- serial number)

Get the serial number through the stored procedure:

create Proc GetSeq ( 
@attr varchar(50),
@year int, 
@month int, 
@Return int output ) 
As 
set @Return=(select top 1 Seq from tb_b_Seq where Attr=@attr and MONTH=@month and YEAR=@year) 
if(@Return is null) 
begin 
 set @Return=1 
 insert into FMDS_tb_b_Seq (Attr,Year,Month,Seq) values ​​(@attr,@year,@month,@Return) 
end 
else 
begin 
 set @Return=@Return+1 
 update FMDS_tb_b_Seq set Seq=@Return where Attr=@attr and MONTH=@month and YEAR=@year 
end

test:

declare @ret int 
exec GetSeq 'Contract',2017,10,@ret 
print @ret

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Mybatis+mysql uses stored procedures to generate serial number implementation code
  • oracle (plsql) generates serial number
  • How to get the query time of MySQL SQL statement in PHP
  • ThinkPHP framework obtains the last executed SQL statement and variable debugging simple operation example
  • MySQL example of getting today and yesterday's 0:00 timestamp
  • mysql gets yesterday's date, today's date, tomorrow's date, and the time of the previous hour and the next hour

<<:  jQuery implements shopping cart function

>>:  Linux /etc/network/interfaces configuration interface method

Recommend

JS implements random roll call system

Use JS to implement a random roll call system for...

Code analysis of user variables in mysql query statements

In the previous article, we introduced the MySQL ...

Example of Vue routing listening to dynamically load the same page

Table of contents Scenario Analysis Development S...

Detailed explanation of the new features of ES9: Async iteration

Table of contents Asynchronous traversal Asynchro...

IIS and APACHE implement HTTP redirection to HTTPS

IIS7 Download the HTTP Rewrite module from Micros...

Summary of several APIs or tips in HTML5 that cannot be missed

In previous blog posts, I have been focusing on so...

11 Linux KDE applications you didn't know about

KDE Abbreviation for Kool Desktop Environment. A ...

Navigation Design and Information Architecture

<br />Most of the time when we talk about na...

Pure CSS to achieve input box placeholder animation and input verification

For more exciting content, please visit https://g...

MySQL 8.0.15 winx64 installation and configuration method graphic tutorial

This article shares the installation and configur...

mysql show simple operation example

This article describes the mysql show operation w...

Analysis of Vue element background authentication process

Preface: Recently, I encountered a management sys...

Detailed instructions for installing mysql5.7 database under centos7.2

The mysql on the server is installed with version...

...