Advance Query works in studio but after published

Advance Query works in studio but after published

  
Hi Gurus...
I am running attached query and work properly in studio when I test. Althoug when I published and run I got following error :Error in advanced query Test2 in Preparation in CalculateUploadHours in MainFlow in SAPTestIntegration (select           z.PERNR,              z.ENDDA,          z.BUKRS,          z.KOSTL,          z.AUFNR,          z.LGART,          SUM(z.ZRHRS) as ZRHRS,          sum(z.ZOHRS) as ZOHRS  from (      select           t.PERNR,           convert(varchar(8),@end_date,112) AS ENDDA,          t.BUKRS,          t.KOSTL,          case when upper(t.AUFNR) = 'NONE' then '' else t.AUFNR end as AUFNR,          t.LGART,          SUM(t.RegHours) as ZRHRS,          sum(t.OTHours) as ZOHRS      from(                SELECT                        e.individual_id as PERNR                      ,week_start_date as ENDDA                      ,week_end_date as enddate                      ,f.company_code as BUKRS                      ,CASE WHEN ISNULL(c.cost_center_name, c2.cost_center_name) = 'None' THEN c2.cost_center_name ELSE                      ISNULL(c.cost_center_name, c2.cost_center_name) END  as KOSTL                      ,isnull(d.internal_orders_name,'') as AUFNR                      ,isnull(sap.WAG ...): Incorrect syntax near ','.

This is the query :

select 
        PERNR,    
        ENDDA,
        BUKRS,
        KOSTL,
        AUFNR,
        LGART,
        SUM(ZRHRS) as ZRHRS,
        sum(ZOHRS) as ZOHRS
from (
    select 
        PERNR, 
        convert(varchar(8),@end_date,112) AS ENDDA,
        BUKRS,
        KOSTL,
        case when upper(t.AUFNR) = 'NONE' then '' else t.AUFNR end as AUFNR,
        LGART,
        SUM(RegHours) as ZRHRS,
        sum(OTHours) as ZOHRS
    from(
            SELECT  
                    individual_id as PERNR
                    ,week_start_date as ENDDA
                    ,week_end_date as enddate
                    ,company_code as BUKRS
                    ,CASE WHEN ISNULL(c.cost_center_name, c2.cost_center_name) = 'None' THEN c2.cost_center_name ELSE
                    ISNULL(c.cost_center_name, c2.cost_center_name) END  as KOSTL
                    ,isnull(internal_orders_name,'') as AUFNR
                    ,isnull(WAGETYPE,'') as LGART
                  ,CASE 
                        WHEN r = 'day1_regular_hrs' THEN DATEADD(DAY, 0, week_start_date)
                        WHEN r = 'day2_regular_hrs' THEN DATEADD(DAY, 1, week_start_date)
                        WHEN r = 'day3_regular_hrs' THEN DATEADD(DAY, 2, week_start_date)
                        WHEN r = 'day4_regular_hrs' THEN  DATEADD(DAY, 3, week_start_date)
                        WHEN r = 'day5_regular_hrs' THEN  DATEADD(DAY, 4, week_start_date)
                        WHEN r = 'day6_regular_hrs' THEN  DATEADD(DAY, 5, week_start_date)
                        WHEN r = 'day7_regular_hrs' THEN  DATEADD(DAY, 6, week_start_date)
                    END AS DayDate
                  ,CASE 
                        WHEN r = 'day1_regular_hrs' THEN day1_regular_hrs
                        WHEN r = 'day2_regular_hrs' THEN day2_regular_hrs
                        WHEN r = 'day3_regular_hrs' THEN day3_regular_hrs
                        WHEN r = 'day4_regular_hrs' THEN day4_regular_hrs
                        WHEN r = 'day5_regular_hrs' THEN day5_regular_hrs
                        WHEN r = 'day6_regular_hrs' THEN day6_regular_hrs
                        WHEN r = 'day7_regular_hrs' THEN day7_regular_hrs
                    END as RegHours
                  ,CASE 
                        WHEN o = 'day1_over_time_hrs' THEN day1_over_time_hrs
                        WHEN o = 'day2_over_time_hrs' THEN day2_over_time_hrs
                        WHEN o = 'day3_over_time_hrs' THEN day3_over_time_hrs
                        WHEN o = 'day4_over_time_hrs' THEN day4_over_time_hrs
                        WHEN o = 'day5_over_time_hrs' THEN day5_over_time_hrs
                        WHEN o = 'day6_over_time_hrs' THEN day6_over_time_hrs
                        WHEN o = 'day7_over_time_hrs' THEN day7_over_time_hrs
                    END as OTHours
              FROM 
                     [SAPlus].[dbo].[WTS_Timesheet] 
                    JOIN  [SAPlus].[dbo].[WTS_Employees]  
                        ON  [SAPlus].[dbo].[WTS_Employees].[EMPLOYEE_ID] =  [SAPlus].[dbo].[WTS_Timesheet].[EMPLOYEE_ID] and  [SAPlus].[dbo].[WTS_Employees].[DELETED]=0 
                    JOIN   [SAPlus].[dbo].[WTS_Cost_centers] c2
                        ON c2.cost_center_id =  [SAPlus].[dbo].[WTS_Employees].[COST_CENTER_ID] AND c2.deleted=0 
                    LEFT JOIN  [SAPlus].[dbo].[WTS_Companies] f
                        ON f.company_id =   [SAPlus].[dbo].[WTS_Employees].[COMPANY_ID] and f.deleted=0
                    LEFT JOIN   [SAPlus].[dbo].[WTS_Cost_centers] c
                        ON c.cost_center_id =  [SAPlus].[dbo].[WTS_Timesheet].[COST_CENTER_ID]   and c.deleted=0
                    LEFT JOIN  [SAPlus].[dbo].[WTS_Internal_Orders] d
                        ON d.internal_orders_id =  [SAPlus].[dbo].[WTS_Timesheet].[INTERNAL_ORDERS_ID] and d.deleted=0
                    LEFT JOIN  [SAPlus].[dbo].[WTS_Concepts_SAP] sap
                        ON sap.sap_concept_id =  [SAPlus].[dbo].[WTS_Timesheet].[CONCEPT_ID]  and sap.deleted=0
                    CROSS JOIN (
                        SELECT 'day1_regular_hrs', 'day1_over_time_hrs' UNION ALL
                        SELECT 'day2_regular_hrs', 'day2_over_time_hrs' UNION ALL
                        SELECT 'day3_regular_hrs', 'day3_over_time_hrs' UNION ALL
                        SELECT 'day4_regular_hrs', 'day4_over_time_hrs' UNION ALL
                        SELECT 'day5_regular_hrs', 'day5_over_time_hrs' UNION ALL
                        SELECT 'day6_regular_hrs', 'day6_over_time_hrs' UNION ALL
                        SELECT 'day7_regular_hrs', 'day7_over_time_hrs'
                        ) AS Cols(r,o)        
                WHERE
                     [SAPlus].[dbo].[WTS_Employees].[COMPANY_ID]  not in (9,10,7) and 
                     [SAPlus].[dbo].[WTS_Timesheet].[DELETED]  = 0 
                    and  [SAPlus].[dbo].[WTS_Timesheet].[CONCEPT_ID]  NOT IN (    SELECT  [SAPlus].[dbo].[WTS_Concepts_SAP].[SAP_CONCEPT_ID]
                                            FROM  [SAPlus].[dbo].[WTS_Concepts_SAP]
                                            where  [SAPlus].[dbo].[WTS_Concepts_SAP].[DONT_UPLOAD_SAP] = 1)                                          
                    AND ( 
                            ( c2.cost_center_name NOT LIKE '200018%'
                              AND CASE 
                                        WHEN r = 'day1_regular_hrs' THEN DATEADD(DAY, 0, week_start_date)
                                        WHEN r = 'day2_regular_hrs' THEN DATEADD(DAY, 1, week_start_date)
                                        WHEN r = 'day3_regular_hrs' THEN DATEADD(DAY, 2, week_start_date)
                                        WHEN r = 'day4_regular_hrs' THEN  DATEADD(DAY, 3, week_start_date)
                                        WHEN r = 'day5_regular_hrs' THEN  DATEADD(DAY, 4, week_start_date)
                                        WHEN r = 'day6_regular_hrs' THEN  DATEADD(DAY, 5, week_start_date)
                                        WHEN r = 'day7_regular_hrs' THEN  DATEADD(DAY, 6, week_start_date)
                                  END BETWEEN @start_date AND @end_date
                            )  
                         OR ( c2.cost_center_name LIKE '200018%'
                              AND CASE 
                                        WHEN r = 'day1_regular_hrs' THEN DATEADD(DAY, 0, week_start_date)
                                        WHEN r = 'day2_regular_hrs' THEN DATEADD(DAY, 1, week_start_date)
                                        WHEN r = 'day3_regular_hrs' THEN DATEADD(DAY, 2, week_start_date)
                                        WHEN r = 'day4_regular_hrs' THEN  DATEADD(DAY, 3, week_start_date)
                                        WHEN r = 'day5_regular_hrs' THEN  DATEADD(DAY, 4, week_start_date)
                                        WHEN r = 'day6_regular_hrs' THEN  DATEADD(DAY, 5, week_start_date)
                                        WHEN r = 'day7_regular_hrs' THEN  DATEADD(DAY, 6, week_start_date)
                                  END BETWEEN @start_date_mac AND @end_date_mac
                            ) 
                        )

        ) t
        GROUP BY         
            t.PERNR,
            convert(varchar(8),DATEADD(s,-1,DATEADD(mm,DATEDIFF(m,0,t.DayDate )+1,0)),112),
            --t.DayDate,
            t.BUKRS,
            t.KOSTL,
            t.AUFNR,
            t.LGART
        HAVING SUM(t.RegHours)>0 or SUM(t.OTHours)>0 
        ) z
        GROUP BY         
            z.PERNR,
            z.ENDDA,
            z.BUKRS,
            z.KOSTL,
            z.AUFNR,
            z.LGART
        ORDER BY 1,2,3,4,5,6

Any clue?
Hi Edgardo,

Remove all the comments.
Queries are sent to the database without new lines, so the "--t.DayDate," comments the rest of the query.

Regards,
João Rosado
Thanks Buddy!!! It is working now..