Jesse
Jesse

Reputation: 562

MySQL use variable in WHILE loop

I can't seem to find why this gives me an error. It can't use the variable 'art' in the loop. But when I just do

 select art;

It gives me the correct number of items.

    drop procedure if exists bepaal_lijst_van_bij_te_bestellen_artikelen;

    delimiter //
    create procedure bepaal_lijst_van_bij_te_bestellen_artikelen()
    begin
        DECLARE art INT;
        DECLARE i INT;

        SELECT @art := COUNT(artikel_id) FROM artikelen;

        SET i = 1;
        WHILE i <= art DO

        END WHILE;
    end;
    //

    delimiter ;

Upvotes: 1

Views: 4604

Answers (3)

Jesse
Jesse

Reputation: 562

Apparantly the while loop couldn't be empty and should contain something. fixed it by adding

SET i = 1 + i;

Upvotes: 1

Kiwi
Kiwi

Reputation: 2773

You could just do (note: I know how the assignment goes)

SELECT count(*) from Items into art;

Upvotes: 1

mavili
mavili

Reputation: 3424

I think you should do

SET art := SELECT COUNT(artikel_id) FROM artikelen

Upvotes: 1

Related Questions