Ошибка Mysql Stored Proc Call

У меня есть хранимая процедура, которая выдает ошибку.

Вот вызов:

CALL campaign_strat_placement_test ('10541', '2013-1-1', '2013-1-7', 2,0, 2 , 0,0,32)

Я выделил часть, которая вызывает ошибку, потому что, если я изменю эту опцию на 0 или 1, процедура будет запущена.

Вот и вся процедура:

    DELIMITER $$

    USE `reporting`$$

    DROP PROCEDURE IF EXISTS `campaign_strat_placement_test`$$

    CREATE DEFINER=`username`@`%` PROCEDURE `campaign_strat_placement_test`(strat_id VARCHAR(255),rpt_start_date DATE, rpt_end_date DATE,mrt_opt INT, mrt_rev_opt INT, mro_opt INT, mro_rev_opt INT, media_cost_opt INT, metrics_opt INT)
COMMENT 'returns client report'
    BEGIN
SELECT report_title FROM reporting.campaign_meta_cron_daily WHERE strategy_id = strat_id;
SELECT CONCAT('Data Updated Through ',DATE_FORMAT(ADDDATE(CURDATE(),-1),'%M %d, %Y'));    

SET @report_code:=CONCAT('
    select date(date) as `Date`
    , placement_id
    , placement_name
    , strategy_name as strategy
    , concept
    , size
    , exchange
    , targeting_strat
    , ifnull(IMPS_3P,0) as Imps
    , ifnull(CLICKS_3P,0) as Clicks
    ',CASE WHEN mrt_opt = 1 THEN
        ' ,ifnull(MRT_PC_3P,0) as MRT_PC'
        WHEN mrt_opt = 2 THEN 
        ' ,ifnull(MRT_PC_3P,0) as MRT_PC
        ,ifnull(MRT_PV_3P*goal1_pv_discount,0) as MRT_PV'
        WHEN mrt_opt = 3 THEN 
        ' ,ifnull(MRT_PC_3P+(MRT_PV_3P*goal1_pv_discount),0) as MRT_Tot'
        ELSE '' END
    ,CASE WHEN mrt_rev_opt = 1 THEN
        ' ,ifnull(round(sum(MRT_PC_REV_3P),2),0) as MRT_PC_Rev'
        WHEN mrt_rev_opt = 2 THEN 
        ' ,ifnull(MRT_PC_REV_3P,0) as MRT_PC_Rev
        ,ifnull(MRT_PV_REV_3P*goal1_pv_discount,0) as MRT_PV_Rev'
        WHEN mrt_rev_opt = 3 THEN 
        ' ,ifnull(MRT_PC_REV_3P+(MRT_PV_REV_3P*goal1_pv_discount),0) as MRT_Rev'
        ELSE '' END
    ,CASE WHEN mro_opt = 1 THEN
        ' ,ifnull(MRO_PC_3P,0) as MRO_PC'
        WHEN mro_opt = 2 THEN 
        ' ,ifnull(MRO_PC_3P,0) as MRO_PC
        ,ifnull(MRO_PV_3P*goal1_pv_discount,0) as MRO_PV'
        WHEN mro_opt = 3 THEN 
        ' ,ifnull((MRO_PC_3P+(MRO_PV_3P*goal1_pv_discount)),0) as MRO_Tot'
        ELSE '' END
    ,CASE WHEN mro_rev_opt = 1 THEN
        ' ,ifnull(round(MRO_PC_REV_3P,2),0) as MRO_PC_Rev'
        WHEN mro_rev_opt = 2 THEN 
        ' ,ifnull(round(MRO_PC_REV_3P,2),0) as MRO_PC_Rev
        ,ifnull(round(MRO_PV_REV_3P,2),0) as MRO_PV_Rev'
        WHEN mro_rev_opt = 3 THEN 
        ' ,ifnull(round(MRO_PC_REV_3P+sum(MRO_PV_REV_3P*goal1_pv_discount),2),0) as MRO_Rev'
        ELSE '' END
    ,CASE WHEN media_cost_opt = 1 THEN
        ' ,ifnull(round(gross_media_cost,2),0) as Media_Cost'
        WHEN media_cost_opt = 2 THEN 
        ' ,ifnull(round(net_media_cost,2),0) as Net_Media_Cost
         ,ifnull(round(gross_media_cost,2),0) as Gross_Media_Cost'
        ELSE ' ,ifnull(round(net_media_cost,2),0) as Media_Cost' END
    ,CASE WHEN metrics_opt%2 >= 1 THEN 
        ' ,round(ifnull(CLICKS_3P/IMPS_3P,0),4) as CTR' ELSE '' END
    ,CASE WHEN metrics_opt%4 >= 2 THEN 
        ' ,round(ifnull((MRT_PC_3P+(sum(MRT_PV_3P)*goal1_pv_discount))/IMPS_3P*1000,0),4) as RR_per_M' ELSE '' END
    ,CASE WHEN metrics_opt%8 >= 4 THEN 
        ' ,round(ifnull((net_media_cost/IMPS_3P)*1000,0),2) as Net_CPM' ELSE '' END
    ,CASE WHEN metrics_opt%16 >= 8 THEN 
        ' ,round(ifnull(net_media_cost/CLICKS_3P,0),2) as Net_CPC' ELSE '' END
    ,CASE WHEN metrics_opt%32 >= 16 THEN 
        ' ,round(ifnull(net_media_cost/(MRT_PC_3P+(MRT_PV_3P*goal1_pv_discount)),0),2) as Net_CPA' ELSE '' END
    ,CASE WHEN metrics_opt%64 >= 32 THEN 
        ' ,round(ifnull((gross_media_cost/IMPS_3P)*1000,0),2) as Gross_CPM' ELSE '' END
    ,CASE WHEN metrics_opt%128 >= 64 THEN 
        ' ,round(ifnull(gross_media_cost/CLICKS_3P,0),2) as Gross_CPC' ELSE '' END
    ,CASE WHEN metrics_opt%256 >= 128 THEN 
        ' ,round(ifnull(gross_media_cost/(MRT_PC_3P+(MRT_PV_3P*goal1_pv_discount)),0),2) as Gross_CPA' ELSE '' END
    ,CASE WHEN metrics_opt%512 >= 256 THEN 
        ' ,round(ifnull(((MRT_PC_REV_3P+(MRT_PV_REV_3P*goal1_pv_discount))/net_media_cost)-1,0),2) as Net_ROI' ELSE '' END
    ,CASE WHEN metrics_opt%1024 >= 512 THEN 
        ' ,round(ifnull(((MRT_PC_REV_3P+(MRT_PV_REV_3P*goal1_pv_discount))/gross_media_cost)-1,0),2) as Gross_ROI' ELSE '' END
    ,CASE WHEN metrics_opt%2048 >= 1024 THEN 
        ' ,round(ifnull(net_media_cost/(MRT_PC_3P+(MRT_PV_3P*goal1_pv_discount)),0),2) as Alt_Net_CPA' ELSE '' END
    ,CASE WHEN metrics_opt%4096 >= 2048 THEN 
        ' ,round(ifnull(gross_media_cost/(MRT_PC_3P+(MRT_PV_3P*goal1_pv_discount)),0),2) as Alt_Gross_CPA' ELSE '' END
    ,CASE WHEN metrics_opt%8192 >= 4096 THEN 
        ' ,round(ifnull(((MRO_PC_REV_3P+(MRO_PV_REV_3P*goal1_pv_discount))/net_media_cost)-1,0),2) as Alt_Net_ROI' ELSE '' END
    ,CASE WHEN metrics_opt%16384 >= 8192 THEN 
        ' ,round(ifnull(((MRO_PC_REV_3P+(MRO_PV_REV_3P*goal1_pv_discount))/gross_media_cost)-1,0),2) as Alt_Gross_ROI' ELSE '' END
    ,CASE WHEN metrics_opt%32768 >= 16384 THEN 
        ' ,round(ifnull((MRO_PC_3P+(sum(MRO_PV_3P)*goal1_pv_discount))/IMPS_3P*1000,0),4) as Alt_RR_per_M' ELSE '' END
,' from external_02.creative
where strategy_id in (',strat_id,')
    and date >= ''',rpt_start_date,'''
and date <= ''',rpt_end_date,'''
group by date(date) 
    ,advsym
    ,strategy_name
    ,placement_id
    ,placement_name
    ,concept
    ,size
    ,exchange
    ,targeting_strat
UNION ALL 
select ''Total'' as `Date`
    , '''' as placement_id
    , '''' as placement_name
    , '''' as strategy
    , '''' as concept
    , '''' as size
    , '''' as exchange
    , '''' as targeting_strat
    , ifnull(sum(IMPS_3P),0) as Imps
    , ifnull(sum(CLICKS_3P),0) as Clicks
    ',CASE WHEN mrt_opt = 1 THEN
        ' ,ifnull(sum(MRT_PC_3P),0) as MRT_PC'
        WHEN mrt_opt = 2 THEN 
        ' ,ifnull(sum(MRT_PC_3P),0) as MRT_PC
        ,ifnull(sum(MRT_PV_3P*goal1_pv_discount),0) as MRT_PV'
        WHEN mrt_opt = 3 THEN 
        ' ,ifnull(sum(MRT_PC_3P)+sum(MRT_PV_3P*goal1_pv_discount),0) as MRT_Tot'
        ELSE '' END
    ,CASE WHEN mrt_rev_opt = 1 THEN
        ' ,ifnull(sum(MRT_PC_REV_3P),0) as MRT_PC_Rev'
        WHEN mrt_rev_opt = 2 THEN 
        ' ,ifnull(sum(MRT_PC_REV_3P),0) as MRT_PC_Rev
        ,ifnull(sum(MRT_PV_REV_3P*goal1_pv_discount),0) as MRT_PV_Rev'
        WHEN mrt_rev_opt = 3 THEN 
        ' ,ifnull(sum(MRT_PC_REV_3P)+sum(MRT_PV_REV_3P*goal1_pv_discount),0) as MRT_Rev'
        ELSE '' END
    ,CASE WHEN mro_opt = 1 THEN
        ' ,ifnull(sum(MRO_PC_3P),0) as MRO_PC'
        WHEN mro_opt = 2 THEN 
        ' ,ifnull(sum(MRO_PC_3P),0),0) as MRO_PC
        ,ifnull(sum(MRO_PV_3P*goal1_pv_discount),0) as MRO_PV'
        WHEN mro_opt = 3 THEN 
        ' ,ifnull(sum(MRO_PC_3P)+sum(MRO_PV_3P*goal1_pv_discount),0) as MRO_Tot'
        ELSE '' END
    ,CASE WHEN mro_rev_opt = 1 THEN
        ' ,ifnull(round(sum(MRO_PC_REV_3P),2),0) as MRO_PC_Rev'
        WHEN mro_rev_opt = 2 THEN 
        ' ,ifnull(sum(MRO_PC_REV_3P),0) as MRO_PC_Rev
        ,ifnull(sum(MRO_PV_REV_3P*goal1_pv_discount),0) as MRO_PV_Rev'
        WHEN mro_rev_opt = 3 THEN 
        ' ,ifnull(round(sum(MRO_PC_REV_3P)+sum(MRO_PV_REV_3P*goal1_pv_discount),2),0) as MRO_Rev'
        ELSE '' END
    ,CASE WHEN media_cost_opt = 1 THEN
        ' ,ifnull(round(sum(gross_media_cost),2),0) as Media_Cost'
        WHEN media_cost_opt = 2 THEN 
        ' ,ifnull(round(sum(net_media_cost),2),0) as Net_Media_Cost
         ,ifnull(round(sum(gross_media_cost),2),0) as Gross_Media_Cost'
        ELSE ' ,ifnull(round(sum(net_media_cost),2),0) as Media_Cost' END
    ,CASE WHEN metrics_opt%2 >= 1 THEN 
        ' ,round(ifnull(sum(CLICKS_3P)/sum(IMPS_3P),0),4) as CTR' ELSE '' END
    ,CASE WHEN metrics_opt%4 >= 2 THEN 
        ' ,round(ifnull((sum(MRT_PC_3P)+(sum(MRT_PV_3P)*goal1_pv_discount))/sum(IMPS_3P)*1000,0),4) as RR_per_M' ELSE '' END
    ,CASE WHEN metrics_opt%8 >= 4 THEN 
        ' ,round(ifnull((sum(net_media_cost)/sum(IMPS_3P))*1000,0),2) as Net_CPM' ELSE '' END
    ,CASE WHEN metrics_opt%16 >= 8 THEN 
        ' ,round(ifnull(sum(net_media_cost)/sum(CLICKS_3P),0),2) as Net_CPC' ELSE '' END
    ,CASE WHEN metrics_opt%32 >= 16 THEN 
        ' ,round(ifnull((sum(net_media_cost))/((sum(MRT_PC_3P))+(sum(MRT_PV_3P)*goal1_pv_discount)),0),2) as Net_CPA' ELSE '' END
    ,CASE WHEN metrics_opt%64 >= 32 THEN 
        ' ,round(ifnull((sum(gross_media_cost)/sum(IMPS_3P))*1000,0),2) as Gross_CPM' ELSE '' END
    ,CASE WHEN metrics_opt%128 >= 64 THEN 
        ' ,round(ifnull(sum(gross_media_cost)/sum(CLICKS_3P),0),2) as Gross_CPC' ELSE '' END
    ,CASE WHEN metrics_opt%256 >= 128 THEN 
        ' ,round(ifnull((sum(gross_media_cost))/((sum(MRT_PC_3P))+(sum(MRT_PV_3P)*goal1_pv_discount)),0),2) as Gross_CPA' ELSE '' END
    ,CASE WHEN metrics_opt%512 >= 256 THEN 
        ' ,round(ifnull(((sum(MRT_PC_REV_3P)+(sum(MRT_PV_REV_3P)*goal1_pv_discount))/sum(net_media_cost))-1,0),2) as Net_ROI' ELSE '' END
    ,CASE WHEN metrics_opt%1024 >= 512 THEN 
        ' ,round(ifnull(((sum(MRT_PC_REV_3P)+(sum(MRT_PV_REV_3P)*goal1_pv_discount))/sum(gross_media_cost))-1,0),2) as Gross_ROI' ELSE '' END
    ,CASE WHEN metrics_opt%2048 >= 1024 THEN 
        ' ,round(ifnull((sum(net_media_cost))/((sum(MRT_PC_3P))+(sum(MRT_PV_3P)*goal1_pv_discount)),0),2) as Alt_Net_CPA' ELSE '' END
    ,CASE WHEN metrics_opt%4096 >= 2048 THEN 
        ' ,round(ifnull((sum(gross_media_cost))/((sum(MRT_PC_3P))+(sum(MRT_PV_3P)*goal1_pv_discount)),0),2) as Alt_Gross_CPA' ELSE '' END
    ,CASE WHEN metrics_opt%8192 >= 4096 THEN 
        ' ,round(ifnull(((sum(MRO_PC_REV_3P)+(sum(MRO_PV_REV_3P)*goal1_pv_discount))/sum(net_media_cost))-1,0),2) as Alt_Net_ROI' ELSE '' END
    ,CASE WHEN metrics_opt%16384 >= 8192 THEN 
        ' ,round(ifnull(((sum(MRO_PC_REV_3P)+(sum(MRO_PV_REV_3P)*goal1_pv_discount))/sum(gross_media_cost))-1,0),2) as Alt_Gross_ROI' ELSE '' END
    ,CASE WHEN metrics_opt%32768 >= 16384 THEN 
        ' ,round(ifnull((sum(MRO_PC_3P)+(sum(MRO_PV_3P)*goal1_pv_discount))/sum(IMPS_3P)*1000,0),4) as Alt_RR_per_M' ELSE '' END
,' from external_02.creative
where strategy_id in (',strat_id,')
and date >= ''',rpt_start_date,'''
and date <= ''',rpt_end_date,''';');



# Prepares and executes the dynamic SQL statement.
PREPARE ExecStatement FROM @report_code;
EXECUTE ExecStatement;
# Deallocates statement.
DEALLOCATE PREPARE ExecStatement;
# Prepares and executes the dynamic SQL statement.
END$$

    DELIMITER ;

Вот ошибка, которую я получаю:

    Error Code: 1064
    You have an error in your SQL syntax; check the manual that corresponds to your               
    MySQL server version for the right syntax to use near ') as MRO_PC
,ifnull(sum(MRO_PV_3P*goal1_pv_discount),0) as MRO_PV ,ifnull(rou' at line 38

Это сбивает с толку, так как я не могу найти этот код в строке 38 ...

Ошибка, похоже, вращается вокруг части WHEN mrt_opt = 2 THEN . Поскольку (как я сказал ранее) Если этот параметр равен 0 или 1, процедура работает.

Я долгое время смотрел на это и хотел бы увидеть свежие глаза.

Благодаря!

0
nl ja de

1 ответы

У вас есть дополнительный , 0) в вашем заявлении вокруг этого кода.

Найдите и измените следующие строки:

WHEN mro_opt = 2 THEN 
    ' ,ifnull(sum(MRO_PC_3P),0),0) as MRO_PC

к этому:

WHEN mro_opt = 2 THEN 
    ' ,ifnull(sum(MRO_PC_3P),0) as MRO_PC
0
добавлено
Это сработало, спасибо.
добавлено автор Blake, источник
DBA - русскоговорящее сообщество
DBA - русскоговорящее сообщество
1 345 участник(ов)

Общаемся и обсуждаем темы, посвященные DBA, PostgreSQL, Redis, MongoDB, MySQL, neo4j, riak и т.д. См. также: @devops_ru, @kubernetes_ru, @docker_ru, @nodejs_ru Рекомендуем сразу отключить уведомления, чтобы пребывание здесь было полезным и комфортным.

MySQL
MySQL
995 участник(ов)

The group is about MySQL. For code use hastebin.com. Admin: @smlkw