mysql存储过程

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介: 此存储过程实例,主要注意 在存储过程中 if 语句的使用,已经如果存在则不添加 not exists的使用, insert into table_name(column1) select column1 from table_name2 where colunm2=’condition1’ and not exists (select 1 from table_na

此存储过程实例,主要注意 在存储过程中 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)

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
相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
20天前
|
存储 Java 关系型数据库
java调用mysql存储过程
在 Java 中调用 MySQL 存储过程主要借助 JDBC(Java Database Connectivity)。其核心原理是通过 JDBC 与 MySQL 建立连接,调用存储过程并处理结果。具体步骤包括:加载 JDBC 驱动、建立数据库连接、创建 CallableStatement 对象、设置存储过程参数并执行调用。此过程实现了 Java 程序与 MySQL 数据库的高效交互。
|
4月前
|
存储 SQL 关系型数据库
[MySQL]存储过程
本文介绍了存储过程的概念、优点和缺点,并通过具体示例详细讲解了存储过程的创建、使用和调用方法。此外,还介绍了游标的定义和使用,帮助读者更好地理解和应用存储过程。文章旨在为读者提供一个学习和使用存储过程的实用指南。
407 18
[MySQL]存储过程
|
4月前
|
存储 SQL NoSQL
|
5月前
|
存储 SQL 关系型数据库
MySql数据库---存储过程
MySql数据库---存储过程
75 5
|
5月前
|
存储 关系型数据库 MySQL
MySQL 存储过程返回更新前记录
MySQL 存储过程返回更新前记录
113 3
|
5月前
|
存储 SQL 关系型数据库
MySQL 存储过程错误信息不打印在控制台
MySQL 存储过程错误信息不打印在控制台
114 1
|
7月前
|
存储 关系型数据库 MySQL
Mysql表结构同步存储过程(适用于模版表)
Mysql表结构同步存储过程(适用于模版表)
73 0
|
7月前
|
存储 SQL 关系型数据库
MySQL 创建存储过程注意项
MySQL 创建存储过程注意项
70 0
|
8月前
|
存储 SQL 关系型数据库
(十四)全解MySQL之各方位事无巨细的剖析存储过程与触发器!
前面的MySQL系列章节中,一直在反复讲述MySQL一些偏理论、底层的知识,很少有涉及到实用技巧的分享,而在本章中则会阐述MySQL一个特别实用的功能,即MySQL的存储过程和触发器。
155 0
|
9月前
|
存储 SQL 关系型数据库
MySQL数据库进阶第四篇(视图/存储过程/触发器)
MySQL数据库进阶第四篇(视图/存储过程/触发器)