IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_calc_worktime]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_calc_worktime]
GO
CREATE procedure [dbo].[mps_gen_calc_worktime](
@cmpid int 
) as
begin 
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED 
declare @sql nvarchar(4000) 
declare @s1 varchar(10) 
set @s1 = convert(varchar(10),@cmpid) 
declare @now varchar(10) 
declare @now1 varchar(10) 
set @now = convert(varchar(10),getdate(),120) 
set @now1 = convert(varchar(10),getdate()+1,120) 

declare @t3 varchar(10)
declare @t4 varchar(10)
declare @t5 varchar(10)
declare @t6 varchar(10)

declare @h varchar(10) 
select top 1 @h=convert(varchar(10),hd_holiday)  from mps_holidays where hd_cmpid=@cmpid and hd_date=@now 
set @h = isnull(@h,'1') 

select top 1 @t3=wt_time1,@t4=wt_time2  from mps_worktime where wt_cmpid=@cmpid and wt_title='上午上班' and wt_iswork=0  
select top 1 @t5=wt_time1,@t6=wt_time2  from mps_worktime where wt_cmpid=@cmpid and wt_title='下午上班' and wt_iswork=0  

set @t3=isnull(@t3,'23:59:59') 
set @t4=isnull(@t4,'23:59:59') 
set @t5=isnull(@t5,'23:59:59') 
set @t6=isnull(@t6,'23:59:59') 


if (dbo.checkprocedure('mps_calc_work_time_'+@s1)=0) set @sql='alter ' else set @sql='create '
set @sql = @sql+' procedure mps_calc_work_time_'+@s1+char(13)+
'as '+char(13)+
'begin'+char(13)+
' -- 本程序只能2分名运行一次 '+char(13)+
'declare @t0 datetime '+char(13)+
'set @t0 = '''+@now1+''' '+char(13)+
'declare @t3 datetime '+char(13)+
'set @t3 = '''+@now+' '+@t3+''' '+char(13)+
'declare @t4 datetime '+char(13)+
'set @t4 = '''+@now+' '+@t4+''''+char(13)+
'declare @t5 datetime '+char(13)+
'set @t5 = '''+@now+' '+@t5+''''+char(13)+
'declare @t6 datetime '+char(13)+
'set @t6 = '''+@now+' '+@t6+''''+char(13)+
'declare @n datetime '+char(13)+
'set @n=getdate() '+char(13)+
'if (@n>=@t0) '+char(13)+
'begin'+char(13)+
'-- 重新生成本程序'+char(13)+
'exec mps_gen_calc_worktime '+@s1 +' '+char(13)+
'end else'+char(13)+
'if (('+@h+'=1) and ((@n>=@t3 and @n<=@t4) or (@n>=@t5 and @n<=@t6)))  '+char(13)+
'begin'+char(13)+
'update mps_job set jb_elapsedtime =  jb_elapsedtime + 2 '+char(13)+
'where jb_cmpid=1 and jb_limitedtime>0 and jb_confirm=0 and jb_wsid=0 and jb_isauto=1 and jb_back=1 and jb_enabled=0 '+char(13)+
'end '+char(13)+
'end'+char(13) 
print  @sql 
exec sp_executesql @sql 
if (dbo.checkprocedure('mps_calc_work_time_'+@s1)=0)
begin
set @sql = 'mps_calc_work_time_'+@s1 
--exec sp_executesql @sql 
end
end