SQL代码在SSMS中工作但在Report Builder 3.0中没有

时间:2015-12-03 07:49:46

标签: sql reporting-services ssms reportbuilder3.0

我有一个在SSMS中有效但在Report Builder中没有的代码。在第一部分我创建一个临时表,在第二部分我将该表(使用连接)附加到我的整个查询。在SSMS中我可以声明两次参数并在创建临时表后使用GO但是RP Builder我不能使用go。 您可以在下面找到代码。

任何提示都表示赞赏。谢谢!

    IF OBJECT_ID('tempdb..#TempResTable') IS NOT NULL   DROP TABLE #TempResTable
IF OBJECT_ID('tempdb..#Temp123') IS NOT NULL   DROP TABLE #Temp123

declare @fromDate date
set @fromDate='2015-10-26'
declare @toDate date
set @toDate='2015-11-17'

    Create table #tempResTable (id_resource int, Mins int)
    while (@fromDate <= @toDate)
    begin 
    Insert into #TempResTable 
        select
        r.id_resource
        ,datediff(mi,coalesce(cw.from_time,convert(time, '12:00:00 AM')),coalesce(cw.till_time,convert(time , '23:59:59 PM')))  as 'MinutesAvailable'
        from calendar
        join resource r on r.id_calendar=calendar.id_calendar
        left join calendarVersion cv on cv.id_calendarVersion=calendar.id_calendar
        left join calendarweekdayentry cw on cw.id_calendarVersion=cv.id_calendarVersion  
        --where r.id_resource=@resource 
        where cw.id_availabilitykind in(2,8)
        and cw.weekday=(case when (datediff(dd,cv.from_date,@fromDate)+1)-((datediff(dd,cv.from_date,@fromDate)+1)/(nofweeks*7)*(nofweeks*7))=0 then nofweeks*7
                        else (datediff(dd,cv.from_date,@fromDate)+1)-((datediff(dd,cv.from_date,@fromDate)+1)/(nofweeks*7)*(nofweeks*7))
                        end)
        Group By r.id_resource
        ,cv.from_date
        ,cw.from_time
        ,cw.till_time,cw.id_availabilitykind
        --)  

        set @fromDate=dateadd(dd,1,@fromDate)
    end
go
declare @fromDate date
set @fromDate='2015-10-26'
declare @toDate date
set @toDate='2015-11-17'


select * into #temp123 from
(
select vwdepartment.departmentName 
 ,row_number() over (order by resource.ID_resource) as 'rowNumber'
,resource.resourceName
,resource.id_resource
,B.AvailabilityHours
--,vwplannedShift.id_shift
--,vwplannedShift.plannedstartinstant
--,vwplannedShift.plannedfinishinstant 
--,datediff(mi,vwplannedShift.plannedstartinstant,vwplannedShift.plannedfinishinstant)
, ( select  sum(distinct(datediff(mi,vwplannedShift.plannedstartinstant,vwplannedShift.plannedfinishinstant))) from resource r1 where r1.resourceName=resource.resourceName group by r1.resourceName   ) as 'MinsPlanned'
--,cw.from_time
--,cw.till_time
--,[dbo].[sp_ResourceAvailability]
from vwplannedShift
join vwshift on vwshift.id_shift = vwplannedShift.id_shift
and datediff(dd,@fromDate , vwplannedShift.plannedStartInstant) >=0
and datediff(dd,@toDate , vwplannedShift.plannedStartInstant) <=0
join vwdepartment on vwdepartment.id_department = vwplannedShift.id_department
join vwaction actions on coalesce(actions.t3_shift, actions.id_shift) = vwshift.id_shift 
join vwresourceCombinationDriver driver on actions.id_unionResourceCombi = driver.id_resourceCombination                        
join resource on driver.id_resource = resource.id_resource  
join resourceKind rk on rk.id_resourceKind=resource.id_resourceKind
join calendar c on c.id_calendar=resource.id_calendar
join calendarversion cv on cv.id_calendar=c.id_calendar
join calendarweekdayentry cw on cw.id_calendarVersion=cv.id_calendarVersion
left join availabilityKind ak on ak.id_availabilityKind=cw.id_availabilityKind and cw.id_availabilityKind in (2,8)
LEFT JOIN (select id_resource, sum(mins)AS AvailabilityHours from #tempResTable GROUP BY #tempResTable.id_resource) B ON B.ID_RESOURCE=RESOURCE.ID_RESOURCE
group by resource.resourcename,resource.id_resource,vwdepartment.departmentName,B.AvailabilityHours

union

select vwdepartment.departmentName 
 ,row_number() over (order by resource.ID_resource) as 'rowNumber'
 ,resource.resourceName
,resource.id_resource
,B.AvailabilityHours
--,vwplannedShift.id_shift
--,vwplannedShift.plannedstartinstant
--,vwplannedShift.plannedfinishinstant
--,datediff(mi,vwplannedShift.plannedstartinstant,vwplannedShift.plannedfinishinstant) 
,( select sum(distinct(datediff(mi,vwplannedShift.plannedstartinstant,vwplannedShift.plannedfinishinstant))) from resource r1 where r1.resourceName=resource.resourceName group by r1.resourceName    ) as 'MinsPlanned'
--,cw.from_time
--,cw.till_time
from vwplannedShift
join vwshift on vwshift.id_shift = vwplannedShift.id_shift
and datediff(dd,@fromdate , vwplannedShift.plannedStartInstant) >=0
and datediff(dd,@todate , vwplannedShift.plannedStartInstant) <=0
join vwdepartment on vwdepartment.id_department = vwplannedShift.id_department
join vwaction actions on coalesce(actions.t3_shift, actions.id_shift) = vwshift.id_shift 
join vwresourcecombinationtruck truck on actions.id_unionResourceCombi = truck.id_resourceCombination
join resource on truck.id_resource = resource.id_resource
join resourceKind rk on rk.id_resourceKind=resource.id_resourceKind
left join calendar c on c.id_calendar=resource.id_calendar
left join calendarversion cv on cv.id_calendar=c.id_calendar
left join calendarweekdayentry cw on cw.id_calendarVersion=cv.id_calendarVersion
left join availabilityKind ak on ak.id_availabilityKind=cw.id_availabilityKind and cw.id_availabilityKind in (2,8)
LEFT JOIN (select id_resource, sum(mins)AS AvailabilityHours from #tempResTable GROUP BY #tempResTable.id_resource) B ON B.ID_RESOURCE=RESOURCE.ID_RESOURCE
group by resource.resourcename,resource.id_resource,vwdepartment.departmentName,B.AvailabilityHours
) as cte


select* from #temp123

2 个答案:

答案 0 :(得分:0)

引用documentation on GO

  

GO不是Transact-SQL语句;它是sqlcmd和osql实用程序以及SQL Server Management Studio代码编辑器识别的命令。

报表生成器无法识别。

答案 1 :(得分:0)

虽然TT已经确定了您所拥有的代码的问题,但解决方法是在数据库中创建一个新的存储过程,这将允许您随意创建,填充和查询临时表。

或许这样的事情?

CREATE PROCEDURE [dbo].[GetMyData] ( @startDate DATETIME, @endDate DATEIME )

    Create table #tempResTable (id_resource int, Mins int)
    ...
    (All the rest of your code (without the variable declarations and 'GOs')
    ...
    select* from #temp123
GO

GRANT EXECUTE ON [dbo].[GetMyData] TO [MyUser]
GO

然后选择&#34;存储过程&#34;直接从数据集中引用它。而不是&#34;文字&#34;