2015-11-11 166 views
3

我當SSMS跑,拉準確的數據,但是當我創建一個使用完全相同的查詢SSRS的報告,它錯過的是來自我用兩個臨時表的一個結果的查詢。查詢結果不同於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報表生成器似乎移除#temp表任何結果返回的數據。您會注意到我最初使用CTE作爲第二張表,但我嘗試使用臨時表,而不是SSRS與CTE一起掙扎。

任何幫助將不勝感激!

+0

您是否嘗試過使用SQL事件探查器捕獲查詢SSRS實際執行完全一樣獨立SSMS中運行查詢? – Aquillo

+0

我還沒有嘗試過,這真的很有幫助,謝謝。我會給它一個去看看它帶領我的地方。我感謝您的幫助! – Fabian0510

回答

2

您應該避免使用#TEMP表中報告服務,如果沒有其他原因,它會引起各種各樣的麻煩,如果人們嘗試同時運行報告。

這將是更好地爲您而是創建一個存儲過程,你的報告可以調用。這將允許您根據需要申請指標和其他性能改裝,然後它會

+0

非常好,謝謝喬納斯。 碰巧,該報告現已返回相同的數據時SSMS跑了,儘管沒有更改查詢或報告文件。 我經常在想什麼嵌入查詢VS調用存儲過程是帶來的好處;感謝您的建議。 – Fabian0510

+0

我沒有讚譽的聲望,但我已經接受了答案!再次感謝。 – Fabian0510

+0

報表生成器也將緩存其結果,直到對數據集進行更改,因此即使在普通查詢中也不會始終匹配SSMS。 – bushell