File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change
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;
You can’t perform that action at this time.
0 commit comments