title: Mysql dynamically build query string in a stored procedure based on logic
body: <p>The objective is to alter a query string within a Mysql stored procedure based on input variables.</p>
  
  <p>Something like this:</p>
  
  <pre><code>CREATE DEFINER=`root`@`localhost` PROCEDURE `func`(type VARCHAR(15))
  BEGIN
      SET @type = type;
  
      -- Check for the sort parameter
      if @type="asc" THEN
          SET @sort = " order by name asc";
      elseif @type="desc" THEN
          SET @sort = " order by name desc";
      else
          SET @sort ="";
      end if;
  
  SELECT id, name from table @sort;
  
  END    
  </code></pre>