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;
  • 相关阅读:
    [BZOJ1934][Shoi2007]Vote 善意的投票[最小割]
    [BZOJ1066][SCOI2007]蜥蜴[最大流]
    [BZOJ2818][P2568]Gcd[欧拉函数]
    [BZOJ2208][P4306][JSOI2010]连通数[bitset优化floyd]
    [BZOJ1877][SDOI2009]晨跑[最大流+费用流]
    [BZOJ1040][P2607][ZJOI2008]骑士[树形DP+基环树]
    [BZOJ5347]冒泡排序[思维]
    [BZOJ2875][Noi2012]随机数生成器[等比数列求和+取模]
    [bzoj2809] 派遣
    [bzoj1965] 洗牌
  • 原文地址:https://www.cnblogs.com/Brambling/p/9259543.html
Copyright © 2011-2022 走看看