0

I am working on an Attendance project, and this table holds the status of the employee for every month (Permission, Missed, Late etc.). By default, it has be 'Yet to Attend'. I am feeding records into the database for the entire year. Here is my code :

use attendance;
drop procedure setDefault;
DELIMITER $$
create procedure setDefault()
    BEGIN 
        DECLARE theDate date;
        set @theDate = '2020-01-25';

        while  theDate < '2021-01-24'   DO
                INSERT INTO Attendance.empStatus VALUES('4' , theDate , 'YET TO ATTEND');
                set @theDate = @theDate+1;
        end while;
    end $$
 DELIMITER      


call setDefault();        

Ps. '4' is the employee id.

Issue is that this is not getting executed. The 'CREATE PROCEDURE...' line is executed, after which it skips to the call line but I don't know why.

2
  • Welcome to SmackOverflow. What is your question? Commented Apr 3, 2020 at 16:20
  • I edited it now . Commented Apr 3, 2020 at 16:21

1 Answer 1

1

Fixes are needed on date arithmetics, stop condition of the loop, and user variables.

This should work:

delimiter $$
create procedure setdefault()
    begin 
        set @thedate := '2020-01-25';
        while @thedate < '2021-01-24'   
        do
            insert into attendance.empStatus values(4 , @thedate , 'yet to attend');
            set @thedate := @thedate + interval 1 day;
        end while;
    end $$  
 delimiter ;

Here is a small demo.

Note that if you are using MySQL 8.0, you can also do this with a recursive query:

insert into empStatus
with recursive cte as (
    select '2020-01-25' dt
    union all select dt + interval 1 day from cte where dt + interval 1 day < '2021-01-24'
)
select 4, dt, 'yet to attend' from cte;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.