title: Concat a dynamic sql query
body: <p>I'm taking my first steps into developing dynamic sql queries and I've got a question for the forum. Can a concatenated select statement be appended with additional concatenated statements (see below code)? </p>
  
  <pre><code>use test;
  drop procedure if exists test.spds;
  delimiter $$
  create procedure test.spds (
  xage varchar(75),
  xgender varchar(2),
  xquery varchar(254))
  begin
  set @xan:= REPLACE(xage,'''','');
  set @xgn:= REPLACE(xgender,'''','');
  
  set xquery:=concat('select count(*) from test.ratings where quota=1')
  
  if @xan is not null then
  xquery:=xquery concat(' and age in (',@xan,')')
  
  if @xgn is not null then
   xquery:=xquery concat(' and gender = ',@xgn,')
  
  end if;
  
  xquery=xquery concat(';');
  
  prepare x1 from xquery;
  execute x1;
  deallocate prepare x1;
  
  end if;
  end $$
  </code></pre>
  
  <p>Again this is my first foray so maybe I'm approaching this all too simplistically and any guidance/suggestions would be appreciated. Thanks!</p>