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

Detailed explanation of the difference between CSS link and @import

How to add css in html? There are three ways to s...

Recommended 20 best free English handwriting fonts

Jellyka BeesAntique Handwriting [ank]* Jellyka Cut...

JS realizes the automatic playback effect of pictures

This article shares the specific code of JS to ac...

Pricing table implemented with CSS3

Result: Implementation Code html <div id="...

How to quickly log in to MySQL database without password under Shell

background When we want to log in to the MySQL da...

Component design specifications for WeChat mini-program development

WeChat Mini Program Component Design Specificatio...

Complete steps to use element in vue3.0

Preface: Use the element framework in vue3.0, bec...

How to change the MySQL database file directory in Ubuntu

Preface The company's Ubuntu server places th...

Implementation of Docker container connection and communication

Port mapping is not the only way to connect Docke...

Summary of commonly used SQL statements for creating MySQL tables

Recently, I have been working on a project and ne...

Detailed steps to install Docker mongoDB 4.2.1 and collect springboot logs

1: Install mongodb in docker Step 1: Install mong...

MySQL transaction autocommit automatic commit operation

The default operating mode of MySQL is autocommit...

Things You Don’t Know About the CSS ::before and ::after Pseudo-Elements

CSS has two pseudo-classes that are not commonly ...

JavaScript to achieve all or reverse selection function

This article shares the specific code of JavaScri...