PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 1, 2022

[FIXED] MySQL (MariaDB) while loop

 March 01, 2022     lamp, mariadb, mysql, sql     No comments   

Issue

Hey there,

I'm trying to update a table with rack and shelf locations in our storage room. there are 15 racks and each rack has 5 shelves. I'm running the loop to 20 to add some extra locations for when we get more shelves. So far this is the procedure I've been trying torun, but; I am getting a syntax error near my first END IF;

Here is my statement:

    drop PROCEDURE if exists updateLocations;
    DELIMITER //
    CREATE PROCEDURE updateLocations()
    begin
    DECLARE rack INT default 1;
    DECLARE shelf INT default 1;
    WHILE rack<21 DO
        insert into tblStorageLocations values ("", rack, shelf);
            IF (shelf=5, SET rack=rack+1, set rack=rack);
            END IF;
            IF (shelf<5, SET shelf=shelf+1, set shelf=1);
            END IF;
            END WHILE;
END;
    //
    DELIMITER ;


    ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'IF;

    IF (shelf<5, SET shelf=shelf+1, set shelf=1);
    END IF;
    END WHILE' at line 8

Solution

You are processing if as a function, rather than as a statement.

drop PROCEDURE if exists updateLocations;
DELIMITER //

CREATE PROCEDURE updateLocations()
BEGIN
    DECLARE rack INT default 1;
    DECLARE shelf INT default 1;
    WHILE rack < 21 DO
        INSERT INTO tblStorageLocations 
            VALUES ('', rack, shelf);
        IF shelf = 5 THEN
            SET rack = rack + 1;
        END IF;
        IF shelf < 5 THEN
            SET shelf = shelf + 1;
        ELSE 
            SET shelf = 1;
        END IF;
    END WHILE;
END;
//
DELIMITER ;


Answered By - Gordon Linoff
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing