【数据库-MySql】函数-遍历一个表,并修改状态

BEGIN
	declare prehisid varchar(50);
	declare prehisid2 varchar(50);
	declare recordNum int;
	declare ListCount int;
	declare iRow int;
	declare iRow2 int;
	-- 获取处方表的行数 --
  select count(*)  into ListCount from prescription ;
	set iRow = 1;
	
	-- 遍历处方表 --
	WHILE (iRow <= ListCount) do
		set iRow2 = iRow-1;
		
		-- 获取处方表的HIS id --
		SELECT PRESCRIPTION_HIS_ID into prehisid  from prescription ORDER BY PRESCRIPTION_HIS_ID LIMIT iRow2,iRow;
		if prehisid is not null THEN
			-- 获取状态表的处方hisid 和 相同处方hisid个数 --
			 select count(*) ,prescriptionhisid into recordNum,prehisid2 from rcaccept where prescriptionhisid=PreHisId  limit 1;
			-- 存在则更新(恢复原始状态) ,不存在则插入 --
			if(recordNum > 0) then
				update rcaccept rc set rc.bybs=0 where prescriptionhisid= prehisid2;
			else
				insert into rcaccept(prescriptionhisid,bybs)values(PreHisId,0);
			end if;
		end if;
	set	iRow = iRow+1;
	end while; 
END

你可能感兴趣的:(数据库)