Category

Routines/procedures: queries with conditions

Assume that you have incoming parameter "ModerationStatus" that is responsible for filtering results.
Depending on this parameter you need to show either all rows or limited number of rows. Here's the example of the routine how this can be made with the least effort:

CREATE PROCEDURE `Photo_Get_List`(ModerationStatus tinyint(1))
BEGIN
SET @query = 'SELECT * FROM Photos';
IF ModerationStatus IS NOT NULL THEN
SET @query = CONCAT(@query, ' WHERE ModerationStatus = ', ModerationStatus);
END IF;
PREPARE statement FROM @query;
EXECUTE statement;
DEALLOCATE PREPARE statement;
END;

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <em> <strong> <cite> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.
© 2008-2009. Konstantin Artemov