1:
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go ALTER procedure [dbo].[PR_XZFW_ZiDongFaPai] --自动发预警和红黄牌 @alarmyujing int, --预警提前天数 @alarmyellow int, --超期多少天发黄牌 @alarmred int --超期多少天发红牌 as declare @isweekday int --是否是工作日,1-工作日,0-休息日 declare @nextIsweekday int --下一天是否是休息日 1-工作日,0-休息日 declare @nextholidays int --接下来的几天是休息日的 create table #tmp_fapaiList ( YXTYWLSH varchar(100), --原业务流水号 hzbh varchar(100), --回执编号 HJDM varchar(10), --环节代码 SLSJ datetime, --受理时间 YBJSJ datetime, --应办结时间 GDBLSX int, --规定办理时限 GDBLSXDW char(1), --规定办理时限单位 SQDWHSQRXM varchar(100), --申请单位或申请人名称 SLJGZZJGDM varchar(20), --受理机关组织机构代码 SLJGMC varchar(50) --受理机关名称 ) declare @YXTYWLSH varchar(100) declare @hzbh varchar(100) declare @HJDM varchar(10) declare @SLSJ datetime declare @YBJSJ datetime declare @GDBLSX int declare @GDBLSXDW char(1) declare @SQDWHSQRXM varchar(100) declare @SLJGZZJGDM varchar(20) declare @SLJGMC varchar(50) declare @primarykey varchar(20) declare lsh_cur cursor for select * from #tmp_fapaiList begin --计算是否为节假日 set @isweekday = 1 set @nextholidays = 0 if ((datepart(dw,getdate())=7) or (datepart(dw,getdate())=1)) --周六、周日 set @isweekday = (select case when count(1)>0 then 1 else 0 end from Sys_Holiday where workflag='W' and datediff(dy,getdate(),today)=0) else set @isweekday = (select case when count(1)>0 then 0 else 1 end from Sys_Holiday where workflag='H' and datediff(dy,getdate(),today)=0) if( @isweekday = 1) --如果是工作日,则发牌,并判断明天是否是休息日,是则发明天的牌,以此类推 begin set @nextIsweekday = 0 while(@nextIsweekday=0) begin set @nextholidays = @nextholidays + 1 if ((datepart(dw,getdate()+@nextholidays)=7) or (datepart(dw,getdate()+@nextholidays)=1)) --周六、周日 set @nextIsweekday = (select case when count(1)>0 then 1 else 0 end from Sys_Holiday where workflag='W' and datediff(dy,getdate()+@nextholidays,today)=0) else set @nextIsweekday = (select case when count(1)>0 then 0 else 1 end from Sys_Holiday where workflag='H' and datediff(dy,getdate()+@nextholidays,today)=0) end set @nextholidays = @nextholidays - 1 --发预警 delete from #tmp_fapaiList insert into #tmp_fapaiList(YXTYWLSH, hzbh, SLSJ, YBJSJ, GDBLSX, GDBLSXDW, SQDWHSQRXM, SLJGZZJGDM, SLJGMC) select YXTYWLSH, hzbh, SLSJ, YBJSJ, GDBLSX, GDBLSXDW, SQDWHSQRXM, SLJGZZJGDM, SLJGMC from ZW_ShouLi where not exists (select 1 from ZW_BanJie where ZW_BanJie.YXTYWLSH=ZW_ShouLi.YXTYWLSH) and not exists (select 1 from ZWJC_YuJing where ZWJC_YuJing.YXTYWLSH=ZW_ShouLi.YXTYWLSH and YJLX='1') and convert(varchar(10), getdate()+@nextholidays, 120) >= convert(varchar(10), YBJSJ-@alarmyujing, 120) and convert(varchar(10), getdate(), 120) < convert(varchar(10), dbo.getWorkDayADDWithType(YBJSJ,@alarmyellow,'G'), 120) delete from #tmp_fapaiList where YXTYWLSH in (select distinct yxtywlsh from dbo.ZW_TeBieChengXuShenQing a where not exists (select 1 from dbo.ZW_TeBieChengXuJieGuo b where a.yxtywlsh=b.yxtywlsh and a.tbcxyxtywlsh=b.tbcxyxtywlsh)) delete from #tmp_fapaiList where YXTYWLSH in (select distinct a.yxtywlsh from dbo.ZW_BuJiaoGaoZhi a where not exists (select 1 from dbo.ZW_BuJiaoShouLi b where a.YXTYWLSH=b.YXTYWLSH) or a.bjgzsj>(select isnull(max(bjsj), '2000-01-01') from dbo.ZW_BuJiaoShouLi b where a.YXTYWLSH=b.YXTYWLSH)) update #tmp_fapaiList set HJDM = '0005' update #tmp_fapaiList set HJDM = (select max(b.STEPCODE) from zw_workflow_StepStatus a,zw_workflow_stepdesc b where a.YXTYWLSH=#tmp_fapaiList.YXTYWLSH and a.BLSTATUS='0' and a.WORKFLOWID=b.WORKFLOWID and a.STEPDESCID=b.STEPDESCID) where exists (select 1 from zw_workflow_StepStatus a,zw_workflow_stepdesc b where a.YXTYWLSH=#tmp_fapaiList.YXTYWLSH and a.BLSTATUS='0' and a.WORKFLOWID=b.WORKFLOWID and a.STEPDESCID=b.STEPDESCID) open lsh_cur fetch next from lsh_cur into @YXTYWLSH,@hzbh,@HJDM,@SLSJ,@YBJSJ,@GDBLSX,@GDBLSXDW,@SQDWHSQRXM,@SLJGZZJGDM,@SLJGMC while @@fetch_status=0 begin exec PR_XZFW_getPrimaryKey @primarykey output insert into ZWJC_YuJing(OID, YXTYWLSH, HZBH, YJHJDM, YJYJ, YJSJ, YJLX, SLSJ, YBSJ, GDBLSX, GDBLSXDW, SQDWHSQRXM, SLJGZZJGDM, SLJGMC, BZ) values (@primarykey, @YXTYWLSH, @hzbh, @HJDM,'即将超时', getdate(), '1', @SLSJ, @YBJSJ, @GDBLSX, @GDBLSXDW, @SQDWHSQRXM, @SLJGZZJGDM, @SLJGMC, '') fetch next from lsh_cur into @YXTYWLSH,@hzbh,@HJDM,@SLSJ,@YBJSJ,@GDBLSX,@GDBLSXDW,@SQDWHSQRXM,@SLJGZZJGDM,@SLJGMC end close lsh_cur --deallocate lsh_cur --发黄牌 delete from #tmp_fapaiList insert into #tmp_fapaiList(YXTYWLSH, hzbh, SLSJ, YBJSJ, GDBLSX, GDBLSXDW, SQDWHSQRXM, SLJGZZJGDM, SLJGMC) select YXTYWLSH, hzbh, SLSJ, YBJSJ, GDBLSX, GDBLSXDW, SQDWHSQRXM, SLJGZZJGDM, SLJGMC from ZW_ShouLi where not exists (select 1 from ZW_BanJie where ZW_BanJie.YXTYWLSH=ZW_ShouLi.YXTYWLSH) and not exists (select 1 from ZWJC_RedYellow where ZWJC_RedYellow.YXTYWLSH=ZW_ShouLi.YXTYWLSH and FPLX='1' and PLX='1') and convert(varchar(10), getdate(), 120) >= convert(varchar(10), dbo.getWorkDayADDWithType(YBJSJ,@alarmyellow,'G'), 120) and convert(varchar(10), getdate(), 120) < convert(varchar(10), dbo.getWorkDayADDWithType(YBJSJ,@alarmred,'G'), 120) delete from #tmp_fapaiList where YXTYWLSH in (select distinct yxtywlsh from dbo.ZW_TeBieChengXuShenQing a where not exists (select 1 from dbo.ZW_TeBieChengXuJieGuo b where a.yxtywlsh=b.yxtywlsh and a.tbcxyxtywlsh=b.tbcxyxtywlsh)) delete from #tmp_fapaiList where YXTYWLSH in (select distinct a.yxtywlsh from dbo.ZW_BuJiaoGaoZhi a where not exists (select 1 from dbo.ZW_BuJiaoShouLi b where a.YXTYWLSH=b.YXTYWLSH) or a.bjgzsj>(select isnull(max(bjsj), '2000-01-01') from dbo.ZW_BuJiaoShouLi b where a.YXTYWLSH=b.YXTYWLSH)) update #tmp_fapaiList set HJDM = '0005' update #tmp_fapaiList set HJDM = (select max(b.STEPCODE) from zw_workflow_StepStatus a,zw_workflow_stepdesc b where a.YXTYWLSH=#tmp_fapaiList.YXTYWLSH and a.BLSTATUS='0' and a.WORKFLOWID=b.WORKFLOWID and a.STEPDESCID=b.STEPDESCID) where exists (select 1 from zw_workflow_StepStatus a,zw_workflow_stepdesc b where a.YXTYWLSH=#tmp_fapaiList.YXTYWLSH and a.BLSTATUS='0' and a.WORKFLOWID=b.WORKFLOWID and a.STEPDESCID=b.STEPDESCID) open lsh_cur fetch next from lsh_cur into @YXTYWLSH,@hzbh,@HJDM,@SLSJ,@YBJSJ,@GDBLSX,@GDBLSXDW,@SQDWHSQRXM,@SLJGZZJGDM,@SLJGMC while @@fetch_status=0 begin exec PR_XZFW_getPrimaryKey @primarykey output insert into ZWJC_RedYellow(OID, YXTYWLSH, HZBH, FPHJDM, FPSJ,FPLX,PLX, FPYY, SLSJ, YBSJ, GDBLSX, GDBLSXDW, SQDWHSQRXM, SLJGZZJGDM, SLJGMC, BZ) values (@primarykey, @YXTYWLSH, @hzbh, @HJDM, getdate(),'1','1', '超时' + cast(@alarmyellow as varchar(5)) + '天未办结', @SLSJ, @YBJSJ, @GDBLSX, @GDBLSXDW, @SQDWHSQRXM, @SLJGZZJGDM, @SLJGMC, '') fetch next from lsh_cur into @YXTYWLSH,@hzbh,@HJDM,@SLSJ,@YBJSJ,@GDBLSX,@GDBLSXDW,@SQDWHSQRXM,@SLJGZZJGDM,@SLJGMC end close lsh_cur --deallocate lsh_cur --发红牌 delete from #tmp_fapaiList insert into #tmp_fapaiList(YXTYWLSH, hzbh, SLSJ, YBJSJ, GDBLSX, GDBLSXDW, SQDWHSQRXM, SLJGZZJGDM, SLJGMC) select YXTYWLSH, hzbh, SLSJ, YBJSJ, GDBLSX, GDBLSXDW, SQDWHSQRXM, SLJGZZJGDM, SLJGMC from ZW_ShouLi where not exists (select 1 from ZW_BanJie where ZW_BanJie.YXTYWLSH=ZW_ShouLi.YXTYWLSH) and not exists (select 1 from ZWJC_RedYellow where ZWJC_RedYellow.YXTYWLSH=ZW_ShouLi.YXTYWLSH and FPLX='1' and PLX='2') and convert(varchar(10), getdate(), 120) >= convert(varchar(10), dbo.getWorkDayADDWithType(YBJSJ,@alarmred,'G'), 120) delete from #tmp_fapaiList where YXTYWLSH in (select distinct yxtywlsh from dbo.ZW_TeBieChengXuShenQing a where not exists (select 1 from dbo.ZW_TeBieChengXuJieGuo b where a.yxtywlsh=b.yxtywlsh and a.tbcxyxtywlsh=b.tbcxyxtywlsh)) delete from #tmp_fapaiList where YXTYWLSH in (select distinct a.yxtywlsh from dbo.ZW_BuJiaoGaoZhi a where not exists (select 1 from dbo.ZW_BuJiaoShouLi b where a.YXTYWLSH=b.YXTYWLSH) or a.bjgzsj>(select isnull(max(bjsj), '2000-01-01') from dbo.ZW_BuJiaoShouLi b where a.YXTYWLSH=b.YXTYWLSH)) update #tmp_fapaiList set HJDM = '0005' update #tmp_fapaiList set HJDM = (select max(b.STEPCODE) from zw_workflow_StepStatus a,zw_workflow_stepdesc b where a.YXTYWLSH=#tmp_fapaiList.YXTYWLSH and a.BLSTATUS='0' and a.WORKFLOWID=b.WORKFLOWID and a.STEPDESCID=b.STEPDESCID) where exists (select 1 from zw_workflow_StepStatus a,zw_workflow_stepdesc b where a.YXTYWLSH=#tmp_fapaiList.YXTYWLSH and a.BLSTATUS='0' and a.WORKFLOWID=b.WORKFLOWID and a.STEPDESCID=b.STEPDESCID) open lsh_cur fetch next from lsh_cur into @YXTYWLSH,@hzbh,@HJDM,@SLSJ,@YBJSJ,@GDBLSX,@GDBLSXDW,@SQDWHSQRXM,@SLJGZZJGDM,@SLJGMC while @@fetch_status=0 begin exec PR_XZFW_getPrimaryKey @primarykey output insert into ZWJC_RedYellow(OID, YXTYWLSH, HZBH, FPHJDM, FPSJ,FPLX,PLX, FPYY, SLSJ, YBSJ, GDBLSX, GDBLSXDW, SQDWHSQRXM, SLJGZZJGDM, SLJGMC, BZ) values (@primarykey, @YXTYWLSH, @hzbh, @HJDM, getdate(),'1','2', '超时' + cast(@alarmred as varchar(5)) + '天未办结', @SLSJ, @YBJSJ, @GDBLSX, @GDBLSXDW, @SQDWHSQRXM, @SLJGZZJGDM, @SLJGMC, '') delete from ZWJC_RedYellow where YXTYWLSH=@YXTYWLSH and FPLX='1' and PLX='1' fetch next from lsh_cur into @YXTYWLSH,@hzbh,@HJDM,@SLSJ,@YBJSJ,@GDBLSX,@GDBLSXDW,@SQDWHSQRXM,@SLJGZZJGDM,@SLJGMC end close lsh_cur deallocate lsh_cur end end
2:
set ANSI_NULLS ON set QUOTED_IDENTIFIER ON go ALTER procedure [dbo].[PR_XZFW_ZiDongFaPai] --自动发预警和红黄牌 @alarmyujing int, --预警提前天数 @alarmyellow int, --超期多少天发黄牌 @alarmred int --超期多少天发红牌 as declare @isweekday int --是否是工作日,1-工作日,0-休息日 declare @nextIsweekday int --下一天是否是休息日 1-工作日,0-休息日 declare @nextholidays int --接下来的几天是休息日的 create table #tmp_fapaiList ( YXTYWLSH varchar(100), --原业务流水号 hzbh varchar(100), --回执编号 HJDM varchar(10), --环节代码 SLSJ datetime, --受理时间 YBJSJ datetime, --应办结时间 GDBLSX int, --规定办理时限 GDBLSXDW char(1), --规定办理时限单位 SQDWHSQRXM varchar(100), --申请单位或申请人名称 SLJGZZJGDM varchar(20), --受理机关组织机构代码 SLJGMC varchar(50) --受理机关名称 ) declare @YXTYWLSH varchar(100) declare @hzbh varchar(100) declare @HJDM varchar(10) declare @SLSJ datetime declare @YBJSJ datetime declare @GDBLSX int declare @GDBLSXDW char(1) declare @SQDWHSQRXM varchar(100) declare @SLJGZZJGDM varchar(20) declare @SLJGMC varchar(50) declare @primarykey varchar(20) declare lsh_cur cursor for select * from #tmp_fapaiList begin --计算是否为节假日 set @isweekday = 1 set @nextholidays = 0 if ((datepart(dw,getdate())=7) or (datepart(dw,getdate())=1)) --周六、周日 set @isweekday = (select case when count(1)>0 then 1 else 0 end from Sys_Holiday where workflag='W' and datediff(dy,getdate(),today)=0) else set @isweekday = (select case when count(1)>0 then 0 else 1 end from Sys_Holiday where workflag='H' and datediff(dy,getdate(),today)=0) if( @isweekday = 1) --如果是工作日,则发牌,并判断明天是否是休息日,是则发明天的牌,以此类推 begin set @nextIsweekday = 0 while(@nextIsweekday=0) begin set @nextholidays = @nextholidays + 1 if ((datepart(dw,getdate()+@nextholidays)=7) or (datepart(dw,getdate()+@nextholidays)=1)) --周六、周日 set @nextIsweekday = (select case when count(1)>0 then 1 else 0 end from Sys_Holiday where workflag='W' and datediff(dy,getdate()+@nextholidays,today)=0) else set @nextIsweekday = (select case when count(1)>0 then 0 else 1 end from Sys_Holiday where workflag='H' and datediff(dy,getdate()+@nextholidays,today)=0) end set @nextholidays = @nextholidays - 1 --发预警 delete from #tmp_fapaiList insert into #tmp_fapaiList(YXTYWLSH, hzbh, SLSJ, YBJSJ, GDBLSX, GDBLSXDW, SQDWHSQRXM, SLJGZZJGDM, SLJGMC) select YXTYWLSH, hzbh, SLSJ, YBJSJ, GDBLSX, GDBLSXDW, SQDWHSQRXM, SLJGZZJGDM, SLJGMC from ZW_ShouLi where not exists (select 1 from ZW_BanJie where ZW_BanJie.YXTYWLSH=ZW_ShouLi.YXTYWLSH) and not exists (select 1 from ZWJC_YuJing where ZWJC_YuJing.YXTYWLSH=ZW_ShouLi.YXTYWLSH and YJLX='1') and convert(varchar(10), getdate()+@nextholidays, 120) >= convert(varchar(10), YBJSJ-@alarmyujing, 120) and convert(varchar(10), getdate(), 120) < convert(varchar(10), dbo.getWorkDayADDWithType(YBJSJ,@alarmyellow,'G'), 120) delete from #tmp_fapaiList where YXTYWLSH in (select distinct yxtywlsh from dbo.ZW_TeBieChengXuShenQing a where not exists (select 1 from dbo.ZW_TeBieChengXuJieGuo b where a.yxtywlsh=b.yxtywlsh and a.tbcxyxtywlsh=b.tbcxyxtywlsh)) delete from #tmp_fapaiList where YXTYWLSH in (select distinct a.yxtywlsh from dbo.ZW_BuJiaoGaoZhi a where not exists (select 1 from dbo.ZW_BuJiaoShouLi b where a.YXTYWLSH=b.YXTYWLSH) or a.bjgzsj>(select isnull(max(bjsj), '2000-01-01') from dbo.ZW_BuJiaoShouLi b where a.YXTYWLSH=b.YXTYWLSH)) update #tmp_fapaiList set HJDM = '0005' update #tmp_fapaiList set HJDM = (select max(b.STEPCODE) from zw_workflow_StepStatus a,zw_workflow_stepdesc b where a.YXTYWLSH=#tmp_fapaiList.YXTYWLSH and a.BLSTATUS='0' and a.WORKFLOWID=b.WORKFLOWID and a.STEPDESCID=b.STEPDESCID) where exists (select 1 from zw_workflow_StepStatus a,zw_workflow_stepdesc b where a.YXTYWLSH=#tmp_fapaiList.YXTYWLSH and a.BLSTATUS='0' and a.WORKFLOWID=b.WORKFLOWID and a.STEPDESCID=b.STEPDESCID) open lsh_cur fetch next from lsh_cur into @YXTYWLSH,@hzbh,@HJDM,@SLSJ,@YBJSJ,@GDBLSX,@GDBLSXDW,@SQDWHSQRXM,@SLJGZZJGDM,@SLJGMC while @@fetch_status=0 begin exec PR_XZFW_getPrimaryKey @primarykey output insert into ZWJC_YuJing(OID, YXTYWLSH, HZBH, YJHJDM, YJYJ, YJSJ, YJLX, SLSJ, YBSJ, GDBLSX, GDBLSXDW, SQDWHSQRXM, SLJGZZJGDM, SLJGMC, BZ) values (@primarykey, @YXTYWLSH, @hzbh, @HJDM,'即将超时', getdate(), '1', @SLSJ, @YBJSJ, @GDBLSX, @GDBLSXDW, @SQDWHSQRXM, @SLJGZZJGDM, @SLJGMC, '') fetch next from lsh_cur into @YXTYWLSH,@hzbh,@HJDM,@SLSJ,@YBJSJ,@GDBLSX,@GDBLSXDW,@SQDWHSQRXM,@SLJGZZJGDM,@SLJGMC end close lsh_cur --deallocate lsh_cur --发黄牌 delete from #tmp_fapaiList insert into #tmp_fapaiList(YXTYWLSH, hzbh, SLSJ, YBJSJ, GDBLSX, GDBLSXDW, SQDWHSQRXM, SLJGZZJGDM, SLJGMC) select YXTYWLSH, hzbh, SLSJ, YBJSJ, GDBLSX, GDBLSXDW, SQDWHSQRXM, SLJGZZJGDM, SLJGMC from ZW_ShouLi where not exists (select 1 from ZW_BanJie where ZW_BanJie.YXTYWLSH=ZW_ShouLi.YXTYWLSH) and not exists (select 1 from ZWJC_RedYellow where ZWJC_RedYellow.YXTYWLSH=ZW_ShouLi.YXTYWLSH and FPLX='1' and PLX='1') and convert(varchar(10), getdate(), 120) >= convert(varchar(10), dbo.getWorkDayADDWithType(YBJSJ,@alarmyellow,'G'), 120) and convert(varchar(10), getdate(), 120) < convert(varchar(10), dbo.getWorkDayADDWithType(YBJSJ,@alarmred,'G'), 120) delete from #tmp_fapaiList where YXTYWLSH in (select distinct yxtywlsh from dbo.ZW_TeBieChengXuShenQing a where not exists (select 1 from dbo.ZW_TeBieChengXuJieGuo b where a.yxtywlsh=b.yxtywlsh and a.tbcxyxtywlsh=b.tbcxyxtywlsh)) delete from #tmp_fapaiList where YXTYWLSH in (select distinct a.yxtywlsh from dbo.ZW_BuJiaoGaoZhi a where not exists (select 1 from dbo.ZW_BuJiaoShouLi b where a.YXTYWLSH=b.YXTYWLSH) or a.bjgzsj>(select isnull(max(bjsj), '2000-01-01') from dbo.ZW_BuJiaoShouLi b where a.YXTYWLSH=b.YXTYWLSH)) update #tmp_fapaiList set HJDM = '0005' update #tmp_fapaiList set HJDM = (select max(b.STEPCODE) from zw_workflow_StepStatus a,zw_workflow_stepdesc b where a.YXTYWLSH=#tmp_fapaiList.YXTYWLSH and a.BLSTATUS='0' and a.WORKFLOWID=b.WORKFLOWID and a.STEPDESCID=b.STEPDESCID) where exists (select 1 from zw_workflow_StepStatus a,zw_workflow_stepdesc b where a.YXTYWLSH=#tmp_fapaiList.YXTYWLSH and a.BLSTATUS='0' and a.WORKFLOWID=b.WORKFLOWID and a.STEPDESCID=b.STEPDESCID) open lsh_cur fetch next from lsh_cur into @YXTYWLSH,@hzbh,@HJDM,@SLSJ,@YBJSJ,@GDBLSX,@GDBLSXDW,@SQDWHSQRXM,@SLJGZZJGDM,@SLJGMC while @@fetch_status=0 begin exec PR_XZFW_getPrimaryKey @primarykey output insert into ZWJC_RedYellow(OID, YXTYWLSH, HZBH, FPHJDM, FPSJ,FPLX,PLX, FPYY, SLSJ, YBSJ, GDBLSX, GDBLSXDW, SQDWHSQRXM, SLJGZZJGDM, SLJGMC, BZ) values (@primarykey, @YXTYWLSH, @hzbh, @HJDM, getdate(),'1','1', '超时' + cast(@alarmyellow as varchar(5)) + '天未办结', @SLSJ, @YBJSJ, @GDBLSX, @GDBLSXDW, @SQDWHSQRXM, @SLJGZZJGDM, @SLJGMC, '') fetch next from lsh_cur into @YXTYWLSH,@hzbh,@HJDM,@SLSJ,@YBJSJ,@GDBLSX,@GDBLSXDW,@SQDWHSQRXM,@SLJGZZJGDM,@SLJGMC end close lsh_cur --deallocate lsh_cur --发红牌 delete from #tmp_fapaiList insert into #tmp_fapaiList(YXTYWLSH, hzbh, SLSJ, YBJSJ, GDBLSX, GDBLSXDW, SQDWHSQRXM, SLJGZZJGDM, SLJGMC) select YXTYWLSH, hzbh, SLSJ, YBJSJ, GDBLSX, GDBLSXDW, SQDWHSQRXM, SLJGZZJGDM, SLJGMC from ZW_ShouLi where not exists (select 1 from ZW_BanJie where ZW_BanJie.YXTYWLSH=ZW_ShouLi.YXTYWLSH) and not exists (select 1 from ZWJC_RedYellow where ZWJC_RedYellow.YXTYWLSH=ZW_ShouLi.YXTYWLSH and FPLX='1' and PLX='2') and convert(varchar(10), getdate(), 120) >= convert(varchar(10), dbo.getWorkDayADDWithType(YBJSJ,@alarmred,'G'), 120) delete from #tmp_fapaiList where YXTYWLSH in (select distinct yxtywlsh from dbo.ZW_TeBieChengXuShenQing a where not exists (select 1 from dbo.ZW_TeBieChengXuJieGuo b where a.yxtywlsh=b.yxtywlsh and a.tbcxyxtywlsh=b.tbcxyxtywlsh)) delete from #tmp_fapaiList where YXTYWLSH in (select distinct a.yxtywlsh from dbo.ZW_BuJiaoGaoZhi a where not exists (select 1 from dbo.ZW_BuJiaoShouLi b where a.YXTYWLSH=b.YXTYWLSH) or a.bjgzsj>(select isnull(max(bjsj), '2000-01-01') from dbo.ZW_BuJiaoShouLi b where a.YXTYWLSH=b.YXTYWLSH)) update #tmp_fapaiList set HJDM = '0005' update #tmp_fapaiList set HJDM = (select max(b.STEPCODE) from zw_workflow_StepStatus a,zw_workflow_stepdesc b where a.YXTYWLSH=#tmp_fapaiList.YXTYWLSH and a.BLSTATUS='0' and a.WORKFLOWID=b.WORKFLOWID and a.STEPDESCID=b.STEPDESCID) where exists (select 1 from zw_workflow_StepStatus a,zw_workflow_stepdesc b where a.YXTYWLSH=#tmp_fapaiList.YXTYWLSH and a.BLSTATUS='0' and a.WORKFLOWID=b.WORKFLOWID and a.STEPDESCID=b.STEPDESCID) open lsh_cur fetch next from lsh_cur into @YXTYWLSH,@hzbh,@HJDM,@SLSJ,@YBJSJ,@GDBLSX,@GDBLSXDW,@SQDWHSQRXM,@SLJGZZJGDM,@SLJGMC while @@fetch_status=0 begin exec PR_XZFW_getPrimaryKey @primarykey output insert into ZWJC_RedYellow(OID, YXTYWLSH, HZBH, FPHJDM, FPSJ,FPLX,PLX, FPYY, SLSJ, YBSJ, GDBLSX, GDBLSXDW, SQDWHSQRXM, SLJGZZJGDM, SLJGMC, BZ) values (@primarykey, @YXTYWLSH, @hzbh, @HJDM, getdate(),'1','2', '超时' + cast(@alarmred as varchar(5)) + '天未办结', @SLSJ, @YBJSJ, @GDBLSX, @GDBLSXDW, @SQDWHSQRXM, @SLJGZZJGDM, @SLJGMC, '') delete from ZWJC_RedYellow where YXTYWLSH=@YXTYWLSH and FPLX='1' and PLX='1' fetch next from lsh_cur into @YXTYWLSH,@hzbh,@HJDM,@SLSJ,@YBJSJ,@GDBLSX,@GDBLSXDW,@SQDWHSQRXM,@SLJGZZJGDM,@SLJGMC end close lsh_cur deallocate lsh_cur end end