SSRS错误“关键字'附近的语法错误为'。”

时间:2017-05-05 17:59:59

标签: sql reporting-services

我有一个在Management Studio中运行良好的查询。我试图使用查询在SSRS中创建一个DataSet。

SSRS抛出错误

  

关键字'as'

附近的语法不正确

不幸的是,这不是很有帮助,我无法弄清楚导致错误的原因。以下是针对SSRS修改的查询:

declare STARDTZ as datetime2
declare LASTDTZ as datetime2

SET STARDTZ = DATEADD(dd, DATEDIFF(dd, 0, @STARTDT), 0)
SET LASTDTZ = DATEADD(dd, DATEDIFF(dd, 0, @LASTDT), 0)
;
with dates(dt) as (select STARDTZ union all select dateadd(day,1,dt) from dates where dt < LASTDTZ)

select 

Name
,SUM(IntervalMin) as Minutes
,DateBin

from 
(

select t.Name
,case when t.StartDT>=d.dt then t.StartDT else d.dt end as StartDT
,case when datediff(day,LastDT,dt)=0 then t.LastDT else dateadd(day,1,d.dt) end as LastDT 
,datediff(millisecond,case when t.StartDT>=d.dt then t.StartDT else d.dt end
,case when datediff(day,LastDT,dt)=0 then t.LastDT else dateadd(day,1,d.dt) end)/60000.0 as IntervalMin
,t.FullName
,DATEADD(dd, DATEDIFF(dd, 0, dt), 0) as DateBin

from (
    select
    case 
       when CoatingChamberStates.Name in 
              ('run_parts','running_parts') 
              then 'RUNNING'
       when CoatingChamberStates.Name in 
              ('idle', 'idle_resources', 'idle_waiting_parts', 'idle_waiting_analysis','idle_complete', 'idle_abort','idle_error')
              then 'IDLE'
       when CoatingChamberStates.Name in 
              ('running', 'unknown', 'error_deposition', 'maintenance_general', 
                     'running_warmup', 'running_centering','running_test_shot', 'running_other', 'maintenance_technical_problems',
                     'wait_start', 'warm_up', 'run_warm_up', 'run_center','run_test','maintenance_problem',
                     'pump_down','run_other','offline','run_test')
              then 'WASTE'
       else 'WASTE'
    end as Name 
    ,c.Startdt as Startdt
    ,c.Lastdt as Lastdt
    ,c.IntervalMin
    ,CoatingChambers.FullName
    ,dateadd(day, datediff(day, 0, Startdt), 0) as DateBin
    from [reo].[dbo].CoatingChamberStateLogs as c
      inner join CoatingChamberStates 
        on c.CoatingChamberStatesid = CoatingChamberStates.CoatingChamberStatesid
      inner join CoatingChambers 
        on c.CoatingChambersid = CoatingChambers.CoatingChambersid
    where c.Startdt >= STARDTZ
      and c.Lastdt <= LASTDTZ
    ) as t
    join dates d 
      on d.dt >= cast(t.Startdt as date) 
     and d.dt<=cast(t.Lastdt as date)
  group by DateBin, Startdt, dt, Lastdt, FullName, t.Name

) as foo
group by Name, DateBin

1 个答案:

答案 0 :(得分:1)

我假设您将STARDTZLASTDTZ声明为局部变量,我是对的吗?

你正在使用,正如错误所指出的那样,语法错误。

DECLARE @STARDTZ datetime2
SET @STARDTZ = DATEADD(dd, DATEDIFF(dd, 0, @STARTDT), 0)

您必须使用空格定义类型。在SQL中,as会调用别名,但事实并非如此。

虽然SSRS似乎不能很好地处理局部变量..也许您应该考虑使用Params functionality,在那里获取值并在查询中使用Param

我希望我能帮到你! 问候, 佩德罗S.