SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO --alter 2016.8.22 ALTER proc [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 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(12,4)) BBY23,@VAA07 VAF06,* INTO #TmpVAF from OpenXml(@iDOM,'/Root/VAF1/Ie',8) WITH (CBMID int ,ROWNR int ,VAF11 tinyint ,BDA01 varchar(2) ,BBX01 int ,BBY01 int ,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 ) where lSign = 0 order by cbmid,vaf59,rownr 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) ) 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) ) exec sp_xml_removedocument @iDOM declare @gpara69 varchar(10),@adate datetime,@edate datetime,@tcount int,@smsg varchar(128),@vaj47 datetime declare @kbmBAG table(BBY01 int,BAG46 numeric(18,2)) declare @zyPara386 varchar(10),@para356_392 varchar(10),@mzPara153 varchar(10),@IsYbbr tinyint set @IsYbbr = 0 set @smsg = '' --返回的提示消息,默认禁止发送医嘱,当内容以“==”开头时,提示后依然发送医嘱 set @edate = GETDATE() set @vaj47 = dateadd(month,-2,@edate) update #TmpVAF set BCK01B = 0 where @asign = 0 and rCurrent = 1 --限制最大销售数量或金额判断 set @gpara69 = dbo.GetSysParamValue(9999,9999,69) --限定药品最大销售额判断 0:不限制 1=按当月销售额计算 2=按当月销售数量计算 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) --抗生素类药品必须输入用药目的 if exists(select a.VAC01 from VAC1 a join BDP1 b on b.BDP02 = a.BDP02 where a.VAC01 = @VAA07 and b.BDP07 = 1) set @IsYbbr = 1 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) --抗生素类药品必须输入用药目的 if exists(select a.VAE01 from VAE1 a join BDP1 b on b.BDP02 = a.BDP02 where a.VAE01 = @VAA07 and b.BDP07 = 1) set @IsYbbr = 1 end --判断药品有效期 if exists(select a.bby01 from #tmpVAF a join BBY1 b 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 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 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 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 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 on b.BBY01 = a.BBY01 where a.BDA01 in ('4','M') and c.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 join DPT1 b 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 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 * from #TmpVAF a where a.BDA01 in ('S','0') and exists(select * from BAZ1 b where b.BCK01 = a.BCK01B and BAU01 = '03')) begin if @zyPara386 = '1' begin if exists(select * from #TmpVAF a where a.BDA01 = 'S' and exists(select * from BAZ1 b 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 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 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 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 where BLP02 = 30) and exists(select * from #TmpVAF a where a.BDA01 in ('S','0') and exists(select * from BAZ1 b 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 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 select @tcount = COUNT(*) from #TmpVAF where BDA01 >= '1' AND BDA01 <= '3' --西成药品医嘱行数 --抗生素类药品用药目的判断 if @tcount >= 1 and @para356_392='1' begin if exists(select a.* from #TmpVAF a join BBT1 b on b.BBX01 = a.BBX01 where b.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 join BBT1 b on b.BBX01 = a.BBX01 where b.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 exists(select * from BLP1 where BLP02 in (40,41)) and exists(select * from #TmpVAF a join BBT1 b on b.BBX01 = a.BBX01 where b.BBT11>=1) begin declare @tvaf36 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 join #TmpVAF b on b.bby01 = a.BBY01 join BBT1 c 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 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 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 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 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 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 on c.BBX01 = a.bbx01 where c.BBT11 >= 1 and not exists(select * from #tmpvdz b where b.BBY01 = a.bby01)) begin set @smsg = '' 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小时,不能再使用抗菌素药品。'; RAISERROR(@smsg, 16, 1) with nowait RETURN 40 end end select @tvaf36 = MAX(VAF36) from #kbVAF where BDA01 in ('S','0') if exists(select * from BLP1 where BLP02 =41) and exists(select b.VAF01 from #kbVAF b join VBF1 c on c.VAF01 = b.VAF01 where b.BDA01 in ('S','0') and b.VAF36 = @tvaf36 and DATEDIFF(HOUR,@edate,CAST(b.VAF14 as datetime))>2 and c.VBF03='切口等级' and c.VBF07='Ⅰ' ) 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 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 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 join VAE1 b on b.VAE01 =a.VAF06 where a.vaf36 <= b.VAE11) begin select @smsg = a.VAF22+' 医嘱开始时间不能小于入院时间.' from #tmpVAF a join VAE1 b on b.VAE01 =a.VAF06 where a.vaf36 <= b.VAE11 set @smsg = @smsg+',不能下达此医嘱.' RAISERROR(@smsg, 16, 1) with nowait RETURN 1 end --只判断西成药品处方 update a set a.BAG46 = isnull(b.BAG46,0),a.BBT07 = ISNULL(c.BBT07,0),a.BBY23 = ISNULL(d.BBY23,0) from #TmpVAF a join BAG1 b on b.BBY01 = a.BBY01 join BBT1 c on c.BBX01 = b.BBX01 join bby1 d on d.bby01 = a.bby01 where @tcount>=1 and a.BDA01>='1' and a.BDA01<='2' if @gpara69 > '0' and @tcount >= 1 and exists(select * from #TmpVAF where BAG46 > 1 and BDA01 in ('1','2')) begin 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 into #tmpvaj from V_VAJ_FULL a where a.BDN01 <= '2' and a.VAJ05 < 9 and a.VAJ47 >= @adate and ((a.ACF01 = 2)or(a.ACF01 = 1 and VAK01 > 0)) 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 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 not exists(select * from BLP1) return 0 declare @BDP12 int,@blp05 int,@blp04 int,@blp03 tinyint,@bjw02 varchar(30),@iaa01 int,@iay03 varchar(20),@iay04 varchar(50) declare @mz_para161 int,@mz_cfxz22 int,@iak05 varchar(20),@mz_cfxz29 int,@bdp02 varchar(50),@bck01 int,@iar16 varchar(20) set @BDP12 = 0 set @blp05 = 0 set @blp04 = 0 set @blp03 = 0 set @bjw02 = '医保外处方' set @mz_cfxz22 = 0 set @mz_cfxz29 = 0 set @iaa01=0 if @IsYbbr=1 and @ACF01=2 and exists(select * from BLP1 where BLP02=75 and BLP05 > 0) and @tcount>=1 begin select @iaa01 = IAA01 from IAR1 m 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 join IAH1 f 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 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 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 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 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 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 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 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 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 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 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 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 where BLP02 = 22) set @mz_cfxz22 = 1 if exists(select * from BLP1 where BLP02 = 29) begin set @mz_cfxz29 = 1 set @mz_cfxz22 = 1 end --所有病人 if @tcount >= 1 and exists(select * from BLP1 where BLP02 = 28 ) begin select top 1 @smsg = a.VAF22+' 单次用量已经超出最大限制用量('+cast(a.BBY23 as varchar)+b.BDG02+').' from #TmpVAF a join BBX1 b on b.BBX01 = a.bbx01 where a.BDA01 in ('1','2') 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 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 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 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 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 where BLP02 = 34 and BLP06 > '' ) begin set @smsg = '' select top 1 @smsg = a.vaf22+'-- 请输入病历摘要.' from #TmpVAF a join BBX1 c 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 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 where BLP02 = 35 and BLP06 > '' ) begin set @smsg = '' select top 1 @smsg = a.vaf22+'-- 请输入辅助检查.' from #TmpVAF a join BBX1 c 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 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 where BLP02 = 36 and BLP06 > '' ) begin set @smsg = '' select top 1 @smsg = a.vaf22+'-- 请输入检查目的.' from #TmpVAF a join BBX1 c 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 where e.BLP02=36 and e.BLP06=c.BBX13 ) if @smsg > '' begin RAISERROR(@smsg, 16, 1) with nowait RETURN 36 end end --医保病人判断 if @IsYbbr = 1 and @ACF01 = 1 begin select @iaa01 = IAA01,@iay03=IAR19,@iay04=IAR45,@iak05 = m.IAK05,@iar16 = m.IAR16 from IAR1 m where m.IAR32 <> 2 and m.ACF01 =0 and m.VAA07 = @VAA07 set @iaa01 = ISNULL(@iaa01,0) set @iay03 = ISNULL(@iay03,'') if @tcount >= 1 and exists(select * from BLP1 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 join IAH1 f 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 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 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 where BLP02 = 8 and ISNULL(BLP05,1)>=1) begin select @blp05 = BLP05 from BLP1 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 select top 1 @smsg = c.VAF22+' 用药天数超过医保限定用药('+CAST(@blp05 as varchar)+')天数'+CAST(c.VAF17 - @blp05 as varchar)+'天.' 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)) RAISERROR(@smsg, 16, 1) with nowait RETURN 8 end end if @tcount >= 1 and exists(select * from BLP1 where BLP02 = 16) begin if exists(select a.CBMID,f.IAH04,COUNT(1) fnum from #TmpVAF a join BBT1 b on b.BBX01 = a.BBX01 join BBY1 c on c.BBY01 = a.bby01 join #TmpCBM d on d.CBMID = a.CBMID join IAM1 e on e.BBY01 = c.BBY01 and e.IAA01 = @iaa01 join IAH1 f on f.IAH01 = e.IAH01 where a.BDA01 in ('1','2') and d.BJW02 <> @bjw02 and (b.BBT18 <> 1 and b.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 on e.BBY01 = c.BBY01 and e.IAA01 = @iaa01 join IAH1 f on f.IAH01 = e.IAH01 join (select a.CBMID,f.IAH04,COUNT(1) fnum from #TmpVAF a join BBT1 b on b.BBX01 = a.BBX01 join BBY1 c on c.BBY01 = a.bby01 join #TmpCBM d on d.CBMID = a.CBMID join IAM1 e on e.BBY01 = c.BBY01 and e.IAA01 = @iaa01 join IAH1 f on f.IAH01 = e.IAH01 where a.BDA01 in ('1','2') and d.BJW02 <> @bjw02 and (b.BBT18 <> 1 and b.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 exists(select * from BLP1 where BLP02 = 7 ) begin if @tcount >= 1 and exists(select f.IAH04,COUNT(1) fnum from #TmpVAF a join BBT1 b on b.BBX01 = a.BBX01 join BBY1 c on c.BBY01 = a.bby01 join #TmpCBM d on d.CBMID = a.CBMID join IAM1 e on e.BBY01 = c.BBY01 and e.IAA01 = @iaa01 join IAH1 f on f.IAH01 = e.IAH01 where (b.BBT18 <> 1 and b.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 on e.BBY01 = c.BBY01 and e.IAA01 = @iaa01 join IAH1 f on f.IAH01 = e.IAH01 join (select f.IAH04,COUNT(1) fnum from #TmpVAF a join BBT1 b on b.BBX01 = a.BBX01 join BBY1 c on c.BBY01 = a.bby01 join #TmpCBM d on d.CBMID = a.CBMID join IAM1 e on e.BBY01 = c.BBY01 and e.IAA01 = @iaa01 join IAH1 f on f.IAH01 = e.IAH01 where (b.BBT18 <> 1 and b.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 exists(select * from BLP1 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 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 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 where BLP02 = 9 and ISNULL(BLP05,1)>=1 ) begin select @blp05 = BLP05 from BLP1 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 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 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 where BLP02 = 47 ) begin if exists( select a.* from #TmpVAF a join BBY1 b on b.BBY01 = a.BBY01 join #TmpCBM c on c.CBMID = a.CBMID join BBT1 d 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') ) begin set @smsg = '医保保内处方不能下达自费药品,请改成医保保外处方。' RAISERROR(@smsg, 16, 1) with nowait RETURN 47 end end if @tcount >= 1 and exists(select * from BLP1 where BLP02 = 52 ) begin if exists( select a.* from #TmpVAF a join BBY1 b on b.BBY01 = a.BBY01 join #TmpCBM c on c.CBMID = a.CBMID join BBT1 d 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') ) begin set @smsg = '医保保内处方不能下达草药不可报药品,请改成医保保外处方。' RAISERROR(@smsg, 16, 1) with nowait RETURN 52 end end if exists(select * from BLP1 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') ) begin set @smsg = '医保保内处方不能下达执行性质为自费的项目,请改成医保保外处方。' RAISERROR(@smsg, 16, 1) with nowait RETURN 50 end end if @tcount >= 1 and exists(select * from BLP1 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 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') 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 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 on b.BBX01 = a.BBX01 join BBY1 c 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 where BLP02 = 10 and ISNULL(BLP05,1)>=1) and exists(select * from IAR1 m where m.VAA07=@VAA07 and m.IAR32 <> 2 and m.ACF01 =0 and m.IAI03 in ('18','19')) begin select @blp05 = BLP05 from BLP1 where BLP02 = 10 and ISNULL(BLP05,1)>=1 if exists(select * from #TmpVAF c join BBY1 b 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 select top 1 @smsg = c.VAF22+' 用药天数超过医保挂急诊号病人限定用药('+CAST(@blp05 as varchar)+')天数'+CAST(c.VAF17 - @blp05 as varchar)+'天.' from #TmpVAF c join BBY1 b 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)) RAISERROR(@smsg, 16, 1) with nowait RETURN 10 end end if @tcount >= 1 and exists(select * from BLP1 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 where BLP02 = 11 and ISNULL(BLP05,1)>=1 if exists(select c.* from #TmpVAF c join BBY1 b 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 select top 1 @smsg = c.VAF22+' 用药天数超过医保处方诊断为行动不便病人限定用药('+CAST(@blp05 as varchar)+')天数'+CAST(c.VAF17 - @blp05 as varchar)+'天.' from #TmpVAF c join BBY1 b 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 '%行动不便%') RAISERROR(@smsg, 16, 1) with nowait RETURN 11 end end if @tcount >= 1 and exists(select * from BLP1 where BLP02 = 13 ) and exists(select m.* from #TmpVAF m join BBT1 n 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 ) begin select top 1 @smsg = m.VAF22+ '此药单方不可以报,请选择医保外处方。' from #TmpVAF m join BBT1 n 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 RAISERROR(@smsg, 16, 1) with nowait RETURN 13 end if @tcount >= 1 and @iay03>'' and exists(select * from BLP1 where BLP02 = 14 and BLP06 = CAST(@iaa01 as varchar) and BLP03 = @iay03) and exists(select a.* from #TmpVAF a join BAG1 b 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 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 where blp02 =15) and exists(select * from IBK1 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 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 into #kbmvaj from V_VAJ_1 a join VAA1 b on b.VAA01 = a.VAA01 where b.IAK05 = @iak05 and a.VAJ05 <= 2 and a.VAJ47 >= @vaj47 and ((DATEDIFF(DAY,a.VAJ47,@edate)<>0 and 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 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 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 from VAF1 a join VAA1 b on b.VAA01 = a.VAA01 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 if exists(select * from BLP1 where BLP02 = 21 ) begin set @smsg='' select top 1 @smsg=c.BBX13 +' : '+a.VAF22+' 违反医保处方当天相同检查类型的诊疗项目一天内只允许开一次的限定' from #TmpVAF a join BBX1 c 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 where e.BLP02=21 and e.BLP06=c.BBX13 and ISNULL(c.BBX13,'')<>'') and exists(select * from #kbmvaf m join BBX1 n on n.BBX01 = m.bbx01 where m.BDA01 >= 'A' and m.VAF01A = 0 and n.BBX13 = c.BBX13 and DATEDIFF(DAY,m.VAJ47,@edate)=0) if @smsg > '' begin RAISERROR(@smsg, 16, 1) with nowait RETURN 21 end end if @tcount >= 1 and exists(select * from BLP1 where BLP02 = 7 ) begin if @tcount >= 1 and exists(select a.* from #TmpVAF a join BBT1 b on b.BBX01 = a.BBX01 join BBY1 c on c.BBY01 = a.bby01 join #tmpcbm f on f.cbmid = a.cbmid join IAM1 m on m.BBY01 = c.BBY01 and m.IAA01 = @iaa01 join IAH1 n on n.IAH01 = m.IAH01 where a.BDA01 in ('1','2') and (b.BBT18 <> 1 and b.BBT07<>1) and c.AAS01 in ('01','02') and f.bjw02<>@bjw02 and ( exists(select d.* from #kbmvaj d join IAM1 m1 on m1.BBY01 = d.BBY01 and m1.IAA01 = @iaa01 join IAH1 n1 on n1.IAH01 = m1.IAH01 where n1.IAH04 = n.IAH04 and DATEDIFF(DAY,d.vaj47,@edate)=0) or exists(select * from #kbmvaf d1 join IAM1 m2 on m2.BBY01 = d1.BBY01 and m2.IAA01 = @iaa01 join IAH1 n2 on n2.IAH01 = m2.IAH01 where d1.sflag = 1 and n2.IAH04=n.IAH04 and DATEDIFF(DAY,d1.vaj47,@edate)=0) ) ) begin select top 1 @smsg = a.VAF22+' 违反医保处方当天不允许开相同规格药品规定.' from #TmpVAF a join BBT1 b on b.BBX01 = a.BBX01 join BBY1 c on c.BBY01 = a.bby01 join #tmpcbm f on f.cbmid = a.cbmid join IAM1 m on m.BBY01 = c.BBY01 and m.IAA01 = @iaa01 join IAH1 n on n.IAH01 = m.IAH01 where a.BDA01 in ('1','2') and (b.BBT18 <> 1 and b.BBT07<>1) and c.AAS01 in ('01','02') and f.bjw02<>@bjw02 and ( exists(select d.* from #kbmvaj d join IAM1 m1 on m1.BBY01 = d.BBY01 and m1.IAA01 = @iaa01 join IAH1 n1 on n1.IAH01 = m1.IAH01 where n1.IAH04 = n.IAH04 and DATEDIFF(DAY,d.vaj47,@edate)=0) or exists(select * from #kbmvaf d1 join IAM1 m2 on m2.BBY01 = d1.BBY01 and m2.IAA01 = @iaa01 join IAH1 n2 on n2.IAH01 = m2.IAH01 where d1.sflag = 1 and n2.IAH04=n.IAH04 and DATEDIFF(DAY,d1.vaj47,@edate)=0) ) RAISERROR(@smsg, 16, 1) with nowait RETURN 7 end end if @tcount >= 1 and exists(select * from BLP1 where BLP02 = 23) begin set @blp05 = ISNULL((select BLP05 from BLP1 where BLP02 = 23),0) set @blp04 = ISNULL((select BLP04 from BLP1 where BLP02 = 23),0) if (@blp05 > 0) and exists(select c.VAF22 from #TmpVAF c join IAM1 e on e.BBY01 = c.BBY01 and e.IAA01 = @iaa01 join IAH1 f on f.IAH01 = e.IAH01 where c.BBT07 = 0 and c.bda01 in ('1','2') and ((@mz_cfxz22<>1 and c.VAF21 >= 1)or(@mz_cfxz22=1 and c.VAF21 > 1)) and c.VAF17 > @blp05 ) begin select top 1 @smsg = c.VAF22+' 用药天数超过限定用药('+CAST(@blp05 as varchar)+')天数'+CAST(c.VAF17 - @blp05 as varchar)+'天.' from #TmpVAF c join IAM1 e on e.BBY01 = c.BBY01 and e.IAA01 = @iaa01 join IAH1 f on f.IAH01 = e.IAH01 where c.BBT07 = 0 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 if (@blp04 >= 1) and exists(select a.VAF22 from #TmpVAF a join IAM1 e on e.BBY01 = a.BBY01 and e.IAA01 = @iaa01 join IAH1 f on f.IAH01 = e.IAH01 join (select f1.IAH04,b.svaf17 from #kbmvaf b join IAM1 e1 on e1.BBY01 = b.BBY01 and e1.IAA01 = @iaa01 join IAH1 f1 on f1.IAH01 = e1.IAH01 where b.BDA01 in ('1','2') ) s on s.IAH04 = f.IAH04 where s.sVAF17 > @blp04) begin select top 1 @smsg = a.VAF22+' 距上次开药还有'+CAST(s.svaf17 as varchar)+'天用药量,请再过' +CAST(s.svaf17-@blp04 as varchar)+'天来医院开此药品.' from #TmpVAF a join IAM1 e on e.BBY01 = a.BBY01 and e.IAA01 = @iaa01 join IAH1 f on f.IAH01 = e.IAH01 join (select f1.IAH04,b.svaf17 from #kbmvaf b join IAM1 e1 on e1.BBY01 = b.BBY01 and e1.IAA01 = @iaa01 join IAH1 f1 on f1.IAH01 = e1.IAH01 where b.BDA01 in ('1','2') ) s on s.IAH04 = f.IAH04 where s.sVAF17 > @blp04 RAISERROR(@smsg, 16, 1) with nowait RETURN 23 end end if @tcount >= 1 and exists(select * from BLP1 where BLP02 = 12) begin if exists(select * from IAR1 m 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 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 b.sVAF17 >= 1 and a.VAF17 = 3 and f.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 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 b.sVAF17 >= 1 and a.VAF17 = 3 and f.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 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 b.sVAF17 > 2 and a.VAF17 > 3 and a.VAF17 <= 7 and f.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 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 b.sVAF17 > 2 and a.VAF17 > 3 and a.VAF17 <= 7 and f.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 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 b.sVAF17 > 3 and a.VAF17 > 7 and a.VAF17 <= 14 and f.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 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 b.sVAF17 > 3 and a.VAF17 > 7 and a.VAF17 <= 14 and f.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 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 b.sVAF17 > 4 and a.VAF17 > 14 and f.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 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 b.sVAF17 > 4 and a.VAF17 > 14 and f.bjw02 <> @bjw02 RAISERROR(@smsg, 16, 1) with nowait RETURN 3 end end if exists(select * from BLP1 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 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 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 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.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 where BLP02 = 3) set @blp03 = 1 set @blp05 = ISNULL((select BLP05 from BLP1 where BLP02 = 3),0) set @blp04 = ISNULL((select BLP04 from BLP1 where BLP02 = 3),0) select @BDP12 = ISNULL(b.BDP12,0) from VAC1 a join BDP1 b on b.BDP02 = a.BDP02 where a.VAC01 = @VAA07 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 on b.BAK01 = a.BAK01A select a.BLP02,d.BBY01,a.BLP04,a.BLP05 into #tmpbby from BLP1 a join BBX1 b on b.BBX01 = a.BLP03 join BAG1 c on c.BBX01 = b.BBX01 join BBY1 d 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 join BBY1 b 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 join BAK1 b 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.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 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 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 exists(select * from BLP1 where BLP02=25) and exists(select * from #TmpVAF a join BBT1 b 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 select @smsg= '==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 where BLP02=55) begin set @smsg = '' select top 1 @smsg = f.IAH44 from #TmpVAF a join IAM1 e on e.BBY01 = a.BBY01 and e.IAA01 = @iaa01 join IAH1 f on f.IAH01 = e.IAH01 where f.IAH44 > '' if @smsg > '' begin set @smsg = '=='+@smsg RAISERROR(@smsg, 16, 1) with nowait RETURN 55 end end