mysql 存储过程 sqlstate_mysql 存储过程 sqlstate

"

这是典型的游标应用,给你个例子

这里是mysql 存储过程的官方文档,请参考http://www.ostools.net/apidocs/apidoc?api=mysql-5.1-zh

BEGIN

DECLARE done INT DEFAULT 0;

DECLARE _emp_id int;

DECLARE _emp_name varchar(30);

declare _travel_days int default 0; -- 出差天数

declare _add_hour decimal(7,2) default 0.0; -- 加班工时

declare _travle_cost decimal(10,2) default 0.0; -- 差旅成本

-- 根据deptid查询部门所有员工

DECLARE cursor_emps CURSOR FOR select emp_id,emp_name from emp where dept_id = deptid and sts = 'A' and srv_dept_id = 30;

DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;

-- 创建临时表存储结果集

create TEMPORARY table tmp_return_lst(

empname varchar(30) not null

,traveldays int not null

,addhour decimal(7,2) not null

,travlecost decimal(10,2) not null

);

-- 清除表数据

truncate table tmp_return_lst;

OPEN cursor_emps;

REPEAT

FETCH cursor_emps INTO _emp_id, _emp_name;

IF NOT done THEN

-- 查询每名员工的出差天数

select f_travel_day(_emp_id,sd , ed) into _travel_days;

-- 查询每名员工的实际加班工时

select f_actual_add_hour(_emp_id,sd , ed) into _add_hour;

-- 获得每名员工的出差报销金额

select sum(t.tm) into _travle_cost from (

select

distinct b.est_id

,round(b.total_price/b.empcount,2) as tm

from expenseaccount_check_repeat a, expenseaccount_travel b

where a.emp_id = _emp_id and a.check_date between sd and ed

and a.est_id = b.est_id) as t;

-- 将整理的数据insert临时表

insert into tmp_return_lst values(_emp_name,ifnull(_travel_days,0),ifnull(_add_hour,0.0),ifnull(_travle_cost,0.0));

END IF;

UNTIL done END REPEAT;

CLOSE cursor_emps;

-- 返回结果

select * from tmp_return_lst;

END######

引用来自“恺哥”的答案

这是典型的游标应用,给你个例子

这里是mysql 存储过程的官方文档,请参考http://www.ostools.net/apidocs/apidoc?api=mysql-5.1-zh

BEGIN

DECLARE done INT DEFAULT 0;

DECLARE _emp_id int;

DECLARE _emp_name varchar(30);

declare _travel_days int default 0; -- 出差天数

declare _add_hour decimal(7,2) default 0.0; -- 加班工时

declare _travle_cost decimal(10,2) default 0.0; -- 差旅成本

-- 根据deptid查询部门所有员工

DECLARE cursor_emps CURSOR FOR select emp_id,emp_name from emp where dept_id = deptid and sts = 'A' and srv_dept_id = 30;

DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;

-- 创建临时表存储结果集

create TEMPORARY table tmp_return_lst(

empname varchar(30) not null

,traveldays int not null

,addhour decimal(7,2) not null

,travlecost decimal(10,2) not null

);

-- 清除表数据

truncate table tmp_return_lst;

OPEN cursor_emps;

REPEAT

FETCH cursor_emps INTO _emp_id, _emp_name;

IF NOT done THEN

-- 查询每名员工的出差天数

select f_travel_day(_emp_id,sd , ed) into _travel_days;

-- 查询每名员工的实际加班工时

select f_actual_add_hour(_emp_id,sd , ed) into _add_hour;

-- 获得每名员工的出差报销金额

select sum(t.tm) into _travle_cost from (

select

distinct b.est_id

,round(b.total_price/b.empcount,2) as tm

from expenseaccount_check_repeat a, expenseaccount_travel b

where a.emp_id = _emp_id and a.check_date between sd and ed

and a.est_id = b.est_id) as t;

-- 将整理的数据insert临时表

insert into tmp_return_lst values(_emp_name,ifnull(_travel_days,0),ifnull(_add_hour,0.0),ifnull(_travle_cost,0.0));

END IF;

UNTIL done END REPEAT;

CLOSE cursor_emps;

-- 返回结果

select * from tmp_return_lst;

END

感谢。

" ![image.png](https://ucc.alicdn.com/pic/developer-ecology/6482b3113436489caeaba4f5c5495f03.png)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值