Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up a WHILE loop with IF statement in MySQL?

Tags:

I would like to create a stored routine for MySQL that figures out the number of business or working days for a month (Working Days are Monday thru Friday).

It's a syntax error however I don't know what the syntax error is. All it tells me is:

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHILE(@daycount < @totaldays) DO IF (WEEKDAY(@checkweekday) < 6) THEN ' at line 2

My Syntax Error is in the following:

WHILE(@daycount < @totaldays) DO
      IF (WEEKDAY(@checkweekday) < 6) THEN

My Code:

SELECT MONTH(CURDATE()) INTO @curmonth;
SELECT MONTHNAME(CURDATE()) INTO @curmonthname;
SELECT DAY(LAST_DAY(CURDATE())) INTO @totaldays;
SELECT FIRST_DAY(CURDATE()) INTO @checkweekday;
SELECT DAY(@checkweekday) INTO @checkday;
SET @daycount = 0;
SET @workdays = 0;

BEGIN
  WHILE(@daycount < @totaldays) DO
      IF (WEEKDAY(@checkweekday) < 6) THEN
        SET @workdays = @workdays+1;
      END IF;
      SET @daycount = @daycount+1;
      SELECT ADDDATE('@checkweekday', INTERVAL 1 DAY) INTO @checkweekday;
  END WHILE;
END;
SELECT @workdays;
    

Is someone able to assist?

UPDATE I receive the same error with the following bit of code so it probably has something to do with this:

SET @workdays = 0;
IF (WEEKDAY('2013-06-13') < 6) THEN
  SET @workdays = @workdays+1;
END IF;
SELECT @workdays;
like image 282
scrfix Avatar asked Jun 17 '13 00:06

scrfix


People also ask

Can we use while loop in MySQL?

MySQL WHILE loop statement is used to execute one or more statements again and again, as long as a condition is true. We can use the loop when we need to execute the task with repetition while condition is true.

Can you use if statements in MySQL?

The MySQL IF() function is used for validating a condition. The IF() function returns a value if the condition is TRUE and another value if the condition is FALSE. The MySQL IF() function can return values that can be either numeric or strings depending upon the context in which the function is used.


1 Answers

I have discovered that you cannot have conditionals outside of the stored procedure in mysql. This is why the syntax error. As soon as I put the code that I needed between

BEGIN
SELECT MONTH(CURDATE()) INTO @curmonth;
SELECT MONTHNAME(CURDATE()) INTO @curmonthname;
SELECT DAY(LAST_DAY(CURDATE())) INTO @totaldays;
SELECT FIRST_DAY(CURDATE()) INTO @checkweekday;
SELECT DAY(@checkweekday) INTO @checkday;
SET @daycount = 0;
SET @workdays = 0;

  WHILE(@daycount < @totaldays) DO
    IF (WEEKDAY(@checkweekday) < 5) THEN
      SET @workdays = @workdays+1;
    END IF;
    SET @daycount = @daycount+1;
    SELECT ADDDATE(@checkweekday, INTERVAL 1 DAY) INTO @checkweekday;
  END WHILE;
END

Just for others:

If you are not sure how to create a routine in phpmyadmin you can put this in the SQL query

delimiter ;;
drop procedure if exists test2;;
create procedure test2()
begin
select ‘Hello World’;
end
;;

Run the query. This will create a stored procedure or stored routine named test2. Now go to the routines tab and edit the stored procedure to be what you want. I also suggest reading http://net.tutsplus.com/tutorials/an-introduction-to-stored-procedures/ if you are beginning with stored procedures.

The first_day function you need is: How to get first day of every corresponding month in mysql?

Showing the Procedure is working Simply add the following line below END WHILE and above END

SELECT @curmonth,@curmonthname,@totaldays,@daycount,@workdays,@checkweekday,@checkday;

Then use the following code in the SQL Query Window.

call test2 /* or whatever you changed the name of the stored procedure to */

NOTE: If you use this please keep in mind that this code does not take in to account nationally observed holidays (or any holidays for that matter).

like image 148
scrfix Avatar answered Sep 27 '22 17:09

scrfix