/****** Object: StoredProcedure [dbo].[Clinic_Drug_Judge] Script Date: 2018-07-06 12:49:22 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO --alter 2018.7.5 主要用于第三方下达医嘱的判断。 ALTER proc [dbo].[Clinic_Drug_Judge] @lxml ntext ,@ACF01 tinyint ,@VAA01 int ,@VAA07 int ,@type int ,@lbce01 int ,@upCBM01 int = 0 ,@asign tinyint = 0 --0=暂存 1=确认保存前 as DECLARE @iDOM int, @iret int Exec @iret = sp_xml_preparedocument @iDOM OUTPUT, @lXml if @iret > 0 BEGIN exec sp_xml_removedocument @iDOM RAISERROR('解析XML文档时发生错误,错误号: %d.', 16, 1, @iret) with nowait return @iret END --提取内容 SELECT IDENTITY(int,1,1) AS ID,* INTO #TmpCBM from OpenXml(@iDOM,'/Root/CBM1/Ie',8) WITH (CBMID int ,CBM06 int ,CBM07 int ,BJW02 varchar(30) ) If not exists(select * from #TmpCBM) BEGIN exec sp_xml_removedocument @iDOM RETURN 0 END --vaj25对应基本单位数量 SELECT IDENTITY(int,1,1) AS ID,cast(0.00 as numeric(18,4)) BAG46,cast(0 as int) BBT07,cast(0.00 as numeric(18,4)) VAJ25 ,cast(0.00 as numeric(12,4)) BBY23,@VAA07 VAF06,cast(0 as tinyint) BBT18,cast(0 as tinyint) BBT10,cast(0 as tinyint) BBT11,* INTO #TmpVAF from OpenXml(@iDOM,'/Root/VAF1/Ie',8) WITH (CBMID int ,ROWNR int ,VAF11 tinyint ,BDA01 varchar(2) ,BBX01 int ,BBY01 int ,VAF14 varchar(128) ,VAF15 varchar(256) ,VAF17 int ,VAF18 numeric(18,4) ,VAF19 varchar(10) ,VAF20 numeric(18,4) ,VAF21 numeric(18,4) ,VAF22 varchar(1024) ,VAF26 varchar(20) ,VAF27 int ,VAF28 tinyint ,VAF29 varchar(4) ,VAF31 tinyint ,VAF32 tinyint ,VAF35 tinyint ,VAF36 datetime ,VAF37 datetime ,VAF58 tinyint ,VAF59 int ,VAF60 varchar(10) ,lSign int ,BDA01A varchar(2) ,lRoute int,BCK01B int,BIW02 varchar(64),rCurrent int,rUpdate tinyint,VEH02 varchar(64),zxbzyyts int,DSK01 int ) where lSign = 0 order by cbmid,vaf59,rownr --zxbzyyts 最小包装用药天数 delete a from #TmpVAF a where not exists(select * from #TmpCBM b where b.CBMid = a.CBMid) If not exists(select * from #TmpVAF) BEGIN exec sp_xml_removedocument @iDOM RETURN 0 END SELECT IDENTITY(int,1,1) AS ID,cast('' as varchar(20)) BAK02,* INTO #TmpVAO from OpenXml(@iDOM,'/Root/VAO1/Ie',8) WITH (CBMID int ,VAO06 int ,VAO07 int ,VAO10 tinyint ,VAO11 tinyint ,BAK01A int ,CAM01 int ,VAO15 varchar(150) ) where isnull(VAO15,'')>'' SELECT IDENTITY(int,1,1) AS ID,0 vbqid,* INTO #TmpVBQ from OpenXml(@iDOM,'/Root/VBQ1/Ie',8) WITH (VBQ01 int ,VAA01 int--病人ID ,VAA07 int--就诊ID ,ACF01 tinyint--病人来源 ,VAP01 int ,VBQ07 int ,BCK01A int ,BCK01B int ,BCE03A varchar(20) ,BCE01A int ,VBQ27 varchar(1024) ,VBQ28 varchar(1024) ,VBQ29 varchar(2048) ,VBQ30 varchar(2048) ) SELECT IDENTITY(int,1,1) AS ID,0 VAF01,0 VAX01,0 VAT01,* INTO #TmpVAX from OpenXml(@iDOM,'/Root/VAX1/Ie',8) WITH (CBMID int ,VAA01 int--病人ID ,VAA07 int--就诊ID ,ACF01 int--病人来源 ,VAF59 int ,Rownr int ,ACE01 varchar(2) ,BCE01 int ,BCE02 varchar(20) ,BCE03 varchar(64) ) SELECT IDENTITY(int,1,1) AS ID,name,value INTO #TmpCase from OpenXml(@iDOM,'/Root/Case/Ie',8) WITH (name varchar(512),value varchar(5000) ) exec sp_xml_removedocument @iDOM declare @gpara69 varchar(10),@adate datetime,@edate datetime,@tcount int,@smsg varchar(2048),@vaj47 datetime,@vae11 datetime declare @kbmBAG table(BBY01 int,BAG46 numeric(18,2)) declare @zyPara386 varchar(10),@para356_392 varchar(10),@mzPara153 varchar(10),@IsYbbr tinyint,@BDP12 int,@bdp02 varchar(50) declare @mzPara146 int,@mzPara147 varchar(10),@fp_137 int,@fp_150 int,@fp_151 int,@fp_157 int,@gpara22 varchar(10) set @fp_137 = 0 set @IsYbbr = 0 set @fp_151 = 0 set @fp_157 = 0 set @smsg = '' --返回的提示消息,默认禁止发送医嘱,当内容以“==”开头时,提示后依然发送医嘱 set @edate = GETDATE() set @vaj47 = dateadd(month,-5,@edate) --限制最大销售数量或金额判断 --限定药品最大销售额判断 0:不限制 1=按当月销售额计算 2=按当月销售数量计算 -- 3=门诊按本病人最近一月内销售数量计算(对应基本单位数量) set @gpara69 = dbo.GetSysParamValue(9999,9999,69) set @gpara22 = dbo.GetSysParamValue(9999,9999,22) set @zyPara386 = dbo.GetSysParamValue(100,105003,386) --手术医嘱执行科室为手术室时,必须输入主刀、及麻醉方式 set @mzPara153 = dbo.GetSysParamValue(100,105002,153) --无诊断医嘱单,不允许保存 if @ACF01 = 1 begin set @para356_392 = dbo.GetSysParamValue(100,105002,356) --抗生素类药品必须输入用药目的 select @bdp02=BDP02 from VAC1 with(nolock) where VAC01=@VAA07 if exists(select BDP02 from BDP1 with(nolock) where BDP02=@bdp02 and BDP07 = 1) set @IsYbbr = 1 if exists(select a.* from #tmpCBM a join #tmpVAF b on b.CBMID=a.CBMID where b.BDA01 in ('1','2','3') and a.CBM06 not in (1,2,4)) begin set @smsg ='药品医嘱必须对应药品处方类型,请检查.' RAISERROR(@smsg, 16, 1) with nowait RETURN 1 end select @fp_157=BLP04 from blp1 with(nolock) where blp02=157 and BLP04>0 SET @fp_157= ISNULL(@fp_157,0) end if @ACF01 = 2 begin if @type = 1 set @para356_392 = dbo.GetSysParamValue(100,105003,392) --抗生素类药品必须输入用药目的 if @type = 2 set @para356_392 = dbo.GetSysParamValue(100,105001,392) --抗生素类药品必须输入用药目的 select @bdp02=BDP02,@vae11=VAE11 from VAE1 with(nolock) where VAE01=@VAA07 if exists(select BDP02 from BDP1 with(nolock) where BDP02=@bdp02 and BDP07 = 1) set @IsYbbr = 1 if dbo.GetSysParamValue(100,105003,262)='1' and exists(select * from #tmpvaf where bda01>='A' and bda01<>'M' and VAF11=1 and isnull(vaf26,'')='' and (@asign=1 or (@asign=0 and rCurrent=1))) begin select @smsg = VAF22 from #tmpVAF where bda01>='A' and bda01<>'M' and VAF11=1 and isnull(vaf26,'')='' and (@asign=1 or (@asign=0 and rCurrent=1)) set @smsg = @smsg+',根据系统设置"非药品长嘱启用首日次数",对应长嘱必须要输入频次.' RAISERROR(@smsg, 16, 1) with nowait RETURN 1 end select @fp_137=BLP05 from blp1 with(nolock) where blp02=137 and blp05>0 SET @fp_137= ISNULL(@fp_137,0) select @fp_157=BLP05 from blp1 with(nolock) where blp02=157 and BLP05>0 SET @fp_157= ISNULL(@fp_157,0) end --判断药品有效期 if exists(select a.bby01 from #tmpVAF a join BBY1 b with(nolock) on b.BBY01 = a.BBY01 where b.BBY31<=@edate and (@asign=1 or (@asign=0 and a.rCurrent=1))) begin select @smsg = a.VAF22 from #tmpVAF a join BBY1 b with(nolock) on b.BBY01 = a.BBY01 where b.BBY31<=@edate and (@asign=1 or (@asign=0 and a.rCurrent=1)) set @smsg = @smsg+',已过有效期,不能使用此项目,请检查.' RAISERROR(@smsg, 16, 1) with nowait RETURN 1 end if exists(select a.bbx01 from #tmpVAF a join BBX1 b with(nolock) on b.BBX01 = a.BBX01 where a.BDA01 >='8' and a.BDA01 <>'M' and b.BBX25<=@edate and (@asign=1 or (@asign=0 and a.rCurrent=1))) begin select @smsg = a.VAF22 from #tmpVAF a join BBX1 b with(nolock) on b.BBX01 = a.BBX01 where a.BDA01 >='8' and a.BDA01 <>'M' and b.BBX25<=@edate and (@asign=1 or (@asign=0 and a.rCurrent=1)) set @smsg = @smsg+',已过有效期,不能使用此项目,请检查.' RAISERROR(@smsg, 16, 1) with nowait RETURN 1 end if exists(select a.bby01 from #tmpVAF a join BCT1 c with(nolock) on c.bby01 = a.bby01 where a.BDA01 in ('4','M') and c.BCT33 <> 1 and (@asign=1 or (@asign=0 and a.rCurrent=1))) begin select @smsg = a.VAF22 from #tmpVAF a join BCT1 b with(nolock) on b.BBY01 = a.BBY01 where a.BDA01 in ('4','M') and b.BCT33 <> 1 and (@asign=1 or (@asign=0 and a.rCurrent=1)) set @smsg = @smsg+',不是临床应用项目,不能使用此项目,请检查.' RAISERROR(@smsg, 16, 1) with nowait RETURN 1 end --材料库存判断 if exists(select a.bby01 from #tmpVAF a where a.BDA01 in ('4','M') and a.BBY01>0 and (@asign=1 or (@asign=0 and a.rCurrent=1))) and exists(select * from blp1 with(nolock) where (@ACF01=1 and blp04>0)or(@ACF01=2 and blp05>0)) begin set @smsg='' select @smsg = '当成材料['+a.VAF22 +']库存剩余'+CAST(b.LSQty as varchar)+'已不足,请调整' from #tmpVAF a join V_MDK4 b with(nolock) on b.BBY01=a.BBY01 and b.BCK01=a.BCK01B where a.BDA01 in ('4','M') and a.BBY01>0 and (a.VAF21-b.LSQty>0.0001) if @smsg>'' begin RAISERROR(@smsg, 16, 1) with nowait RETURN 1 end -- end end if exists(select a.bby01 from #tmpVAF a join DPT1 b with(nolock) on b.BBY01 = a.BBY01 and b.BCK01 = a.BCK01B where a.BDA01>='1' and a.BDA01 <= '3' and b.BAL08=1 and (@asign=1 or (@asign=0 and a.rCurrent=1))) begin select @smsg = a.VAF22 from #tmpVAF a join DPT1 b with(nolock) on b.BBY01 = a.BBY01 and b.BCK01 = a.BCK01B where a.BDA01>='1' and a.BDA01 <= '3' and b.BAL08=1 and (@asign=1 or (@asign=0 and a.rCurrent=1)) set @smsg = @smsg+',此药房药品已禁售,不能使用此项目,请检查.' RAISERROR(@smsg, 16, 1) with nowait RETURN 1 end --手术医嘱判断 if @ACF01 = 2 and exists(select a.cbmid from #TmpVAF a where a.BDA01 in ('S','0') and exists(select * from BAZ1 b with(nolock) where b.BCK01 = a.BCK01B and BAU01 = '03')) begin if exists(select * from blp1 where blp02=102) and exists(select a.cbmid from #TmpVAF a where a.BDA01 in ('S','0') and exists(select * from BAZ1 b with(nolock) where b.BCK01 = a.BCK01B and BAU01 = '03') and not exists(select * from #TmpVAO c where c.CBMID=a.CBMID)) begin set @smsg = '根据系统限定,手术医嘱必须要有诊断.' RAISERROR(@smsg, 16, 1) with nowait RETURN 1 end if @zyPara386 = '1' and @asign=1 begin if exists(select * from #TmpVAF a where a.BDA01 = 'S' and exists(select * from BAZ1 b with(nolock) where b.BCK01 = a.BCK01B and b.BAU01 = '03') and not exists(select * from #TmpVAF c where c.CBMID = a.CBMID and c.VAF59 = a.VAF59 and c.BDA01 = 'A') ) begin select @smsg = a.VAF22 from #TmpVAF a where a.BDA01 = 'S' and exists(select * from BAZ1 b with(nolock) where b.BCK01 = a.BCK01B and b.BAU01 = '03') and not exists(select * from #TmpVAF c where c.CBMID = a.CBMID and c.VAF59 = a.VAF59 and c.BDA01 = 'A') set @smsg = @smsg+',没有麻醉方式,请检查.' RAISERROR(@smsg, 16, 1) with nowait RETURN 1 end if exists(select * from #TmpVAF a where a.BDA01 in ('S','0') and exists(select * from BAZ1 b with(nolock) where b.BCK01 = a.BCK01B and b.BAU01 = '03') and not exists(select * from #TmpVAX c where c.CBMID = a.CBMID and c.VAF59 = a.VAF59 and c.ACE01 = '01')) begin select @smsg = a.VAF22 from #TmpVAF a where a.BDA01 in ('S','0') and exists(select * from BAZ1 b with(nolock) where b.BCK01 = a.BCK01B and b.BAU01 = '03') and not exists(select * from #TmpVAX c where c.CBMID = a.CBMID and c.VAF59 = a.VAF59 and c.ACE01 = '01') set @smsg = @smsg+',没有主刀医师,请检查.' RAISERROR(@smsg, 16, 1) with nowait RETURN 2 end end if exists(select * from BLP1 with(nolock) where BLP02 = 30) and @asign=1 and exists(select * from #TmpVAF a where a.BDA01 in ('S','0') and exists(select * from BAZ1 b with(nolock) where b.BCK01 = a.BCK01B and b.BAU01 = '03') and not exists(select * from #TmpVAX c where c.CBMID = a.CBMID and c.VAF59 = a.VAF59 and c.BCE02='切口等级' and ISNULL(c.bce03,'')>'')) begin select @smsg = a.VAF22 from #TmpVAF a where a.BDA01 in ('S','0') and exists(select * from BAZ1 b with(nolock) where b.BCK01 = a.BCK01B and b.BAU01 = '03') and not exists(select * from #TmpVAX c where c.CBMID = a.CBMID and c.VAF59 = a.VAF59 and c.BCE02='切口等级' and ISNULL(c.bce03,'')>'') set @smsg = @smsg+',没有切口等级,请检查.' RAISERROR(@smsg, 16, 1) with nowait RETURN 2 end end if @asign=1 and @ACF01 = 1 and exists(select a.cbmid from #TmpVAF a where exists(select * from #TmpVAF b where b.cbmid = a.cbmid and b.vaf58=5) and a.VAF58 <> 5) begin select @smsg=a.VAF22 from #TmpVAF a where exists(select * from #TmpVAF b where b.cbmid = a.cbmid and b.vaf58=5) and a.VAF58 <> 5 order by a.rownr desc set @smsg = '外配处方,需要整张处方药品执行性质都是外配处方,不能有别的执行性质,请检查.' RAISERROR(@smsg, 16, 1) with nowait RETURN 99 end if @asign=1 and exists(select a.cbmid from #TmpVAF a where exists(select * from #TmpVAF b where b.cbmid = a.cbmid and b.vaf59 = a.vaf59 and b.vaf58=2) and a.VAF58 <> 2) begin select @smsg=a.VAF22 from #TmpVAF a where exists(select * from #TmpVAF b where b.cbmid = a.cbmid and b.vaf59 = a.vaf59 and b.vaf58=2) and a.VAF58 <> 2 order by a.rownr desc set @smsg = '离院带药,需要处方药品中本组药品执行性质都是离院带药,本组不能有别的执行性质,请检查.' RAISERROR(@smsg, 16, 1) with nowait RETURN 99 end --判断中草药处方限制味数 if exists(select * from #TmpCBM where CBM06=2) and exists(select * from BLP1 with(nolock) where BLP02=150 and BLP04>1) begin select @fp_150=BLP04 from BLP1 with(nolock) where BLP02=150 if exists(select a.CBMID,COUNT(1) fnum from #TmpVAF a join #TmpCBM b on b.CBMID=a.CBMID where b.CBM06=2 and a.BDA01='3' group by a.CBMID having COUNT(1)<@fp_150) begin set @smsg = '根据系统限定,草药处方不能低于'+CAST(@fp_150 as varchar)+'味草药,请修改调整.' RAISERROR(@smsg, 16, 1) with nowait RETURN 99 end end select @tcount = COUNT(*) from #TmpVAF where BDA01 >= '1' AND BDA01 <= '3' --西成药品医嘱行数 --只判断西成药品处方 update a set a.BAG46 = isnull(b.BAG46,0),a.BBT07 = ISNULL(c.BBT07,0) ,a.BBY23 = case when a.BDA01='3' then ISNULL(c.BBT14,0) else ISNULL(d.BBY23,0) end ,a.VAJ25= Case when a.BDA01='3' then case when @ACF01=1 then a.vaf20*b.BAG05 else a.vaf20*b.BAG07 end else case when @ACF01=1 then a.vaf21*b.BAG05 else a.vaf21*b.BAG07 end end ,a.BBT18 = ISNULL(c.BBT18,0),a.BBT10 = ISNULL(c.BBT10,0),a.BBT11 = ISNULL(c.BBT11,0) ,a.zxbzyyts =case when a.BDA01='3' then a.VAF17 else ABS(floor((1 * b.BAG05* b.BAG03)/(a.VAF18 * a.VAF27))) end from #TmpVAF a join BAG1 b with(nolock) on b.BBY01 = a.BBY01 join BBT1 c with(nolock) on c.BBX01 = b.BBX01 join bby1 d with(nolock) on d.bby01 = a.bby01 where @tcount>=1 and a.BDA01>='1' and a.BDA01<='3' --抗生素类药品用药目的判断 if @tcount >= 1 and @para356_392='1' begin if exists(select a.* from #TmpVAF a where a.BBT11>=1 and ISNULL(a.BIW02,'')='' and a.BDA01 in ('1','2') and (@asign=1 or (@asign=0 and a.rCurrent=1)) ) begin select top 1 @smsg = a.VAF22+' 没有用药目的' from #TmpVAF a where a.BBT11>=1 and ISNULL(a.BIW02,'')='' and a.BDA01 in ('1','2') and (@asign=1 or (@asign=0 and a.rCurrent=1)) set @smsg = @smsg+',请填写用药目的.' RAISERROR(@smsg, 16, 1) with nowait RETURN 1 end end if @tcount>=1 and @ACF01=2 and @fp_137>0 and exists(select * from #TmpVAF where BBT10=1 and BBT11>0 and BDA01 in ('1','2') and (@asign=1 or (@asign=0 and rCurrent=1)) ) begin if exists( select a.BBY01,b.BBX01,a.VAF36 from VAF2 a with(nolock) join BBT1 b with(nolock) on b.BBX01=a.BBX01 where a.VAF06=@VAA07 and a.BDA01 in ('1','2') and b.BBT10=1 and b.BBT11>0 and a.VAF25='?' and DATEDIFF(MINUTE,a.VAF36,@edate)>@fp_137 ) begin select top 1 @smsg = VAF22+' 现在不能下达需要皮试抗生素类药品,因为前面下达的抗生素药品皮试结果在限定时间内还未出结果.' from #TmpVAF where BBT10=1 and BBT11>0 and BDA01 in ('1','2') and (@asign=1 or (@asign=0 and rCurrent=1)) RAISERROR(@smsg, 16, 1) with nowait RETURN 1 end end if @tcount >= 1 and @ACF01=2 and exists(select * from BLP1 with(nolock) where BLP02 in (40,41)) and exists(select * from #TmpVAF a join BBT1 b with(nolock) on b.BBX01 = a.BBX01 where b.BBT11>=1) begin declare @tvaf36 datetime,@yzvaf36 datetime select a.VAA07,a.VAA01,a.VDZ14,a.VDZ12,a.VDZ13,a.BBY01,b.vaf36,b.vaf37,b.vaf11,b.VAF22 into #tmpvdz from VDZ1 a with(nolock) join #TmpVAF b on b.bby01 = a.BBY01 join BBT1 c with(nolock) on c.BBX01 = b.bbx01 where a.VAA07 = @VAA07 and a.VDZ17 = 1 and a.VDZ14 > 0 and DATEDIFF(DAY,a.VDZ12,@edate) >=0 and DATEDIFF(DAY,@edate,a.VDZ13) > 0 and c.BBT11 >= 1 and not exists(select * from VAF2 d with(nolock) where d.VAF06 = @VAA07 and d.VAF10 in (1,3) and d.BDA01 in ('1','2') and d.BBY01 = a.BBY01 ) select a.vaf01,a.VAF36,a.BDA01,b.BBX13,a.VAF14 into #kbVAF from vaf2 a with(nolock) left join BBX1 b with(nolock) on b.bbx01 = a.BBX01 where a.VAF06 = @vaa07 and a.VAF10 in (8,9) and ((a.BDA01 in ('S','0') and exists(select * from BAZ1 f with(nolock) where f.BCK01 = a.BCK01B And f.BAU01 = '03' AND f.ACF01 IN (2,3) ) ) or (b.BDA01 = 'Z' and b.BBX13='6')) update #kbVAF set vaf14='1900-01-01' where bda01 = '0' and (vaf14='0000-00-00 00:00' or vaf14 = '') select @tvaf36 = MAX(VAF36) from #kbVAF where BDA01='Z' if exists(select * from BLP1 with(nolock) where BLP02 =40) and exists(select b.VAF01 from #kbVAF b where b.BDA01 = 'Z' and b.BBX13='6' and b.VAF36 = @tvaf36 and DATEDIFF(HOUR,b.VAF36,@edate)>24) and exists(select * from #kbVAF a join VBF1 c with(nolock) on c.VAF01 = a.VAF01 where a.BDA01 in ('S','0') and a.vaf36 < @tvaf36 and c.VBF03='切口等级' and c.VBF07='Ⅰ') begin if exists(select * from #tmpvdz a where a.vaf11=1 and (DATEDIFF(DAY,@edate,a.vaf37)<=0 or DATEDIFF(DAY,a.vaf36,a.vaf37)>a.VDZ14 )) begin set @smsg = '' select @smsg = a.VAF22+' 审批用药'+CAST(a.VDZ14 as varchar)+'天' from #tmpvdz a where a.vaf11=1 and (DATEDIFF(DAY,@edate,a.vaf37)<=0 or DATEDIFF(DAY,a.vaf36,a.vaf37)>a.VDZ14 ) set @smsg = @smsg+' 请正确填写长嘱终止时间,或长嘱用药天数已经超过审批用药天数.' RAISERROR(@smsg, 16, 1) with nowait RETURN 40 end if exists(select a.* from #TmpVAF a join BBT1 c with(nolock) on c.BBX01 = a.bbx01 where c.BBT11 >= 1 and not exists(select * from #tmpvdz b where b.BBY01 = a.bby01)) begin set @smsg = '' if exists(select a.* from #TmpVAF a join BBT1 c with(nolock) on c.BBX01 = a.bbx01 join BIW1 d with(nolock) on d.BIW02=a.BIW02 where c.BBT11 >= 1 and d.BIW07=1 and ISNULL(d.BIW06,'')>'') begin select top 1 @smsg='=='+d.BIW06 from #TmpVAF a join BBT1 c with(nolock) on c.BBX01 = a.bbx01 join BIW1 d with(nolock) on d.BIW02=a.BIW02 where c.BBT11 >= 1 and d.BIW07=1 and ISNULL(d.BIW06,'')>'' end else begin select top 1 @smsg = a.VAF22+' 不能使用,原因:' from #TmpVAF a join BBT1 c on c.BBX01 = a.bbx01 where c.BBT11 >= 1 and not exists(select * from #tmpvdz b where b.BBY01 = a.bby01) select @smsg= @smsg + 'I类切口手术术后已超过24小时,不能再使用抗菌素药品。' end RAISERROR(@smsg, 16, 1) with nowait RETURN 40 end end select @tvaf36 = MAX(VAF36) from #kbVAF where BDA01 in ('S','0') --and DATEDIFF(HOUR,@edate,CAST(b.VAF14 as datetime))>2 if exists(select * from BLP1 with(nolock) where BLP02 =41) and exists(select b.VAF01 from #kbVAF b join VBF1 c with(nolock) on c.VAF01 = b.VAF01 where b.BDA01 in ('S','0') and b.VAF36 = @tvaf36 and c.VBF03='切口等级' and c.VBF07='Ⅰ' and exists(select a1.vaf36 from #TmpVAF a1 join BBT1 b1 with(nolock) on b1.BBX01 = a1.BBX01 where b1.BBT11>=1 and DATEDIFF(HOUR,a1.VAF36,CAST(b.VAF14 as datetime))>2) ) begin select @smsg= 'I类切口手术术前0.5到2小时之内才能使用抗菌素药品。'; RAISERROR(@smsg, 16, 1) with nowait RETURN 40 end end --判断处方诊断 if @tcount >= 1 and @mzPara153='1' and exists(select * from #TmpCBM where CBM06 in (1,2,4)) and not exists(select * from #TmpCBM a join #TmpVAO b on b.CBMID = a.CBMID where a.CBM06 in (1,2,4)) begin RAISERROR('药品处方没有诊断,请填写处方诊断.', 16, 1) with nowait RETURN 1 end -- if @tcount>=1 and @ACF01=1 and exists(select * from blp1 with(nolock) where blp02=77) and exists(select * from #TmpCBM where CBM06 in (1,4)) begin if exists(select m.CBMID,COUNT(1) fnum from( select distinct a.CBMID,case when c.BBX20=0 then 1 else 2 end bbx20 from #TmpCBM a join #TmpVAF b on b.cbmid=a.cbmid join BBX1 c with(nolock) on c.BBX01 = b.BBX01 where a.cbm06 in (1,4) and b.BDA01='T' and b.VAF32=1 and c.BBX20 in (0,1,2) ) m group by m.CBMID having COUNT(1)>1 ) begin RAISERROR('根据处方管理办法,口服和注射不能开到同一张处方.', 16, 1) with nowait RETURN 1 end end if @acf01=2 and exists(select a.VAF22 from #tmpVAF a where a.vaf36 <= @vae11) begin select @smsg = a.VAF22+' 医嘱开始时间不能小于入院时间.' from #tmpVAF a where a.vaf36 <= @VAE11 set @smsg = @smsg+',不能下达此医嘱.' RAISERROR(@smsg, 16, 1) with nowait RETURN 1 end --判断当前选择批次库存数量 if @gpara22='2' and @tcount>=1 and @ACF01>0 and exists(select * from #TmpVAF where VAF11=2 and BDA01 in ('1','2','3')) begin if exists(select d.* from (select BCK01,BBY01,SUM(Quantity) LSQty from DPT1 group by BCK01,BBY01) d join (select BBY01,BCK01B,SUM(VAJ25) fvaj25 from #TmpVAF where VAF11=2 and BBY01>0 and BDA01 in ('1','2','3') group by BBY01,BCK01B ) c on c.BCK01B = d.BCK01 and c.BBY01 = d.BBY01 where d.LSQty - c.fVAJ25 < 0 ) begin set @smsg = (select top 1 e.BBY05 from (select BCK01,BBY01,SUM(Quantity) LSQty from DPT1 group by BCK01,BBY01) d join (select BBY01,BCK01B,SUM(VAJ25) fvaj25 from #TmpVAF where VAF11=2 and BBY01>0 and BDA01 in ('1','2','3') group by BBY01,BCK01B ) c on c.BCK01B = d.BCK01 and c.BBY01 = d.BBY01 join BBY1 e with(nolock) on e.BBY01 = c.BBY01 where d.LSQty - c.fVAJ25 < 0 ) set @smsg = '【'+replace(isnull(@smsg,''),'%','%')+'】库存不够,不能保存医嘱.'; RAISERROR(@smsg, 16, 1) with nowait RETURN 7 end end if @fp_157=2 and @tcount>=1 and @ACF01>0 and exists(select * from #TmpVAF where VAF11=2 and DSK01>0 and BDA01 in ('1','2','3')) begin if exists(select b.* from DPT1 a with(nolock) join (select DSK01,BCK01B,SUM(VAJ25) fvaj25 from #TmpVAF where VAF11=2 and DSK01>0 and BDA01 in ('1','2','3') group by DSK01,BCK01B) b on b.DSK01=a.DSK_ID and b.BCK01B=a.BCK01 where a.Quantity-b.fvaj25<0 ) begin set @smsg='' select @smsg=c.BBY05 from DPT1 a with(nolock) join (select DSK01,BCK01B,SUM(VAJ25) fvaj25 from #TmpVAF where VAF11=2 and DSK01>0 and BDA01 in ('1','2','3') group by DSK01,BCK01B) b on b.DSK01=a.DSK_ID and b.BCK01B=a.BCK01 join BBY1 c with(nolock) on c.BBY01=a.BBY01 where a.Quantity-b.fvaj25<0 if @smsg>'' begin set @smsg='【'+replace(isnull(@smsg,''),'%','%')+'】所选批次库存不够,不能保存.'; RAISERROR(@smsg, 16, 1) with nowait RETURN 1 end end end if ((@gpara69 > '0' and @gpara69<='2')or(@gpara69='3' and @ACF01=1)) and @tcount >= 1 and exists(select * from #TmpVAF where BAG46 > 1 and BDA01 in ('1','2')) begin if @gpara69='3' set @adate = DATEADD(MONTH,-1,GETDATE()) else set @adate = CONVERT(varchar(7),@edate,121)+'-01' select a.VAJ04,a.VAJ05,a.VAF01,a.VAK01,a.VAI01,a.BBY01,a.VAJ25,a.VAJ34,a.VAJ38,a.VAJ46,a.VAJ47,a.ACF01,a.VAA01 into #tmpvaj from VAJ1 a join BAG1 b with(nolock) on b.BBY01=a.BBY01 and b.BAG46>1 where a.BDN01 <= '2' and a.VAJ05 < 9 and a.VAJ47 >= @adate and a.ACF01 = 1 and VAK01 > 0 union all select a.VAJ04,a.VAJ05,a.VAF01,a.VAK01,a.VAI01,a.BBY01,a.VAJ25,a.VAJ34,a.VAJ38,a.VAJ46,a.VAJ47,a.ACF01,a.VAA01 from VAJ2 a join BAG1 b with(nolock) on b.BBY01=a.BBY01 and b.BAG46>1 where a.BDN01 <= '2' and a.VAJ05 < 9 and a.VAJ47 >= @adate and @gpara69 in ('1','2') and a.ACF01 = 2 if @gpara69 = '1' and exists(select * from #TmpVAF where BAG46 > 1 and BDA01 in ('1','2')) begin insert into @kbmBAG(BBY01,BAG46) select a.BBY01,SUM(b.VAJ38) from #TmpVAF a join #tmpvaj b on b.BBY01 = a.BBY01 where a.BDA01>='1' and a.BDA01<='2' and b.VAJ05 < 9 and b.VAJ47 >= @adate and b.VAJ47 <= @edate group by a.BBY01 if exists(select * from @kbmBAG a join #TmpVAF b on b.BBY01 = a.BBY01 where b.BAG46 > 1 and a.BAG46 > b.BAG46) begin select @smsg = b.VAF22+' 已超出限售金额:'+CAST(b.BAG46 as varchar) from @kbmBAG a join #TmpVAF b on b.BBY01 = a.BBY01 where b.BAG46 > 1 and a.BAG46 > b.BAG46 set @smsg = @smsg+',不能再下达此药品.' RAISERROR(@smsg, 16, 1) with nowait RETURN 1 end end if @gpara69 = '2' and exists(select * from #TmpVAF where BAG46 > 1 and BDA01 in ('1','2')) begin insert into @kbmBAG(BBY01,BAG46) select a.BBY01, case when b.BBY01 > 0 then SUM(b.VAJ25/b.VAJ34) else SUM(a.vaf21) end from #TmpVAF a left join #tmpvaj b on b.BBY01 = a.BBY01 and b.VAJ05 < 9 and b.VAJ47 >= @adate and b.VAJ47 <= @edate where a.BDA01>='1' and a.BDA01<='2' group by a.BBY01,b.BBY01 if exists(select * from @kbmBAG a join (select BBY01,BAG46,SUM(VAF21) VAF21 from #TmpVAF group by BBY01,BAG46) b on b.BBY01 = a.BBY01 where b.BAG46 > 1 and (a.BAG46+b.VAF21) > b.BAG46) begin select @smsg = c.BBY05+' 时间段内加上本次处方数量已超出限售数量:'+CAST(b.BAG46 as varchar) from @kbmBAG a join (select BBY01,BAG46,SUM(VAF21) VAF21 from #TmpVAF group by BBY01,BAG46) b on b.BBY01 = a.BBY01 join BBY1 c with(nolock) on c.BBY01 = a.BBY01 where b.BAG46 > 1 and (a.BAG46+b.VAF21) > b.BAG46 set @smsg = @smsg+',不能再下达此药品.' RAISERROR(@smsg, 16, 1) with nowait RETURN 1 end end if @gpara69 = '3' and @ACF01=1 and exists(select * from #TmpVAF where BAG46 > 1 and BDA01 in ('1','2') ) begin insert into @kbmBAG(BBY01,BAG46) select a.BBY01, case when b.BBY01 > 0 then SUM(b.VAJ25) else SUM(a.VAJ25) end from #TmpVAF a left join #tmpvaj b on b.BBY01 = a.BBY01 and b.VAA01=@VAA01 and b.VAJ05 < 9 and b.VAJ47 >= @adate and b.VAJ47 <= @edate where a.BDA01>='1' and a.BDA01<='2' group by a.BBY01,b.BBY01 if exists(select * from @kbmBAG a join (select BBY01,BAG46,SUM(VAJ25) VAF21 from #TmpVAF group by BBY01,BAG46) b on b.BBY01 = a.BBY01 where b.BAG46 > 1 and (a.BAG46+b.VAF21) > b.BAG46) begin select @smsg = c.BBY05+' 时间段内加上本次处方数量已超出限售数量:'+CAST(b.BAG46 as varchar) from @kbmBAG a join (select BBY01,BAG46,SUM(VAJ25) VAF21 from #TmpVAF group by BBY01,BAG46) b on b.BBY01 = a.BBY01 join BBY1 c with(nolock) on c.BBY01 = a.BBY01 where b.BAG46 > 1 and (a.BAG46+b.VAF21) > b.BAG46 set @smsg = @smsg+',不能再下达此药品.' RAISERROR(@smsg, 16, 1) with nowait RETURN 1 end end end --医保治疗方案限制 if @ACF01=2 and @bdp02='门诊特病' and exists(select * from BLP1 where BLP02=119) begin declare @iaz01 uniqueidentifier select top 1 @iaz01=IAZ01 from IAZ1 where IAZ07 = 1 and ACF01 = 2 and VAA07 = @VAA07 and IAZ12 = 2 order by IAZ14 desc --set @iaz01=ISNULL(@iaz01,'') set @smsg='' select top 1 @smsg=s.vaf22+'---对应费用:'+s.bby05 from (select a.vaf22,a.bby01,b.BBY05 from #TmpVAF a join BBY1 b with(nolock) on b.BBY01=a.bby01 where (a.BDA01>='1' and a.BDA01<='4') or a.BDA01='M' union all select a.vaf22,b.BBY01,c.BBY05 from #TmpVAF a join BDU1 b with(nolock) on b.BBX01=a.bbx01 and b.BDU07=1 join BBY1 c with(nolock) on c.BBY01=b.BBY01 where a.bda01>='8' and a.bda01<>'M' and (a.rownr=0 or (a.BDA01 not in('E','M') and a.Rownr>0 and isnull(a.VAF15,'')='')) union all select a.vaf22,b.BBY01,c.BBY05 from #TmpVAF a join BDU1 b with(nolock) on b.BBX01=a.bbx01 and b.BDU07=2 and a.VAF14=b.BDU08 and a.VAF15 = b.BDU09 join BBY1 c with(nolock) on c.BBY01=b.BBY01 where a.bda01>='8' and a.bda01='E' and a.rownr>0 union all select a.vaf22,b.BBY01,c.BBY05 from #TmpVAF a join BDU1 b with(nolock) on b.BBX01=a.bbx01 and b.BDU07=2 and a.VAF15 = b.BDU09 join BBY1 c with(nolock) on c.BBY01=b.BBY01 where a.bda01>='8' and a.BDA01 not in('E','M') and isnull(a.VAF15,'') > '' and a.rownr>0 ) s where not exists(select b1.BBY01 from IBH1 b1 with(nolock) where b1.IAZ01=@iaz01 and b1.BBY01=s.bby01 and b1.IBH10<=2 ) if @smsg >'' begin set @smsg = @smsg+',不在医保方案中,不能下达此医嘱.' RAISERROR(@smsg, 16, 1) with nowait RETURN 1 end if exists(select * from #TmpVAF where BDA01>='1' and bda01<='3') begin select b.BBY01,b.IBH04,b.IBH16,c.BAG03,c.BDG02B,b.IBH20 svaj25 into #tmpibh from IBH1 b with(nolock) join BAG1 c with(nolock) on c.BBY01=b.BBY01 where b.IAZ01=@iaz01 and b.IBH10<=2 select a.bby01,sum(a.vaj25) fvaj25 into #kbvaj from VAJ2 a with(nolock) where a.BDN01<='3' and a.vaj05<=2 and a.VAA07=@VAA07 and exists(select * from #tmpibh b where b.BBY01=a.BBY01) group by a.BBY01 if exists(select a.bby01 from (select bby01,SUM(VAJ25) fvaj25 from #TmpVAF where BDA01 >='1' and bda01<='3' group by bby01) a join #tmpibh b on b.BBY01=a.bby01 left join #kbvaj c on c.BBY01=a.bby01 where b.svaj25='1' and bda01<='3' group by bby01) a join #tmpibh b on b.BBY01=a.bby01 join #TmpVAF d on d.bby01=a.bby01 left join #kbvaj c on c.BBY01=a.bby01 where b.svaj25 0) and @tcount>=1 begin select @iaa01 = IAA01 from IAR1 m with(nolock) where m.IAR32 =1 and m.ACF01 =2 and m.VAA07 = @VAA07 and m.IAR25<>'' set @iaa01 = ISNULL(@iaa01,0) --判断医保目录内药品 set @smsg = '' select top 1 @smsg = a.VAF22 from #TmpVAF a where not exists(select * from IAM1 e with(nolock) join IAH1 f with(nolock) on f.IAH01 = e.IAH01 where e.IAA01=@iaa01 and e.BBY01 = a.bby01) if @smsg > '' begin set @smsg = @smsg+' 非医保目录内药品,医保病人不能使用.' RAISERROR(@smsg, 16, 1) with nowait RETURN 75 end end if @ACF01 = 2 and @asign = 1 begin select @blp04=ISNULL(VBM13,0),@blp05=isnull(BEP07,0) from VBM2 with(nolock) where VAA07 = @VAA07 and ACF01=2 and (BEP07 > 0 or VBM13 > 0) if (@blp04 > 0 or @blp05 > 0) begin select case when BDN01 <= '3' then 1 else 0 end nbdn01,SUM(VAJ38) fvaj38 into #kbmMoney from VAJ2 with(nolock) where VAK01 = 0 and VAA07 = @VAA07 and ACF01 = 2 group by case when BDN01 <= '3' then 1 else 0 end if @blp04 > 0 and exists(select * from #TmpVAF a where a.BDA01>='1' and a.BDA01<='2' ) and exists(select * from #kbmMoney where nbdn01='1' and (fvaj38-@blp04)>0.00001) begin set @smsg = '当前病人药品总费用已经超过限制药品费用('+CAST(@blp04 as varchar)+'),不能再下达药品医嘱.' RAISERROR(@smsg, 16, 1) with nowait RETURN 31 end if @blp05 > 0 and exists(select * from #TmpVAF a where a.BDA01 > '0' ) and exists(select * from (select SUM(fvaj38) vaj38 from #kbmMoney) a where (a.vaj38-@blp05)>0.00001) begin set @smsg = '当前病人总费用已经超过限制总费用('+CAST(@blp05 as varchar)+'),不能再下达医嘱.' RAISERROR(@smsg, 16, 1) with nowait RETURN 31 end end end -- if @ACF01 = 1 and exists(select * from BLP1 with(nolock) where BLP02 = 43 and BLP04 > 0) and exists(select * from #TmpVAF a where (a.VAF21 % 1 > 0.00001) and exists(select * from BLP1 e where e.BLP02=43 and e.BLP06=a.bda01 and e.BLP04 > 0) ) begin set @smsg = '' select top 1 @smsg = VAF22+' 数量不能输入小数.' from #TmpVAF a where (a.VAF21 % 1 > 0.00001) and exists(select * from BLP1 e where e.BLP02=43 and e.BLP06=a.bda01 and e.BLP04 > 0) if @smsg > '' begin RAISERROR(@smsg, 16, 1) with nowait RETURN 43 end end if @ACF01 = 2 and exists(select * from BLP1 with(nolock) where BLP02 = 43 and BLP05 > 0) and exists(select * from #TmpVAF a where (a.VAF21 % 1 > 0.00001) and exists(select * from BLP1 e where e.BLP02=43 and e.BLP06=a.bda01 and e.BLP05 > 0) ) begin set @smsg = '' select top 1 @smsg = VAF22+' 数量不能输入小数.' from #TmpVAF where (a.VAF21 % 1 > 0.00001) and exists(select * from BLP1 e with(nolock) where e.BLP02=43 and e.BLP06=a.bda01 and e.BLP05 > 0) if @smsg > '' begin RAISERROR(@smsg, 16, 1) with nowait RETURN 43 end end if @ACF01 = 1 and exists(select * from BLP1 with(nolock) where BLP02 = 49 and BLP04 > 0) and exists(select * from #TmpVAF a where (a.VAF21 < 0.00001) and (a.vaf32 <> 1 or (a.vaf32=1 and a.vaf58 not in (2,5)) ) and exists(select * from BLP1 e with(nolock) where e.BLP02=49 and e.BLP06=a.bda01 and e.BLP04 > 0) ) begin set @smsg = '' select top 1 @smsg = VAF22+' '+cast(a.VAF21 as varchar)+' 数量必须要大于零.' from #TmpVAF a where (a.VAF21 < 0.00001) and (a.vaf32 <> 1 or (a.vaf32=1 and a.vaf58 not in (2,5)) ) and exists(select * from BLP1 e with(nolock) where e.BLP02=49 and e.BLP06=a.bda01 and e.BLP04 > 0) if @smsg > '' begin RAISERROR(@smsg, 16, 1) with nowait RETURN 49 end end if @ACF01 = 2 and exists(select * from BLP1 with(nolock) where BLP02 = 49 and BLP05 > 0) and exists(select * from #TmpVAF a where (a.VAF21 < 0.00001) and (a.vaf32 <> 1 or (a.vaf32=1 and a.vaf58 not in (2,5)) ) and exists(select * from BLP1 e with(nolock) where e.BLP02=49 and e.BLP06=a.bda01 and e.BLP05 > 0) ) begin set @smsg = '' select top 1 @smsg = VAF22+' '+cast(a.VAF21 as varchar)+' 数量必须要大于零.' from #TmpVAF where (a.VAF21 < 0.00001) and (a.vaf32 <> 1 or (a.vaf32=1 and a.vaf58 not in (2,5)) ) and exists(select * from BLP1 e with(nolock) where e.BLP02=49 and e.BLP06=a.bda01 and e.BLP05 > 0) if @smsg > '' begin RAISERROR(@smsg, 16, 1) with nowait RETURN 43 end end if exists(select * from BLP1 with(nolock) where BLP02 = 22) set @mz_cfxz22 = 1 if exists(select * from BLP1 with(nolock) where BLP02 = 29) begin set @mz_cfxz29 = 1 set @mz_cfxz22 = 1 end --所有病人 if @tcount >= 1 and exists(select * from BLP1 with(nolock) where BLP02 = 28 ) begin select top 1 @smsg = a.VAF22+' 单次用量已经超出最大限制用量('+cast(a.BBY23 as varchar)+b.BDG02+').' from #TmpVAF a join BBX1 b with(nolock) on b.BBX01 = a.bbx01 where a.BDA01 in ('1','2','3') and a.BBY23 > 0 and a.VAF18 > 0 and a.VAF18 > a.BBY23 and (@asign=1 or (@asign=0 and a.rCurrent=1)) if @smsg > '' begin RAISERROR(@smsg, 16, 1) with nowait RETURN 28 end end --以下只限制门诊处方 if @acf01 = 1 and @tcount >= 1 and exists(select * from BLP1 with(nolock) where BLP02 = 17 ) begin set @mz_para161 = cast(dbo.GetSysParamValue(100,105002,161) as int) select top 1 @smsg = a.VAF22+' 根据总量与用量关系计算出的用药天数与医师开的用药天数不一致,不能保存.' from #TmpVAF a join BAG1 b with(nolock) on b.BBY01 = a.BBY01 join #TmpCBM c on c.CBMID = a.CBMID where a.BDA01 in ('1','2') and a.VAF21 >= 1 and a.VAF18 > 0 and a.VAF27 > 0 and abs((floor((a.VAF21 * b.BAG05* b.BAG03)/(a.VAF18 * a.VAF27)) - a.VAF17))>=@mz_para161 and @mz_para161>0 and (@asign=1 or (@asign=0 and a.rCurrent=1)) if @smsg > '' begin RAISERROR(@smsg, 16, 1) with nowait RETURN 17 end end if @acf01 = 1 and @tcount >= 1 and exists(select * from BLP1 with(nolock) where BLP02 = 20 ) begin select top 1 @smsg = a.VAF22+' 处方总量为零,违反医院要求处方药品总量不能为零的规定,不能保存.' from #TmpVAF a join #TmpCBM c on c.CBMID = a.CBMID where a.BDA01 in ('1','2') and a.VAF21 = 0 and (@asign=1 or (@asign=0 and a.rCurrent=1)) if @smsg > '' begin RAISERROR(@smsg, 16, 1) with nowait RETURN 20 end end if @acf01 = 2 and @tcount >= 1 and exists(select * from BLP1 with(nolock) where BLP02 = 38 ) begin select top 1 @smsg = a.VAF22+' 没有用药频次,不能保存.' from #TmpVAF a where a.BDA01 in ('1','2','3') and a.VAF11 = 2 and ISNULL(a.VAF26,'')='' if @smsg > '' begin RAISERROR(@smsg, 16, 1) with nowait RETURN 38 end end -- 检查申请单判断 if exists(select * from BLP1 with(nolock) where BLP02 = 34 and BLP06 > '' ) begin set @smsg = '' select top 1 @smsg = a.vaf22+'-- 请输入病历摘要.' from #TmpVAF a join BBX1 c with(nolock) on c.BBX01 = a.bbx01 left join #TmpVBQ d on d.VBQ01 = a.CBMID where a.BDA01 >= 'A' and isnull(d.vbq29,'')='' and ISNULL(c.BBX13,'')>'' and exists(select * from BLP1 e with(nolock) where e.BLP02=34 and e.BLP06=c.BBX13) if @smsg > '' begin RAISERROR(@smsg, 16, 1) with nowait RETURN 34 end end if exists(select * from BLP1 with(nolock) where BLP02 = 35 and BLP06 > '' ) begin set @smsg = '' select top 1 @smsg = a.vaf22+'-- 请输入辅助检查.' from #TmpVAF a join BBX1 c with(nolock) on c.BBX01 = a.bbx01 left join #TmpVBQ d on d.VBQ01 = a.CBMID where a.BDA01 >= 'A' and isnull(d.vbq30,'')='' and ISNULL(c.BBX13,'')>'' and exists(select * from BLP1 e with(nolock) where e.BLP02=35 and e.BLP06=c.BBX13) if @smsg > '' begin RAISERROR(@smsg, 16, 1) with nowait RETURN 35 end end if exists(select * from BLP1 with(nolock) where BLP02 = 36 and BLP06 > '' ) begin set @smsg = '' select top 1 @smsg = a.vaf22+'-- 请输入检查目的.' from #TmpVAF a join BBX1 c with(nolock) on c.BBX01 = a.bbx01 left join #TmpVBQ d on d.VBQ01 = a.CBMID where a.BDA01 >= 'A' and isnull(d.vbq27,'')='' and ISNULL(c.BBX13,'')>'' and exists(select * from BLP1 e with(nolock) where e.BLP02=36 and e.BLP06=c.BBX13 ) if @smsg > '' begin RAISERROR(@smsg, 16, 1) with nowait RETURN 36 end end --医保病人判断 if exists(select * from BLP1 with(nolock) where BLP02 = 91) set @mz_cfxz91 = 1 if @IsYbbr = 1 and @ACF01 = 1 begin if exists( select BLP02 from BLP1 with(nolock) where BLP02=151) set @fp_151=1 if exists(select * from BLP1 with(nolock) where BLP02 = 110) select @mz_cfxz110=BLP04 from BLP1 with(nolock) where BLP02 = 110 and BLP04>1 select @iaa01 = IAA01,@iay03=IAR19,@iay04=IAR45,@iak05 = m.IAK05,@iar16 = m.IAR16,@iai03=m.IAI03 from IAR1 m with(nolock) where m.IAR32 <> 2 and m.ACF01 =0 and m.VAA07 = @VAA07 set @iaa01 = ISNULL(@iaa01,0) set @iay03 = ISNULL(@iay03,'') if @iaa01>0 and exists(select * from BLP1 with(nolock) where BLP02=107) and @tcount>=1 begin if exists(select CBM06,COUNT(1) fnum from #TmpCBM where CBM06 in (1,4) group by CBM06 having COUNT(1)>1) begin set @smsg = '根据系统设定,医保病人当天院内同类处方不能超过一张.' RAISERROR(@smsg, 16, 1) with nowait RETURN 75 end if exists(select b.CBM06 from CBM1 b with(nolock) where b.CBM06 in (1,4) and datediff(day,b.CBM16,@edate)=0) begin set @smsg = '根据系统设定,医保病人当天院内同类处方不能超过一张.' RAISERROR(@smsg, 16, 1) with nowait RETURN 75 end end if @tcount >= 1 and exists(select * from BLP1 with(nolock) where BLP02=75 and BLP04>0) begin --判断医保目录内药品 set @smsg = '' select top 1 @smsg = a.VAF22 from #TmpVAF a where not exists(select * from IAM1 e with(nolock) join IAH1 f with(nolock) on f.IAH01 = e.IAH01 where e.IAA01=@iaa01 and e.BBY01 = a.bby01) if @smsg > '' begin set @smsg = @smsg+' 非医保目录内药品,医保病人不能使用.' RAISERROR(@smsg, 16, 1) with nowait RETURN 75 end end if @tcount >= 1 and exists(select * from BLP1 with(nolock) where BLP02 = 19 and ISNULL(BLP05,1)>=1) begin select @blp05 = BLP05,@smsg = '' from BLP1 where BLP02 = 19 and ISNULL(BLP05,1)>=1 select top 1 @smsg = a.VAF22+' 根据总量与用量计算的用药天数超过限定用药('+CAST(@blp05 as varchar)+')天数' +CAST(floor((a.VAF21 * b.BAG05* b.BAG03)/(a.VAF18 * a.VAF27)) - @blp05 as varchar)+'天.' from #TmpVAF a join BAG1 b with(nolock) on b.BBY01 = a.BBY01 join #TmpCBM c on c.CBMID = a.CBMID where c.BJW02 <> @bjw02 and a.BDA01 in ('1','2') and a.VAF21 >= 1 and a.VAF18 > 0 and a.VAF27 > 0 and ABS(floor((a.VAF21 * b.BAG05* b.BAG03)/(a.VAF18 * a.VAF27))) > @blp05 if @smsg > '' begin RAISERROR(@smsg, 16, 1) with nowait RETURN 19 end end if @tcount >= 1 and exists(select * from BLP1 with(nolock) where BLP02 = 8 and ISNULL(BLP05,1)>=1) begin select @blp05 = BLP05 from BLP1 with(nolock) where BLP02 = 8 and ISNULL(BLP05,1)>=1 if exists(select * from #TmpVAF c join #TmpCBM b on b.CBMID = c.CBMID where c.VAF17 > @blp05 and c.BDA01>='1' and c.BDA01<='2' and b.BJW02 <> @bjw02 and ((@mz_cfxz22<>1 and c.VAF21 >= 1)or(@mz_cfxz22=1 and c.VAF21 > 1)) ) begin if @fp_151>0 and exists(select * from #TmpVAF c join #TmpCBM b on b.CBMID = c.CBMID where c.VAF17 > @blp05 and c.BDA01>='1' and c.BDA01<='2' and b.BJW02 <> @bjw02 and c.VAF21>1 and (c.VAF17 - @blp05) @blp05 and c.BDA01>='1' and c.BDA01<='2' and b.BJW02 <> @bjw02 and c.VAF21>1 and (c.VAF17 - @blp05) @blp05 and c.BDA01>='1' and c.BDA01<='2' and b.BJW02 <> @bjw02 and ((@mz_cfxz22<>1 and c.VAF21 >= 1)or(@mz_cfxz22=1 and c.VAF21 > 1)) RAISERROR(@smsg, 16, 1) with nowait RETURN 8 end end end if @tcount >= 1 and @mz_cfxz91=0 and exists(select * from BLP1 where BLP02 = 16) begin if exists(select a.CBMID,f.IAH04,COUNT(1) fnum from #TmpVAF a join BBY1 c with(nolock) on c.BBY01 = a.bby01 join #TmpCBM d on d.CBMID = a.CBMID join IAM1 e with(nolock) on e.BBY01 = c.BBY01 and e.IAA01 = @iaa01 join IAH1 f with(nolock) on f.IAH01 = e.IAH01 where a.BDA01 in ('1','2') and d.BJW02 <> @bjw02 and (a.BBT18 <> 1 and a.BBT07<>1) and c.AAS01 in ('01','02') group by a.CBMID,f.IAH04 having COUNT(1)>=2 ) begin select top 1 @smsg = c.VAF22+' 违反院内医保处方不允许开相同规格药品规定.' from #TmpVAF c join IAM1 e with(nolock) on e.BBY01 = c.BBY01 and e.IAA01 = @iaa01 join IAH1 f with(nolock) on f.IAH01 = e.IAH01 join (select a.CBMID,f.IAH04,COUNT(1) fnum from #TmpVAF a join BBY1 c with(nolock) on c.BBY01 = a.bby01 join #TmpCBM d on d.CBMID = a.CBMID join IAM1 e with(nolock) on e.BBY01 = c.BBY01 and e.IAA01 = @iaa01 join IAH1 f with(nolock) on f.IAH01 = e.IAH01 where a.BDA01 in ('1','2') and d.BJW02 <> @bjw02 and (a.BBT18 <> 1 and a.BBT07<>1) and c.AAS01 in ('01','02') group by a.CBMID,f.IAH04 having COUNT(1)>=2) a on a.IAH04=f.IAH04 and a.CBMID = c.CBMID RAISERROR(@smsg, 16, 1) with nowait RETURN 16 end end if @tcount >= 1 and @mz_cfxz91=1 and exists(select * from BLP1 where BLP02 = 16) begin if exists(select a.CBMID,c.BBY11,COUNT(1) fnum from #TmpVAF a join BBY1 c with(nolock) on c.BBY01 = a.bby01 join #TmpCBM d on d.CBMID = a.CBMID where c.BBY11>'' and a.BDA01 in ('1','2') and d.BJW02 <> @bjw02 and (a.BBT18 <> 1 and a.BBT07<>1) and c.AAS01 in ('01','02') group by a.CBMID,c.BBY11 having COUNT(1)>=2 ) begin select top 1 @smsg = c.VAF22+' 违反院内医保处方不允许开相同规格药品规定.' from #TmpVAF c join BBY1 c1 with(nolock) on c1.BBY01=c.BBY01 join (select a.CBMID,c.BBY11,COUNT(1) fnum from #TmpVAF a join BBY1 c with(nolock) on c.BBY01 = a.bby01 join #TmpCBM d on d.CBMID = a.CBMID where c.BBY11>'' and a.BDA01 in ('1','2') and d.BJW02 <> @bjw02 and (a.BBT18 <> 1 and a.BBT07<>1) and c.AAS01 in ('01','02') group by a.CBMID,c.BBY11 having COUNT(1)>=2) a on a.BBY11=c1.BBY11 and a.CBMID = c.CBMID where c1.BBY11>'' RAISERROR(@smsg, 16, 1) with nowait RETURN 16 end end if @tcount >= 1 and @mz_cfxz91=0 and exists(select * from BLP1 where BLP02 = 7 ) begin if @tcount >= 1 and exists(select f.IAH04,COUNT(1) fnum from #TmpVAF a join BBY1 c with(nolock) on c.BBY01 = a.bby01 join #TmpCBM d on d.CBMID = a.CBMID join IAM1 e with(nolock) on e.BBY01 = c.BBY01 and e.IAA01 = @iaa01 join IAH1 f with(nolock) on f.IAH01 = e.IAH01 where (a.BBT18 <> 1 and a.BBT07<>1) and c.AAS01 in ('01','02') and a.BDA01 in ('1','2') and d.BJW02 <> @bjw02 group by f.IAH04 having COUNT(1)>=2 ) begin select top 1 @smsg = c.VAF22+' 违反院内医保处方当天不允许开相同规格药品规定.' from #TmpVAF c join IAM1 e with(nolock) on e.BBY01 = c.BBY01 and e.IAA01 = @iaa01 join IAH1 f with(nolock) on f.IAH01 = e.IAH01 join (select f.IAH04,COUNT(1) fnum from #TmpVAF a join BBY1 c with(nolock) on c.BBY01 = a.bby01 join #TmpCBM d on d.CBMID = a.CBMID join IAM1 e with(nolock) on e.BBY01 = c.BBY01 and e.IAA01 = @iaa01 join IAH1 f with(nolock) on f.IAH01 = e.IAH01 where (a.BBT18 <> 1 and a.BBT07<>1) and c.AAS01 in ('01','02') and a.BDA01 in ('1','2') and d.BJW02 <> @bjw02 group by f.IAH04 having COUNT(1)>=2 ) a on a.IAH04 = f.IAH04 join #TmpCBM s on s.cbmid = c.cbmid where s.bjw02 <> @bjw02 RAISERROR(@smsg, 16, 1) with nowait RETURN 7 end end if @tcount >= 1 and @mz_cfxz91=1 and exists(select * from BLP1 where BLP02 = 7 ) begin if @tcount >= 1 and exists(select c.BBY11,COUNT(1) fnum from #TmpVAF a join BBY1 c with(nolock) on c.BBY01 = a.bby01 join #TmpCBM d on d.CBMID = a.CBMID where c.BBY11>'' and (a.BBT18 <> 1 and a.BBT07<>1) and c.AAS01 in ('01','02') and a.BDA01 in ('1','2') and d.BJW02 <> @bjw02 group by c.BBY11 having COUNT(1)>=2 ) begin select top 1 @smsg = c.VAF22+' 违反院内医保处方当天不允许开相同规格药品规定.' from #TmpVAF c join BBY1 c1 with(nolock) on c1.BBY01=c.BBY01 join (select c.BBY11,COUNT(1) fnum from #TmpVAF a join BBY1 c with(nolock) on c.BBY01 = a.bby01 join #TmpCBM d on d.CBMID = a.CBMID where c.BBY11>'' and (a.BBT18 <> 1 and a.BBT07<>1) and c.AAS01 in ('01','02') and a.BDA01 in ('1','2') and d.BJW02 <> @bjw02 group by c.BBY11 having COUNT(1)>=2 ) a on a.BBY11 = c1.BBY11 join #TmpCBM s on s.cbmid = c.cbmid where s.bjw02 <> @bjw02 and c1.BBY11>'' RAISERROR(@smsg, 16, 1) with nowait RETURN 7 end end if exists(select * from BLP1 with(nolock) where BLP02 = 21 ) begin set @smsg = '' select top 1 @smsg = s.BBC01+'--'+s.BBC02+' 违反院内医保处方当天相同检查类型的诊疗项目一天内只允许开一次的限定' from BBC1 s join (select c.BBX13,COUNT(1) fnum from #TmpVAF a join BBX1 c with(nolock) on c.BBX01 = a.bbx01 join #TmpCBM d on d.CBMID = a.CBMID where a.BDA01 >= 'A' and a.Rownr < 1 and d.BJW02 <> @bjw02 and exists(select * from BLP1 e with(nolock) where e.BLP02=21 and e.BLP06=c.BBX13 and ISNULL(c.BBX13,'')<>'') group by c.BBX13 having COUNT(1)>=2 ) m on m.BBX13 = s.BBC01 if @smsg > '' begin RAISERROR(@smsg, 16, 1) with nowait RETURN 21 end end if @tcount >= 1 and exists(select * from BLP1 with(nolock) where BLP02 = 9 and ISNULL(BLP05,1)>=1 ) begin select @blp05 = BLP05 from BLP1 with(nolock) where BLP02 = 9 and ISNULL(BLP05,1)>=1 if exists(select a.* from (select CBMID,COUNT(1) fnum from #TmpVAO group by CBMID) a where a.fnum > @blp05) begin select top 1 @smsg = '处方诊断数已超过医保规定('+CAST(@blp05 as varchar)+')条数'+CAST(a.fnum - @blp05 as varchar)+'条.' from (select CBMID,COUNT(1) fnum from #TmpVAO group by CBMID) a where a.fnum > @blp05 RAISERROR(@smsg, 16, 1) with nowait RETURN 9 end end if @tcount >= 1 and exists(select * from BLP1 with(nolock) where BLP02 = 5 ) begin if exists( select s.CBMid,COUNT(1) tnum from( select a.CBMID,case when b.AAS01 in ('01','02') then 1 else 0 end taas01,COUNT(1) fnum from #TmpVAF a join BBY1 b with(nolock) on b.BBY01 = a.BBY01 join #TmpCBM c on c.CBMID = a.CBMID where a.BDA01>='1' and a.BDA01<='2' and c.BJW02 <> @bjw02 and not (@iay03 in ('31','32') and @iar16='143') group by a.CBMID,case when b.AAS01 in ('01','02') then 1 else 0 end ) s group by s.CBMID having COUNT(1) >= 2 ) begin set @smsg = '医保处方规定,自费和医保药品不能在同一张处方。' RAISERROR(@smsg, 16, 1) with nowait RETURN 5 end end if @tcount >= 1 and exists(select * from BLP1 with(nolock) where BLP02 = 47 ) begin if exists( select a.* from #TmpVAF a join BBY1 b with(nolock) on b.BBY01 = a.BBY01 join #TmpCBM c on c.CBMID = a.CBMID join BBT1 d with(nolock) on d.BBX01 = a.BBX01 where c.BJW02 <> @bjw02 and a.BDA01>='1' and a.BDA01<='2' and b.AAS01 not in ('01','02') and not (@iay03 in ('31','32') and @iar16='143') and (@asign=1 or (@asign=0 and a.rCurrent=1)) ) begin select top 1 @smsg = a.VAF22 from #TmpVAF a join BBY1 b with(nolock) on b.BBY01 = a.BBY01 join #TmpCBM c on c.CBMID = a.CBMID join BBT1 d with(nolock) on d.BBX01 = a.BBX01 where c.BJW02 <> @bjw02 and a.BDA01>='1' and a.BDA01<='2' and b.AAS01 not in ('01','02') and not (@iay03 in ('31','32') and @iar16='143') and (@asign=1 or (@asign=0 and a.rCurrent=1)) set @smsg = @smsg+' 医保保内处方不能下达自费药品,请改成医保保外处方。' RAISERROR(@smsg, 16, 1) with nowait RETURN 47 end end if @tcount >= 1 and exists(select * from BLP1 with(nolock) where BLP02 = 52 ) begin if exists( select a.* from #TmpVAF a join BBY1 b with(nolock) on b.BBY01 = a.BBY01 join #TmpCBM c on c.CBMID = a.CBMID join BBT1 d with(nolock) on d.BBX01 = a.BBX01 where c.BJW02 <> @bjw02 and a.BDA01 = '3' and d.BBT22 not in (2,3) and not (@iay03 in ('31','32') and @iar16='143') and (@asign=1 or (@asign=0 and a.rCurrent=1)) ) begin select top 1 @smsg = a.VAF22 from #TmpVAF a join BBY1 b with(nolock) on b.BBY01 = a.BBY01 join #TmpCBM c on c.CBMID = a.CBMID join BBT1 d with(nolock) on d.BBX01 = a.BBX01 where c.BJW02 <> @bjw02 and a.BDA01 = '3' and d.BBT22 not in (2,3) and not (@iay03 in ('31','32') and @iar16='143') and (@asign=1 or (@asign=0 and a.rCurrent=1)) set @smsg = @smsg+' 医保保内处方不能下达草药不可报药品,请改成医保保外处方。' RAISERROR(@smsg, 16, 1) with nowait RETURN 52 end end if exists(select * from BLP1 with(nolock) where BLP02 = 50 ) begin if exists( select a.* from #TmpVAF a join #TmpCBM c on c.CBMID = a.CBMID where c.BJW02 <> @bjw02 and a.VAF31 = 1 and not (@iay03 in ('31','32') and @iar16='143') and (@asign=1 or (@asign=0 and a.rCurrent=1)) ) begin select top 1 @smsg = a.VAF22 from #TmpVAF a join #TmpCBM c on c.CBMID = a.CBMID where c.BJW02 <> @bjw02 and a.VAF31 = 1 and not (@iay03 in ('31','32') and @iar16='143') and (@asign=1 or (@asign=0 and a.rCurrent=1)) set @smsg =@smsg+' 医保保内处方不能下达执行性质为自费的项目,请改成医保保外处方。' RAISERROR(@smsg, 16, 1) with nowait RETURN 50 end end if @tcount >= 1 and exists(select * from BLP1 with(nolock) where BLP02 = 18 ) begin if exists( select s.CBMid,COUNT(1) tnum from( select a.CBMID,case when b.BBT22 in (2,3) then 1 else 0 end taas01,COUNT(1) fnum from #TmpVAF a join BBT1 b with(nolock) on b.BBX01 = a.BBX01 join #TmpCBM c on c.CBMID = a.CBMID where a.BDA01='3' and c.BJW02 <> @bjw02 and not (@iay03 in ('31','32') and @iar16='143') and (@asign=1 or (@asign=0 and a.rCurrent=1)) group by a.CBMID,case when b.BBT22 in (2,3) then 1 else 0 end ) s group by s.CBMID having COUNT(1) >= 2 ) begin set @smsg = '医保草药处方规定,可报与不可报药品不能在同一张处方。' RAISERROR(@smsg, 16, 1) with nowait RETURN 18 end end if @tcount >= 1 and exists(select * from BLP1 with(nolock) where BLP02 = 6 ) begin if exists( select s.CBMid,COUNT(1) tnum from( select a.CBMID,case when b.BAM01 = '1' then 0 else 1 end tbam01,COUNT(1) fnum from #TmpVAF a join BBT1 b with(nolock) on b.BBX01 = a.BBX01 join BBY1 c with(nolock) on c.BBY01 = a.BBY01 join #TmpCBM d on d.cbmid = a.cbmid where c.AAS01 in ('01','02') and a.bda01 in ('1','2') and d.bjw02 <> @bjw02 group by a.CBMID,case when b.BAM01 = '1' then 0 else 1 end ) s group by s.CBMID having COUNT(1) >= 2 ) begin set @smsg = '医保处方规定,精神类和非精神类药品不能在同一张处方。' RAISERROR(@smsg, 16, 1) with nowait RETURN 6 end end if @tcount >= 1 and exists(select * from BLP1 with(nolock) where BLP02 = 10 and ISNULL(BLP05,1)>=1) and exists(select * from IAR1 m with(nolock) where m.VAA07=@VAA07 and m.IAR32 <> 2 and m.ACF01 =0 and m.IAI03 in ('18','19')) begin select @blp05 = BLP05 from BLP1 with(nolock) where BLP02 = 10 and ISNULL(BLP05,1)>=1 if exists(select * from #TmpVAF c join BBY1 b with(nolock) on b.BBY01 = c.BBY01 join #TmpCBM d on d.cbmid = c.cbmid where b.AAS01 in ('01','02') and c.VAF21 >= 1 and c.VAF17 > @blp05 and c.BDA01>='1' and c.BDA01<='2' and d.bjw02 <> @bjw02 and ((@mz_cfxz22<>1 and c.VAF21 >= 1)or(@mz_cfxz22=1 and c.VAF21 > 1)) ) begin if @fp_151>0 and exists(select * from #TmpVAF c join BBY1 b with(nolock) on b.BBY01 = c.BBY01 join #TmpCBM d on d.cbmid = c.cbmid where b.AAS01 in ('01','02') and c.VAF21 >= 1 and c.VAF17 > @blp05 and c.BDA01>='1' and c.BDA01<='2' and d.bjw02 <> @bjw02 and c.VAF21>1 and ((c.VAF17 - @blp05)= 1 and c.VAF17 > @blp05 and c.BDA01>='1' and c.BDA01<='2' and d.bjw02 <> @bjw02 and c.VAF21>1 and ((c.VAF17 - @blp05)= 1 and c.VAF17 > @blp05 and c.BDA01>='1' and c.BDA01<='2' and b.AAS01 in ('01','02') and d.bjw02 <> @bjw02 and ((@mz_cfxz22<>1 and c.VAF21 >= 1)or(@mz_cfxz22=1 and c.VAF21 > 1)) RAISERROR(@smsg, 16, 1) with nowait RETURN 10 end end end if @tcount >= 1 and exists(select * from BLP1 with(nolock) where BLP02 = 11 and ISNULL(BLP05,1)>=1) and exists(select a.CBMid from #TmpVAO a join #TmpCBM b on b.cbmid = a.cbmid where b.bjw02<>@bjw02 and a.VAO15 like '%行动不便%') begin select @blp05 = BLP05 from BLP1 with(nolock) where BLP02 = 11 and ISNULL(BLP05,1)>=1 if exists(select c.* from #TmpVAF c join BBY1 b with(nolock) on b.BBY01 = c.bby01 join #TmpCBM d on d.cbmid = c.cbmid where c.VAF21 >= 1 and c.VAF17 > @blp05 and c.BDA01>='1' and c.BDA01<='2' and b.AAS01 in ('01','02') and d.bjw02 <> @bjw02 and ((@mz_cfxz22<>1 and c.VAF21 >= 1)or(@mz_cfxz22=1 and c.VAF21 > 1)) and exists(select * from #TmpVAO a where a.CBMid = c.CBMID and a.VAO15 like '%行动不便%')) begin if @fp_151>0 and exists(select * from #TmpVAF c join BBY1 b with(nolock) on b.BBY01 = c.BBY01 join #TmpCBM d on d.cbmid = c.cbmid where b.AAS01 in ('01','02') and c.VAF21 >= 1 and c.VAF17 > @blp05 and c.BDA01>='1' and c.BDA01<='2' and d.bjw02 <> @bjw02 and c.VAF21>1 and ((c.VAF17 - @blp05)= 1 and c.VAF17 > @blp05 and c.BDA01>='1' and c.BDA01<='2' and d.bjw02 <> @bjw02 and c.VAF21>1 and ((c.VAF17 - @blp05)= 1 and c.VAF17 > @blp05 and c.BDA01>='1' and c.BDA01<='2' and b.AAS01 in ('01','02') and d.bjw02 <> @bjw02 and ((@mz_cfxz22<>1 and c.VAF21 >= 1)or(@mz_cfxz22=1 and c.VAF21 > 1)) and exists(select * from #TmpVAO a where a.CBMid = c.CBMID and a.VAO15 like '%行动不便%') RAISERROR(@smsg, 16, 1) with nowait RETURN 11 end end end if @tcount >= 1 and exists(select * from BLP1 with(nolock) where BLP02 = 13 ) and exists(select m.* from #TmpVAF m join BBT1 n with(nolock) on n.BBX01 = m.BBX01 join ( select b.cbmid,COUNT(1) fnum from #TmpVAF a join #TmpCBM b on b.cbmid = a.cbmid where a.bda01 = '3' and b.bjw02 <> @bjw02 and not (@iay03 in ('31','32') and @iar16='143') group by b.cbmid having COUNT(1) = 1 ) s on s.cbmid = m.cbmid where n.BBT22 = 2 and (@asign=1 or (@asign=0 and m.rCurrent=1)) ) begin select top 1 @smsg = m.VAF22+ '此药单方不可以报,请选择医保外处方。' from #TmpVAF m join BBT1 n with(nolock) on n.BBX01 = m.BBX01 join ( select b.cbmid,COUNT(1) fnum from #TmpVAF a join #TmpCBM b on b.cbmid = a.cbmid where a.bda01 = '3' and b.bjw02 <> @bjw02 and not (@iay03 in ('31','32') and @iar16='143') group by b.cbmid having COUNT(1) = 1 ) s on s.cbmid = m.cbmid where n.BBT22 = 2 and (@asign=1 or (@asign=0 and m.rCurrent=1)) RAISERROR(@smsg, 16, 1) with nowait RETURN 13 end if @tcount >= 1 and @iay03>'' and exists(select * from BLP1 with(nolock) where BLP02 = 14 and BLP06 = CAST(@iaa01 as varchar) and BLP03 = @iay03) and exists(select a.* from #TmpVAF a join BAG1 b with(nolock) on b.BBY01 = a.BBY01 where a.BDA01 >= '1' and a.BDA01 <= '2' and b.BAG47 = 1) begin select top 1 @smsg = a.VAF22 + '此药是大额药品,险种:'+@iay04+'被限制开此类大额药品.' from #TmpVAF a join BAG1 b with(nolock) on b.BBY01 = a.BBY01 where a.BDA01 >= '1' and a.BDA01 <= '2' and b.BAG47 = 1 RAISERROR(@smsg, 16, 1) with nowait RETURN 14 end if exists(select * from blp1 with(nolock) where blp02 =15) and exists(select * from IBK1 with(nolock) where bce01=@lbce01 and IBK04 >= IBK03) and exists(select * from #TmpCBM where BJW02 <> @bjw02) begin set @smsg = '您的本月医保分配基金已经用完,不能再开医保内处方.' RAISERROR(@smsg, 16, 1) with nowait RETURN 14 end end --********** set @BDP12 = 0 set @blp05 = 0 set @blp04 = 0 set @blp03 = 0 set @edate = GETDATE() if @ACF01 = 1 and exists(select * from BLP1 where blp02 in (0,1,2,3,26,12,23,7,21)) begin select a.VAJ04,a.VAJ05,a.VAF01,a.VAK01,a.VAI01,a.BBY01,a.VAJ25,a.VAJ34,a.VAJ38,a.VAJ46,a.VAJ47,a.ACF01,d.BJW02 into #kbmvaj from V_VAJ_1 a join VAA1 b on b.VAA01 = a.VAA01 join V_VAI_1 c on c.vai01=a.vai01 left join v_cbm_1 d on d.CBM01=c.CBM01 where b.IAK05 = @iak05 and a.VAJ05 <= 2 and a.VAJ47 >= @vaj47 and ((DATEDIFF(DAY,a.VAJ47,@edate)<>0 and a.VAK01 > 0)or(DATEDIFF(DAY,a.VAJ47,@edate)=0)) select a.BDA01,a.BBX01,a.BBY01,a.VAF17,a.VAF21,b.VAJ47,c.VAK13,(a.VAF17 - DATEDIFF(DAY,b.VAJ47,@edate)) svaf17,0 sflag,a.vaf01,a.vaf01a ,a.VAF27,a.VAF28,a.VAF29,a.VAF17 VAF17bak,d.BJW02 into #kbmvaf from V_VAF_1 a join #kbmvaj b on b.VAF01 = a.VAF01 join V_VAK_FULL c on c.VAK01 = b.VAK01 join V_CBM_1 d on d.CBM01=a.CBM01 union all select a.BDA01,a.BBX01,a.BBY01,a.VAF17,a.VAF21,a.VAF36,a.VAF36,(a.VAF17 - DATEDIFF(DAY,a.VAF36,@edate)) svaf17,1 sflag,a.vaf01,a.vaf01a ,a.VAF27,a.VAF28,a.VAF29,a.VAF17 VAF17bak,d.BJW02 from VAF1 a with(nolock) join VAA1 b with(nolock) on b.VAA01 = a.VAA01 join CBM1 d with(nolock) on d.CBM01=a.CBM01 where a.VAF10=1 and b.IAK05 = @iak05 and DATEDIFF(DAY,a.vaf36,@edate)=0 and a.CBM01 <> @upCBM01 --把频次为周转换为天 update #TmpVAF set VAF17 = VAF17 * 7 where VAF29 in ('W','周') and VAF17 > 0 update #kbmvaf set VAF17 = VAF17bak * 7,svaf17 = (VAF17bak * 7 - DATEDIFF(DAY,VAJ47,@edate)) where VAF29 in ('W','周') and VAF17bak > 0 if @IsYbbr=1 and @ACF01=1 begin --加载医保就诊记录 select a.ZAN05,b.ZAO05 IAH04,b.ZAO07,b.ZAO18 VAF17,a.ZAN04 VAJ47,(b.ZAO18 - DATEDIFF(DAY,a.ZAN04,@edate)) svaf17 into #kbmzan from ZAN1 a join ZAO1 b on b.ZAN01=a.ZAN01 where @mz_cfxz110>1 and a.DelFlag=0 and a.ZAN10=0 and a.ZAN07=@iak05 and b.ZAO07='0' if exists(select * from BLP1 with(nolock) where BLP02 = 21 ) begin set @smsg='' select top 1 @smsg=c.BBX13 +' : '+a.VAF22+' 违反院内医保处方当天相同检查类型的诊疗项目一天内只允许开一次的限定' from #TmpVAF a join BBX1 c with(nolock) on c.BBX01 = a.bbx01 join #TmpCBM d on d.CBMID = a.CBMID where a.BDA01 >= 'A' and a.Rownr < 1 and d.BJW02 <> @bjw02 and exists(select * from BLP1 e with(nolock) where e.BLP02=21 and e.BLP06=c.BBX13 and ISNULL(c.BBX13,'')<>'') and exists(select * from #kbmvaf m join BBX1 n with(nolock) on n.BBX01 = m.bbx01 where m.BDA01 >= 'A' and m.VAF01A = 0 and n.BBX13 = c.BBX13 and m.BJW02<>@bjw02 and DATEDIFF(DAY,m.VAJ47,@edate)=0) if @smsg > '' begin RAISERROR(@smsg, 16, 1) with nowait RETURN 21 end end if @tcount >= 1 and @mz_cfxz91=0 and @IAI03 not in ('18','19') and exists(select * from BLP1 with(nolock) where BLP02 = 7 ) begin if @tcount >= 1 and exists(select a.* from #TmpVAF a join BBY1 c with(nolock) on c.BBY01 = a.bby01 join #tmpcbm f on f.cbmid = a.cbmid join IAM1 m with(nolock) on m.BBY01 = c.BBY01 and m.IAA01 = @iaa01 join IAH1 n with(nolock) on n.IAH01 = m.IAH01 where a.BDA01 in ('1','2') and (a.BBT18 <> 1 and a.BBT07<>1) and c.AAS01 in ('01','02') and f.bjw02<>@bjw02 and ( exists(select d.* from #kbmvaj d join IAM1 m1 with(nolock) on m1.BBY01 = d.BBY01 and m1.IAA01 = @iaa01 join IAH1 n1 with(nolock) on n1.IAH01 = m1.IAH01 where n1.IAH04 = n.IAH04 and d.BJW02<>@bjw02 and DATEDIFF(DAY,d.vaj47,@edate)=0) or exists(select * from #kbmvaf d1 join IAM1 m2 with(nolock) on m2.BBY01 = d1.BBY01 and m2.IAA01 = @iaa01 join IAH1 n2 with(nolock) on n2.IAH01 = m2.IAH01 where d1.sflag = 1 and n2.IAH04=n.IAH04 and d1.BJW02<>@bjw02 and DATEDIFF(DAY,d1.vaj47,@edate)=0) ) ) begin select top 1 @smsg = a.VAF22+' 违反院内医保处方当天不允许开相同规格药品规定.' from #TmpVAF a join BBY1 c with(nolock) on c.BBY01 = a.bby01 join #tmpcbm f on f.cbmid = a.cbmid join IAM1 m with(nolock) on m.BBY01 = c.BBY01 and m.IAA01 = @iaa01 join IAH1 n with(nolock) on n.IAH01 = m.IAH01 where a.BDA01 in ('1','2') and (a.BBT18 <> 1 and a.BBT07<>1) and c.AAS01 in ('01','02') and f.bjw02<>@bjw02 and ( exists(select d.* from #kbmvaj d join IAM1 m1 with(nolock) on m1.BBY01 = d.BBY01 and m1.IAA01 = @iaa01 join IAH1 n1 with(nolock) on n1.IAH01 = m1.IAH01 where n1.IAH04 = n.IAH04 and d.BJW02<>@bjw02 and DATEDIFF(DAY,d.vaj47,@edate)=0) or exists(select * from #kbmvaf d1 join IAM1 m2 with(nolock) on m2.BBY01 = d1.BBY01 and m2.IAA01 = @iaa01 join IAH1 n2 with(nolock) on n2.IAH01 = m2.IAH01 where d1.sflag = 1 and n2.IAH04=n.IAH04 and d1.BJW02<>@bjw02 and DATEDIFF(DAY,d1.vaj47,@edate)=0) ) RAISERROR(@smsg, 16, 1) with nowait RETURN 7 end --医保中心记录判断 if @tcount >= 1 and @mz_cfxz110>1 and exists(select a.* from #TmpVAF a join BBY1 c with(nolock) on c.BBY01 = a.bby01 join #tmpcbm f on f.cbmid = a.cbmid join IAM1 m with(nolock) on m.BBY01 = c.BBY01 and m.IAA01 = @iaa01 join IAH1 n with(nolock) on n.IAH01 = m.IAH01 where a.BDA01 in ('1','2') and (a.BBT18 <> 1 and a.BBT07<>1) and c.AAS01 in ('01','02') and f.bjw02<>@bjw02 and exists(select d.* from #kbmzan d where d.IAH04 = n.IAH04 and DATEDIFF(DAY,d.vaj47,@edate)=0) ) begin select top 1 @smsg = '"'+a.VAF22+'"(跨院['+d.ZAN05+']就诊处方记录)违反医保处方当天不允许开相同规格药品规定.' from #TmpVAF a join BBY1 c with(nolock) on c.BBY01 = a.bby01 join #tmpcbm f on f.cbmid = a.cbmid join IAM1 m with(nolock) on m.BBY01 = c.BBY01 and m.IAA01 = @iaa01 join IAH1 n with(nolock) on n.IAH01 = m.IAH01 join #kbmzan d on d.IAH04=n.IAH04 and DATEDIFF(DAY,d.vaj47,@edate)=0 where a.BDA01 in ('1','2') and (a.BBT18 <> 1 and a.BBT07<>1) and c.AAS01 in ('01','02') and f.bjw02<>@bjw02 --and exists(select d.* from #kbmzan d -- where d.IAH04 = n.IAH04 and DATEDIFF(DAY,d.vaj47,@edate)=0) RAISERROR(@smsg, 16, 1) with nowait RETURN 7 end end if @tcount >= 1 and @mz_cfxz91=1 and exists(select * from BLP1 with(nolock) where BLP02 = 7 ) begin if @tcount >= 1 and exists(select a.* from #TmpVAF a join BBY1 c with(nolock) on c.BBY01 = a.bby01 join #tmpcbm f on f.cbmid = a.cbmid where c.BBY11>'' and a.BDA01 in ('1','2') and (a.BBT18 <> 1 and a.BBT07<>1) and c.AAS01 in ('01','02') and f.bjw02<>@bjw02 and ( exists(select d.* from #kbmvaj d join BBY1 m1 with(nolock) on m1.BBY01 = d.BBY01 and m1.BBY11>'' where c.BBY11=m1.BBY11 and d.BJW02<>@bjw02 and DATEDIFF(DAY,d.vaj47,@edate)=0) or exists(select * from #kbmvaf d1 join BBY1 m2 with(nolock) on m2.BBY01 = d1.BBY01 and m2.BBY11>'' where d1.sflag = 1 and c.BBY11=m2.BBY11 and d1.BJW02<>@bjw02 and DATEDIFF(DAY,d1.vaj47,@edate)=0) ) ) begin select top 1 @smsg = a.VAF22+' 违反院内医保处方当天不允许开相同规格药品规定.' from #TmpVAF a join BBY1 c with(nolock) on c.BBY01 = a.bby01 join #tmpcbm f on f.cbmid = a.cbmid where c.BBY11>'' and a.BDA01 in ('1','2') and (a.BBT18 <> 1 and a.BBT07<>1) and c.AAS01 in ('01','02') and f.bjw02<>@bjw02 and ( exists(select d.* from #kbmvaj d join BBY1 m1 with(nolock) on m1.BBY01 = d.BBY01 and m1.BBY11>'' where c.BBY11=m1.BBY11 and d.BJW02<>@bjw02 and DATEDIFF(DAY,d.vaj47,@edate)=0) or exists(select * from #kbmvaf d1 join BBY1 m2 with(nolock) on m2.BBY01 = d1.BBY01 and m2.BBY11>'' where d1.sflag = 1 and c.BBY11=m2.BBY11 and d1.BJW02<>@bjw02 and DATEDIFF(DAY,d1.vaj47,@edate)=0) ) RAISERROR(@smsg, 16, 1) with nowait RETURN 7 end end if @tcount >= 1 and @IAI03 not in ('18','19') and exists(select * from BLP1 with(nolock) where BLP02 = 23) begin set @blp05 = ISNULL((select BLP05 from BLP1 with(nolock) where BLP02 = 23),0) set @blp04 = ISNULL((select BLP04 from BLP1 with(nolock) where BLP02 = 23),0) if @mz_cfxz91=0 begin if (@blp05 > 0) and exists(select c.VAF22 from #TmpVAF c join #tmpcbm d on d.cbmid = c.cbmid join IAM1 e with(nolock) on e.BBY01 = c.BBY01 and e.IAA01 = @iaa01 join IAH1 f with(nolock) on f.IAH01 = e.IAH01 where c.BBT07 = 0 and c.BBT18=0 and c.bda01 in ('1','2') and d.BJW02 <> @bjw02 and ((@mz_cfxz22<>1 and c.VAF21 >= 1)or(@mz_cfxz22=1 and c.VAF21 > 1)) and c.VAF17 > @blp05 ) begin if @fp_151>0 and exists(select c.VAF22 from #TmpVAF c join #tmpcbm d on d.cbmid = c.cbmid join IAM1 e with(nolock) on e.BBY01 = c.BBY01 and e.IAA01 = @iaa01 join IAH1 f with(nolock) on f.IAH01 = e.IAH01 where c.BBT07 = 0 and c.BBT18=0 and c.bda01 in ('1','2') and d.BJW02 <> @bjw02 and c.VAF21>1 and c.VAF17 > @blp05 and ((c.VAF17 - @blp05) @bjw02 and c.VAF21>1 and c.VAF17 > @blp05 and ((c.VAF17 - @blp05) @bjw02 and ((@mz_cfxz22<>1 and c.VAF21 >= 1)or(@mz_cfxz22=1 and c.VAF21 > 1)) and c.VAF17 > @blp05 RAISERROR(@smsg, 16, 1) with nowait RETURN 23 end end if (@blp04 >= 1) and exists(select a.VAF22 from #TmpVAF a join #tmpcbm d on d.cbmid = a.cbmid join IAM1 e with(nolock) on e.BBY01 = a.BBY01 and e.IAA01 = @iaa01 join IAH1 f with(nolock) on f.IAH01 = e.IAH01 join (select f1.IAH04,b.svaf17 from #kbmvaf b join IAM1 e1 with(nolock) on e1.BBY01 = b.BBY01 and e1.IAA01 = @iaa01 join IAH1 f1 with(nolock) on f1.IAH01 = e1.IAH01 where b.BDA01 in ('1','2') and b.BJW02<>@bjw02 ) s on s.IAH04 = f.IAH04 where a.BDA01 in ('1','2') and (a.BBT18 <> 1 and a.BBT07<>1) and s.sVAF17 > @blp04 and d.BJW02 <> @bjw02) begin select top 1 @smsg = a.VAF22+' 距上次院内开药还有'+CAST(s.svaf17 as varchar)+'天用药量,请再过' +CAST(s.svaf17-@blp04 as varchar)+'天来医院开此药品.' from #TmpVAF a join #tmpcbm d on d.cbmid = a.cbmid join IAM1 e with(nolock) on e.BBY01 = a.BBY01 and e.IAA01 = @iaa01 join IAH1 f with(nolock) on f.IAH01 = e.IAH01 join (select f1.IAH04,b.svaf17 from #kbmvaf b join IAM1 e1 with(nolock) on e1.BBY01 = b.BBY01 and e1.IAA01 = @iaa01 join IAH1 f1 with(nolock) on f1.IAH01 = e1.IAH01 where b.BDA01 in ('1','2') and b.BJW02<>@bjw02 ) s on s.IAH04 = f.IAH04 where a.BDA01 in ('1','2') and (a.BBT18 <> 1 and a.BBT07<>1) and s.sVAF17 > @blp04 and d.BJW02 <> @bjw02 RAISERROR(@smsg, 16, 1) with nowait RETURN 23 end --医保中 if (@blp04 >= 1) and @mz_cfxz110>1 and exists(select a.VAF22 from #TmpVAF a join #tmpcbm d on d.cbmid = a.cbmid join IAM1 e with(nolock) on e.BBY01 = a.BBY01 and e.IAA01 = @iaa01 join IAH1 f with(nolock) on f.IAH01 = e.IAH01 join #kbmzan s on s.IAH04=f.IAH04 where a.BDA01 in ('1','2') and (a.BBT18 <> 1 and a.BBT07<>1) and s.sVAF17 > @blp04 and d.BJW02 <> @bjw02) begin select top 1 @smsg = '"'+a.VAF22+'" 距上次(跨院['+s.ZAN05+']就诊处方记录)开药还有'+CAST(s.svaf17 as varchar)+'天用药量,请再过' +CAST(s.svaf17-@blp04 as varchar)+'天来医院开此药品.' from #TmpVAF a join #tmpcbm d on d.cbmid = a.cbmid join IAM1 e with(nolock) on e.BBY01 = a.BBY01 and e.IAA01 = @iaa01 join IAH1 f with(nolock) on f.IAH01 = e.IAH01 join #kbmzan s on s.IAH04=f.IAH04 where a.BDA01 in ('1','2') and (a.BBT18 <> 1 and a.BBT07<>1) and s.sVAF17 > @blp04 and d.BJW02 <> @bjw02 order by s.svaf17 desc RAISERROR(@smsg, 16, 1) with nowait RETURN 23 end end --@mz_cfxz91=0 else begin if (@blp05 > 0) and exists(select c.VAF22 from #TmpVAF c join #tmpcbm d on d.cbmid = c.cbmid join BBY1 e with(nolock) on e.BBY01 = c.BBY01 and e.BBY11>'' where c.BBT07 = 0 and c.BBT18 = 0 and c.bda01 in ('1','2') and d.BJW02 <> @bjw02 and ((@mz_cfxz22<>1 and c.VAF21 >= 1)or(@mz_cfxz22=1 and c.VAF21 > 1)) and c.VAF17 > @blp05 ) begin if @fp_151>0 and exists(select c.VAF22 from #TmpVAF c join #tmpcbm d on d.cbmid = c.cbmid join BBY1 e with(nolock) on e.BBY01 = c.BBY01 and e.BBY11>'' where c.BBT07 = 0 and c.BBT18 = 0 and c.bda01 in ('1','2') and d.BJW02 <> @bjw02 and c.VAF21>1 and c.VAF17 > @blp05 and ((c.VAF17 - @blp05)'' where c.BBT07 = 0 and c.BBT18 = 0 and c.bda01 in ('1','2') and d.BJW02 <> @bjw02 and c.VAF21>1 and c.VAF17 > @blp05 and ((c.VAF17 - @blp05)'' where c.BDA01 in ('1','2') and c.BBT07 = 0 and c.BBT18 = 0 and d.BJW02 <> @bjw02 and ((@mz_cfxz22<>1 and c.VAF21 >= 1)or(@mz_cfxz22=1 and c.VAF21 > 1)) and c.VAF17 > @blp05 RAISERROR(@smsg, 16, 1) with nowait RETURN 23 end end if (@blp04 >= 1) and exists(select a.VAF22 from #TmpVAF a join #tmpcbm d on d.cbmid = a.cbmid join BBY1 e with(nolock) on e.BBY01 = a.BBY01 and e.BBY11>'' join (select c.BBY11,b.svaf17 from #kbmvaf b join BBY1 c with(nolock) on c.BBY01 = b.BBY01 and c.BBY11>'' where b.BDA01 in ('1','2') and b.BJW02<>@bjw02 ) s on s.BBY11=e.BBY11 where a.BDA01 in ('1','2') and (a.BBT18 <> 1 and a.BBT07<>1) and s.sVAF17 > @blp04 and d.BJW02 <> @bjw02) begin select top 1 @smsg = a.VAF22+' 距上次院内开药还有'+CAST(s.svaf17 as varchar)+'天用药量,请再过' +CAST(s.svaf17-@blp04 as varchar)+'天来医院开此药品.' from #TmpVAF a join #tmpcbm d on d.cbmid = a.cbmid join BBY1 e with(nolock) on e.BBY01 = a.BBY01 and e.BBY11>'' join (select c.BBY11,b.svaf17 from #kbmvaf b join BBY1 c with(nolock) on c.BBY01 = b.BBY01 and c.BBY11>'' where b.BDA01 in ('1','2') and b.BJW02<>@bjw02 ) s on s.BBY11=e.BBY11 where a.BDA01 in ('1','2') and (a.BBT18 <> 1 and a.BBT07<>1) and s.sVAF17 > @blp04 and d.BJW02 <> @bjw02 RAISERROR(@smsg, 16, 1) with nowait RETURN 23 end end--@mz_cfxz91=1 end--23 if @tcount >= 1 and exists(select * from BLP1 with(nolock) where BLP02 = 12) begin if exists(select * from IAR1 m with(nolock) where m.VAA07=@VAA07 and m.IAR32 <> 2 and m.ACF01 =0 and m.IAI03 in ('18','19')) and exists(select a.VAF22 from #TmpVAF a join #kbmvaf b on b.BBY01 = a.BBY01 and b.sflag = 0 join BBY1 c with(nolock) on c.BBY01 = a.BBY01 join #tmpcbm f on f.cbmid = a.cbmid where a.BDA01 in ('1','2') and c.AAS01 in ('01','02') and (a.BBT18 <> 1 and a.BBT07<>1) and b.sVAF17 >= 1 and a.VAF17 = 3 and f.bjw02 <> @bjw02 and b.BJW02<>@bjw02) begin select top 1 @smsg = a.VAF22+' 距上次院内开药还有'+CAST(b.svaf17 as varchar)+'天用药量,请再过' +CAST(b.svaf17 as varchar)+'天来医院开此药品.' from #TmpVAF a join #kbmvaf b on b.BBY01 = a.BBY01 and sflag = 0 join BBY1 c with(nolock) on c.BBY01 = a.BBY01 join #tmpcbm f on f.cbmid = a.cbmid where a.BDA01 in ('1','2') and c.AAS01 in ('01','02') and (a.BBT18 <> 1 and a.BBT07<>1) and b.sVAF17 >= 1 and a.VAF17 = 3 and f.bjw02 <> @bjw02 and b.BJW02<>@bjw02 RAISERROR(@smsg, 16, 1) with nowait RETURN 3 end if exists(select a.VAF22 from #TmpVAF a join #kbmvaf b on b.BBY01 = a.BBY01 and b.sflag = 0 join BBY1 c with(nolock) on c.BBY01 = a.BBY01 join #tmpcbm f on f.cbmid = a.cbmid where a.BDA01 in ('1','2') and c.AAS01 in ('01','02') and (a.BBT18 <> 1 and a.BBT07<>1) and b.sVAF17 > 2 and a.VAF17 > 3 and a.VAF17 <= 7 and f.bjw02 <> @bjw02 and b.BJW02<>@bjw02) begin select top 1 @smsg = a.VAF22+' 距上次院内开药还有'+CAST(b.svaf17 as varchar)+'天用药量,请再过' +CAST(b.svaf17-2 as varchar)+'天来医院开此药品.' from #TmpVAF a join #kbmvaf b on b.BBY01 = a.BBY01 and b.sflag = 0 join BBY1 c with(nolock) on c.BBY01 = a.BBY01 join #tmpcbm f on f.cbmid = a.cbmid where a.BDA01 in ('1','2') and c.AAS01 in ('01','02') and (a.BBT18 <> 1 and a.BBT07<>1) and b.sVAF17 > 2 and a.VAF17 > 3 and a.VAF17 <= 7 and f.bjw02 <> @bjw02 and b.BJW02<>@bjw02 RAISERROR(@smsg, 16, 1) with nowait RETURN 3 end if exists(select a.VAF22 from #TmpVAF a join #kbmvaf b on b.BBY01 = a.BBY01 and b.sflag = 0 join BBY1 c with(nolock) on c.BBY01 = a.BBY01 join #tmpcbm f on f.cbmid = a.cbmid where a.BDA01 in ('1','2') and c.AAS01 in ('01','02') and (a.BBT18 <> 1 and a.BBT07<>1) and b.sVAF17 > 3 and a.VAF17 > 7 and a.VAF17 <= 14 and f.bjw02 <> @bjw02 and b.BJW02<>@bjw02) begin select top 1 @smsg = a.VAF22+' 距上次院内开药还有'+CAST(b.svaf17 as varchar)+'天用药量,请再过' +CAST(b.svaf17-3 as varchar)+'天来医院开此药品.' from #TmpVAF a join #kbmvaf b on b.BBY01 = a.BBY01 and b.sflag = 0 join BBY1 c with(nolock) on c.BBY01 = a.BBY01 join #tmpcbm f on f.cbmid = a.cbmid where a.BDA01 in ('1','2') and c.AAS01 in ('01','02') and (a.BBT18 <> 1 and a.BBT07<>1) and b.sVAF17 > 3 and a.VAF17 > 7 and a.VAF17 <= 14 and f.bjw02 <> @bjw02 and b.BJW02<>@bjw02 RAISERROR(@smsg, 16, 1) with nowait RETURN 3 end if exists(select a.VAF22 from #TmpVAF a join #kbmvaf b on b.BBY01 = a.BBY01 and b.sflag = 0 join BBY1 c with(nolock) on c.BBY01 = a.BBY01 join #tmpcbm f on f.cbmid = a.cbmid where a.BDA01 in ('1','2') and c.AAS01 in ('01','02') and (a.BBT18 <> 1 and a.BBT07<>1) and b.sVAF17 > 4 and a.VAF17 > 14 and f.bjw02 <> @bjw02 and b.BJW02<>@bjw02) begin select top 1 @smsg = a.VAF22+' 距上次院内开药还有'+CAST(b.svaf17 as varchar)+'天用药量,请再过' +CAST(b.svaf17-4 as varchar)+'天来医院开此药品.' from #TmpVAF a join #kbmvaf b on b.BBY01 = a.BBY01 and b.sflag = 0 join BBY1 c with(nolock) on c.BBY01 = a.BBY01 join #tmpcbm f on f.cbmid = a.cbmid where a.BDA01 in ('1','2') and c.AAS01 in ('01','02') and (a.BBT18 <> 1 and a.BBT07<>1) and b.sVAF17 > 4 and a.VAF17 > 14 and f.bjw02 <> @bjw02 and b.BJW02<>@bjw02 RAISERROR(@smsg, 16, 1) with nowait RETURN 3 end end if exists(select * from BLP1 with(nolock) where BLP02 = 26 ) begin set @smsg = '' select top 1 @smsg = case when s.VAF35=1 then '==' else '' end +s.VAF22 + ' 违反院内当天不能重复开处方的限定,是否继续保存医嘱?' from #TmpVAF s join (select a.BBX01,COUNT(1) fnum from #TmpVAF a join #TmpCBM d on d.CBMID = a.CBMID where a.BDA01 >= 'A' and a.rownr < 1 and d.BJW02 <> @bjw02 and a.VAF32 = 0 and exists(select * from BLP1 e with(nolock) where e.BLP02=26 and e.BLP06=a.bda01) group by a.BBX01 having COUNT(1)>=2 ) m on m.bbx01 = s.bbx01 where s.VAF32 = 0 if @smsg > '' begin RAISERROR(@smsg, 16, 1) with nowait RETURN 26 end end if exists(select * from BLP1 with(nolock) where BLP02 = 26 ) begin set @smsg = '' select top 1 @smsg = case when a.VAF35=1 then '==' else '' end + a.VAF22 + ' 违反院内当天不能重复开处方的限定,是否继续保存医嘱?' from #TmpVAF a join #TmpCBM d on d.CBMID = a.CBMID where a.BDA01 >= 'A' and a.rownr < 1 and d.BJW02 <> @bjw02 and a.VAF32=0 and exists(select * from BLP1 e with(nolock) where e.BLP02=26 and e.BLP06=a.bda01) and exists(select * from #kbmvaf m where m.BDA01 >= 'A' and m.VAF01A = 0 and a.VAF32=0 and m.BDA01 = a.BDA01 and m.BJW02<>@bjw02 and m.BBX01 = a.BBX01 and DATEDIFF(DAY,m.VAJ47,@edate)=0) if @smsg > '' begin RAISERROR(@smsg, 16, 1) with nowait RETURN 26 end end end if exists(select * from BLP1 with(nolock) where BLP02 = 3) set @blp03 = 1 set @blp05 = ISNULL((select BLP05 from BLP1 with(nolock) where BLP02 = 3),0) set @blp04 = ISNULL((select BLP04 from BLP1 with(nolock) where BLP02 = 3),0) select @BDP12=BDP12 from BDP1 with(nolock) where BDP02=@bdp02 set @BDP12 = ISNULL(@BDP12,0) if @BDP12 < 1 or @tcount < 1 return 0 update a set a.BAK02 = b.BAK02 from #TmpVAO a join BAK1 b with(nolock) on b.BAK01 = a.BAK01A select a.BLP02,d.BBY01,a.BLP04,a.BLP05 into #tmpbby from BLP1 a with(nolock) join BBX1 b with(nolock) on b.BBX01 = a.BLP03 join BAG1 c with(nolock) on c.BBX01 = b.BBX01 join BBY1 d with(nolock) on d.BBY01 = c.BBY01 where ISNULL(a.BLP03,'')<>'' and a.BLP02 = 1 and a.BLP04 >= 0 and a.BLP05 > 0 and d.BBY31 > @edate union all select a.BLP02,b.BBY01,a.BLP04,a.BLP05 from BLP1 a with(nolock) join BBY1 b with(nolock) on b.BBY01 = a.BLP03 where ISNULL(a.BLP03,'')<>'' and a.BLP02 = 0 and a.BLP04 >= 0 and a.BLP05 > 0 and b.BBY31 > @edate select a.BLP02,b.BAK02,a.BLP04,a.BLP05 into #kbmvao from BLP1 a with(nolock) join BAK1 b with(nolock) on b.BAK02 = a.BLP03 where a.BLP02 = 2 and a.BLP04 >= 0 and a.BLP05 > 0 --用药天数限制 if (@blp05 > 0) and (@blp03 = 1) and exists(select c.VAF22 from #TmpVAF c where c.BDA01 in ('1','2') and c.BBT07 = 0 and c.VAF17 > @blp05 and ((@mz_cfxz29<>1 and c.VAF21 >= 1)or(@mz_cfxz29=1 and c.VAF21 > 1)) ) begin select top 1 @smsg = c.VAF22+' 用药天数超过限定用药('+CAST(@blp05 as varchar)+')天数'+CAST(c.VAF17 - @blp05 as varchar)+'天.' from #TmpVAF c where c.BDA01 in ('1','2') and c.BBT07 = 0 and c.BBT18 = 0 and c.VAF17 > @blp05 and ((@mz_cfxz29<>1 and c.VAF21 >= 1)or(@mz_cfxz29=1 and c.VAF21 > 1)) RAISERROR(@smsg, 16, 1) with nowait RETURN 2 end if (@blp04 >= 1)and (@blp03 = 1)and exists(select a.VAF22 from #TmpVAF a join #kbmvaf b on b.BBY01 = a.BBY01 and b.sflag = 0 where a.BDA01 in ('1','2') and b.sVAF17 > @blp04) begin select top 1 @smsg = a.VAF22+' 距上次开药还有'+CAST(b.svaf17 as varchar)+'天用药量,请再过' +CAST(b.svaf17-@blp04 as varchar)+'天来医院开此药品.' from #TmpVAF a join #kbmvaf b on b.BBY01 = a.BBY01 and b.sflag = 0 where a.BDA01 in ('1','2') and b.svaf17 > @blp04 RAISERROR(@smsg, 16, 1) with nowait RETURN 3 end --(@blp03 = 0) and if exists(select c.VAF22 from #tmpbby a join #TmpVAF c on c.BBY01 = a.BBY01 and c.BBT07 = 0 and c.BBT18 = 0 where c.BDA01 in ('1','2') and c.VAF17 > a.BLP05 and ((@mz_cfxz29<>1 and c.VAF21 >= 1)or(@mz_cfxz29=1 and c.VAF21 > 1)) ) begin select top 1 @smsg = c.VAF22+' 用药天数超过限定用药('+CAST(a.BLP05 as varchar)+')天数'+CAST(c.VAF17 - a.BLP05 as varchar)+'天.' from #tmpbby a join #TmpVAF c on c.BBY01 = a.BBY01 and c.BBT07 = 0 and c.BBT18 = 0 where c.BDA01 in ('1','2') and c.VAF17 > a.BLP05 and ((@mz_cfxz29<>1 and c.VAF21 >= 1)or(@mz_cfxz29=1 and c.VAF21 > 1)) RAISERROR(@smsg, 16, 1) with nowait RETURN 2 end -- if exists(select a.VAF22 from #TmpVAF a join #kbmvaf b on b.BBY01 = a.BBY01 and b.sflag = 0 join #tmpbby c on c.BBY01 = a.BBY01 where a.BDA01 in ('1','2') and b.svaf17 > c.BLP04) begin select top 1 @smsg = a.VAF22+' 距上次开药还有'+CAST(b.svaf17 as varchar)+'天用药量,请再过' +CAST(b.svaf17-c.BLP04 as varchar)+'天来医院开此药品.' from #TmpVAF a join #kbmvaf b on b.BBY01 = a.BBY01 and b.sflag = 0 join #tmpbby c on c.BBY01 = a.BBY01 where a.BDA01 in ('1','2') and b.svaf17 > c.BLP04 RAISERROR(@smsg, 16, 1) with nowait RETURN 3 end end -- end if acf01=1 --根据用药目的对应提醒内容进行提醒 if @tcount >= 1 and @ACF01=2 and @asign=0 and not exists(select * from BLP1 with(nolock) where BLP02 in (40,41)) and exists(select * from #TmpVAF a join BIW1 b on b.BIW02 = a.biw02 where a.rCurrent = 1 and b.BIW06>'') begin insert into @tbmsg_yb(vaf22,smsg) select a.vaf22,'用药目的提醒:'+b.BIW06 from #TmpVAF a join BIW1 b with(nolock) on b.BIW02 = a.biw02 where a.rCurrent = 1 and b.BIW06>'' /* set @smsg='=='+@smsg RAISERROR(@smsg, 16, 1) with nowait RETURN 25 */ end --提醒 if @tcount >= 1 and @ACF01=2 and exists(select * from BLP1 with(nolock) where BLP02=25) and exists(select * from #TmpVAF a join BBT1 b with(nolock) on b.BBX01 = a.BBX01 where b.BBT11>=1) begin if exists(select VAF01 from VAF2 with(nolock) where VAF06=@VAA07 and VAF10 in (8,9) and BDA01 = 'S') begin insert into @tbmsg_yb(VAF22,smsg) select '手术切口信息提醒' ,'手术切口信息提醒:'+'I类切口预防使用抗菌素药品请注意在术前0.5-2小时内使用;'+CHAR(13)+CHAR(10) +'一般外科I类切口手术术后使用抗菌素药品请注意不超过24小时,'+CHAR(13)+CHAR(10) +'心外、神外等深部大型手术请在术后48-72小时内停用抗菌素类药品,超过规定的请在病程记录中说明理由。' --RAISERROR(@smsg, 16, 1) with nowait -- RETURN 25 end end if @asign = 0 and @IsYbbr=1 and exists(select * from BLP1 with(nolock) where BLP02=55) begin set @smsg = '' select top 1 @smsg = f.IAH44 from #TmpVAF a join IAM1 e with(nolock) on e.BBY01 = a.BBY01 and e.IAA01 = @iaa01 join IAH1 f with(nolock) on f.IAH01 = e.IAH01 where f.IAH44 > '' if @smsg > '' begin insert into @tbmsg_yb(VAF22,smsg) select '医保相关信息提醒','医保相关信息提醒:'+@smsg -- set @smsg = '=='+@smsg -- RAISERROR(@smsg, 16, 1) with nowait -- RETURN 55 end end set @mzPara146 = cast(dbo.GetSysParamValue(100,105002,146) as int) if @ACF01=1 and @mzPara146>0 and exists(select * from #TmpCBM where CBM06 in (1,4)) begin declare @kzcs134 varchar(1) set @kzcs134='0' if exists(select * from BLP1 where BLP02=134) set @kzcs134='1' set @mzPara147 = dbo.GetSysParamValue(100,105002,147) if exists(select b.CBMID,COUNT(1) fnum from #TmpCBM a join #TmpVAF b on b.CBMID=a.CBMID where a.CBM06 in (1,4) and b.rUpdate=0 and (@kzcs134='1' or (@kzcs134='0' and b.BBT18=0)) and b.BDA01 in ('1','2') group by b.CBMID having COUNT(1) > @mzPara146) begin set @smsg='' select top 1 @smsg=case when s.CBMID =-1 then '第一张处方' else '第'+CAST(s.CBMID+3 as varchar)+'张处方' end from( select b.CBMID,COUNT(1) fnum from #TmpCBM a join #TmpVAF b on b.CBMID=a.CBMID where a.CBM06 in (1,4) and b.rUpdate=0 and (@kzcs134='1' or (@kzcs134='0' and b.BBT18=0)) and b.BDA01 in ('1','2') group by b.CBMID having COUNT(1) > @mzPara146 ) s if @mzPara147='1' begin set @smsg=@smsg+'药品数量已达到超过规定条数,请调整。' RAISERROR(@smsg, 16, 1) with nowait RETURN 56 end else begin -- set @smsg='==提示:'+@smsg+'药品数量已达到超过规定条数.' insert into @tbmsg_yb(VAF22,smsg) select '处方药品条数限制提醒:','处方药品条数限制提醒:'+@smsg+'药品数量已达到超过规定条数.' end end end if @acf01 = 1 and @tcount >= 1 and exists(select * from BLP1 with(nolock) where BLP02 = 129 ) begin select top 1 @smsg = a.VAF22+' 根据总量与用量关系计算出的用药天数与医师开的用药天数不一致.' from #TmpVAF a join BAG1 b with(nolock) on b.BBY01 = a.BBY01 join #TmpCBM c on c.CBMID = a.CBMID where a.BDA01 in ('1','2') and a.VAF21 >= 1 and a.VAF18 > 0 and a.VAF27 > 0 and ceiling(abs((((a.VAF21 * b.BAG05* b.BAG03)/(a.VAF18 * a.VAF27)) - a.VAF17)))>=1 and @mz_para161>0 and (@asign=1 or (@asign=0 and a.rCurrent=1)) if @smsg > '' begin insert into @tbmsg_yb(VAF22,smsg) select '药品处方天数提示','药品处方天数提醒:'+@smsg --RAISERROR(@smsg, 16, 1) with nowait --RETURN 129 end end --判断当前选择批次库存数量 if @fp_157=1 and @tcount>=1 and @ACF01>0 and exists(select * from #TmpVAF where VAF11=2 and DSK01>0 and BDA01 in ('1','2')) begin if exists(select b.* from DPT1 a with(nolock) join (select DSK01,BCK01B,SUM(VAJ25) fvaj25 from #TmpVAF where VAF11=2 and DSK01>0 and BDA01 in ('1','2') group by DSK01,BCK01B) b on b.DSK01=a.DSK_ID and b.BCK01B=a.BCK01 where a.Quantity-b.fvaj25<0 ) begin set @smsg='' select @smsg=@smsg+'; '+c.BBY05 from DPT1 a with(nolock) join (select DSK01,BCK01B,SUM(VAJ25) fvaj25 from #TmpVAF where VAF11=2 and DSK01>0 and BDA01 in ('1','2') group by DSK01,BCK01B) b on b.DSK01=a.DSK_ID and b.BCK01B=a.BCK01 join BBY1 c with(nolock) on c.BBY01=a.BBY01 where a.Quantity-b.fvaj25<0 if @smsg>'' begin insert into @tbmsg_yb(VAF22,smsg) select '药品库存提醒','药品库存提醒:'+@smsg+' 所选批次对应库存数量已不足,但可以继续保持发送医嘱.' end end end if exists(select * from @tbmsg_yb) begin set @smsg = '' select @smsg=@smsg+smsg++CHAR(13)+CHAR(10) from @tbmsg_yb where smsg>'' set @smsg='=='+@smsg RAISERROR(@smsg, 16, 1) with nowait RETURN 999 end