查询结果与SSRS结果不同

时间:2015-11-11 09:13:30

标签: sql-server tsql reporting-services

我有一个查询,可以在SSMS上运行时提取准确的数据,但是当我使用具有完全相同查询的SSRS创建报表时,它会忽略来自我使用的两个临时表之一的结果。

DECLARE @from int --= @fromparameter
DECLARE @to int --= @toparameter

/*
For debug
*/
set @from = 0
set @to = 50
/*
================================================================================
Build a temp table with all accounts that have a move out date within params
================================================================================
*/

IF OBJECT_ID('tempdb.dbo.#tempProperty', 'U') is not null drop table #tempProperty
select 
    sa.spark_AccountNumber
    ,sa.spark_PropertyIdName
into
    #tempProperty
from
    SparkCRM_MSCRM.dbo.spark_account sa
where
    sa.spark_AccountNumber IN (
                                select distinct
                                    sa.spark_AccountNumber
                                    --,sa.spark_TenantMoveinDate
                                    --,sa.CreatedOn
                                    --,DATEDIFF(day,sa.spark_TenantMoveinDate,sa.CreatedOn) as [Difference]
                                from
                                    SparkCRM_MSCRM.dbo.spark_account sa
                                where
                                    sa.spark_TenantMoveinDate BETWEEN dateadd(DAY,@from,getdate()) AND dateadd(DAY,@to,getdate())
                                )


/*
================================================================================                                                           
--create CTE with all accounts per property
================================================================================
*/
--;with RowRanked (AccountNumber,Name,Rowrank,MoveinDate,MoveOotDate,SProperty,PProperty)

--AS                                                                                                                                           
--( 
IF OBJECT_ID('tempdb.dbo.#temp', 'U') is not null drop table #temp                                                                                                                                     
    SELECT                                                                                                                                 
         sa.spark_AccountNumber [Account Number]                                                                                                           
        ,sa.spark_name [Account Name]
        ,ROW_NUMBER() OVER(PARTITION BY sa.spark_PropertyIDName ORDER BY COALESCE (sa.spark_TenantMoveinDate, sa.spark_agreementdate) DESC) [rowRank]
        ,COALESCE (sa.spark_TenantMoveinDate, sa.spark_agreementdate) [Tenant Move In Date]
        ,sa.spark_TenantMoveoutDate [Tenant Move Out Date] 
        ,sa.spark_PropertyIdName [Property ID]
        ,p.spark_name [Property Name]
    into #temp
    FROM
        SparkCRM_MSCRM.dbo.spark_property p
    LEFT JOIN
        SparkCRM_MSCRM.dbo.spark_account sa
        on sa.spark_PropertyId = p.spark_propertyId
    WHERE
        sa.spark_PropertyIdName IN (SELECT spark_PropertyIdName from #tempProperty)
--)

/*
================================================================================
build final dataset
================================================================================
*/
select distinct
    sa.spark_AccountNumber                      [Account Number]
    ,sa.spark_name                              [Name]
    ,concat (
            sa.spark_HouseNumber ,' ',
            sa.spark_HouseName  ,' ',   
            sa.spark_Address1   ,' ',   
            sa.spark_Address2   ,' ',   
            sa.spark_Address3   ,' ',   
            sa.spark_Address4   ,' ',   
            sa.spark_Postcode
            )                                   [Address]
    ,sa.spark_Email                             [Email]         
    ,sa.spark_HomePhone                         [Landline]
    ,sa.spark_Mobile                            [Mobile Number]
    ,COALESCE(a3.Name,a2.Name,a1.Name)          [Letting Agent Partner]
    ,sa.spark_tariffidName                      [Tariff]
    ,sa.spark_PPMTariffName                     [PPM Tariff]
    ,pm.Option_Label                            [Payment Method]
    ,sa.spark_Balance                           [Account Balance]
    ,sa.spark_IntendedMoveOut                   [Date of Likely Move Out]
    ,sa.spark_TenantMoveoutDate                 [Current Tenant Move Out Date]
    ,rr.[Account Number]                        [New Account Number]
    ,rr.[Tenant Move In Date]                   [New Account Move In Date]

    ,case
        when pc.spark_CallDriver is not null 
            then 'Yes'
        else
            'No'
    end                                         [Arrangement to Pay]
    ,ds.Option_Label                            [Stops]

from
    SparkCRM_MSCRM.dbo.spark_account sa
--inner join
--  DBS.dbo.Meter m    
--  on m.cust_ref = sa.spark_AccountNumber collate DATABASE_default
--  and m.meter_status = 2
left join 
    SparkCRM_MSCRM.dbo.spark_property sp
    on sp.spark_propertyid = sa.spark_propertyid

left join 
    SparkCRM_MSCRM.dbo.account a1                --branch
    on sp.spark_PartnerId = a1.accountid

left join 
    SparkCRM_MSCRM.dbo.account a2                --brand
    on a1.parentaccountid = a2.accountid

left join 
    SparkCRM_MSCRM.dbo.account a3                --partner
    on a2.parentaccountid = a3.accountid

left join       
    SparkCRM_Custom.dbo.GetCRMOptions('spark_account', 'spark_paymentmethod') pm 
    ON pm.Option_Value = sa.spark_paymentmethod
left join
    SparkCRM_Custom.dbo.GetCRMOptions('spark_account','spark_DebtorStatus') ds
    on ds.Option_Value = sa.spark_DebtorStatus
left join
    SparkCRM_MSCRM.dbo.PhoneCall pc
    on pc.spark_Account = sa.spark_accountId
    and pc.spark_CallDriver = 101
left join
    #temp rr
    on rr.[Property ID] = sa.spark_PropertyIdName
    and rr.Rowrank = 1

where
    coalesce(
        sa.spark_IntendedMoveOut
        ,sa.spark_TenantMoveoutDate
        ) 
    BETWEEN 
        dateadd(DAY,@from,getdate()) AND dateadd(DAY,@to,getdate())

and
    sa.spark_name not like '%occupier%'

当我在SQL Server Management Studio中运行查询时返回数据,但将其复制到SSRS Report Builder似乎会删除#temp表中的任何结果。您会注意到我最初使用CTE作为第二个表,但我尝试使用临时表,以防SSRS与CTE挣扎。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:2)

如果没有其他原因,如果人们试图同时运行报告,则应该避免在Reporting Services中使用#temp表。

最好创建一个报告可以调用的存储过程。这将允许您根据需要应用索引和其他性能修改,它的行为与在SSMS中独立运行查询的行为完全相同

相关问题