急:存储过程问题,各位高人帮忙看看错在哪了,不能得出正确结果,谢谢:) 分不够,再加!

angel_lee 2003-01-08 10:47:15
该存储过程是要按月计算每个员工的考勤状况(包括正常出勤次数、异常出勤次数、迟到次数、早退次数、旷工一小时次数、旷工半小时次数、旷工一天次数、加班次数、加班时长、考勤月份)的信息。

现在的问题:
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

...全文
22 36 打赏 收藏 转发到动态 举报
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
angel_lee 2003-01-17
  • 打赏
  • 举报
回复
to kimcz(东方不败)

这个datepart(month, carddate) = @p_st_month 能行, 我已经试过了,用的很好,谢谢:)

各位高手再帮忙提些建议吧,你们见多识广
angel_lee 2003-01-14
  • 打赏
  • 举报
回复
CREATE PROCEDURE statis_proc
(@p_st_year char(4)='',
@p_st_month char(2)='')
AS


set nocount on
/* 看统计表中是否是要统计的数据
declare @count int
select @count = count(st_month) from Emstatistic where st_month = @p_st_month and st_year = @p_st_year

/*看原始表中是否有满足参数据的待统计记录,
declare @count_ori int
select @count_ori = count(carddate) from Datastate where datepart(year, carddate) = @p_st_year and datepart(month, carddate) = @p_st_month

print @count_ori
print @count
print @p_st_year
print @p_st_month
/* 如果统计表中没有要统计的记录,而原始数据表中有未统计的记录,则进行统计,并插入
if (@count = 0 and @count_ori > 0)
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, st_year)

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),
datepart(year,Datastate.carddate)

from Employee, Datastate

where Employee.em_id=Datastate.card_id and datepart(year,Datastate.carddate) = @p_st_year
and datepart(month, Datastate.carddate) = @p_st_month and
getdate()>dateadd(month,1,@p_st_year+'-'+@p_st_month+'-'+'1')

group by datepart(month, Datastate.carddate), Employee.em_name, datepart(year, Datastate.carddate)
end

else
begin
if (@count > 0)
begin
select em_name, em_state1, em_state2, late_num, early_num,
abhour_num, abhalf_num, abday_num, addwork_num,
addwork_long, st_month, st_year
from Emstatistic

where st_month = @p_st_month and st_year = @p_st_year
end
end
GO
angel_lee 2003-01-14
  • 打赏
  • 举报
回复
今天上午发现了一个问题,
就是执行插入时,应该没有符合条件的记录,就不进行统计,也不插入;
但事实是如果没有符合条件的记录,sp不会执行统计,会插入一条空记录,
所以下面的选择就不一定会发生什么了,
我就又加了一些判断,来控制不插入空记录,可还是不行。
错在哪呢?


kimcz 2003-01-14
  • 打赏
  • 举报
回复
是不是asp里面刷新没有达到效果?
把第一次 2003.12的数据现在继续读出来?
asp经常出现这种情况。把ie全关了,从新打开.



或者, 我也很郁闷, 这个datepart(month, carddate) = @p_st_month 能行吗?整数 = 字符?
帮你up
大健 2003-01-14
  • 打赏
  • 举报
回复
查询分析器结果正常,说明了这储存过程没问题。
那就要从你的程序里入手了,设断点调试看看~~
plife 2003-01-14
  • 打赏
  • 举报
回复
up
angel_lee 2003-01-14
  • 打赏
  • 举报
回复
奇怪,在sql server 的查询分析器中运行,一切正常,但在项目中运行,就不对了, 例子如下:

输入参数 表Emstatistic中的数据 查询分析器结果 报表显示结果
2002.12. 有数据 能查出结果来 能查出结果

2003.1. 没有数据 查不出结果 查不出结果

2003.12. 没有数据 查不出结果 查出2002.12.的结果


为什么页面的报表与查询分析器的结果不一样呢,代码是没问题的,
why??
kimcz 2003-01-14
  • 打赏
  • 举报
回复
datepart的返回值是整数, 而@p_st_month这些是字符
看看是不是这个原因?
不是,就当帮你up
KnowLittle 2003-01-13
  • 打赏
  • 举报
回复

declare @flag int
set @flag=isnumeric(@p_st_year)
print @flag
print convert(int,@p_st_year)
if (@flag=1)
begin
if (convert(int,@p_st_year)<2200 and convert(int,@p_st_year)>1900)
begin
其他所有的代码
end
end
KnowLittle 2003-01-13
  • 打赏
  • 举报
回复
不过现在还是有个问题,就是加完对年参数的判断后,项目中还是不能正确判断年,输入错误的年份,也会输出数据,再帮忙看看呀!

在sp前面加一个判断
declare @yearInt
select @yearInt=convert(int,@p_st_year)
if (@yearInt<2200 and @year>1900)
begin
其他所有的代码
end
angel_lee 2003-01-13
  • 打赏
  • 举报
回复
数据库中有2002年的数据,但没有2003年12月的数据呀,输入2003年12月,也会把2002年的数据取出来,why??
KnowLittle 2003-01-13
  • 打赏
  • 举报
回复
这样限制肯定是不会有问题的
你肯定库里的确没有数据吗?
angel_lee 2003-01-13
  • 打赏
  • 举报
回复
还是不行,例如我输入: 2003 年 12 月, 库里是没有03年的数据的,可它会把2002年12月的数据取出来,是什么原因呢?

应该是这句的问题,可还要加什么判断吗?

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 and st_year = @p_st_year
lizongqi 2003-01-11
  • 打赏
  • 举报
回复
mark一下
angel_lee 2003-01-11
  • 打赏
  • 举报
回复
to zzhuz(大件)

i do insert data to st_year column.

insert Emstatistic (st_year) select datepart(year,Datastate.carddate)
大健 2003-01-11
  • 打赏
  • 举报
回复
你“年”的字段的新添加上去的,添加的时候有为它设默认值吗?也就是说现在的表里那字段对应的数据是什么呢?
大健 2003-01-11
  • 打赏
  • 举报
回复
建议把生成统计的操作从这存储过程中分离开去。也就是把原来的分成两块:
1.负责生成统计的存储过程pro_Create_Static:要做的是生成当前时间前一月的统计(insert into Emstatistic。。。 )。把这存储过程交给SQL Server的“作业”定时去完成,调度时间就设为每月第一天的0时0分。这样生成统计就完全交给了系统去做,不用人手干预了。
2.负责取出统计信息的存储过程pro_Get_Static:要做的仅仅是根据输入参数取出(select from Emstatistic。。。)统计信息。我想Emstatistic这个表的结构还是要改改,把st_month字段改成DateTime类型或增加一个“年”的字段,这样做也方便你的程序~~
angel_lee 2003-01-11
  • 打赏
  • 举报
回复
上面的问题解决了,原因是当表中数据为空时,@st_month还是被赋上了12。

select @st_month = st_month from Emstatistic where st_month

= @p_st_month

这句出的问题。现在解决了把代码贴上(加了年的参数和字段):

CREATE PROCEDURE statis_proc
(@p_st_month char(2)='',
@p_st_year char(4)='')
AS

set nocount on

declare @count int
select @count=count(st_month) from Emstatistic where st_month = @p_st_month and st_year = @p_st_year

if @count = 0 /*No data found using the month and year parameter, insert new data

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, st_year)

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),
datepart(year,Datastate.carddate)

from Employee, Datastate

where Employee.em_id=Datastate.card_id and datepart(year,Datastate.carddate) = @p_st_year
and datepart(month, Datastate.carddate) = @p_st_month and
getdate()>dateadd(month,1,@p_st_year+'-'+@p_st_month+'-'+'1')

group by datepart(month, Datastate.carddate), Employee.em_name, datepart(year, Datastate.carddate)

else
/* now select data out

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 and st_year = @p_st_year

GO



不过现在还是有个问题,就是加完对年参数的判断后,项目中还是不能正确判断年,输入错误的年份,也会输出数据,再帮忙看看呀!
多谢了:)
angel_lee 2003-01-10
  • 打赏
  • 举报
回复
统计部分对了,但因为加了@year这个参数,直接从表中取数据部分也需要做判断,不然,2003.12月也会把02年12月的数据取出来,
可Emstatistic表中没有年的字段,要加一个吗?
不然,不好判断条件的?
大健 2003-01-10
  • 打赏
  • 举报
回复
同意楼上~~
加载更多回复(16)

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧