MySQL 存储过程简单demo

2018-03-01 11:15:19来源:oschina作者:AmosWang人点击

分享
一、基础数据保存在以下两张表中
1、要校验的记录表
CREATE TABLE `record` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '用户 ID',
`MEMBER_ID` bigint(20) DEFAULT '10000' COMMENT '会员编号',
`AAA_COUNT` bigint(10) DEFAULT NULL COMMENT 'AAA 数量',
`BBB_COUNT` bigint(10) DEFAULT NULL COMMENT 'BBB 数量',
`CREATE_TIME` datetime DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;2、校验后的结果表
CREATE TABLE `policy_result` (
`ID` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增ID',
`MEMBER_ID` bigint(20) NOT NULL COMMENT '会员编号',
`RULE_CODE` varchar(32) DEFAULT NULL COMMENT '命中规则代码',
`DETAIL_MESSAGE` varchar(512) DEFAULT NULL COMMENT '具体原因',
`COMPANY` varchar(32) DEFAULT NULL COMMENT '哪个资信局',
`CREATE_TIME` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='申请命中策略表';二、存储过程
BEGIN
-- aaa 数量
DECLARE aaaCount INT DEFAULT 0;
-- bbb 数量
DECLARE bbbCount INT DEFAULT 0;
-- 临时变量
DECLARE count INT DEFAULT 0;
-- 固定变量
DECLARE company VARCHAR (64) DEFAULT 'HELLO';
-- 去掉update安全模式
SET SQL_SAFE_UPDATES = 0;hello_verify_count : BEGIN
-- 查询record主表
SELECT count(*) INTO count FROM record WHERE member_id = memberId;
-- 如果成功只有一条数据,则开始校验规则
IF count IS NOT NULL AND count >= 1 THEN SELECT aaa_count, bbb_count INTO aaaCount, bbbCount FROM record
WHERE member_id = memberId ORDER BY create_time DESC LIMIT 1; IF aaaCount >= 10 THEN
SET resultCode = 'F1';
SET resultReason = concat('AAA COUNT 大于等于10, 实际COUNT ', aaaCount);
INSERT INTO policy_result ( member_id, rule_code, detail_message, company )
VALUES ( memberId, resultCode, resultReason, company );
END IF; IF bbbCount >= 10 THEN
SET resultCode = 'F2';
SET resultReason = concat('BBB COUNT 大于等于10, 实际COUNT ', bbbCount);
INSERT INTO policy_result ( member_id, rule_code, detail_message, company )
VALUES ( memberId, resultCode, resultReason, company );
END IF; END IF;END hello_verify_count;END
三、存储过程传入的参数,以及返回的参数
IN `memberId` varchar(128),OUT `resultCode` varchar(64),OUT `resultReason` varchar(128)
四、执行存储过程,获得返回结果
-- 参数
-- IN `memberId` varchar(128),OUT `resultCode` varchar(64),OUT `resultReason` varchar(128)
-- 数据库名.存储过程名 (memberId, resultCode, resultReason)
CALL boot.hello (10000 ,@resultCode ,@resultReason);
SELECT
@resultCode,
@resultReason;

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台