DROP PROCEDURE IF EXISTS pro_getNewCG;-- 如果存在,删除名为 pro_getNewCG 的存储过程
CREATE PROCEDURE pro_getNewCG (OUT id VARCHAR(20)) -- 创建存储过程 名为:pro_getNewCG 带一个输出参数 id
BEGIN -- 固定格式 以 BEGIN 开始 ,END 结束
DECLARE taday VARCHAR (20);-- 申明变量 格式: Declare 变量名 变量类型
DECLARE counts INT;
DECLARE adds INT;
DECLARE maxx VARCHAR (20);
SET taday = DATE_FORMAT(NOW(), '%Y%m%d');-- 给变量赋值 格式 Set = 值
SELECT count(*) INTO counts FROM Stock WHERE substring(sId, 3, 8) = taday; -- 改变变量值 格式 SELECT 结果 INTO 变量名 FROM 表 WHERE 条件
IF (counts > 0) THEN SELECT max( CONVERT (substring(sId, 3, 12), signed)) INTO maxx FROM Stock WHERE substring(sId, 3, 8) = taday;
SET maxx = substring(maxx, 9, LENGTH(maxx) - 8);
IF (maxx = '') THEN SET adds = 1; -- 判断 格式 IF ELSE END IF
ELSE SET adds = CAST(maxx AS UNSIGNED INTEGER) + 1;
END IF;
SET maxx = CONCAT(taday, adds);
SET id = CONCAT('CG', taday);
SET id = CONCAT('CG', CONVERT(maxx, signed));
ELSE SET id = CONCAT('CG', taday);
END IF;
END;