2013-01-09 109 views
0

我有一個引發錯誤的存儲過程。Mysql存儲過程調用錯誤

這裏是呼叫:

CALL campaign_strat_placement_test( '10541', '2013年1月1日', '2013年1月7日',2,0,,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碼...

錯誤似乎圍繞着當mrt_opt = 2那麼部分代碼。因爲(如我之前所說)如果該選項是0或1,則該過程起作用。

我花了很長時間看這個,想要一雙新鮮的眼睛。

謝謝!

回答

0

您在該代碼的聲明中有額外的,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

這工作,感謝這麼多。 – Alex