Home > database >  Beginners mysql stored procedures met bald crisis. Please bosses to guide!
Beginners mysql stored procedures met bald crisis. Please bosses to guide!

Time:10-11

Problem: I want to take advantage of two layers of cursor nested loop to do attendance information statistics, outer cursor loop to: t_student students' id value table,
And by iterating through its dynamic assignment individually to the inner loop of the definition of a cursor query condition parameters: students_id, inner cursor defined the purpose is to query each students' attendance record of the current month, by the while loop for each person every day during the month of judge attendance abnormal attendance record, and eventually everyone will eventually reach the abnormal attendance data into statistics; But the operation result is outer loop is successful, inserted in the final statistics for all the students' attendance record, but miserable is only the first attendance record is correct, other abnormal 0 (students_id if remove the outer cursor, specify parameters, final insert statistics data advantage normal)!
I just started to contact mysql stored procedures, this two days quickly be Fried by playing...
Hope which bosses can help me to Chou Chou, I thank humbly!
Due to space constraints so adjusted the code format, please forgive me!

Slag on white code:
The BEGIN
# define variables
# 3 dynamic parameter
Declare select_year int.
Declare select_month int.
Declare students_id;

Declare check_temp_date varchar (256);
Declare check_date date;
Declare week_day int.
Declare check_id int.
Declare check_year int.
Declare check_month int.
Declare check_day int.
Declare check_IN time;
Declare check_OUT time;
Declare student_Id int.
Declare v_student_Name varchar (256);
Declare v_Late int the default 0;
Declare v_Early int the default 0;
Declare v_Half_absenteeism int the default 0;
Declare v_Absenteeism int the default 0;
Declare v_Work_day int the default 0;
Declare I int the default 1; Declare v_month_days int.

# set the traverse the end condition
Declare flag int the default 0;
Declare id_info cursor for the select id from t_student;
DECLARE the CONTINUE HANDLER FOR the NOT FOUND SET flag=1;
# to get the current time years
Select DATE_FORMAT (NOW (), '% Y) into select_year;
Select DATE_FORMAT (NOW (), '% m) into select_month;
The open id_info;
Read_loop: Loop
# traversal cursor, values
The fetch id_info into students_id;
Countless according to # can be traversed to exit the loop
If flag=1 then
Leave read_loop;
End the if;
The BEGIN
Declare flags int the default 0;
Declare check_info cursor for the select * from t_check as c.s. tudent_id=c where students_id and
C.y ear=select_year and c. onth=select_month;
DECLARE the CONTINUE HANDLER FOR the NOT FOUND SET flags=1;
The open check_info;
The fetch check_info into check_id check_year, check_month, check_day, check_In, check_Out, student_Id
Things of their actions #
# configuration attendance status

# harvest need to traverse the data of the year, month, day field joining together into a date variable splicing function: concat (field_2 field_1, "-", "-", field_3)
Select concat (select_month select_year, '-', '-' I) into check_temp_date;
Select date_format (check_temp_date, 'y - m - % d % %) into check_date;
# query that month days
Select day (LAST_DAY (check_date)) into v_month_days;
# cycle
WHILE i<=v_month_days do

IF the EXISTS (select * from t_check as c.s. tudent_id=c where students_id and c.y ear=select_year and
C. onth=select_month and c.d ay=I) THEN
Select concat (select_month select_year, '-', '-' I) into check_temp_date;
Select date_format (check_temp_date, 'y - m - % d % %) into check_date;
# according to the stitching date variable rule out working day: return into the date for the day, 1 on behalf of the Sunday, 7 representative Saturday
The select DAYOFWEEK (check_date) into week_day;
# to judge whether it is the weekend
If week_day in (1, 7) THEN
# jump out of the loop operation, the assignment I begin the next cycle
The set I=I;
# determine whether for the May Day holiday (1, 2, 3) the holiday
ELSEIF select_month=5 and in (1, 2, 3) THEN I
# jump out of the loop operation, the assignment I begin the next cycle
The set I=I;
# is for the National Day holiday
,2,3,4,5,6,7 ELSEIF select_month=10 and I in (1) THEN
# jump out of the loop operation, the assignment I begin the next cycle
The set I=I;
The ELSE
# excluding double null, a total of 15 kinds of circumstances
# to check in is null, check out the normal, absenteeism half-day + 1
If check_In is null and check_Out & gt; '17:00:00 THEN
The set v_Half_absenteeism=v_Half_absenteeism + 1;
# signing is null, check out the exceed the maximum limit, absenteeism day + 1
ELSEIF check_In is null and check_Out & lt; '16:30:00 THEN
The set v_Absenteeism=v_Absenteeism + 1;
# to check in is null, check out did not exceed the maximum limit, absenteeism along while + 1, + 1
leave earlyELSEIF check_In is null and check_Out & lt;='17:00:00 and check_Out & gt; THEN='16:30:00'
The set v_Early=v_Early + 1;
The set v_Half_absenteeism=v_Half_absenteeism + 1;
# to check in is normal, the checkout is null, absenteeism half-day + 1
ELSEIF check_In & lt; '09:00:00 and check_Out is null THEN
The set v_Half_absenteeism=v_Half_absenteeism + 1;
# to check in as normal, checkout is normal, no happen
ELSEIF check_In & lt; '09:00:00 and check_Out & gt; '17:00:00 THEN
The set I=I;
# to check in is normal, check out did not exceed the maximum limit, leave early + 1
ELSEIF check_In & lt; '09:00:00 and check_Out & gt;='16:30:00 & amp; & Check_Out & lt; THEN='17:00:00'
The set v_Early=v_Early + 1;
# to check in is normal, check out more than maximum limit, absenteeism half-day + 1
ELSEIF check_In & lt; '09:00:00 & amp; & Check_Out & lt; '16:30:00 THEN
The set v_Half_absenteeism=v_Half_absenteeism + 1;
# signing did not exceed the maximum limit, the checkout is null, late + 1, absent for half a day + 1
ELSEIF check_In & gt;='09:00:00 and check_In & lt;='09:30:00 and check_Out is null THEN
The set v_Half_absenteeism=v_Half_absenteeism + 1;
The set v_Late=v_Late + 1;
# signing did not exceed the maximum limit, late checkout as normal, + 1
ELSEIF check_In & gt;='09:00:00 and check_In & lt;='09:30:00 and check_Out & gt; '17:00:00 THEN
The set v_Late=v_Late + 1;
# signing did not exceed the maximum limit, check out did not exceed the maximum limit, + 1 late, leave early + 1
ELSEIF check_In & gt;='09:00:00 and check_In & lt;='09:30:00 and check_Out & gt;='16:30:00 & amp; &
Check_Out & lt; THEN='17:00:00'
The set v_Late=v_Late + 1;
The set v_Early=v_Early + 1;
# signing did not exceed the maximum limit, check out the exceed the maximum limit, late + 1, absent for half a day + 1
ELSEIF check_In & gt;='09:00:00 and check_In & lt;='09:30:00 and check_Out & lt; '16:30:00 THEN
The set v_Late=v_Late + 1;
The set v_Half_absenteeism=v_Half_absenteeism + 1;
# checkin exceed the maximum limit, the checkout is null, absenteeism day + 1
ELSEIF check_In & gt; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related