mysql存储过程
2015-08-12 00:00
405 查看
此存储过程实例,主要注意 在存储过程中 if 语句的使用,已经如果存在则不添加 not exists的使用,
insert into table_name(column1) select column1 from table_name2
where colunm2=’condition1’
and not exists (select 1 from table_name2 where table_name2.column1=table_name.column1)
insert into table_name(column1) select column1 from table_name2
where colunm2=’condition1’
and not exists (select 1 from table_name2 where table_name2.column1=table_name.column1)
DELIMITER $$ CREATE PROCEDURE `sp_create_empty_account`( p_cellphone varchar(20), p_channel_code varchar(50), out p_return varchar(100) ) BEGIN declare v_user_id ,v_account_id,v_channel_id,v_channel_account_id bigint ; declare v_is_main_channel bit; DECLARE exit HANDLER FOR SQLEXCEPTION begin ROLLBACK; set p_return='fail'; end; set p_return=""; select c_channel_id,c_is_default into v_channel_id,v_is_main_channel from t_channel where c_code=p_channel_code and c_type=0 and c_status=1; if (v_channel_id>0 && exists (select 1 from t_user where c_cellphone =p_cellphone)) then select c_user_id into v_user_id from t_user where c_cellphone=p_cellphone; ** set autocommit=0; ** start transaction; INSERT INTO t_account(`c_user_id`) select v_user_id from dual where not exists (select 1 from t_account where c_user_id=v_user_id); select c_account_id into v_account_id from t_account where c_user_id=v_user_id; if (v_account_id>0 && not exists (select 1 from t_channel_account where c_account_id=v_account_id and c_channel_id=v_channel_id)) then insert into t_channel_account (c_account_id,c_channel_id,c_is_active) values(v_account_id,v_channel_id,v_is_main_channel); if v_is_main_channel=b'1' then select c_channel_account_id into v_channel_account_id from t_channel_account where c_account_id=v_account_id and c_is_active=b'1'; update t_account set c_channel_account_id=v_channel_account_id where c_account_id=v_account_id; end if; end if; commit; set p_return="success"; else set p_return="not found user or channel"; end if; END
相关文章推荐
- mybatis数据批量插入
- 让Gitlab的CE版本,使用mysql数据库
- MySQL运行状态show status中文详解
- Mysql命令大全
- Mysql当中的on和where的区别
- Ubuntu下忘记MySQL root密码解决方法
- Mysql动态打开日志 show profiles详细信息
- ubuntu mysql 备份 卸载 升级
- 进行mysql压力测试需要注意的几点
- mysql show grants查看权限
- mysql命令
- 【MySql】使用记录<五>
- MySQL 数据库ut8mb4字符集
- 5步轻松搞定windows系统下mysql命令行操作,完成简单mysql配置
- MySQL的分页
- MySQL建议列属性尽量为NOT NULL
- MySQL数据库事务隔离级别(Transaction Isolation Level)
- Unity3D 连接MySQL数据库笔记4-Unity3d代码
- MYSQL隔离级别介绍
- what-to-tune-in-mysql-56-after-installation.html