初学者 请教上下班的存储过程。。在线等
现在有如下表:
CREATE TABLE paiban
(
id varchar(100) primary key,
employee_id varchar(50) NOT NULL default '', --员工id
employee_name varchar(50) not null default '',--员工姓名
work_date varchar(50) NOT NULL default '', -- 上班日期
start_time varchar(50) NOT NULL default '', -- 上班时间
end_time varchar(50) NOT NULL default '', --下班时间
zaotui_or_chidao varchar(50) not null default '',--迟到或早退
shi_jia char(16) NOT NULL default '', --事假
xiu_jia char(16) NOT NULL default '', --休假
kuang_gong char(16) NOT NULL default ''--矿工
)
===========================
上下班时间用jsp已经记录到此表,我想写个每天24:00(光知道用waitfor time)自动执行上下班的存储过程,要是晚于8点,是迟到,下班时间要是早于18:00 ,就是早退,要是24小时内没有上下班时间,即为旷工,要是48小时没有记录,则是事假,(休假不处理),要是光有上班时间,或光有下班时间,即为矿工,其他我想 start_time-8:00=迟到时间,18:00-end_time=早退时间 ,我这种逻辑对吗,因为初学sql ,请谁给演示下这个存储过程怎么写?(最好有详细注释),谢谢热心朋友...
[解决办法]
[code=SQL][/code]
--数据的设计不敢恭维,我写么多肯定也让行家笑话了,砸鸡蛋吧
create procedure sp_worktime_proc
as
declare cursor_employeeid cursor for
select employee_id,employee_name from employee --假设有你有一张employee表,应该有吧,你
declare @tempEmployeeID varchar(50),@tempEmployeeName varchar(50) --暂存employee_id
declare @Judge varchar(20),@SQL varchar(100)
open cursor_employeeid
fetch next from cursor_employeeid
into @tempEmployeeid,@tempEmployeeName
while(@@FETCH_STATUS = 0)
begin
if exists(select *from paiban where employee_id=@tempEmployeeID and datediff(day,work_date,getdate())=0)
begin
if len((select start_time from paiban where employee_id=@tempEmployeeID and datediff(day,work_date,getdate())=0))>0 --有上班报到
begin
select getdate() --太烦了,具体自己写了,计算机与8:00的时间差,再update zaotui_or_chidao
end
if len((select end_time from paiban where employee_id=@tempEmployeeID and datediff(day,work_date,getdate())=0))>0 --有上班报到
begin
select getdate() --太烦了,具体自己写了,计算机与18:00的时间差,再update zaotui_or_chidao
end
end
else
begin
set @Judge=(select zaotui+shi_jia+xiu_jia+kuang_gong from paiban where employee_id=@tempEmployeeID and
datediff(day,cast(work_date as datetime),getdate())=1) --取昨天的状态
set @SQL='insert into paiban(employee_id,employee_name,work_date'
if (ltrim(rtrim(@Judge))= '旷工') or (ltrim(rtrim(@Judge))='事假')
set @SQL=@SQL+',shi_jia) values('''+@tempemployeeid+''','''+@tempemployeename+''','''+getdate()+''',''事假'')'
else
set @SQL=@SQL+',kuang_gong) values('''+@tempemployeeid+''','''+@tempemployeename+''','''+getdate()+''',''旷工'')'
exec @SQL
end
end
close cursor_employeeid
deallocate cursor_employeeid
go