SQL PROCEDURE ISSUE,不确定数据的放置

时间:2017-11-01 06:36:48

标签: mysql stored-procedures

编写一个存储过程,更新员工的每周工作名单,并在给定的分支中为他/她分配最多5个工作班次。要更新名册,该程序可确保: 一世。该员工拥有分配给她/他的现有名册。如果给定员工没有现有名单,则该过程不会更新名单,而是打印相应的错误消息。 II。当前名册的日期在更新的名册中移动了一天。例如,如果员工的当前名单显示从周一到周五的工作班次,则更新的名单将从周二到周六分配工作班次。为简单起见,我们假设分配给员工的工作班次的类型和数量仍然存在 除非出现例如过度分配的异常,否则每周都相同。但是,经理可能希望手动向员工添加任何额外的工作班次。 III。如果员工的分配工作时数超过标准工作小时数(每周35小时),则会显示警告消息。

任何帮助都表示很难解决这个问题.. MYSQL

DELIMITER //


DROP PROCEDURE IF EXISTS updateRoster 

CREATE PROCEDURE UpdateWeeklyRoster (IN e CHAR(8), IN b INT)
BEGIN
    -- variable declaration
DECLARE WORKING_HOURS INT(35);
DECLARE Updated_Working_Shift_ID INT;

    -- cursor declaration
    DECLARE c1 CURSOR FOR
  EmployeeID,BranchID,WorkingShiftID;
    DECLARE CONTINUE HANDLER FOR NOT FOUND set finished = 1; 

    -- check whether the employee exists or not
    SELECT EmployeeID into e
    FROM DutyRoster 
    WHERE EmployeeID = e;

    IF e IS NULL THEN
SIGNAL SQLSTATE '45000' set MESSAGE_TEXT = 'THERE IS EXISTING ROSTER';
ELSE
-- actual part
        -- delete existing tuples relevant to the given employee and branch id from the Duty Roster table
        OPEN c1;
        -- execute a loop
        REPEAT
FETCH c1 into .... , ....., .....;
            -- find the workingShiftWeekDay: assign appropriate values into Current_Week_Day
            -- Find the Updated_Week_Day
            SET Updated_Week_Day = 
  CASE Current_Week_Day
WHEN Week_day='MONDAY' THEN Updated_Week_Day='TUESDAY';
                                    WHEN 'TUESDAY' THEN 'WEDNESDAY';
                                    WHEN 'THURSDAY' THEN 'FRIDAY';
                                    WHEN 'FRIDAY' THEN 'SATURDAY';
                                    WHEN 'SATURDAY' THEN 'SUNDAY';
                                    WHEN 'SUNDAY' THEN 'MONDAY';

ELSE .....
  END CASE;

            -- Find the current_duty_type

            -- for the updated_week_day, find an appropriate working shift id
            SELECT dutyType into WorkingShiftID
            FROM WorkingShift
            WHERE EmployeeID AND dutyType = CurrentDutyType;

            -- insert the new record
            INSERT INTO DutyRoster VALUES (e,b,Updated_Working_Shift_ID);
            UNTIL ........;
END REPEAT;
        CLOSE c1;
        -- Checking whether an employee works for more than 35 hours
SELECT HOUR(TIMEDIFF(WorkingShiftEndTime,WorkingShiftStartTime)) INTO TOTAL, WORKING_HOURS
from WorkingShift, DutyRoster
WHERE DutyRoster, WorkingShiftID = WorkingShift, WorkingShiftID
AND EmployeeID = e;       
IF TOTAL, WORKING HOURS > 35 THEN
SIGNAL SQLSTATE '45001' SET MESSAGE_TEXT  = 'Employee is working over 35 hours....';
    END IF;
END IF;
END

DELIMITER ; 

DELIMITER //

0 个答案:

没有答案