I am writing a SP and have tested the sql outside the SP.. works fine. I am joining two counts to a column of dates. Here's the relevant section of the SP:

the SQL Workbench error tells me I have an 'error near " ' in the last line of the sql below (bolded). I have tried everything... when I remove the second 'left join' the SP runs fine. So, I am led to believe that the way I have structured the joins violates the SP rules somehow.

SET sql_text = concat("Create table ", @table_Names, " AS
select full_date as date, b.Enabled, c.Disabled
from
(select full_date from bibluedb.dim_day
where full_date between '2013-09-01' and '2013-11-01') a
left join
(select date(autoreloadenableddate) as Date, count(*) as Enabled
from blimp_dev.account_gift_card
where autoreloadenableddate is not null
group by date(autoreloadenableddate)) b
on a.full_date = b.date
left join
(select DATE(autoreloaddisableddate) as date,
count(*) as Disabled from blimp_dev.account_gift_card
where autoreloaddisableddate is not null
group by date(autoreloaddisableddate)) c
on a.full_date = c.date;");


-- Create new table
SET @dynamicSQL = sql_text;
PREPARE stmt FROM @dynamicSQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Here is the SQL that runs fine outside the SP - though the COALESCE statements are not removing the nulls:

select full_date as date, b.Enabled, c.Disabled
from
(
select full_date from bibluedb.dim_day
where full_date between '2013-09-01' and curdate()) a
left join
(
select date(autoreloadenableddate) as Date, COALESCE(count(*)) as Enabled from blimp_dev.account_gift_card
where autoreloadenableddate is not null
group by date(autoreloadenableddate)) b
on a.full_date = b.date
left join
(
select date(autoreloaddisableddate) as Date, COALESCE(count(*)) as Disabled from blimp_dev.account_gift_card
where autoreloaddisableddate is not null
group by date(autoreloaddisableddate)) c
on a.full_date = c.date
;