i’m new to mysql and any help will be good
I created the following stored procedure
and when I call the procedure i’m getting an error message
the sp initializing parameters input and 1 output
and insert data to table according to the event type
and it checks if the value I want to insert is allowed and doesn’t create duplicates in the
table and if it’s okay it inserts the data else it sends an err msg with the output parameters
CREATE PROCEDURE `employees`.`update_clock`(
IN i_emp_no INT,
IN i_event_type INT,
IN i_event_time DATETIME,
IN i_start_time DATETIME,
IN i_end_time DATETIME,
OUT o_err_msg VARCHAR(200)
)
BEGIN
DECLARE current_time_a DATETIME;
-- Initialize current_time_a based on input or current time
SET current_time_a = IFNULL(i_event_time, NOW());
IF i_event_type = 0 THEN -- Entry
IF EXISTS (
SELECT 1 FROM time_attendance
WHERE emp_no = i_emp_no
AND DATE(start_date) = DATE(i_start_time)
AND (DATE(i_start_time) = DATE(start_date) OR i_start_time < end_date OR i_end_time = end_date)
) THEN
SET o_err_msg = 'Entry already exists for the same day or overlaps with existing time.';
ELSE
INSERT INTO time_attendance (emp_no, start_date, end_date)
VALUES (i_emp_no, i_start_time, NULL);
END IF;
ELSEIF i_event_type = 1 THEN -- Exit
UPDATE time_attendance
SET end_date = current_time_a
WHERE emp_no = i_emp_no AND DATE(start_date) = DATE(current_time_a) AND end_date IS NULL;
ELSEIF i_event_type = 2 THEN -- Manual
IF i_start_time IS NOT NULL AND i_end_time IS NOT NULL AND DATE(i_start_time) = DATE(i_end_time) THEN
INSERT INTO time_attendance (emp_no, start_date, end_date)
VALUES (i_emp_no, i_start_time, i_end_time);
ELSE
SET o_err_msg = 'Manual entry requires both start and end times on the same day.';
END IF;
END IF;
END;
first, i insert data directly:
insert into time_attendance (emp_no,start_date,end_date) values(10001,'2024-08-13 08:30:00','2024-08-13 09:30:00')
and this is the testing:
SET @error_message = '';
CALL update_clock(10001, 0, null, '2024-08-13 08:30:00', null, @error_message);
SELECT @error_message;