1

I am running a stored procedure. The issue seems to be that it will go into the if statement. Also for some reason or another regardless of how many selects I use it will only return the first. I've copied this from another stored procedure that works like a charm, but this one just won't go. Any ideas?

DROP PROCEDURE IF EXISTS genSelPriceTier;
DELIMITER $$
CREATE PROCEDURE genSelPriceTier(tier_id INT, default_id INT)
    BEGIN
       DECLARE rowCount INT DEFAULT 0;  
          SELECT * FROM price_tier WHERE price_tier_id = tier_id;
          SET rowCount = FOUND_ROWS();
        IF rowCount < 1 THEN
            SELECT * FROM price_tier WHERE price_tier_id = default_id;
            END IF;
    END$$
DELIMITER ;

1 Answer 1

1

There is a bug reported related to the usage of FOUND_ROWS(). So, I recommend using Count(*) for the number of rows returned. Something like the following should work.

DROP PROCEDURE IF EXISTS genSelPriceTier;
DELIMITER $$
CREATE PROCEDURE genSelPriceTier(tier_id INT, default_id INT)
    BEGIN
       DECLARE rowCount INT DEFAULT 0;  
          SELECT COUNT(*) INTO rowCount FROM price_tier WHERE price_tier_id = tier_id 
        IF rowCount < 1 THEN
            SELECT * FROM price_tier WHERE price_tier_id = default_id;
            END IF;
    END$$
DELIMITER ;
Sign up to request clarification or add additional context in comments.

12 Comments

Yes certianly less than 1 or rowCount = 0 would work, but sorry to say this answer does not work either. Very perplexed
There is no error, it just doesn't go into the If statement and return the second select if nothing is found in the first select e.g. if rowCount returns a value les than 1
Looking at it more it must be returning a value higher than 1 in the found rows. I only have 2 recored in the DB id 1 and 2 respectively. so if I pass (0,2) i should return record 2 as the default but it does not
I've edited my answer - added a limit for no rows to be returned which might be required for FOUND_ROWS() to work correctly. Can you try this?
I have and still no dice. Strange thing is i'm running the following in another stored procedure and it works SELECT hit_date FROM hit_counter WHERE page = this_page AND site = this_site AND hit_date = CURDATE(); SET rowCount = FOUND_ROWS(); IF rowCount < 1 THEN INSERT INTO hit_counter (hit_date, site, page, hits) VALUES (CURDATE(), this_site, this_page, 1); ELSE
|

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.