mysql stored procedure "2"

First, let's explain the effect of the stored procedure function used:

By passing the primary key of the basic table of holiday information and the start time of the holiday, adding or modifying the information of the holiday and the day of the holiday in the date information table, the function of configuring holiday information is achieved .

The first table, the basic table of holiday information:

Table description: primary key, festival name, festival days.

The second table, the date information table:


 Table description: primary key, year, month, day, day of the week, lunar calendar, festival, day and date of the festival.

Stored procedure:

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
	with CURSOR FOR SELECT
		date
	FROM
		useful_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 with;

read_loop :
LOOP
	FETCH cu INTO dateOne;


IF done THEN
	LEAVE read_loop;


END
IF;

UPDATE util_date
SET holidayid = holiday,
 dayoffset = offsetValue
WHERE
	date = dateOne;


SET offsetValue = offsetValue + 1;


END
LOOP
;

CLOSE cu;


END

 Stored procedure description:

1. Pass two IN type parameters: an INT, the primary key of the basic table of holiday information, and a VARCHAR, the start time of the holiday.

2. Define variables, C is used to record the number of holiday days, dateOne is used to record the date when the cursor iterates, offsetValue is used to assign the day of the holiday, done is used to record whether the iteration of the cursor value ends, to end the LOOP loop at the appropriate time, cu A cursor defined to store dates within holidays.

3. Before opening the cursor, update the holiday, year holiday and holiday day information in the passed parameters to the default values.

4. Open the cursor, open the loop, take the value in the cursor, update the data, and update the relevant variable value; after taking the cursor value, it is necessary to judge whether all the values ​​have been taken to jump out of the loop and close the cursor.

5. It should be noted that the query assignment or update database operation in the stored procedure should be placed after the definition cursor.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327013525&siteId=291194637