1、在触发器中调用存储过程
Attention:
a、进行update的时候,不能修改触发表(
sys_user
)的数据。
b、
innodb引擎上的数据,触发器sql执行sql失败会进行回滚
c、
mysql触发器里不能包含动态sql语句,PREPARE之类的,这个在创建时一般会提示
mysql触发器里不能包含动态sql语句,PREPARE之类的,这个在创建时一般会提示
DROP TRIGGER IF EXISTS `user_master`;
CREATE TRIGGER `user_master` BEFORE UPDATE ON `sys_user`
FOR EACH ROW
BEGIN
DECLARE _user_type CHAR(20);
DECLARE _type_infos_id char(20);
SET _user_type = '1';
IF new.ADVER_ID = NULL THEN
SET _user_type = '0';
END IF;
CALL proc1(_user_type);
set _type_infos_id = proc2();
INSERT INTO user_master_info (USERID,TYPE_ID,CREATE_TIME) VALUES (new.USER_ID,_type_infos_id,NOW());
END;
2、在存储过程生成视图
Attention:
a、
存储过程的参数如果是输入中文的话,要在定义存储过程的后面加上character set gbk这个编码,不然调用存储过程使用中文参数的时候会出错。eg:
create procedure countpro(out a_out int,in b_date date, in unit_name varchar(45) character set gbk)
DROP PROCEDURE IF EXISTS `proc1`;
CREATE PROCEDURE `proc1`(IN type_m CHAR)
BEGIN
DROP VIEW IF EXISTS view_master_type;
SET @sqlstr = 'CREATE VIEW view_master_type as';
SET @sqlstr = CONCAT(@sqlstr , 'SELECT * FROM master_type_info WHERE TYPE_MODE = ', type_m);
PREPARE stmt FROM @sqlstr;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END;
3、通过游标遍历视图筛选出来的集合,并返回值
Attention:
a、
游标只能向前读取
DROP PROCEDURE IF EXISTS `proc2`;
CREATE PROCEDURE `proc2`(OUT _type_ids CHAR)
BEGIN
DECLARE _b char(20);
DECLARE _type_infos CHAR(20);
DECLARE _found boolean DEFAULT true;
DECLARE _num CURSOR for SELECT TYPE_ID FROM view_master_type;
DECLARE CONTINUE handler for NOT FOUND set _found = false;
FETCH _num into _b;
while _found do
set _type_infos = CONCAT(_b,',',_type_infos);
FETCH _num into _b;
END WHILE;
CLOSE _num;
set _type_ids = _type_infos;
END;