zoukankan      html  css  js  c++  java
  • MySQL存储过程

    一、查看存储过程

    -- 显示所有数据库中所有存储过程的基本信息,如所属数据库、存储过程名、创建时间等
    show procedure status;
    
    -- 显示指定数据库中所有存储过程的基本信息,如 Demo 数据库
    show procedure status where db='Demo';

    二、创建存储过程

    -- 定义结束符为“$$”,mysql默认结束符为“;”
    -- 意思是告诉mysql解释器,该段命令是否已经结束了,即标识一段命令起始和结束
    delimiter $$
    
    -- 创建存储过程
    -- sp_char_split_inser:存储过程名称
    -- strs:存储过程参数名称
    -- in:表示该参数为输入参数;out:表示该参数为输出参数;inout:表示该参数为输入输出参数。不写时默认为in,即输入参数。
    create procedure sp_char_split_inser(in strs text)
    begin 
        declare i int default 0;
        declare leng int default 0;
        declare word char(1);
        
        -- 判断字符串是否为空或空字符串
        if(strs is not null && strs <> '') then 
            -- 获取字符串长度
            set leng = char_length(strs);
            -- 循环
            while i < leng do 
                -- 获取第一个字符
                set word=left(strs,1);
                if(word is not null && word <> '') then 
                    -- 判断该条数据是否存在
                    if not exists(select 1 from demo.charinfo where Hanzi=word limit 1) then 
                        -- 插入数据
                        insert into demo.charinfo(Hanzi) values(word);
                    end if;
                end if;
                -- 截取除第一个字符之外的所有字符
                set strs=substring(strs,2);
                set i=i+1;
            end while;
        end if;
    end;
    -- 命令结束
    $$
    delimiter ;

    三、调用存储过程

    -- 调用存储过程
    set @s='测试文字';
    call sp_char_split_inser(@s);
    call sp_char_split_inser('测试一下');

    四、删除存储过程

    -- 删除存储过程
    drop procedure sp_char_split_inser;
    drop procedure if exists sp_char_split_inser;
  • 相关阅读:
    记录慕课学习爬取中国大学排名(由上交大计算的排名结果)
    SuperMap iMobile for Android室内导航APP
    Android studio入坑记录(SuperMap iMobile开发)
    2019年的十月和十一月
    python学习国庆期间
    学习python——collections系列
    又是快乐学习python的一天
    学习MATLAB
    Python学习练习题
    使用javaScript来实现一个有序链表
  • 原文地址:https://www.cnblogs.com/Brambling/p/9259543.html
Copyright © 2011-2022 走看看