I have three user defined variables, and have only figured out how to concatenate at most, two at a time by performing the following query.
SET @sql := (CONCAT(@sql_q1, ' UNION ', @sql_med));
SET @sql := CONCAT_WS(' UNION ', @sql_q1, @sql_med, @sql_q3);
Note: I would recommend that you use
UNION ALL rather than
Also, if one or more of the variables are
NULL, then it string will still look okay.