急:存储过程问题,各位高人帮忙看看错在哪了,不能得出正确结果,谢谢:) 分不够,再加!
该存储过程是要按月计算每个员工的考勤状况(包括正常出勤次数、异常出勤次数、迟到次数、早退次数、旷工一小时次数、旷工半小时次数、旷工一天次数、加班次数、加班时长、考勤月份)的信息。
现在的问题:
1、每page_load一次,就统计一次,我就加了个月份字段,每次取出来判断一下,如果已经统计过,就直接取数据,如果没有统计过,就进行统计。
可现在库中有统计完的数据,他还是要再统计,是什么原因呢?
2、我的库中可能有不止一个月的数据,如果通过参数,也只能判断要统计月的数据,而别的月的数据还会重复统计,怎么办呢?
3、比如现在是一月中旬,我要查一下12月的数据,因为Datastate中已经有一定的当月数据,就会把一月的数据提前统计,以后再统计,就又会出问题的,怎么解决呀?
存储过程如下:
/*参数说明:@p_st_month:从.aspx页面输入的月份参数
CREATE PROCEDURE statis_proc
@p_st_month char
AS
declare @st_month char
select @st_month = st_month from Emstatistic where st_month = p_st_month
//先判断表里,有没有这个参数对应的记录,如果有,就直接select出来,如果没有,再insert
if (@p_st_month = @st_month)
begin
select em_name, em_state1, em_state2, late_num, early_num, abhour_num, abhalf_num, abday_num, addwork_num, addwork_long, st_month
from Emstatistic
where st_month = @p_st_month
end
else
begin
insert into Emstatistic (em_name, em_state1, em_state2, late_num, early_num, abhour_num, abhalf_num, abday_num, addwork_num, addwork_long, st_month)
select Employee.em_name,
sum(case em_state when '1' then 1 else 0 end),
sum(case em_state when '2' then 1 else 0 end),
sum(case late_state when '1' then 1 else 0 end),
sum(case early_state when '1' then 1 else 0 end),
sum(case abhour_state when '1' then 1 else 0 end),
sum(case abhalf_state when '1' then 1 else 0 end),
sum(case abday_state when '1' then 1 else 0 end),
sum(case addwork_state when '1' then 1 else 0 end),
sum(addwork), datepart(month, Datastate.carddate)
from Employee, Datastate
where Employee.em_id=Datastate.card_id and
group by datepart(month, Datastate.carddate),
Employee.em_name
GO