欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mysql存储过程《2》续 博客分类: MySQL mysql存储过程mysql游标mysql循环mysql使用whilemysql使用repeat 

程序员文章站 2024-03-11 18:34:19
...

通过传递节假日信息基础表主键节日开始时间,添加或修改日期信息表中所属节日、所属节日第几天信息,达到配置节假日信息功能。

在mysql存储过程《2》中使用了loop完成此配置节假日的功能,现在使用另外两种循环方式完成此功能。

一:使用while。

DROP PROCEDURE
IF EXISTS holiday;

CREATE PROCEDURE holiday (
	IN holiday INT,
	IN startTime VARCHAR (12)
)
BEGIN
	DECLARE
		C INT DEFAULT 0;

DECLARE
	dateOne VARCHAR (12);

DECLARE
	offsetValue INT DEFAULT 1;

DECLARE
	done INT DEFAULT FALSE;

DECLARE
	cu CURSOR FOR SELECT
		date
	FROM
		util_date
	WHERE
		date >= startTime
	LIMIT C;

DECLARE
	CONTINUE HANDLER FOR NOT FOUND
SET done = TRUE;

SELECT
	days INTO C
FROM
	util_holiday
WHERE
	id = holiday;

UPDATE util_date
SET holidayid = '0',
 dayoffset = '0'
WHERE
	YEAR = SUBSTRING(startTime, 1, 4)
AND holidayid = holiday;

OPEN cu;

FETCH cu INTO dateOne;


WHILE done = FALSE DO
	UPDATE util_date
SET holidayid = holiday,
 dayoffset = offsetValue
WHERE
	date = dateOne;


SET offsetValue = offsetValue + 1;

FETCH cu INTO dateOne;


END
WHILE;

CLOSE cu;


END

 二:使用repeat。

DROP PROCEDURE
IF EXISTS holiday;

CREATE PROCEDURE holiday (
	IN holiday INT,
	IN startTime VARCHAR (12)
)
BEGIN
	DECLARE
		C INT DEFAULT 0;

DECLARE
	dateOne VARCHAR (12);

DECLARE
	offsetValue INT DEFAULT 1;

DECLARE
	done INT DEFAULT FALSE;

DECLARE
	cu CURSOR FOR SELECT
		date
	FROM
		util_date
	WHERE
		date >= startTime
	LIMIT C;

DECLARE
	CONTINUE HANDLER FOR NOT FOUND
SET done = TRUE;

SELECT
	days INTO C
FROM
	util_holiday
WHERE
	id = holiday;

UPDATE util_date
SET holidayid = '0',
 dayoffset = '0'
WHERE
	YEAR = SUBSTRING(startTime, 1, 4)
AND holidayid = holiday;

OPEN cu;


REPEAT
	FETCH cu INTO dateOne;


IF done = FALSE THEN
	UPDATE util_date
SET holidayid = holiday,
 dayoffset = offsetValue
WHERE
	date = dateOne;


SET offsetValue = offsetValue + 1;


END
IF;

UNTIL done
END
REPEAT
;

CLOSE cu;


END

 mysql中的while和repeat就像是java中的while和do……while。