mysql 存储过程 和存储函数

存储过程

delimiter //

drop procedure if exists reku_2_search_charActionLog_noLogin_count; -- 如果存在则删除
create definer `pay_center`@`localhost` procedure reku_2_search_charActionLog_noLogin_count(start_date varchar(20),end_date varchar(20)) --definer `pay_center`@`localhost` 相当于只 允许本机的pay_center权限的用户创建
begin lable_exit:  -- 这儿可以理解为开启跳转
begin
if start_date is not null and end_date is not null then -- 如果 start_date  和end_date  参数不为空的话  定义用户变量,全局的
    set @start_date =  unix_timestamp(start_date);  此函数 相当于把具体时间 转换为时间戳  和php中的 strtotime()一个意思
    set @end_date =  unix_timestamp(end_date);
else
    if start_date is  null then
    set @start_date = unix_timestamp(date_format(current_timestamp(),'%Y-%m-%d 00:00:00'));
    set @end_date =  unix_timestamp(end_date);
    end if;
    if end_date is  null then
    set @start_date =  unix_timestamp(start_date);
    set @end_date   = unix_timestamp(date_format(current_timestamp(),'%Y-%m-%d 23:59:59'));
    end if;
end if;
    
if @start_date - @end_date > 0 then
 select '时间不对';
 leave lable_exit;
end if;
set @query = 'select count(*) as cnt from game_forbidinfo where phpTime >=? and phpTime <=?';
prepare stmt_sql_str from @query; --预定义sql
execute stmt_sql_str using @start_date,@end_date;   执行预定义同时 按照顺序 给? 赋值

end lable_exit;  跳出
end;


drop procedure if exists reku_2_search_charActionLog_noLogin_res;
create procedure reku_2_search_charActionLog_noLogin_res(start_date varchar(20),end_date varchar(20),limits varchar(40))
begin
set @limit = limits;
set @query = "select gf.*, ru.rolename from game_forbidinfo as gf left join role_user as ru on gf.charid=ru.roleid where phpTime >=? and phpTime <=?  limit ?";
prepare stmt_sql_str from @query;

execute stmt_sql_str using @start_date,@end_date,@limit;
end;

delimiter ;

删除 存储过程:drop procedure reku_2_search_charActionLog_noLogin_res;

查看 所有存储过程: show procedure status where db='reku';

查看 指定存储过程代码: show create procedure where 'reku.reku_2_search_charActionLog_noLogin_res';

存储过程和存储函数的不同点在于  编写存储代码 应用的环境(同时语法有两处不同   第一行多了一个:returns  类型   最后一行 返回 return 值 ) ,当编写存储代码 只是为了 返回 一个 状态 或者一个标示时,建议用 存储函数



create function factorypp (num int)

returns int
DETERMINISTIC  --必须加这个
begin
declare result int default 1;
while num >0 do
set result = num * result;
set num = num -1;
end while;
return result;  --返回结果
end;

调用存储函数  用 select  (注意:不用call)

select factorypp (10);

delimiter //

create definer `pay_center`@`localhost` function factorypp(num int)

    begin

        declare result int(11) default 1;

        while num >0 do

            set result = num * result;

            set num = num -1;

        end while;

    return result;

    end;

  //

delimiter ;

返回结果为一个数字。



存储过程优点:

. 简化了应用程序(php  ,java,asp)的编写 ,

. 因为存储过程是在服务器内部执行 离数据最近,不需要带宽,和网络延迟的影响。(通过配合{内存临时表}有可能提高一些where条件的查询速度)

.也能得到应用程序的复用,比如 分页逻辑写成存储过程,

.不需要每次执行时 进行解析 和编译,创建时已经进行了编译。

缺点:

.不调好调试。

.更改应用程序 和 表结构逻辑时。

.mysql  里的函数有限,


存储函数 优缺点 同上。

.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值