Skip to content

Commit 8ac6c31

Browse files
committed
sql生成序列号
1 parent c81baee commit 8ac6c31

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
存储过程,订单号:
2+
DECLARE v_cnt INT;
3+
DECLARE v_timestr INT;
4+
DECLARE rowcount BIGINT;
5+
SET v_timestr = DATE_FORMAT(NOW(),'%Y%m%d');
6+
SELECT ROUND(RAND()*100,0)+1 INTO v_cnt;
7+
START TRANSACTION;
8+
UPDATE order_seq SET order_sn = order_sn + v_cnt WHERE timestr = v_timestr;
9+
IF ROW_COUNT() = 0 THEN
10+
INSERT INTO order_seq(timestr,order_sn) VALUES(v_timestr,v_cnt);
11+
END IF;
12+
SELECT CONCAT(v_timestr,LPAD(order_sn,7,0))AS order_sn
13+
FROM order_seq WHERE timestr = v_timestr;
14+
COMMIT;
15+
16+
知识点:
17+
1、在sql语句中添加变量。
18+
declare @local_variable data_type
19+
声明时需要指定变量的类型,可以使用SET、SELECT、SELECT...INTO对变量进行赋值,
20+
在sql语句中就可以使用@local_variable来调用变量。
21+
2、RAND()返回一个介于 0 到 1(不包括 0 和 1)之间的伪随机 float 值。
22+
3、事务
23+
4、ROW_COUNT()函数返回查询语句执行后,被影响的列数目
24+
5、IF...THEN...END IF;

0 commit comments

Comments
 (0)